This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathengine.cpp
More file actions
1811 lines (1590 loc) · 61.8 KB
/
engine.cpp
File metadata and controls
1811 lines (1590 loc) · 61.8 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 <SDL2/SDL.h>
#include <SDL2/SDL_platform.h>
#include <SDL2/SDL_opengl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "core/lua.h"
#include "core/system.h"
#include "core/gl_util.h"
#include "core/gl_font.h"
#include "core/console.h"
#include "core/vmath.h"
#include "core/polygon.h"
#include "core/gl_text.h"
#include "render/camera.h"
#include "render/render.h"
#include "script/script.h"
#include "physics/physics.h"
#include "fmv/tiny_codec.h"
#include "fmv/stream_codec.h"
#include "gui/gui.h"
#include "gui/gui_inventory.h"
#include "vt/vt_level.h"
#include "audio/audio.h"
#include "audio/audio_stream.h"
#include "game.h"
#include "mesh.h"
#include "skeletal_model.h"
#include "entity.h"
#include "gameflow.h"
#include "room.h"
#include "world.h"
#include "resource.h"
#include "engine.h"
#include "controls.h"
#include "trigger.h"
#include "character_controller.h"
#include "render/bsp_tree.h"
#include "render/shader_manager.h"
#include "image.h"
static SDL_Window *sdl_window = NULL;
static SDL_Joystick *sdl_joystick = NULL;
static SDL_GameController *sdl_controller = NULL;
static SDL_Haptic *sdl_haptic = NULL;
static SDL_GLContext sdl_gl_context = 0;
static stream_codec_t engine_video;
static char base_path[1024] = {0};
static volatile int engine_done = 0;
static int engine_set_zero_time = 0;
float time_scale = 1.0f;
engine_container_p last_cont = NULL;
static float ray_test_point[3] = {0.0f, 0.0f, 0.0f};
static ss_bone_frame_t test_model = {0};
static int32_t test_model_index = 0;
struct engine_control_state_s control_states = {0};
struct control_settings_s control_mapper = {0};
float engine_frame_time = 0.0;
lua_State *engine_lua = NULL;
struct camera_s engine_camera;
struct camera_state_s engine_camera_state;
enum debug_view_state_e
{
no_debug = 0,
player_anim,
sector_info,
room_objects,
ai_boxes,
bsp_info,
model_view,
debug_states_count
};
extern "C" int Engine_ExecCmd(char *ch);
void Engine_Init_Pre();
void Engine_Init_Post();
void Engine_LoadConfig(const char *filename);
void Engine_InitGL();
void Engine_InitSDLVideo();
void Engine_InitSDLSubsystems();
void Engine_InitDefaultGlobals();
void Engine_Display(float time);
void Engine_PollSDLEvents();
void Engine_Resize(int nominalW, int nominalH, int pixelsW, int pixelsH);
void TestModelApplyKey(int key);
void SetTestModel(int index);
void ShowModelView(float time);
void ShowDebugInfo();
void Engine_Start(int argc, char **argv)
{
char *config_name = NULL;
char *autoexec_name = NULL;
Engine_InitDefaultGlobals();
for(int i = 1; i < argc; ++i)
{
if(0 == strncmp(argv[i], "-config", 7))
{
if((i + 1 < argc) && (Sys_FileFound(argv[i + 1], 0)))
{
config_name = argv[i + 1];
}
++i;
}
else if(0 == strncmp(argv[i], "-autoexec", 9))
{
if((i + 1 < argc) && (Sys_FileFound(argv[i + 1], 0)))
{
autoexec_name = argv[i + 1];
}
++i;
}
else if(0 == strncmp(argv[i], "-base_path", 10))
{
if(i + 1 < argc)
{
strncpy(base_path, argv[i + 1], sizeof(base_path) - 1);
if(base_path[0])
{
char *ch = base_path;
for(; *ch; ++ch)
{
if(*ch == '\\')
{
*ch = '/';
}
}
if(*(ch - 1) != '/')
{
*ch = '/';
++ch;
*ch = 0;
}
}
}
++i;
}
else
{
puts("usage:");
puts("-config \"path_to_config_file\"");
puts("-autoexec \"path_to_autoexec_file\"");
puts("-base_path \"path_to_base_folder_location (contains data, resource, save and script folders)\"");
exit(0);
}
}
// Primary initialization.
Engine_Init_Pre();
Engine_LoadConfig(config_name ? config_name : "config.lua");
// Init generic SDL interfaces.
Engine_InitSDLSubsystems();
Engine_InitSDLVideo();
Audio_CoreInit();
// Additional OpenGL initialization.
Engine_InitGL();
renderer.DoShaders();
// Secondary (deferred) initialization.
Engine_Init_Post();
// Make splash screen.
Gui_LoadScreenAssignPic("resource/graphics/legal");
// Initial window resize.
Engine_Resize(screen_info.w, screen_info.h, screen_info.w, screen_info.h);
// Clearing up memory for initial level loading.
World_Prepare();
// Setting up mouse.
SDL_SetRelativeMouseMode(SDL_TRUE);
SDL_WarpMouseInWindow(sdl_window, screen_info.w / 2, screen_info.h / 2);
SDL_ShowCursor(0);
luaL_dofile(engine_lua, autoexec_name ? autoexec_name : "autoexec.lua");
}
void Engine_Shutdown(int val)
{
renderer.ResetWorld(NULL, 0, NULL, 0);
SSBoneFrame_Clear(&test_model);
World_Clear();
stream_codec_clear(&engine_video);
if(engine_lua)
{
lua_close(engine_lua);
engine_lua = NULL;
}
Gameflow_Destroy();
Physics_Destroy();
Gui_Destroy();
Con_Destroy();
GLText_Destroy();
glf_destroy();
Sys_Destroy();
/* no more renderings */
SDL_GL_DeleteContext(sdl_gl_context);
sdl_gl_context = 0;
SDL_DestroyWindow(sdl_window);
sdl_window = NULL;
if(sdl_joystick)
{
SDL_JoystickClose(sdl_joystick);
sdl_joystick = NULL;
}
if(sdl_controller)
{
SDL_GameControllerClose(sdl_controller);
sdl_controller = NULL;
}
if(sdl_haptic)
{
SDL_HapticClose(sdl_haptic);
sdl_haptic = NULL;
}
Audio_CoreDeinit();
Sys_Destroy();
SDL_Quit();
exit(val);
}
const char *Engine_GetBasePath()
{
return base_path;
}
void Engine_SetDone()
{
stream_codec_stop(&engine_video, 0);
StreamTrack_Stop(Audio_GetStreamExternal());
engine_done = 1;
}
void Engine_InitDefaultGlobals()
{
Sys_InitGlobals();
Con_InitGlobals();
Controls_InitGlobals();
Game_InitGlobals();
Audio_InitGlobals();
}
// First stage of initialization.
void Engine_Init_Pre()
{
stream_codec_init(&engine_video);
Sys_Init();
glf_init();
GLText_Init();
Con_Init();
Gameflow_Init();
Con_SetExecFunction(Engine_ExecCmd);
Script_LuaInit();
Script_CallVoidFunc(engine_lua, "loadscript_pre", true);
Cam_Init(&engine_camera);
engine_camera_state.state = CAMERA_STATE_NORMAL;
engine_camera_state.target_id = ENTITY_ID_NONE;
engine_camera_state.flyby = NULL;
engine_camera_state.sink = NULL;
engine_camera_state.shake_value = 0.0f;
engine_camera_state.time = 0.0f;
Mat4_E_macro(engine_camera_state.cutscene_tr);
Physics_Init();
}
// Second stage of initialization.
void Engine_Init_Post()
{
Script_CallVoidFunc(engine_lua, "loadscript_post", true);
Con_InitFont();
Gui_Init();
Con_AddLine("Engine inited!", FONTSTYLE_CONSOLE_EVENT);
}
void Engine_InitGL()
{
InitGLExtFuncs();
qglClearColor(0.0, 0.0, 0.0, 1.0);
qglEnable(GL_DEPTH_TEST);
qglDepthFunc(GL_LEQUAL);
if(renderer.settings.antialias)
{
qglEnable(GL_MULTISAMPLE);
}
else
{
qglDisable(GL_MULTISAMPLE);
}
// Default state: Vertex array and color array are enabled, all others disabled.. Drawable
// items can rely on Vertex array to be enabled (but pointer can be
// anything). They have to enable other arrays based on their need and then
// return to default state
qglEnableClientState(GL_VERTEX_ARRAY);
qglEnableClientState(GL_COLOR_ARRAY);
// function use anyway.
qglAlphaFunc(GL_GEQUAL, 0.5);
}
void Engine_InitSDLVideo()
{
Uint32 video_flags = SDL_WINDOW_OPENGL | SDL_WINDOW_MOUSE_FOCUS | SDL_WINDOW_INPUT_FOCUS;
PFNGLGETSTRINGPROC lglGetString = NULL;
if(screen_info.fullscreen)
{
video_flags |= SDL_WINDOW_FULLSCREEN;
}
else
{
video_flags |= (SDL_WINDOW_RESIZABLE | SDL_WINDOW_SHOWN);
}
///@TODO: is it really needede for correct work?
if(SDL_GL_LoadLibrary(NULL) < 0)
{
Sys_Error("Could not init OpenGL driver");
}
// Check for correct number of antialias samples.
if(renderer.settings.antialias)
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, renderer.settings.antialias);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, renderer.settings.antialias_samples);
}
else
{
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 0);
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 0);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, renderer.settings.z_depth);
#if STENCIL_FRUSTUM
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
#endif
// set the opengl context version
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
//SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
sdl_window = SDL_CreateWindow("OpenTomb", screen_info.x, screen_info.y, screen_info.w, screen_info.h, video_flags);
if(!sdl_window)
{
Sys_Error("Could not create SDL window");
}
sdl_gl_context = SDL_GL_CreateContext(sdl_window);
if(!sdl_gl_context)
{
Sys_Error("Could not create GL context");
}
lglGetString = (PFNGLGETSTRINGPROC)SDL_GL_GetProcAddress("glGetString");
Con_AddLine((const char*)lglGetString(GL_VENDOR), FONTSTYLE_CONSOLE_INFO);
Con_AddLine((const char*)lglGetString(GL_RENDERER), FONTSTYLE_CONSOLE_INFO);
Con_Printf("OpenGL version %s", lglGetString(GL_VERSION));
Con_AddLine((const char*)lglGetString(GL_SHADING_LANGUAGE_VERSION), FONTSTYLE_CONSOLE_INFO);
}
void Engine_InitSDLSubsystems()
{
int NumJoysticks;
Uint32 init_flags = SDL_INIT_VIDEO | SDL_INIT_EVENTS; // These flags are used in any case.
if(control_mapper.use_joy == 1)
{
init_flags |= SDL_INIT_GAMECONTROLLER; // Update init flags for joystick.
if(control_mapper.joy_rumble)
{
init_flags |= SDL_INIT_HAPTIC; // Update init flags for force feedback.
}
SDL_Init(init_flags);
NumJoysticks = SDL_NumJoysticks();
if((NumJoysticks < 1) || ((NumJoysticks - 1) < control_mapper.joy_number))
{
Sys_DebugLog(SYS_LOG_FILENAME, "Error: there is no joystick #%d present.", control_mapper.joy_number);
return;
}
if(SDL_IsGameController(control_mapper.joy_number)) // If joystick has mapping (e.g. X360 controller)
{
SDL_GameControllerEventState(SDL_ENABLE); // Use GameController API
sdl_controller = SDL_GameControllerOpen(control_mapper.joy_number);
if(!sdl_controller)
{
Sys_DebugLog(SYS_LOG_FILENAME, "Error: can't open game controller #%d.", control_mapper.joy_number);
SDL_GameControllerEventState(SDL_DISABLE); // If controller init failed, close state.
control_mapper.use_joy = 0;
}
else if(control_mapper.joy_rumble) // Create force feedback interface.
{
sdl_haptic = SDL_HapticOpenFromJoystick(SDL_GameControllerGetJoystick(sdl_controller));
if(!sdl_haptic)
{
Sys_DebugLog(SYS_LOG_FILENAME, "Error: can't initialize haptic from game controller #%d.", control_mapper.joy_number);
}
}
}
else
{
SDL_JoystickEventState(SDL_ENABLE); // If joystick isn't mapped, use generic API.
sdl_joystick = SDL_JoystickOpen(control_mapper.joy_number);
if(!sdl_joystick)
{
Sys_DebugLog(SYS_LOG_FILENAME, "Error: can't open joystick #%d.", control_mapper.joy_number);
SDL_JoystickEventState(SDL_DISABLE); // If joystick init failed, close state.
control_mapper.use_joy = 0;
}
else if(control_mapper.joy_rumble) // Create force feedback interface.
{
sdl_haptic = SDL_HapticOpenFromJoystick(sdl_joystick);
if(!sdl_haptic)
{
Sys_DebugLog(SYS_LOG_FILENAME, "Error: can't initialize haptic from joystick #%d.", control_mapper.joy_number);
}
}
}
if(sdl_haptic) // To check if force feedback is working or not.
{
SDL_HapticRumbleInit(sdl_haptic);
SDL_HapticRumblePlay(sdl_haptic, 1.0, 300);
}
}
else
{
SDL_Init(init_flags);
}
}
void Engine_LoadConfig(const char *filename)
{
if(filename && Sys_FileFound(filename, 0))
{
lua_State *lua = luaL_newstate();
if(lua != NULL)
{
luaL_openlibs(lua);
lua_register(lua, "bind", lua_BindKey); // get and set key bindings
lua_pushstring(lua, Engine_GetBasePath());
lua_setglobal(lua, "base_path");
luaL_dofile(lua, filename);
Script_ParseScreen(lua, &screen_info);
Script_ParseRender(lua, &renderer.settings);
Script_ParseAudio(lua, &audio_settings);
Script_ParseConsole(lua);
Script_ParseControls(lua, &control_mapper);
lua_close(lua);
}
}
else
{
Sys_Warn("Could not find config file");
}
}
void Engine_Display(float time)
{
if(!engine_done)
{
qglClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//| GL_ACCUM_BUFFER_BIT);
Cam_Apply(&engine_camera);
Cam_RecalcClipPlanes(&engine_camera);
// GL_VERTEX_ARRAY | GL_COLOR_ARRAY
screen_info.debug_view_state %= debug_states_count;
if(screen_info.debug_view_state)
{
ShowDebugInfo();
}
qglPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT); ///@PUSH <- GL_VERTEX_ARRAY | GL_COLOR_ARRAY
qglEnableClientState(GL_NORMAL_ARRAY);
qglEnableClientState(GL_TEXTURE_COORD_ARRAY);
qglFrontFace(GL_CW);
if(screen_info.debug_view_state != debug_view_state_e::model_view)
{
renderer.GenWorldList(&engine_camera);
renderer.DrawList();
}
else
{
/*qglPolygonMode(GL_FRONT, GL_FILL);
qglDisable(GL_CULL_FACE);*/
qglDisable(GL_BLEND);
qglEnable(GL_ALPHA_TEST);
ShowModelView(time);
}
Gui_SwitchGLMode(1);
qglEnable(GL_ALPHA_TEST);
qglPopClientAttrib(); ///@POP -> GL_VERTEX_ARRAY | GL_COLOR_ARRAY
Gui_Render();
Gui_SwitchGLMode(0);
renderer.DrawListDebugLines();
SDL_GL_SwapWindow(sdl_window);
}
}
void Engine_GLSwapWindow()
{
SDL_GL_SwapWindow(sdl_window);
}
void Engine_Resize(int nominalW, int nominalH, int pixelsW, int pixelsH)
{
const float scale_coeff = 1024.0f;
screen_info.w = nominalW;
screen_info.h = nominalH;
screen_info.scale_factor = (screen_info.w < screen_info.h) ? (screen_info.h / scale_coeff) : (screen_info.w / scale_coeff);
GLText_UpdateResize(screen_info.w, screen_info.h, screen_info.scale_factor);
Con_UpdateResize();
Gui_UpdateResize();
Cam_SetFovAspect(&engine_camera, screen_info.fov, (float)nominalW / (float)nominalH);
Cam_RecalcClipPlanes(&engine_camera);
qglViewport(0, 0, pixelsW, pixelsH);
}
void Engine_PollSDLEvents()
{
SDL_Event event;
static int mouse_setup = 0;
const float color[3] = {1.0f, 0.0f, 0.0f};
static float from[3], to[3];
while(SDL_PollEvent(&event))
{
switch(event.type)
{
case SDL_MOUSEMOTION:
if(!Con_IsShown() && control_states.mouse_look != 0 &&
((event.motion.x != (screen_info.w / 2)) ||
(event.motion.y != (screen_info.h / 2))))
{
if(mouse_setup) // it is not perfect way, but cursor
{ // every engine start is in one place
control_states.look_axis_x = event.motion.xrel * control_mapper.mouse_sensitivity_x;
control_states.look_axis_y = event.motion.yrel * control_mapper.mouse_sensitivity_y;
}
if((event.motion.x < ((screen_info.w / 2) - (screen_info.w / 4))) ||
(event.motion.x > ((screen_info.w / 2) + (screen_info.w / 4))) ||
(event.motion.y < ((screen_info.h / 2) - (screen_info.h / 4))) ||
(event.motion.y > ((screen_info.h / 2) + (screen_info.h / 4))))
{
SDL_WarpMouseInWindow(sdl_window, screen_info.w / 2, screen_info.h / 2);
}
}
mouse_setup = 1;
break;
case SDL_MOUSEWHEEL:
if(Con_IsShown())
{
Con_Scroll(event.wheel.y);
}
break;
case SDL_MOUSEBUTTONDOWN:
if(event.button.button == 1) //LM = 1, MM = 2, RM = 3
{
Controls_PrimaryMouseDown(from, to);
}
else if(event.button.button == 3)
{
Controls_SecondaryMouseDown(&last_cont, ray_test_point);
if(last_cont && last_cont->object_type == OBJECT_ENTITY)
{
entity_p player = World_GetPlayer();
if(player && player->character)
{
Character_SetTarget(player, ((entity_p)last_cont->object)->id);
}
}
else
{
entity_p player = World_GetPlayer();
if(player && player->character)
{
Character_SetTarget(player, ENTITY_ID_NONE);
}
}
}
break;
// Controller events are only invoked when joystick is initialized as
// game controller, otherwise, generic joystick event will be used.
case SDL_CONTROLLERAXISMOTION:
Controls_WrapGameControllerAxis(event.caxis.axis, event.caxis.value);
break;
case SDL_CONTROLLERBUTTONDOWN:
case SDL_CONTROLLERBUTTONUP:
Controls_WrapGameControllerKey(event.cbutton.button, event.cbutton.state);
break;
// Joystick events are still invoked, even if joystick is initialized as game
// controller - that's why we need sdl_joystick checking - to filter out
// duplicate event calls.
case SDL_JOYAXISMOTION:
if(sdl_joystick)
{
Controls_JoyAxis(event.jaxis.axis, event.jaxis.value);
}
break;
case SDL_JOYHATMOTION:
if(sdl_joystick)
{
Controls_JoyHat(event.jhat.value);
}
break;
case SDL_JOYBUTTONDOWN:
case SDL_JOYBUTTONUP:
// NOTE: Joystick button numbers are passed with added JOY_BUTTON_MASK (1000).
if(sdl_joystick)
{
Controls_Key((event.jbutton.button + JOY_BUTTON_MASK), event.jbutton.state);
}
break;
case SDL_TEXTINPUT:
case SDL_TEXTEDITING:
if(Con_IsShown() && event.key.state)
{
Con_Filter(event.text.text);
return;
}
break;
case SDL_KEYUP:
case SDL_KEYDOWN:
if((event.key.keysym.scancode == SDL_SCANCODE_F4) &&
(event.key.state == SDL_PRESSED) &&
(event.key.keysym.mod & KMOD_ALT))
{
Engine_SetDone();
break;
}
if(Con_IsShown() && event.key.state)
{
switch(event.key.keysym.sym)
{
case SDLK_RETURN:
case SDLK_UP:
case SDLK_DOWN:
case SDLK_LEFT:
case SDLK_RIGHT:
case SDLK_HOME:
case SDLK_END:
case SDLK_PAGEUP:
case SDLK_PAGEDOWN:
case SDLK_BACKSPACE:
case SDLK_DELETE:
Con_Edit(event.key.keysym.sym);
break;
default:
break;
}
return;
}
else
{
Controls_Key(event.key.keysym.scancode, event.key.state);
// DEBUG KEYBOARD COMMANDS
Controls_DebugKeys(event.key.keysym.scancode, event.key.state);
if((screen_info.debug_view_state == debug_view_state_e::model_view) && event.key.state)
{
TestModelApplyKey(event.key.keysym.scancode);
}
}
break;
case SDL_QUIT:
Engine_SetDone();
break;
case SDL_WINDOWEVENT:
if(event.window.event == SDL_WINDOWEVENT_RESIZED)
{
Engine_Resize(event.window.data1, event.window.data2, event.window.data1, event.window.data2);
}
break;
default:
break;
}
}
renderer.debugDrawer->DrawLine(from, to, color, color);
}
void Engine_JoyRumble(float power, int time)
{
// JoyRumble is a simple wrapper for SDL's haptic rumble play.
if(sdl_haptic)
{
SDL_HapticRumblePlay(sdl_haptic, power, time);
}
}
void Engine_MainLoop()
{
float time = 0.0f;
float newtime = 0.0f;
float oldtime = Sys_FloatTime();
float time_cycl = 0.0f;
const int max_cycles = 64;
int cycles = 0;
char fps_str[32] = "0.0";
while(!engine_done)
{
newtime = Sys_FloatTime();
time = newtime - oldtime;
oldtime = newtime;
time *= time_scale;
if(engine_set_zero_time)
{
engine_set_zero_time = 0;
time = 0.0f;
}
else if(time > 1.0f / 30.0f)
{
time = 1.0f / 30.0f;
}
engine_frame_time = time;
if(cycles < max_cycles)
{
cycles++;
time_cycl += time;
}
else
{
screen_info.fps = ((float)max_cycles / time_cycl);
snprintf(fps_str, 32, "%.1f", screen_info.fps);
cycles = 0;
time_cycl = 0.0f;
}
Sys_ResetTempMem();
Engine_PollSDLEvents();
gl_text_line_p fps = GLText_OutTextXY(10.0f, 10.0f, fps_str);
if(fps)
{
fps->x_align = GLTEXT_ALIGN_RIGHT;
fps->y_align = GLTEXT_ALIGN_BOTTOM;
fps->font_id = FONT_PRIMARY;
fps->style_id = FONTSTYLE_MENU_TITLE;
}
int codec_end_state = stream_codec_check_end(&engine_video);
if(codec_end_state == 1)
{
StreamTrack_Stop(Audio_GetStreamExternal());
}
if(codec_end_state >= 0)
{
if(screen_info.debug_view_state != debug_view_state_e::model_view)
{
Game_Frame(time);
Gameflow_ProcessCommands();
}
Audio_Update(time);
Engine_Display(time);
}
else
{
stream_track_p s = Audio_GetStreamExternal();
stream_codec_audio_lock(&engine_video);
if(engine_video.codec.audio.buff && (engine_video.codec.audio.buff_offset >= s->buffer_offset))
{
s->current_volume = audio_settings.sound_volume;
StreamTrack_UpdateBuffer(s, engine_video.codec.audio.buff, engine_video.codec.audio.buff_size,
engine_video.codec.audio.bits_per_sample, engine_video.codec.audio.channels, engine_video.codec.audio.sample_rate);
}
if(StreamTrack_IsNeedUpdateBuffer(s))
{
engine_video.update_audio = 1;
}
stream_codec_audio_unlock(&engine_video);
StreamTrack_Play(s);
stream_codec_video_lock(&engine_video);
if(engine_video.codec.video.rgba)
{
Gui_SetScreenTexture(engine_video.codec.video.rgba, engine_video.codec.video.width, engine_video.codec.video.height, 32);
}
stream_codec_video_unlock(&engine_video);
Gui_DrawLoadScreen(-1);
if(control_states.gui_inventory)
{
stream_codec_stop(&engine_video, 0);
}
}
}
}
void TestModelApplyKey(int key)
{
switch(key)
{
case SDL_SCANCODE_LEFTBRACKET:
test_model_index--;
SetTestModel(test_model_index);
break;
case SDL_SCANCODE_RIGHTBRACKET:
test_model_index++;
SetTestModel(test_model_index);
break;
case SDL_SCANCODE_O:
if(test_model.animations.prev_animation > 0)
{
Anim_SetAnimation(&test_model.animations, test_model.animations.prev_animation - 1, 0);
}
break;
case SDL_SCANCODE_P:
Anim_SetAnimation(&test_model.animations, test_model.animations.prev_animation + 1, 0);
break;
default:
break;
}
}
void SetTestModel(int index)
{
skeletal_model_p sm;
uint32_t sm_count;
World_GetSkeletalModelsInfo(&sm, &sm_count);
index = (index >= 0) ? (index) : (sm_count - 1);
index = (index >= sm_count) ? (0) : (index);
if(sm_count > 0)
{
test_model_index = index;
SSBoneFrame_Clear(&test_model);
SSBoneFrame_CreateFromModel(&test_model, sm + index);
}
}
void ShowModelView(float time)
{
static engine_transform_t tr;
static float test_model_angles[3] = {45.0f, 45.0f, 0.0f};
static float test_model_dist = 1024.0f;
static float test_model_z_offset = 256.0f;
uint32_t sm_count;
skeletal_model_p sm = NULL;
World_GetSkeletalModelsInfo(&sm, &sm_count);
if((test_model_index >= 0) && (test_model_index < sm_count))
{
sm += test_model_index;
}
if(sm && (test_model.animations.model == sm))
{
float subModelView[16], subModelViewProjection[16];
float *cam_pos = engine_camera.transform.M4x4 + 12;
animation_frame_p af = sm->animations + test_model.animations.prev_animation;
const int current_light_number = 0;
const lit_shader_description *shader = renderer.shaderManager->getEntityShader(current_light_number);
if(control_states.look_right || control_states.move_right)
{
test_model_angles[0] += time * 256.0f;
}
if(control_states.look_left || control_states.move_left)
{
test_model_angles[0] -= time * 256.0f;
}
if(control_states.look_up)
{
test_model_angles[1] += time * 256.0f;
}
if(control_states.look_down)
{
test_model_angles[1] -= time * 256.0f;
}
if(control_states.move_forward && (test_model_dist >= 8.0f))
{
test_model_dist -= time * 512.0f;
}
if(control_states.move_backward)
{
test_model_dist += time * 512.0f;
}
if(control_states.move_up)
{
test_model_z_offset += time * 512.0f;
}
if(control_states.move_down)
{
test_model_z_offset -= time * 512.0f;
}
test_model.transform = &tr;
Mat4_E_macro(tr.M4x4);
Mat4_E_macro(engine_camera.transform.M4x4);
engine_camera.transform.angles[0] = test_model_angles[0];
engine_camera.transform.angles[1] = test_model_angles[1] + 90.0f;
engine_camera.transform.angles[2] = test_model_angles[2];
Mat4_SetAnglesZXY(engine_camera.transform.M4x4, engine_camera.transform.angles);
cam_pos[0] = -engine_camera.transform.M4x4[8 + 0] * test_model_dist;
cam_pos[1] = -engine_camera.transform.M4x4[8 + 1] * test_model_dist;
cam_pos[2] = -engine_camera.transform.M4x4[8 + 2] * test_model_dist + test_model_z_offset;
Cam_Apply(&engine_camera);
test_model.animations.frame_time += time;
test_model.animations.prev_frame = test_model.animations.frame_time / test_model.animations.period;
if(test_model.animations.prev_frame >= af->frames_count)