-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathengine.cpp
More file actions
1313 lines (1137 loc) · 41 KB
/
engine.cpp
File metadata and controls
1313 lines (1137 loc) · 41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdarg.h>
#include <signal.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include "texture.h"
#include "engine.h"
#include "c_console.h"
#include "c_complete.h"
#include "c_config.h"
#include "r_common.h"
#include "r_model.h"
#include "r_world.h"
#include "u_file.h"
#include "u_misc.h"
#include "u_set.h"
#include "u_log.h"
#include "s_runtime.h"
#include "s_memory.h"
#include "s_parser.h"
#include "s_object.h"
#include "s_util.h"
#include "s_gc.h"
#include "s_vm.h"
#include "m_vec.h"
///! Query the operating system name
static char gOperatingSystem[1024];
#if !defined(_WIN32) && !defined(_WIN64)
# include <sys/utsname.h>
#else
# define _WIN32_LEAN_AND_MEAN
# include <windows.h>
#endif
static struct queryOperatingSystem {
queryOperatingSystem() {
// Operating system is unknown until further checking
strcpy(gOperatingSystem, "Unknown");
#if !defined(_WIN32) && !defined(_WIN64)
struct utsname n;
if (uname(&n) == 0)
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s %s",
n.sysname, n.release, n.machine);
#else
static const size_t kRegQuerySize = 255;
static char name[kRegQuerySize];
static char version[kRegQuerySize];
static char architecture[kRegQuerySize] = { '\0' };
ULONG type = REG_SZ;
ULONG size = kRegQuerySize;
HKEY key = nullptr;
bool inWine = false;
// Find the CPU architecture using the registry
LONG n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"),
0, KEY_QUERY_VALUE, &key);
if (n != ERROR_SUCCESS)
goto failedArchitecture;
if (RegQueryValueEx(key, "PROCESSOR_ARCHITECTURE", nullptr, &type,
(LPBYTE)&architecture[0], &size) != ERROR_SUCCESS) {
RegCloseKey(key);
goto failedArchitecture;
}
RegCloseKey(key);
failedArchitecture:
type = REG_SZ;
size = kRegQuerySize;
// Find the version using the registry
n = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
TEXT("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"),
0, KEY_QUERY_VALUE, &key);
if (n != ERROR_SUCCESS)
goto failedProductName;
if (RegQueryValueEx(key, "ProductName", nullptr, &type,
(LPBYTE)&name[0], &size) != ERROR_SUCCESS) {
RegCloseKey(key);
goto failedProductName;
}
if (RegQueryValueEx(key, "CSDVersion", nullptr, &type,
(LPBYTE)&version[0], &size) != ERROR_SUCCESS) {
RegCloseKey(key);
goto failedCSDVersion;
}
RegCloseKey(key);
static constexpr const char kStripBuf[] = "Microsoft ";
static constexpr size_t kStripLen = sizeof kStripBuf - 1;
if (char *strip = strstr(name, kStripBuf))
u::moveMemory(name, strip + kStripLen, 1 + strlen(strip + kStripLen));
// Check if we're running in Wine
n = RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Wine"), 0, KEY_QUERY_VALUE, &key);
if (n == ERROR_SUCCESS) {
inWine = true;
RegCloseKey(key);
}
#if defined(_WIN32) && !defined(_WIN64)
if (!strcmp(architecture, "AMD64")) {
snprintf(gOperatingSystem, sizeof gOperatingSystem,
"%s %s %s (32-bit binary on 64-bit CPU)", name, version,
(inWine ? "(in Wine)" : ""));
} else {
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s (32-bit)",
name, version);
}
#elif defined(_WIN64)
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s %s (64-bit)",
name, version, (inWine ? "(in Wine)" : ""));
#else
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s %s",
name, version, (inWine ? "(in Wine)" : "");
#endif
return;
failedCSDVersion:
#if defined(_WIN32) && !defined(_WIN64)
if (!strcmp(architecture, "AMD64")) {
snprintf(gOperatingSystem, sizeof gOperatingSystem,
"%s %s (32-bit binary on 64-bit CPU)", name,
(inWine ? "(in Wine)" : ""));
} else {
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s (32-bit)",
name, (inWine ? "(in Wine)" : ""));
}
#elif defined(_WIN64)
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s (64-bit)",
name, (inWine ? "(in Wine)" : ""));
#else
snprintf(gOperatingSystem, sizeof gOperatingSystem, "%s %s",
name, (inWine ? "(in Wine)" : ""));
#endif
return;
failedProductName:
#if defined(_WIN32) && !defined(_WIN64)
if (!strcmp(architecture, "AMD64")) {
snprintf(gOperatingSystem, sizeof gOperatingSystem,
"Windows %s (32-bit binary on 64-bit CPU)",
(inWine ? "(in Wine)" : ""));
} else {
snprintf(gOperatingSystem, sizeof gOperatingSystem,
"Windows %s (32-bit)", (inWine ? "(in Wine)" : ""));
}
#elif defined(_WIN64)
snprintf(gOperatingSystem, sizeof gOperatingSystem,
"Windows %s (64-bit)", (inWine ? "(in Wine)" : ""));
#else
snprintf(gOperatingSystem, sizeof gOperatingSystem,
"Windows %s", (inWine ? "(in Wine)" : ""));
#endif
#endif
}
} gQueryOperatingSystem;
static volatile bool gShutdown = false;
static void neoSignalHandler(int) {
c::Config::write(neoUserPath());
gShutdown = true;
}
// maximum resolution is 15360x8640 (8640p) (16:9)
// default resolution is 1024x768 (XGA) (4:3)
static constexpr int kDefaultScreenWidth = 1024;
static constexpr int kDefaultScreenHeight = 768;
static constexpr size_t kRefreshRate = 60;
VAR(int, vid_vsync, "vertical syncronization (-1 = late swap tearing, 0 = disabled, 1 = enabled, 2 = cap to refresh rate)", -1, kSyncRefresh, kSyncNone);
VAR(int, vid_fullscreen, "toggle fullscreen (0 = windowed mode, 1 = fullscreen)", 0, 1, 1);
VAR(int, vid_width, "resolution width (resuolution or if 0 and vid_height is also 0 engine uses desktop resolution)", 0, 15360, 0);
VAR(int, vid_height, "resolution height (resolution or if 0 and vid_weight is also 0 engine uses desktop resolution)", 0, 8640, 0);
VAR(int, vid_maxfps, "cap framerate (maximum fps allowed, 0 = unlimited)", 0, 3600, 0);
VAR(u::string, vid_driver, "video driver");
VAR(u::string, vid_display, "name of the display to use for video");
VAR(int, scr_info, "embed engine info in screenshot", 0, 1, 1);
VAR(int, scr_format, "screenshot file format (0 = BMP, 1 = TGA, 2 = PNG, 3 = JPG)", 0, 3, 3);
VAR(float, scr_quality, "screenshot quality", 0.0f, 1.0f, 1.0f);
/// pimpl context
struct Context {
struct Controller {
Controller();
Controller(int id);
const char *name() const;
int instance() const;
void update(float delta, const m::vec3 &limits);
protected:
void close();
private:
friend struct Engine;
friend struct Context;
SDL_GameController *m_gamePad;
SDL_Joystick *m_joyStick;
const char *m_name;
int m_instance;
m::vec3 m_position[2]; // L, R
m::vec3 m_velocity[4]; // L, R, Rest L, Rest R
};
~Context();
Context();
void addController(int id);
void delController(int id);
Controller *getController(int id);
void updateController(int id, unsigned char axis, int16_t value);
void begTextInput();
void endTextInput();
private:
friend struct Engine;
u::map<int, Controller> m_controllers;
SDL_GLContext m_gl;
SDL_Window *m_window;
u::string m_textString;
TextState m_textState;
};
#define CTX(X) ((Context*)(X))
///! Context
inline Context::Context()
: m_window(nullptr)
, m_textState(TextState::kInactive)
{
memset(&m_gl, 0, sizeof m_gl);
}
inline Context::~Context() {
if (m_window) {
SDL_GL_DeleteContext(m_gl);
SDL_DestroyWindow(m_window);
}
SDL_Quit();
}
inline void Context::addController(int id) {
// Add the new game controller
Controller cntrl(id);
m_controllers.insert({ cntrl.instance(), cntrl });
u::Log::out("[input] => gamepad %d (%s) connected\n", cntrl.m_instance, cntrl.name());
}
inline void Context::delController(int instance) {
auto cntrl = getController(instance);
if (!cntrl) return;
u::Log::out("[input] => gamepad %d (%s) disconnected\n", instance, cntrl->name());
cntrl->close();
}
inline Context::Controller *Context::getController(int id) {
if (m_controllers.find(id) == m_controllers.end())
return nullptr;
return &m_controllers[id];
}
inline void Context::updateController(int instance, unsigned char axis, int16_t value) {
auto cntrl = getController(instance);
if (!cntrl) return;
switch (axis) {
case SDL_CONTROLLER_AXIS_LEFTX:
cntrl->m_velocity[0].x = value / 32767.0f;
break;
case SDL_CONTROLLER_AXIS_LEFTY:
cntrl->m_velocity[0].y = value / 32767.0f;
break;
case SDL_CONTROLLER_AXIS_RIGHTX:
cntrl->m_velocity[1].x = value / 32767.0f;
break;
case SDL_CONTROLLER_AXIS_RIGHTY:
cntrl->m_velocity[1].y = value / 32767.0f;
break;
}
}
inline void Context::begTextInput() {
m_textState = TextState::kInputting;
m_textString = "";
}
inline void Context::endTextInput() {
if (m_textState != TextState::kInputting)
return;
m_textState = TextState::kFinished;
}
///! Context::Controller
inline Context::Controller::Controller()
: m_gamePad(nullptr)
, m_joyStick(nullptr)
, m_name(nullptr)
, m_instance(0)
{
}
inline Context::Controller::Controller(int id)
: m_gamePad(SDL_GameControllerOpen(id))
, m_joyStick(SDL_GameControllerGetJoystick(m_gamePad))
, m_name(SDL_GameControllerNameForIndex(id))
, m_instance(SDL_JoystickInstanceID(m_joyStick))
{
// Get idle positions
const auto lx = SDL_GameControllerGetAxis(m_gamePad, SDL_CONTROLLER_AXIS_LEFTX);
const auto ly = SDL_GameControllerGetAxis(m_gamePad, SDL_CONTROLLER_AXIS_LEFTY);
const auto rx = SDL_GameControllerGetAxis(m_gamePad, SDL_CONTROLLER_AXIS_RIGHTX);
const auto ry = SDL_GameControllerGetAxis(m_gamePad, SDL_CONTROLLER_AXIS_RIGHTY);
m_velocity[2] = m::vec3(lx / 32767.0f, ly / 32767.0f, 0.0f);
m_velocity[3] = m::vec3(rx / 32767.0f, ry / 32767.0f, 0.0f);
}
inline const char *Context::Controller::name() const {
return m_name;
}
inline int Context::Controller::instance() const {
return m_instance;
}
inline void Context::Controller::update(float delta, const m::vec3 &limits) {
// If the velocity is close to the idle position kill it
if (m::abs(m_velocity[2].x - m_velocity[0].x) < 0.1f) m_velocity[0].x = 0.0f;
if (m::abs(m_velocity[2].y - m_velocity[0].y) < 0.1f) m_velocity[0].y = 0.0f;
if (m::abs(m_velocity[3].x - m_velocity[1].x) < 0.1f) m_velocity[1].x = 0.0f;
if (m::abs(m_velocity[3].y - m_velocity[1].y) < 0.1f) m_velocity[1].y = 0.0f;
m_position[0] = m::clamp(m_position[0] + m_velocity[0] * delta, m::vec3::origin, limits);
m_position[1] = m::clamp(m_position[1] + m_velocity[1] * delta, m::vec3::origin, limits);
}
inline void Context::Controller::close() {
SDL_GameControllerClose(m_gamePad);
}
/// engine
static Engine gEngine;
inline Engine::Engine()
: m_textInputHistoryCursor(0)
, m_autoCompleteCursor(0)
, m_screenWidth(0)
, m_screenHeight(0)
, m_refreshRate(0)
, m_context(nullptr)
{
}
inline Engine::~Engine() {
delete CTX(m_context);
}
bool Engine::init(int &argc, char **argv) {
// Establish local timers for the engine
if (!initTimers())
return false;
// Establish game and user directories + configuration
if (!initData(argc, argv))
return false;
// Launch the context
if (!initContext())
return false;
setVSyncOption(vid_vsync);
m_frameTimer.cap(vid_maxfps);
return true;
}
bool Engine::initContext() {
const u::string &videoDriver = vid_driver.get();
if (videoDriver.size()) {
if (SDL_GL_LoadLibrary(videoDriver.c_str()) != 0) {
u::Log::err("[video] => failed to load video driver: %s\n", SDL_GetError());
return false;
} else {
u::Log::out("[video] => loaded video driver: %s\n", videoDriver);
}
}
// search for a suitable display
const u::string &displayName = vid_display.get();
int display = -1;
int displays = SDL_GetNumVideoDisplays();
u::Log::out("[video] => found %d displays\n", displays);
if (displayName.size())
u::Log::out("[video] => searching for display `%s'\n", displayName);
for (int i = 0; i < displays; i++) {
const char *name = SDL_GetDisplayName(i);
u::Log::out("[video] => found %s display `%s' ",
name == displayName ? "matching" : "a", name);
if (name == displayName)
display = i;
SDL_Rect bounds;
if (SDL_GetDisplayBounds(i, &bounds))
u::Log::out("\n");
else
u::Log::out("(%d x %d)\n", bounds.w, bounds.h);
}
if (display == -1) {
// by default SDL uses display 0
const char *name = SDL_GetDisplayName(0);
if (name)
vid_display.set(name);
display = 0;
}
const char *selectedDisplayName = SDL_GetDisplayName(display);
if (selectedDisplayName)
u::Log::out("[video] => using display `%s'\n", selectedDisplayName);
// Get the display mode resolution
SDL_DisplayMode mode;
if (SDL_GetDesktopDisplayMode(0, &mode) != 0) {
// Failed to get the desktop mode
int displays = SDL_GetNumVideoDisplays();
if (displays < 1) {
// Failed to even get a display, worst case we'll assume a default
m_screenWidth = kDefaultScreenWidth;
m_screenHeight = kDefaultScreenHeight;
} else {
// Try all the display devices until we find one that actually contains
// display information
for (int i = 0; i < displays; i++) {
if (SDL_GetCurrentDisplayMode(i, &mode) != 0)
continue;
// Found a display mode
m_screenWidth = mode.w;
m_screenHeight = mode.h;
}
}
} else {
// Use the desktop display mode
m_screenWidth = mode.w;
m_screenHeight = mode.h;
}
if (m_screenWidth <= 0 || m_screenHeight <= 0) {
// In this event we fall back to the default resolution
m_screenWidth = kDefaultScreenWidth;
m_screenHeight = kDefaultScreenHeight;
}
m_refreshRate = (mode.refresh_rate == 0)
? kRefreshRate : mode.refresh_rate;
// Coming from a config
if (vid_width != 0 && vid_height != 0) {
m_screenWidth = size_t(vid_width.get());
m_screenHeight = size_t(vid_height.get());
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE | SDL_GL_CONTEXT_DEBUG_FLAG);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
u::unique_ptr<Context> ctx(new Context);
uint32_t flags = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE;
if (vid_fullscreen)
flags |= SDL_WINDOW_FULLSCREEN;
#if defined(_MSC_VER)
if (IsDebuggerPresent())
{
flags &= ~SDL_WINDOW_FULLSCREEN;
m_screenWidth = 800;
m_screenHeight = 600;
}
#endif
char name[1024];
snprintf(name, sizeof name, "Neothyne [%s]", gOperatingSystem);
ctx->m_window = SDL_CreateWindow(
name,
display == -1 ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED_DISPLAY(display),
display == -1 ? SDL_WINDOWPOS_UNDEFINED : SDL_WINDOWPOS_CENTERED_DISPLAY(display),
m_screenWidth,
m_screenHeight,
flags
);
if (!ctx->m_window || !(ctx->m_gl = SDL_GL_CreateContext(ctx->m_window))) {
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,
"Neothyne: Initialization error",
"OpenGL 3.0 or higher is required",
nullptr
);
return false;
}
// Hide the cursor for the window
SDL_ShowCursor(0);
// Application icon
Texture icon;
if (icon.load("icon")) {
SDL_Surface *iconSurface = nullptr;
if (icon.bpp() == 4) {
iconSurface = SDL_CreateRGBSurfaceFrom((void *)icon.data(),
icon.width(), icon.height(), icon.bpp()*8, icon.pitch(),
0x000000FF, 0x0000FF00, 0x00FF0000, 0xFF000000);
} else if (icon.bpp() == 3) {
iconSurface = SDL_CreateRGBSurfaceFrom((void *)icon.data(),
icon.width(), icon.height(), icon.bpp()*8, icon.pitch(),
0x0000FF, 0x00FF00, 0xFF0000, 0);
} else {
neoFatal("Application icon must be 3bpp or 4bpp");
return false;
}
SDL_SetWindowIcon(ctx->m_window, iconSurface);
SDL_FreeSurface(iconSurface);
}
// Find all gamepads:
// We ignore events since we need to calculate the idle state of the joysticks
// on the devices. These idles states are used to know when the joysticks are
// in resting position.
//
// This is not accurate. If the engine is launched with the joystick buttons
// in a non-resting position the engine will take on that value as the new
// resting position.
SDL_GameControllerEventState(SDL_IGNORE);
for (int i = 0; i < SDL_NumJoysticks(); ++i) {
if (!SDL_IsGameController(i))
continue;
ctx->addController(i);
}
SDL_GameControllerEventState(SDL_ENABLE);
// Own the context now
m_context = (void*)ctx.release();
return true;
}
bool Engine::initTimers() {
m_frameTimer.reset();
m_frameTimer.cap(FrameTimer::kMaxFPS);
return true;
}
void Engine::deleteConfigRecursive(const u::string &pathName) {
if (u::dir::isFile(pathName)) {
u::remove(pathName, u::kFile);
return;
}
u::dir directory(pathName);
for (const auto &it : directory)
deleteConfigRecursive(u::fixPath(u::format("%s/%s", pathName, it)));
u::remove(pathName, u::kDirectory);
}
void Engine::deleteConfig() {
deleteConfigRecursive(m_userPath);
}
bool Engine::initData(int &argc, char **argv) {
// Get a path for the game, can come from command line as well
const char *directory = nullptr;
for (int i = 1; i < argc - 1; i++) {
if (argv[i] && argv[i + 1] && !strcmp(argv[i], "-gamedir")) {
directory = argv[i + 1];
argc -= 2;
// Found a game directory, remove it and the directory from argv
u::moveMemory(argv+i, argv+i+2, (argc-i) * sizeof *argv);
break;
}
}
// Check if the game directory even exists. But fix it to the platforms
// path separator rules before verifying.
// Engine paths
static const char *kPaths[] = {
"screenshots", "cache", "tmp"
};
u::string fixedDirectory;
if (directory) {
fixedDirectory = u::fixPath(directory);
if (!u::exists(fixedDirectory, u::kDirectory)) {
u::Log::out("Game directory `%s' doesn't exist (falling back to %s)\n",
fixedDirectory, u::fixPath("./game/"));
}
}
m_gamePath = fixedDirectory.empty() ? u::fixPath("./game") : fixedDirectory;
// Add trailing path separator
if (m_gamePath.end()[-1] != u::kPathSep)
m_gamePath += u::kPathSep;
// Verify that path even exists
if (!u::exists(m_gamePath, u::kDirectory)) {
u::Log::err("Game directory `%s' doesn't exist", u::fixPath(m_gamePath));
return false;
}
// Get a path for the user
const auto get = SDL_GetPrefPath("Neothyne", "");
m_userPath = get;
m_userPath.pop_back(); // Remove additional path separator
m_userPath = u::fixPath(m_userPath); // Fix path separator (w.r.t platform)
SDL_free(get);
// Verify all the paths exist for the user directory. If they don't exist
// create them.
for (auto &it : kPaths) {
u::string path = m_userPath + it;
if (u::exists(path, u::kDirectory))
continue;
if (u::mkdir(path))
continue;
}
// Established game and user data paths, now load the config
c::Config::read(m_userPath);
return true;
}
u::map<u::string, int> &Engine::keyState(const u::string &key, bool keyDown, bool keyUp) {
if (keyDown)
m_keyMap[key]++;
if (keyUp)
m_keyMap[key] = 0;
return m_keyMap;
}
void Engine::mouseDelta(int *deltaX, int *deltaY) {
if (SDL_GetRelativeMouseMode() == SDL_TRUE)
SDL_GetRelativeMouseState(deltaX, deltaY);
}
MouseState Engine::mouse() const {
return m_mouseState;
}
void Engine::bindSet(const u::string &what, void (*handler)()) {
m_binds[what] = handler;
}
void Engine::swap() {
SDL_GL_SwapWindow(CTX(m_context)->m_window);
m_frameTimer.update();
auto callBind = [this](const char *what) {
if (m_binds.find(what) != m_binds.end())
m_binds[what]();
};
m_mouseState.wheel = 0;
char format[1024];
const char *keyName;
for (SDL_Event e; SDL_PollEvent(&e); ) {
switch (e.type) {
case SDL_QUIT:
gShutdown = true;
break;
case SDL_CONTROLLERDEVICEADDED:
CTX(m_context)->addController(e.cdevice.which);
break;
case SDL_CONTROLLERDEVICEREMOVED:
CTX(m_context)->delController(e.cdevice.which);
break;
case SDL_CONTROLLERAXISMOTION:
CTX(m_context)->updateController(e.caxis.which, e.caxis.axis, e.caxis.value);
break;
case SDL_WINDOWEVENT:
switch (e.window.event) {
case SDL_WINDOWEVENT_RESIZED:
resize(e.window.data1, e.window.data2);
break;
}
break;
case SDL_KEYDOWN:
if (CTX(m_context)->m_textState == TextState::kInputting) {
if (e.key.keysym.sym != SDLK_TAB) {
m_autoComplete.destroy();
m_autoCompleteCursor = 0;
}
if (e.key.keysym.sym == SDLK_RETURN) {
CTX(m_context)->endTextInput();
// Keep input history if not empty-string
if (CTX(m_context)->m_textString.size()) {
if (m_textInputHistory.full()) {
m_textInputHistory.pop_back();
m_textInputHistoryCursor--;
}
m_textInputHistory.push_back(CTX(m_context)->m_textString);
m_textInputHistoryCursor++;
}
} else if (e.key.keysym.sym == SDLK_BACKSPACE) {
u::string &input = CTX(m_context)->m_textString;
if (input.size())
input.pop_back();
} else if (e.key.keysym.sym == SDLK_TAB) {
if (m_autoComplete.empty())
m_autoComplete = c::Console::suggestions(CTX(m_context)->m_textString);
if (m_autoComplete.size()) {
if (m_autoComplete.size() <= m_autoCompleteCursor)
m_autoCompleteCursor = 0;
CTX(m_context)->m_textString = m_autoComplete[m_autoCompleteCursor++];
}
} else if (e.key.keysym.sym == SDLK_UP && m_textInputHistory.size()) {
m_textInputHistoryCursor--;
if (m_textInputHistoryCursor == size_t(-1))
m_textInputHistoryCursor = m_textInputHistory.size() - 1;
CTX(m_context)->m_textString =
m_textInputHistory[m_textInputHistoryCursor];
} else if (e.key.keysym.sym == SDLK_DOWN && m_textInputHistory.size()) {
m_textInputHistoryCursor++;
if (m_textInputHistoryCursor >= m_textInputHistory.size())
m_textInputHistoryCursor = 0;
CTX(m_context)->m_textString =
m_textInputHistory[m_textInputHistoryCursor];
}
break;
} else if (e.key.keysym.sym == SDLK_SLASH) {
CTX(m_context)->begTextInput();
m_textInputHistoryCursor = m_textInputHistory.size();
break;
} else {
keyName = SDL_GetKeyName(e.key.keysym.sym);
snprintf(format, sizeof format, "%sDn", keyName);
callBind(format);
keyState(keyName, true);
break;
}
break;
case SDL_KEYUP:
keyName = SDL_GetKeyName(e.key.keysym.sym);
snprintf(format, sizeof format, "%sUp", keyName);
callBind(format);
keyState(keyName, false, true);
break;
case SDL_MOUSEMOTION:
m_mouseState.x = e.motion.x;
m_mouseState.y = m_screenHeight - e.motion.y;
break;
case SDL_MOUSEWHEEL:
m_mouseState.wheel = e.wheel.y;
break;
case SDL_MOUSEBUTTONDOWN:
switch (e.button.button) {
case SDL_BUTTON_LEFT:
callBind("MouseDnL");
m_mouseState.button |= MouseState::kMouseButtonLeft;
break;
case SDL_BUTTON_RIGHT:
callBind("MouseDnR");
m_mouseState.button |= MouseState::kMouseButtonRight;
break;
}
break;
case SDL_MOUSEBUTTONUP:
switch (e.button.button) {
case SDL_BUTTON_LEFT:
callBind("MouseUpL");
m_mouseState.button &= ~MouseState::kMouseButtonLeft;
break;
case SDL_BUTTON_RIGHT:
callBind("MouseUpR");
m_mouseState.button &= ~MouseState::kMouseButtonRight;
break;
}
break;
case SDL_TEXTINPUT:
// Ignore text input if we're not inputting
if (CTX(m_context)->m_textState != TextState::kInputting)
break;
// Ignore the first "/"
if (CTX(m_context)->m_textString.empty() && !strcmp(e.text.text, "/"))
break;
CTX(m_context)->m_textString += e.text.text;
break;
}
}
for (auto &cntrl : CTX(m_context)->m_controllers)
cntrl.second.update(m_frameTimer.delta(), { float(m_screenWidth), float(m_screenHeight), 0.0f });
}
size_t Engine::width() const {
return m_screenWidth;
}
size_t Engine::height() const {
return m_screenHeight;
}
void Engine::relativeMouse(bool state) {
#if defined(_MSC_VER)
if (IsDebuggerPresent())
SDL_SetRelativeMouseMode(SDL_FALSE);
else
#endif
SDL_SetRelativeMouseMode(state ? SDL_TRUE : SDL_FALSE);
}
bool Engine::relativeMouse() {
return SDL_GetRelativeMouseMode() == SDL_TRUE;
}
void Engine::centerMouse() {
SDL_WarpMouseInWindow(CTX(m_context)->m_window, m_screenWidth / 2, m_screenHeight / 2);
}
void Engine::setWindowTitle(const char *title) {
char name[1024];
snprintf(name, sizeof name, "%s [%s]", title, gOperatingSystem);
SDL_SetWindowTitle(CTX(m_context)->m_window, name);
}
void Engine::resize(size_t width, size_t height) {
SDL_SetWindowSize(CTX(m_context)->m_window, width, height);
m_screenWidth = width;
m_screenHeight = height;
gl::Viewport(0, 0, width, height);
vid_width.set(width);
vid_height.set(height);
}
void Engine::setVSyncOption(int option) {
switch (option) {
case kSyncTear:
if (SDL_GL_SetSwapInterval(-1) == -1)
setVSyncOption(kSyncRefresh);
break;
case kSyncNone:
SDL_GL_SetSwapInterval(0);
break;
case kSyncEnabled:
SDL_GL_SetSwapInterval(1);
break;
case kSyncRefresh:
SDL_GL_SetSwapInterval(0);
m_frameTimer.unlock();
m_frameTimer.cap(m_refreshRate);
m_frameTimer.lock();
break;
}
m_frameTimer.reset();
}
void Engine::screenShot() {
// Generate a unique filename from the time
const time_t t = time(nullptr);
const struct tm tm = *localtime(&t);
const u::string file = u::format("%sscreenshots/%d-%d-%d-%d%d%d",
neoUserPath(), tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
tm.tm_hour, tm.tm_min, tm.tm_sec);
// Get metrics for reading the final composite from GL
const int screenWidth = int(neoWidth());
const int screenHeight = int(neoHeight());
const int screenSize = screenWidth * screenHeight;
auto pixels = u::unique_ptr<unsigned char>(new unsigned char[screenSize * 3]);
// make sure we're reading from the final framebuffer when obtaining the pixels
// for the screenshot.
gl::BindFramebuffer(GL_READ_FRAMEBUFFER, 0);
gl::PixelStorei(GL_PACK_ALIGNMENT, 1);
gl::ReadPixels(0, 0, screenWidth, screenHeight, GL_RGB, GL_UNSIGNED_BYTE,
(GLvoid *)pixels.get());
gl::PixelStorei(GL_PACK_ALIGNMENT, 8);
// Construct a texture object from the pixel data
Texture screenShot(&pixels[0], screenSize*3, screenWidth, screenHeight, false, kTexFormatRGB);
screenShot.flip();
if (scr_info) {
size_t line = 0;
auto vendor = (const char *)gl::GetString(GL_VENDOR);
auto renderer = (const char *)gl::GetString(GL_RENDERER);
auto version = (const char *)gl::GetString(GL_VERSION);
auto shader = (const char *)gl::GetString(GL_SHADING_LANGUAGE_VERSION);
screenShot.drawString(line, gOperatingSystem);
screenShot.drawString(line, u::CPUDesc());
screenShot.drawString(line, u::RAMDesc());
screenShot.drawString(line, vendor);
screenShot.drawString(line, renderer);
screenShot.drawString(line, version);
screenShot.drawString(line, shader);
screenShot.drawString(line, "Extensions");
for (auto &it : gl::extensions()) {
char buffer[2048];
snprintf(buffer, sizeof buffer, " %s", gl::extensionString(it));
screenShot.drawString(line, buffer);
}
}
auto fixedPath = u::fixPath(file);
auto format = SaveFormat(scr_format.get());
if (screenShot.save(file, format, scr_quality))
u::Log::out("[screenshot] => %s.%s\n", fixedPath, kSaveFormatExtensions[scr_format]);
}
const u::string &Engine::userPath() const {
return m_userPath;
}
const u::string &Engine::gamePath() const {
return m_gamePath;
}
TextState Engine::textInput(u::string &what) {
if (CTX(m_context)->m_textState == TextState::kInactive)
return TextState::kInactive;
what = CTX(m_context)->m_textString;
if (CTX(m_context)->m_textState == TextState::kFinished) {
CTX(m_context)->m_textState = TextState::kInactive;
return TextState::kFinished;
}
return TextState::kInputting;
}
// An accurate frame rate timer and capper
FrameTimer::FrameTimer()
: m_maxFrameTicks(0.0f)
, m_lastSecondTicks(0)
, m_frameCount(0)
, m_minTicks(0)
, m_maxTicks(0)
, m_averageTicks(0.0f)
, m_deltaTime(0.0f)
, m_lastFrameTicks(0)
, m_currentTicks(0)
, m_targetTicks(0)
, m_frameMin(0)
, m_frameMax(0)
, m_frameAverage(0.0f)
, m_framesPerSecond(0)
, m_lock(false)
{
}
void FrameTimer::lock() {
m_lock = true;
}
void FrameTimer::unlock() {
m_lock = false;
}
void FrameTimer::cap(float maxFps) {
if (m_lock)
return;
m_maxFrameTicks = maxFps <= 0.0f
? -1 : (1000.0f / maxFps) - kDampenEpsilon;
}
void FrameTimer::reset() {
m_frameCount = 0;
m_minTicks = 1000;
m_maxTicks = 0;
m_averageTicks = 0;
m_lastSecondTicks = SDL_GetTicks();
}
bool FrameTimer::update() {
m_frameCount++;
m_targetTicks = m_maxFrameTicks != -1 ?
m_lastSecondTicks + uint32_t(m_frameCount * m_maxFrameTicks) : 0;
m_currentTicks = SDL_GetTicks();
m_averageTicks += m_currentTicks - m_lastFrameTicks;
if (m_currentTicks - m_lastFrameTicks <= m_minTicks)
m_minTicks = m_currentTicks - m_lastFrameTicks;
if (m_currentTicks - m_lastFrameTicks >= m_maxTicks)
m_maxTicks = m_currentTicks - m_lastFrameTicks;
if (m_targetTicks && m_currentTicks < m_targetTicks) {
uint32_t beforeDelay = SDL_GetTicks();
SDL_Delay(m_targetTicks - m_currentTicks);
m_currentTicks = SDL_GetTicks();
m_averageTicks += m_currentTicks - beforeDelay;
}
m_deltaTime = 0.001f * (m_currentTicks - m_lastFrameTicks);
m_lastFrameTicks = m_currentTicks;
if (m_currentTicks - m_lastSecondTicks >= 1000) {
m_framesPerSecond = m_frameCount;
m_frameAverage = m_averageTicks / m_frameCount;
m_frameMin = m_minTicks;
m_frameMax = m_maxTicks;
reset();
return true;