bad char
[vg.git] / vg.h
1 /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
2
3 /*
4
5 .-. VG Event loop
6 | 0 |
7 | | .---------------------------------------------------------.
8 |API| | vg_enter( int argc, char *argv[], const char *window_name |
9 | | '---------------------------------------------------------'
10 | | |
11 | | v
12 |IMP| vg_launch_opt(void) <--.
13 | | | |
14 | | |'---------------'
15 | | | .-.
16 | | |'-----------------------------------| 1 |------.
17 | | | | | |
18 | | | | | v
19 | | | |IMP| vg_preload(void)
20 | | | | | |
21 | | .-----+. | | v
22 | | | | |IMP| vg_load(void)
23 | | | v '___' |
24 |IMP| | vg_framebuffer_resize(void) |
25 | | | | |
26 |IMP| | |.------------- vg_start(void) ---------------'
27 | | | |
28 | | | v
29 |IMP| | vg_update(void)
30 | | | |
31 | | | .-----+.
32 | | | | |
33 | | | | v
34 |IMP| | '- vg_update_fixed(void)
35 | | | |
36 | | | .-'
37 | | | |
38 | | | v
39 |IMP| | vg_update_post(void)
40 | | | |
41 | | | v
42 |IMP| | vg_render(void)
43 | | | |
44 | | | v
45 |IMP| | vg_ui(void)
46 | | | |
47 | | '----'
48 '___'
49
50 .-.
51 | ? |
52 | | .-------------------------------------.
53 |API| | vg_fatal_exit_loop( const char *err ) |
54 | | '-------------------------------------'
55 | | |
56 | | .------+.
57 | | | |
58 | | | v
59 |IMP| '- vg_framebuffer_resize(void)
60 '___'
61
62 */
63
64 #ifndef VG_STATIC
65 #define VG_STATIC
66 #endif
67
68 /* API */
69 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name );
70
71 /* Thread 1 */
72 VG_STATIC void vg_preload(void);
73 VG_STATIC void vg_load(void);
74
75 /* Main thread */
76 VG_STATIC void vg_launch_opt(void);
77 VG_STATIC void vg_start(void);
78
79 VG_STATIC void vg_framebuffer_resize(int w, int h);
80 VG_STATIC void vg_update(void);
81 VG_STATIC void vg_update_fixed(void);
82 VG_STATIC void vg_update_post(void);
83
84 VG_STATIC void vg_render(void);
85 VG_STATIC void vg_ui(void);
86
87 #ifndef VG_HEADER_H
88 #define VG_HEADER_H
89
90 #include "vg_platform.h"
91 #include "vg_mem.h"
92
93 #ifndef _WIN32
94 #include <execinfo.h>
95 #endif
96
97 #ifdef VG_GAME
98 #include "dep/glad/glad.h"
99 #include "submodules/SDL/include/SDL.h"
100 #include "vg_stdint.h"
101
102 void vg_register_exit( void( *funcptr )(void), const char *name );
103
104 #include "vg_m.h"
105 #include "vg_io.h"
106 #include "vg_log.h"
107 #include "vg_steam.h"
108
109 //#define VG_SYNC_DEBUG
110 #ifdef VG_SYNC_DEBUG
111 #define VG_SYNC_LOG(STR,...) \
112 vg_info(STR,SDL_GetThreadID(NULL),##__VA_ARGS__)
113 #else
114 #define VG_SYNC_LOG(...)
115 #endif
116
117 struct vg
118 {
119 /* Engine sync */
120 SDL_Window *window;
121 SDL_GLContext gl_context;
122
123 SDL_SpinLock sl_context;
124 SDL_sem *sem_allow_exec,
125 *sem_exec_finished,
126 *sem_loader;
127
128 SDL_threadID thread_id_main,
129 thread_id_loader,
130 thread_id_with_opengl_context;
131 int context_ownership_depth;
132
133 int exec_context;
134
135 enum engine_status
136 {
137 k_engine_status_none,
138 k_engine_status_running,
139 k_engine_status_crashed
140 }
141 engine_status;
142 const char *str_const_engine_err;
143 int is_loaded;
144
145 /* Window information */
146 int window_x,
147 window_y,
148 samples,
149 window_should_close;
150
151 float refresh_rate;
152
153 double mouse_pos[2];
154 v2f mouse_delta,
155 mouse_wheel;
156
157 /* Runtime */
158 double time,
159 time_delta,
160 frame_delta,
161 time_real,
162 time_real_last,
163 time_rate,
164 accumulator;
165
166 int fixed_iterations;
167
168 enum engine_stage
169 {
170 k_engine_stage_none,
171 k_engine_stage_update,
172 k_engine_stage_update_fixed,
173 k_engine_stage_rendering,
174 k_engine_stage_ui
175 }
176 engine_stage;
177
178 /* graphics */
179 m4x4f pv;
180 enum quality_profile
181 {
182 k_quality_profile_high = 0,
183 k_quality_profile_low = 1,
184 }
185 quality_profile;
186 }
187 VG_STATIC vg = { .time_rate = 1.0 };
188
189 enum vg_thread_purpose
190 {
191 k_thread_purpose_nothing,
192 k_thread_purpose_main,
193 k_thread_purpose_loader
194 };
195
196 VG_STATIC void vg_fatal_exit_loop( const char *error );
197
198 /*
199 * Checks if the engine is running
200 */
201 VG_STATIC void _vg_ensure_engine_running(void)
202 {
203 /* Check if the engine is no longer running */
204 SDL_AtomicLock( &vg.sl_context );
205 enum engine_status status = vg.engine_status;
206 SDL_AtomicUnlock( &vg.sl_context );
207
208 if( status != k_engine_status_running )
209 {
210 while(1)
211 {
212 VG_SYNC_LOG( "[%d] No longer running...\n");
213 SDL_Delay(1000);
214 }
215 }
216 }
217
218 VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
219 {
220 SDL_AtomicLock( &vg.sl_context );
221
222 if( vg.thread_id_main == SDL_GetThreadID(NULL) )
223 {
224 SDL_AtomicUnlock( &vg.sl_context );
225 return k_thread_purpose_main;
226 }
227 else
228 {
229 SDL_AtomicUnlock( &vg.sl_context );
230 return k_thread_purpose_loader;
231 }
232 }
233
234 /*
235 * Sync execution so that the OpenGL context is switched onto this thread.
236 * Anything after this call will be in a valid context.
237 */
238 VG_STATIC void vg_acquire_thread_sync(void)
239 {
240 /* We dont want to do anything if this is the main thread */
241
242 if( vg_thread_purpose() == k_thread_purpose_loader )
243 {
244 VG_SYNC_LOG( "[%d] vg_acquire_thread_sync()\n" );
245 _vg_ensure_engine_running();
246
247 SDL_AtomicLock( &vg.sl_context );
248 if( vg.context_ownership_depth == 0 )
249 {
250 vg.context_ownership_depth ++;
251 vg.exec_context = 1;
252 SDL_AtomicUnlock( &vg.sl_context );
253
254 /* wait until told we can go */
255 VG_SYNC_LOG( "[%d] Waiting to acuire sync.\n" );
256 SDL_SemWait( vg.sem_allow_exec );
257
258 _vg_ensure_engine_running();
259
260 SDL_GL_MakeCurrent( vg.window, vg.gl_context );
261 VG_SYNC_LOG( "[%d] granted\n" );
262 }
263 else
264 {
265 vg.context_ownership_depth ++;
266 VG_SYNC_LOG( "[%d] granted\n" );
267 SDL_AtomicUnlock( &vg.sl_context );
268 }
269 }
270 }
271
272 /*
273 * Signify that we are done with the OpenGL context in this thread.
274 * Anything after this call will be in an undefined context.
275 */
276 VG_STATIC void vg_release_thread_sync(void)
277 {
278 if( vg_thread_purpose() == k_thread_purpose_loader )
279 {
280 VG_SYNC_LOG( "[%d] vg_release_thread_sync()\n" );
281
282 SDL_AtomicLock( &vg.sl_context );
283 vg.context_ownership_depth --;
284
285 if( vg.context_ownership_depth == 0 )
286 {
287 SDL_AtomicUnlock( &vg.sl_context );
288 VG_SYNC_LOG( "[%d] Releasing context.\n" );
289 SDL_GL_MakeCurrent( NULL, NULL );
290 SDL_SemPost( vg.sem_exec_finished );
291 }
292 else
293 SDL_AtomicUnlock( &vg.sl_context );
294 }
295 }
296
297 VG_STATIC void _vg_run_synced(void)
298 {
299 SDL_AtomicLock( &vg.sl_context );
300
301 if( vg.exec_context != 0 )
302 {
303 VG_SYNC_LOG( "[%d] _vg_run_synced() (%d).\n", vg.exec_context );
304 vg.exec_context = 0;
305 SDL_AtomicUnlock( &vg.sl_context );
306
307 /* allow operations to go */
308 SDL_GL_MakeCurrent( NULL, NULL );
309 SDL_SemPost( vg.sem_allow_exec );
310
311 /* wait for operations to complete */
312 VG_SYNC_LOG( "[%d] Waiting for content.\n" );
313 SDL_SemWait( vg.sem_exec_finished );
314
315 /* check if we killed the engine */
316 _vg_ensure_engine_running();
317
318 /* re-engage main thread */
319 VG_SYNC_LOG( "[%d] Re-engaging.\n" );
320 SDL_GL_MakeCurrent( vg.window, vg.gl_context );
321 }
322 else
323 {
324 VG_SYNC_LOG( "[%d] Nothing to do.\n" );
325 SDL_AtomicUnlock( &vg.sl_context );
326 }
327 }
328
329 VG_STATIC void _vg_opengl_sync_init(void)
330 {
331 vg.sem_allow_exec = SDL_CreateSemaphore(0);
332 vg.sem_exec_finished = SDL_CreateSemaphore(0);
333 vg.sem_loader = SDL_CreateSemaphore(1);
334 }
335
336 VG_STATIC void vg_checkgl( const char *src_info );
337 #define VG_STRINGIT( X ) #X
338 #define VG_CHECK_GL_ERR() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
339
340 #include "vg_console.h"
341 #include "vg_profiler.h"
342 #include "vg_audio.h"
343 #include "vg_shader.h"
344 #include "vg_tex.h"
345 #include "vg_input.h"
346 #include "vg_ui.h"
347 #include "vg_lines.h"
348 #include "vg_loader.h"
349 #include "vg_opt.h"
350
351 /* Diagnostic */
352 VG_STATIC struct vg_profile vg_prof_update = {.name="update()"},
353 vg_prof_render = {.name="render()"};
354
355 VG_STATIC void vg_checkgl( const char *src_info )
356 {
357 int fail = 0;
358
359 GLenum err;
360 while( (err = glGetError()) != GL_NO_ERROR )
361 {
362 vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
363 fail = 1;
364 }
365
366 if( fail )
367 vg_fatal_exit_loop( "OpenGL Error" );
368 }
369
370 VG_STATIC void vg_bake_shaders(void)
371 {
372 vg_acquire_thread_sync();
373
374 vg_function_push( (struct vg_cmd)
375 {
376 .name = "reload_shaders",
377 .function = vg_shaders_live_recompile
378 });
379
380 vg_shaders_compile();
381 vg_release_thread_sync();
382 }
383
384 VG_STATIC void _vg_load_full(void)
385 {
386 vg_preload();
387
388 /* internal */
389 vg_loader_step( vg_input_init, vg_input_free );
390 vg_loader_step( vg_lines_init, NULL );
391 vg_loader_step( vg_audio_init, vg_audio_free );
392 vg_loader_step( vg_profiler_init, NULL );
393
394 /* client */
395 vg_load();
396 }
397
398 VG_STATIC void _vg_process_events(void)
399 {
400 /* Update timers */
401 vg.time_real_last = vg.time_real;
402 vg.time_real = (double)SDL_GetTicks64() / 1000.0;
403 vg.frame_delta = vg.time_real-vg.time_real_last;
404
405 v2_zero( vg.mouse_wheel );
406 v2_zero( vg.mouse_delta );
407
408 /* SDL event loop */
409 SDL_Event event;
410 while( SDL_PollEvent( &event ) )
411 {
412 if( event.type == SDL_KEYDOWN )
413 {
414 console_proc_key( event.key.keysym );
415 }
416 else if( event.type == SDL_MOUSEWHEEL )
417 {
418 vg.mouse_wheel[0] += event.wheel.preciseX;
419 vg.mouse_wheel[1] += event.wheel.preciseY;
420 }
421 else if( event.type == SDL_CONTROLLERAXISMOTION ||
422 event.type == SDL_CONTROLLERBUTTONDOWN ||
423 event.type == SDL_CONTROLLERBUTTONUP ||
424 event.type == SDL_CONTROLLERDEVICEADDED ||
425 event.type == SDL_CONTROLLERDEVICEREMOVED
426 )
427 {
428 vg_input_controller_event( &event );
429 }
430 else if( event.type == SDL_MOUSEMOTION )
431 {
432 vg.mouse_delta[0] += event.motion.xrel;
433 vg.mouse_delta[1] += event.motion.yrel;
434 }
435 else if( event.type == SDL_WINDOWEVENT )
436 {
437 if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED )
438 {
439 int w, h;
440 SDL_GL_GetDrawableSize( vg.window, &w, &h );
441
442 if( !w || !h )
443 {
444 vg_warn( "Got a invalid framebuffer size: "
445 "%dx%d... ignoring\n", w, h );
446 }
447 else
448 {
449 vg.window_x = w;
450 vg.window_y = h;
451
452 vg_framebuffer_resize(w,h);
453 }
454 }
455 else if( event.window.event == SDL_WINDOWEVENT_CLOSE )
456 {
457 vg.window_should_close = 1;
458 }
459 }
460 else if( event.type == SDL_TEXTINPUT )
461 {
462 console_proc_utf8( event.text.text );
463 }
464 }
465
466 vg.mouse_pos[0] += vg.mouse_delta[0];
467 vg.mouse_pos[1] += vg.mouse_delta[1];
468
469 /* Update input */
470 vg_update_inputs();
471 }
472
473 VG_STATIC void _vg_gameloop_update(void)
474 {
475 vg_profile_begin( &vg_prof_update );
476
477 vg.engine_stage = k_engine_stage_update;
478 vg_update();
479
480 /* Fixed update loop */
481 vg.engine_stage = k_engine_stage_update_fixed;
482 vg.accumulator += vg.time_delta;
483
484 vg.fixed_iterations = 0;
485 vg_lines.allow_input = 1;
486 while( vg.accumulator >= (VG_TIMESTEP_FIXED-0.00125) )
487 {
488 vg_update_fixed();
489 vg_lines.allow_input = 0;
490
491 vg.accumulator -= VG_TIMESTEP_FIXED;
492 vg.accumulator = VG_MAX( 0.0, vg.accumulator );
493
494 vg.fixed_iterations ++;
495 if( vg.fixed_iterations == 8 )
496 {
497 break;
498 }
499 }
500 vg_lines.allow_input = 1;
501
502 vg.engine_stage = k_engine_stage_update;
503 vg_update_post();
504 vg_profile_end( &vg_prof_update );
505 }
506
507 VG_STATIC void _vg_gameloop_render(void)
508 {
509 vg_profile_begin( &vg_prof_render );
510
511 if( vg.is_loaded )
512 {
513 /* render */
514 vg.engine_stage = k_engine_stage_rendering;
515 vg_render();
516
517 /* ui */
518 vg.engine_stage = k_engine_stage_ui;
519 {
520 ui_begin( vg.window_x, vg.window_y );
521
522 /* TODO */
523 ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 );
524
525 vg_profile_drawn(
526 (struct vg_profile *[]){&vg_prof_update,&vg_prof_render}, 2,
527 (1.0f/(float)vg.refresh_rate)*1000.0f,
528 (ui_rect){ 4, 4, 250, 0 }, 0
529 );
530
531 if( vg_profiler )
532 {
533
534 char perf[128];
535
536 snprintf( perf, 127,
537 "x: %d y: %d\n"
538 "refresh: %.1f (%.1fms)\n"
539 "samples: %d\n"
540 "iterations: %d (acc: %.3fms%%)\n",
541 vg.window_x, vg.window_y,
542 vg.refresh_rate, (1.0f/vg.refresh_rate)*1000.0f,
543 vg.samples,
544 vg.fixed_iterations,
545 (vg.accumulator/VG_TIMESTEP_FIXED)*100.0f );
546
547 ui_text( (ui_rect){258, 4+24+12,0,0},perf, 1,0);
548 }
549
550 /* FIXME */
551 audio_debug_ui( vg.pv );
552 vg_ui();
553 _vg_console_draw();
554
555 ui_resolve();
556 ui_draw( NULL );
557 }
558 }
559
560 vg_profile_end( &vg_prof_render );
561 }
562
563 VG_STATIC void _vg_gameloop(void)
564 {
565 vg.accumulator = 0.75f * (1.0f/60.0f);
566
567 int post_start = 0;
568 while(1)
569 {
570 _vg_process_events();
571
572 if( vg.window_should_close )
573 break;
574
575 /* scaled time */
576 vg.time_delta = vg.frame_delta * vg.time_rate;
577 vg.time += vg.time_delta;
578
579 if( vg.is_loaded )
580 {
581 if( !post_start )
582 {
583 vg_start();
584 post_start = 1;
585 }
586 }
587 else
588 {
589 _vg_loader_render();
590 }
591
592 _vg_gameloop_update();
593 _vg_gameloop_render();
594
595 SDL_GL_SwapWindow( vg.window );
596 _vg_run_synced();
597 }
598 }
599
600 VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] )
601 {
602 char *arg;
603 while( vg_argp( argc, argv ) )
604 {
605 if( (arg = vg_opt_arg( 'w' )) )
606 {
607 vg.window_x = atoi( arg );
608 }
609
610 if( (arg = vg_opt_arg( 'h' )) )
611 {
612 vg.window_y = atoi( arg );
613 }
614
615 if( (arg = vg_long_opt_arg( "samples" )) )
616 {
617 vg.samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
618 }
619
620 if( vg_long_opt( "use-libc-malloc" ) )
621 {
622 vg_mem.use_libc_malloc = 1;
623 }
624
625 if( vg_long_opt( "high-performance" ) )
626 {
627 vg.quality_profile = k_quality_profile_low;
628 }
629
630 vg_launch_opt();
631 }
632 }
633
634 VG_STATIC void _vg_init_window( const char *window_name )
635 {
636 vg_info( "SDL_INIT\n" );
637
638 if( SDL_Init( SDL_INIT_VIDEO ) != 0 )
639 {
640 vg_error( "SDL_Init failed: %s\n", SDL_GetError() );
641 exit(0);
642 }
643
644 SDL_InitSubSystem( SDL_INIT_AUDIO );
645 SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER );
646
647 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
648 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
649 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
650 SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,
651 SDL_GL_CONTEXT_PROFILE_CORE );
652
653 SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
654 SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH );
655
656 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
657 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
658 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
659 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
660 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0 );
661
662 /*
663 * Get monitor information
664 */
665 vg_info( "Getting display count\n" );
666 int display_count = 0, display_index = 0, mode_index = 0;
667 if( (display_count = SDL_GetNumVideoDisplays()) < 1 )
668 {
669 vg_error( "SDL_GetNumVideoDisplays returned: %i\n", display_count );
670 exit(0);
671 }
672
673
674
675
676 /* TODO: Allow chosing the modes at startup */
677 vg_info( "Getting default display mode\n" );
678 SDL_DisplayMode vm_ideal = { 0, 0, 0, 0, 0 };
679 if( SDL_GetDisplayMode( display_index, mode_index, &vm_ideal ) != 0 )
680 {
681 vg_error( "SDL_GetDisplayMode failed: %s", SDL_GetError() );
682 exit(0);
683 }
684
685 if( vg.window_x )
686 vm_ideal.w = vg.window_x;
687
688 if( vg.window_y )
689 vm_ideal.h = vg.window_y;
690
691
692 SDL_DisplayMode vm_best;
693 if( !SDL_GetClosestDisplayMode( display_index, &vm_ideal, &vm_best ) )
694 {
695 vg_error( "SDL_GetClosestDisplayMode failed: %s", SDL_GetError() );
696 exit(0);
697 }
698
699 vg.refresh_rate = vm_best.refresh_rate;
700 vg.window_x = vm_best.w;
701 vg.window_y = vm_best.h;
702
703 vg_info( "CreateWindow( %d %d @%.2fhz )\n", vg.window_x, vg.window_y,
704 vg.refresh_rate );
705
706 /* TODO: Allow selecting closest video mode from launch opts */
707 if((vg.window = SDL_CreateWindow( window_name,
708 SDL_WINDOWPOS_UNDEFINED,
709 SDL_WINDOWPOS_UNDEFINED,
710 vg.window_x, vg.window_y,
711
712 SDL_WINDOW_FULLSCREEN_DESKTOP |
713 SDL_WINDOW_OPENGL |
714 SDL_WINDOW_INPUT_GRABBED )))
715 {
716 if( SDL_SetWindowDisplayMode( vg.window, &vm_best ) )
717 {
718 vg_error( "SDL_SetWindowDisplayMode failed: %s", SDL_GetError() );
719 SDL_Quit();
720 exit(0);
721 }
722 }
723 else
724 {
725 vg_error( "SDL_CreateWindow failed: %s", SDL_GetError() );
726 exit(0);
727 }
728
729 vg_info( "CreateContext\n" );
730
731 /*
732 * OpenGL loading
733 */
734 if( (vg.gl_context = SDL_GL_CreateContext(vg.window) ))
735 {
736 SDL_GL_GetDrawableSize( vg.window, &vg.window_x, &vg.window_y );
737 vg_success( "Window created (%dx%d)\n", vg.window_x, vg.window_y );
738 }
739 else
740 {
741 vg_error( "SDL_GL_CreateContext failed: %s\n", SDL_GetError() );
742 SDL_Quit();
743 exit(0);
744 }
745
746 if( !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) )
747 {
748 vg_error( "Glad Failed to initialize\n" );
749 SDL_GL_DeleteContext( vg.gl_context );
750 SDL_Quit();
751 exit(0);
752 }
753
754 const unsigned char* glver = glGetString( GL_VERSION );
755 vg_success( "Load setup complete, OpenGL version: %s\n", glver );
756
757 vg_info( "Setting swap interval\n" );
758
759 if( SDL_GL_SetSwapInterval( -1 ) == -1 )
760 {
761 vg_warn( "Adaptive Vsync not supported\n" );
762
763 if( SDL_GL_SetSwapInterval( 1 ) == -1 )
764 {
765 vg_fatal_exit_loop( "Cannot enable Vsync! You might be overriding it"
766 " in your graphics control panel.\n" );
767 }
768 else
769 vg_success( "Using vsync\n" );
770 }
771 else
772 vg_success( "Using adaptive Vsync\n" );
773
774 SDL_DisplayMode dispmode;
775 if( !SDL_GetWindowDisplayMode( vg.window, &dispmode ) )
776 {
777 if( dispmode.refresh_rate )
778 {
779 vg.refresh_rate = dispmode.refresh_rate;
780 vg_info( "Refresh rate: %d\n", dispmode.refresh_rate );
781 }
782 }
783 }
784
785 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
786 {
787 _vg_process_launch_opts_internal( argc, argv );
788
789 /* Systems init */
790 vg_alloc_quota();
791 _vg_log_init();
792 _vg_console_init();
793 _vg_init_window( window_name );
794
795 SDL_SetRelativeMouseMode(1);
796 vg.thread_id_main = SDL_GetThreadID(NULL);
797
798 /* Opengl-required systems */
799 _vg_ui_init();
800 _vg_loader_init();
801
802 vg.engine_status = k_engine_status_running;
803
804 _vg_opengl_sync_init();
805 vg_loader_start( _vg_load_full );
806 _vg_gameloop();
807
808 /* Shutdown */
809 _vg_console_write_persistent();
810
811 SDL_AtomicLock( &vg.sl_context );
812 vg.engine_status = k_engine_status_none;
813 SDL_AtomicUnlock( &vg.sl_context );
814
815 _vg_loader_free();
816
817 vg_success( "If you see this it means everything went.. \"well\".....\n" );
818
819 SDL_GL_DeleteContext( vg.gl_context );
820 SDL_Quit();
821 }
822
823 /*
824 * Immediately transfer away from calling thread into a safe loop, signal for
825 * others to shutdown, then free everything once the user closes the window.
826 *
827 * FIXME(bug): glfwWindowShouldClose() never returns 1 in windows via wine, ONLY
828 * when calling the program from outside its normal directory.
829 */
830 VG_STATIC void vg_fatal_exit_loop( const char *error )
831 {
832 /*
833 * https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
834 * thanks gnu <3
835 *
836 * TODO: this on windows?
837 */
838
839 #ifndef _WIN32
840
841 void *array[20];
842 char **strings;
843 int size, i;
844
845 size = backtrace( array, 20 );
846 strings = backtrace_symbols( array, size );
847
848 if( strings != NULL )
849 {
850 vg_error( "---------------- gnu backtrace -------------\n" );
851
852 for( int i=0; i<size; i++ )
853 vg_info( "%s\n", strings[i] );
854
855 vg_error( "---------------- gnu backtrace -------------\n" );
856 }
857
858 free( strings );
859
860 #endif
861
862 vg_error( "Fatal error: %s\n", error );
863
864 SDL_AtomicLock( &vg.sl_context );
865 if( vg.engine_status == k_engine_status_none )
866 {
867 SDL_AtomicUnlock( &vg.sl_context );
868
869 /* TODO: Correct shutdown before other systems */
870 exit(0);
871 }
872 else
873 {
874 SDL_AtomicUnlock( &vg.sl_context );
875
876 vg_acquire_thread_sync();
877
878 SDL_AtomicLock( &vg.sl_context );
879 vg.engine_status = k_engine_status_crashed;
880 vg.str_const_engine_err = error;
881 SDL_AtomicUnlock( &vg.sl_context );
882
883 /* Notify other thread for curtosey */
884 if( vg_thread_purpose() == k_thread_purpose_main )
885 {
886 SDL_AtomicLock( &vg.sl_context );
887
888 if( vg.exec_context != 0 )
889 SDL_SemPost( vg.sem_allow_exec );
890
891 SDL_AtomicUnlock( &vg.sl_context );
892 }
893
894 vg_audio_free();
895
896 while(1)
897 {
898 _vg_process_events();
899 if( vg.window_should_close )
900 break;
901
902 if( vg_getkey( SDLK_ESCAPE ) )
903 break;
904
905 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
906 glEnable(GL_BLEND);
907 glDisable(GL_DEPTH_TEST);
908 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
909 glBlendEquation(GL_FUNC_ADD);
910
911 glClearColor( 0.15f + sinf(vg.time_real)*0.1f, 0.0f, 0.0f,1.0f );
912 glClear( GL_COLOR_BUFFER_BIT );
913 glViewport( 0,0, vg.window_x, vg.window_y );
914
915 _vg_render_log();
916 SDL_GL_SwapWindow( vg.window );
917 }
918
919 /* Can now shutdown and EXIT */
920 _vg_loader_free();
921 SDL_GL_DeleteContext( vg.gl_context );
922 SDL_Quit();
923 exit(0);
924 }
925 }
926
927 #else /* VG_GAME */
928
929 VG_STATIC void vg_fatal_exit_loop( const char *error )
930 {
931 vg_error( "Fatal error: %s\n", error );
932 exit(0);
933 }
934
935 #endif /* VG_GAME */
936
937 /*
938 * Graphic cards will check these to force it to use the GPU
939 */
940 u32 NvOptimusEnablement = 0x00000001;
941 int AmdPowerXpressRequestHighPerformance = 1;
942
943 #endif /* VG_HEADER_H */