d98828e3bd380b2f9d041436e2dc29045110487f
[vg.git] / vg.h
1 /* Copyright (C) 2021-2023 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 #ifndef VG_HEADER_H
53 #define VG_HEADER_H
54
55 #include "vg_platform.h"
56 #include "vg_mem.h"
57
58 #ifndef _WIN32
59 #include <execinfo.h>
60 #endif
61
62 VG_STATIC void vg_print_backtrace(void)
63 {
64 #ifndef _WIN32
65
66 void *array[20];
67 char **strings;
68 int size, i;
69
70 size = backtrace( array, 20 );
71 strings = backtrace_symbols( array, size );
72
73 if( strings != NULL ){
74 vg_error( "---------------- gnu backtrace -------------\n" );
75
76 for( int i=0; i<size; i++ )
77 vg_info( "%s\n", strings[i] );
78
79 vg_error( "---------------- gnu backtrace -------------\n" );
80 }
81
82 free( strings );
83
84 #endif
85 }
86
87 #ifdef VG_GAME
88 #include "dep/glad/glad.h"
89 #include "submodules/SDL/include/SDL.h"
90 #include "vg_stdint.h"
91
92 void vg_register_exit( void( *funcptr )(void), const char *name );
93
94 #include "vg_m.h"
95 #include "vg_io.h"
96 #include "vg_log.h"
97 #include "vg_steam.h"
98 #include <setjmp.h>
99
100 //#define VG_SYNC_DEBUG
101 #ifdef VG_SYNC_DEBUG
102 #define VG_SYNC_LOG(STR,...) \
103 vg_info(STR,SDL_GetThreadID(NULL),##__VA_ARGS__)
104 #else
105 #define VG_SYNC_LOG(...)
106 #endif
107
108 /* API */
109 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name );
110
111 /* Thread 1 */
112 VG_STATIC void vg_preload(void);
113 VG_STATIC void vg_load(void);
114
115 /* Main thread */
116 VG_STATIC void vg_launch_opt(void);
117 VG_STATIC void vg_start(void);
118
119 VG_STATIC void vg_framebuffer_resize(int w, int h);
120 VG_STATIC void vg_update(void);
121 VG_STATIC void vg_update_fixed(void);
122 VG_STATIC void vg_update_post(void);
123
124 VG_STATIC void vg_render(void);
125 VG_STATIC void vg_ui(void);
126
127 struct vg
128 {
129 /* Engine sync */
130 SDL_Window *window;
131 SDL_GLContext gl_context;
132
133 SDL_sem *sem_loader; /* allows only one loader at a time */
134
135 jmp_buf env_loader_exit;
136
137 SDL_threadID thread_id_main,
138 thread_id_loader;
139
140 SDL_SpinLock sl_status;
141 enum engine_status
142 {
143 k_engine_status_none,
144 k_engine_status_load_internal,
145 k_engine_status_running,
146 k_engine_status_crashed
147 }
148 engine_status;
149
150 /* Window information */
151 int window_x,
152 window_y,
153 samples,
154 window_should_close;
155
156 int display_refresh_rate,
157 fps_limit; /* 0: use vsync, >0: cap fps to this, no vsync */
158
159 enum vsync_feature
160 {
161 k_vsync_feature_disabled=0,
162 k_vsync_feature_enabled=1,
163 k_vsync_feature_enabled_adaptive=2,
164 k_vsync_feature_error=3
165 }
166 vsync_feature;
167
168 double mouse_pos[2];
169 v2f mouse_delta,
170 mouse_wheel;
171
172 /* Runtime */
173 double time,
174 time_delta,
175 time_rate,
176
177 time_fixed_accumulator,
178 time_fixed_extrapolate,
179 time_frame_delta;
180
181 u64 time_hp, time_hp_last, time_spinning;
182
183 int fixed_iterations;
184
185 enum engine_stage
186 {
187 k_engine_stage_none,
188 k_engine_stage_update,
189 k_engine_stage_update_fixed,
190 k_engine_stage_rendering,
191 k_engine_stage_ui
192 }
193 engine_stage;
194
195 /* graphics */
196 m4x4f pv;
197 enum quality_profile
198 {
199 k_quality_profile_high = 0,
200 k_quality_profile_low = 1,
201 }
202 quality_profile;
203 }
204 VG_STATIC vg = { .time_rate = 1.0 };
205
206 enum vg_thread_purpose
207 {
208 k_thread_purpose_nothing,
209 k_thread_purpose_main,
210 k_thread_purpose_loader
211 };
212
213 #include "vg_async.h"
214
215 VG_STATIC enum engine_status _vg_engine_status(void)
216 {
217 SDL_AtomicLock( &vg.sl_status );
218 enum engine_status status = vg.engine_status;
219 SDL_AtomicUnlock( &vg.sl_status );
220
221 return status;
222 }
223
224 VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
225 {
226 SDL_AtomicLock( &vg.sl_status );
227
228 if( vg.thread_id_main == SDL_GetThreadID(NULL) ){
229 SDL_AtomicUnlock( &vg.sl_status );
230 return k_thread_purpose_main;
231 }
232 else{
233 SDL_AtomicUnlock( &vg.sl_status );
234 return k_thread_purpose_loader;
235 }
236 }
237
238 VG_STATIC void _vg_opengl_sync_init(void)
239 {
240 vg.sem_loader = SDL_CreateSemaphore(1);
241 }
242
243 VG_STATIC void vg_checkgl( const char *src_info );
244 #define VG_STRINGIT( X ) #X
245 #define VG_CHECK_GL_ERR() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) )
246
247 #include "vg_console.h"
248 #include "vg_profiler.h"
249 #include "vg_audio.h"
250 #include "vg_shader.h"
251 #include "vg_tex.h"
252 #include "vg_input.h"
253 #include "vg_ui.h"
254 #include "vg_lines.h"
255 #include "vg_loader.h"
256 #include "vg_opt.h"
257
258 /* Diagnostic */
259 VG_STATIC struct vg_profile vg_prof_update = {.name="update()"},
260 vg_prof_render = {.name="render()"},
261 vg_prof_swap = {.name="swap"};
262
263 VG_STATIC void vg_checkgl( const char *src_info )
264 {
265 int fail = 0;
266
267 GLenum err;
268 while( (err = glGetError()) != GL_NO_ERROR ){
269 vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
270 fail = 1;
271 }
272
273 if( fail )
274 vg_fatal_error( "OpenGL Error" );
275 }
276
277 VG_STATIC void async_vg_bake_shaders( void *payload, u32 size )
278 {
279 vg_shaders_compile();
280 }
281
282 VG_STATIC void vg_bake_shaders(void)
283 {
284 vg_console_reg_cmd( "reload_shaders", vg_shaders_live_recompile, NULL );
285 vg_async_call( async_vg_bake_shaders, NULL, 0 );
286 }
287
288 void async_internal_complete( void *payload, u32 size )
289 {
290 vg_success( "Internal async setup complete\n" );
291 SDL_AtomicLock( &vg.sl_status );
292
293 if( vg.engine_status == k_engine_status_crashed ){
294 SDL_AtomicUnlock( &vg.sl_status );
295 return;
296 }
297 else{
298 vg.engine_status = k_engine_status_running;
299 }
300
301 SDL_AtomicUnlock( &vg.sl_status );
302 }
303
304 VG_STATIC void _vg_load_full(void)
305 {
306 vg_preload();
307
308 /* internal */
309 vg_loader_step( vg_input_init, vg_input_free );
310 vg_loader_step( vg_lines_init, NULL );
311 vg_loader_step( vg_audio_init, vg_audio_free );
312 vg_loader_step( vg_profiler_init, NULL );
313
314 vg_async_call( async_internal_complete, NULL, 0 );
315
316 /* client */
317 vg_load();
318 }
319
320 VG_STATIC void _vg_process_events(void)
321 {
322 v2_zero( vg.mouse_wheel );
323 v2_zero( vg.mouse_delta );
324
325 /* SDL event loop */
326 SDL_Event event;
327 while( SDL_PollEvent( &event ) ){
328 if( event.type == SDL_KEYDOWN ){
329 console_proc_key( event.key.keysym );
330 }
331 else if( event.type == SDL_MOUSEWHEEL ){
332 vg.mouse_wheel[0] += event.wheel.preciseX;
333 vg.mouse_wheel[1] += event.wheel.preciseY;
334 }
335 else if( event.type == SDL_CONTROLLERAXISMOTION ||
336 event.type == SDL_CONTROLLERBUTTONDOWN ||
337 event.type == SDL_CONTROLLERBUTTONUP ||
338 event.type == SDL_CONTROLLERDEVICEADDED ||
339 event.type == SDL_CONTROLLERDEVICEREMOVED
340 )
341 {
342 vg_input_controller_event( &event );
343 }
344 else if( event.type == SDL_MOUSEMOTION ){
345 vg.mouse_delta[0] += event.motion.xrel;
346 vg.mouse_delta[1] += event.motion.yrel;
347 }
348 else if( event.type == SDL_WINDOWEVENT ){
349 if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ){
350 int w, h;
351 SDL_GL_GetDrawableSize( vg.window, &w, &h );
352
353 if( !w || !h ){
354 vg_warn( "Got a invalid framebuffer size: "
355 "%dx%d... ignoring\n", w, h );
356 }
357 else{
358 vg.window_x = w;
359 vg.window_y = h;
360
361 vg_framebuffer_resize(w,h);
362 }
363 }
364 else if( event.window.event == SDL_WINDOWEVENT_CLOSE ){
365 vg.window_should_close = 1;
366 }
367 }
368 else if( event.type == SDL_TEXTINPUT ){
369 console_proc_utf8( event.text.text );
370 }
371 }
372
373 vg.mouse_pos[0] += vg.mouse_delta[0];
374 vg.mouse_pos[1] += vg.mouse_delta[1];
375
376 /* Update input */
377 vg_update_inputs();
378 }
379
380 VG_STATIC void _vg_gameloop_update(void)
381 {
382 vg_profile_begin( &vg_prof_update );
383
384 vg.engine_stage = k_engine_stage_update;
385 vg_update();
386
387 /* Fixed update loop */
388 vg.engine_stage = k_engine_stage_update_fixed;
389
390 vg.fixed_iterations = 0;
391 vg_lines.allow_input = 1;
392 vg.time_fixed_accumulator += vg.time_delta;
393
394 while( vg.time_fixed_accumulator >= VG_TIMESTEP_FIXED ){
395 vg_update_fixed();
396 vg_lines.allow_input = 0;
397
398 vg.time_fixed_accumulator -= VG_TIMESTEP_FIXED;
399 //vg.accumulator = VG_MAX( 0.0, vg.accumulator );
400
401 vg.fixed_iterations ++;
402 if( vg.fixed_iterations == 8 ){
403 break;
404 }
405 }
406 vg_lines.allow_input = 1;
407 vg.time_fixed_extrapolate = vg.time_fixed_accumulator / VG_TIMESTEP_FIXED;
408
409 vg.engine_stage = k_engine_stage_update;
410 vg_update_post();
411 vg_profile_end( &vg_prof_update );
412 }
413
414 VG_STATIC void _vg_gameloop_render(void)
415 {
416 vg_profile_begin( &vg_prof_render );
417
418 /* render */
419 vg.engine_stage = k_engine_stage_rendering;
420 vg_render();
421
422 /* ui */
423 vg.engine_stage = k_engine_stage_ui;
424 {
425 ui_begin( vg.window_x, vg.window_y );
426
427 /* TODO */
428 ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 );
429
430 int frame_target = vg.display_refresh_rate;
431
432 if( vg.fps_limit > 0 ){
433 frame_target = vg.fps_limit;
434 }
435
436 vg_profile_drawn(
437 (struct vg_profile *[]){
438 &vg_prof_update,&vg_prof_render,&vg_prof_swap}, 3,
439 (1.0f/(float)frame_target)*1000.0f,
440 (ui_rect){ 4, 4, 250, 0 }, 0
441 );
442
443 if( vg_profiler ){
444 char perf[256];
445
446 snprintf( perf, 255,
447 "x: %d y: %d\n"
448 "refresh: %d (%.1fms)\n"
449 "samples: %d\n"
450 "iterations: %d (acc: %.3fms%%)\n"
451 "time: real(%.2f) delta(%.2f) rate(%.2f)\n"
452 #ifdef _WIN32
453 " extrap(%.2f) frame(%.2f) spin( %llu )\n",
454 #else
455 " extrap(%.2f) frame(%.2f) spin( %lu )\n",
456 #endif
457 vg.window_x, vg.window_y,
458 frame_target, (1.0f/(float)frame_target)*1000.0f,
459 vg.samples,
460 vg.fixed_iterations,
461 (vg.time_fixed_accumulator/VG_TIMESTEP_FIXED)*100.0f,
462 vg.time, vg.time_delta, vg.time_rate,
463 vg.time_fixed_extrapolate, vg.time_frame_delta,
464 vg.time_spinning );
465
466 ui_text( (ui_rect){258, 4+24+12+12,0,0},perf, 1,0);
467 }
468
469 /* FIXME */
470 audio_debug_ui( vg.pv );
471 vg_ui();
472 _vg_console_draw();
473
474 ui_resolve();
475 ui_draw( NULL );
476 }
477
478 vg_profile_end( &vg_prof_render );
479 }
480
481 VG_STATIC int vg_framefilter( double dt )
482 {
483 if( (vg.fps_limit <= 0) && (vg.vsync_feature != k_vsync_feature_error) ){
484 /* turn on vsync if not enabled */
485
486 enum vsync_feature requested = k_vsync_feature_enabled;
487 if( vg.fps_limit < 0 ) requested = k_vsync_feature_enabled_adaptive;
488
489 if( vg.vsync_feature != requested ){
490 vg_info( "Setting swap interval\n" );
491
492 int swap_interval = 1;
493 if( requested == k_vsync_feature_enabled_adaptive ) swap_interval = -1;
494
495 if( SDL_GL_SetSwapInterval( swap_interval ) == -1 ){
496 if( requested == k_vsync_feature_enabled ){
497 vg_error( "Vsync is not supported by your system\n" );
498 vg_warn( "You may be overriding it in your"
499 " graphics control panel.\n" );
500 }
501 else{
502 vg_error( "Adaptive Vsync is not supported by your system\n" );
503 }
504
505 vg.vsync_feature = k_vsync_feature_error;
506 vg.fps_limit = vg.display_refresh_rate;
507
508 /* TODO: Make popup to notify user that this happened */
509 return 1;
510 }
511 else{
512 vg_success( "Vsync enabled (%d)\n", requested );
513 vg.vsync_feature = requested;
514 }
515 }
516
517 return 0;
518 }
519
520 if( vg.vsync_feature != k_vsync_feature_disabled ){
521 SDL_GL_SetSwapInterval( 0 );
522 vg.vsync_feature = k_vsync_feature_disabled;
523 }
524
525 if( vg.fps_limit < 25 ) vg.fps_limit = 25;
526 if( vg.fps_limit > 300 ) vg.fps_limit = 300;
527
528 double min_frametime = 1.0/(double)vg.fps_limit;
529 if( vg.time_frame_delta < min_frametime ){
530 /* TODO: we can use high res nanosleep on Linux here */
531 double sleep_ms = (min_frametime-vg.time_frame_delta) * 1000.0;
532 u32 ms = (u32)floor( sleep_ms );
533
534 if( ms ){
535 SDL_Delay( ms );
536 }
537 else{
538 vg.time_spinning ++;
539 }
540
541 return 1;
542 }
543
544 return 0;
545 }
546
547 VG_STATIC int _vg_crashscreen(void)
548 {
549 if( vg.window_should_close )
550 return 1;
551
552 if( vg_getkey( SDLK_ESCAPE ) )
553 return 1;
554
555 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
556 glEnable(GL_BLEND);
557 glDisable(GL_DEPTH_TEST);
558 glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
559 glBlendEquation(GL_FUNC_ADD);
560
561 glClearColor( 0.15f + sinf(vg.time)*0.1f, 0.0f, 0.0f,1.0f );
562 glClear( GL_COLOR_BUFFER_BIT );
563 glViewport( 0,0, vg.window_x, vg.window_y );
564
565 _vg_render_log();
566
567 return 0;
568 }
569
570 VG_STATIC void _vg_gameloop(void)
571 {
572 //vg.time_fixed_accumulator = 0.75f * (1.0f/60.0f);
573
574 vg.time_hp = SDL_GetPerformanceCounter();
575 vg.time_hp_last = vg.time_hp;
576
577 int post_start = 0;
578 while(1){
579
580 vg.time_hp = SDL_GetPerformanceCounter();
581 u64 udt = vg.time_hp - vg.time_hp_last;
582 vg.time_hp_last = vg.time_hp;
583
584 double dt = (double)udt / (double)SDL_GetPerformanceFrequency();
585
586 vg.time_frame_delta += dt;
587
588 if( vg_framefilter( dt ) )
589 continue;
590
591 vg_profile_begin( &vg_prof_swap );
592 SDL_GL_SwapWindow( vg.window );
593 vg_profile_end( &vg_prof_swap );
594
595 enum engine_status status = _vg_engine_status();
596
597 if( status == k_engine_status_crashed ){
598 if( _vg_crashscreen() )
599 break;
600 }
601 else{
602 vg.time_delta = vg.time_frame_delta * vg.time_rate;
603 vg.time += vg.time_delta;
604
605 vg_run_async_checked();
606 _vg_process_events();
607
608 if( vg.window_should_close )
609 break;
610
611 if( status == k_engine_status_running ){
612 _vg_gameloop_update();
613 _vg_gameloop_render();
614 }
615 else{
616 _vg_loader_render();
617 }
618 }
619
620 vg.time_frame_delta = 0.0;
621 vg.time_spinning = 0;
622 }
623 }
624
625 VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] )
626 {
627 char *arg;
628 while( vg_argp( argc, argv ) ){
629 if( (arg = vg_opt_arg( 'w' )) ){
630 vg.window_x = atoi( arg );
631 }
632
633 if( (arg = vg_opt_arg( 'h' )) ){
634 vg.window_y = atoi( arg );
635 }
636
637 if( (arg = vg_long_opt_arg( "samples" )) ){
638 vg.samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
639 }
640
641 if( vg_long_opt( "use-libc-malloc" ) ){
642 vg_mem.use_libc_malloc = 1;
643 }
644
645 if( vg_long_opt( "high-performance" ) ){
646 vg.quality_profile = k_quality_profile_low;
647 }
648
649 vg_launch_opt();
650 }
651 }
652
653 VG_STATIC void _vg_init_window( const char *window_name )
654 {
655 vg_info( "SDL_INIT\n" );
656
657 if( SDL_Init( SDL_INIT_VIDEO ) != 0 ){
658 vg_error( "SDL_Init failed: %s\n", SDL_GetError() );
659 exit(0);
660 }
661
662 SDL_InitSubSystem( SDL_INIT_AUDIO );
663 SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER );
664
665 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
666 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
667 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
668 SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,
669 SDL_GL_CONTEXT_PROFILE_CORE );
670
671 SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
672 SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH );
673
674 SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
675 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
676 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
677 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
678 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0 );
679
680 /*
681 * Get monitor information
682 */
683 vg_info( "Getting display count\n" );
684 int display_count = 0,
685 display_index = 0,
686 mode_index = 0;
687
688 #ifdef VG_DEVWINDOW
689 vg.window_x = 1600;
690 vg.window_y = 1000;
691 #else
692
693 SDL_DisplayMode video_mode;
694 if( SDL_GetDesktopDisplayMode( display_index, &video_mode ) ){
695 vg_error( "SDL_GetDesktopDisplayMode failed: %s\n", SDL_GetError() );
696 SDL_Quit();
697 exit(0);
698 }
699
700 vg.display_refresh_rate = video_mode.refresh_rate;
701 vg.window_x = video_mode.w;
702 vg.window_y = video_mode.h;
703 #endif
704
705 #ifndef _WIN32
706 SDL_SetHint( "SDL_VIDEO_X11_XINERAMA", "1" );
707 SDL_SetHint( "SDL_VIDEO_X11_XRANDR", "0" );
708 SDL_SetHint( "SDL_VIDEO_X11_XVIDMODE", "0" );
709 #endif
710
711 vg_info( "CreateWindow( %d %d @%dhz )\n", vg.window_x, vg.window_y,
712 vg.display_refresh_rate );
713
714 /* TODO: Allow selecting closest video mode from launch opts */
715 if((vg.window = SDL_CreateWindow( window_name,
716
717 #ifdef VG_DEVWINDOW
718 0, 0, vg.window_x, vg.window_y,
719 SDL_WINDOW_BORDERLESS|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED
720 ))){}
721 #else
722 0, 0,
723 vg.window_x, vg.window_y,
724
725 SDL_WINDOW_FULLSCREEN_DESKTOP |
726 SDL_WINDOW_OPENGL |
727 SDL_WINDOW_INPUT_GRABBED
728 )))
729 {
730 if( SDL_SetWindowDisplayMode( vg.window, &video_mode ) ){
731 vg_error( "SDL_SetWindowDisplayMode failed: %s", SDL_GetError() );
732 SDL_Quit();
733 exit(0);
734 }
735 }
736 #endif
737 else{
738 vg_error( "SDL_CreateWindow failed: %s", SDL_GetError() );
739 exit(0);
740 }
741
742 SDL_RaiseWindow( vg.window );
743
744 vg_info( "CreateContext\n" );
745
746 /*
747 * OpenGL loading
748 */
749 if( (vg.gl_context = SDL_GL_CreateContext(vg.window) )){
750 SDL_GL_GetDrawableSize( vg.window, &vg.window_x, &vg.window_y );
751 vg_success( "Window created (%dx%d)\n", vg.window_x, vg.window_y );
752 }
753 else{
754 vg_error( "SDL_GL_CreateContext failed: %s\n", SDL_GetError() );
755 SDL_Quit();
756 exit(0);
757 }
758
759 if( !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) ) {
760 vg_error( "Glad Failed to initialize\n" );
761 SDL_GL_DeleteContext( vg.gl_context );
762 SDL_Quit();
763 exit(0);
764 }
765
766 const unsigned char* glver = glGetString( GL_VERSION );
767 vg_success( "Load setup complete, OpenGL version: %s\n", glver );
768
769 SDL_GL_SetSwapInterval(0); /* disable vsync while loading */
770
771 SDL_DisplayMode dispmode;
772 if( !SDL_GetWindowDisplayMode( vg.window, &dispmode ) ){
773 if( dispmode.refresh_rate ){
774 vg.display_refresh_rate = dispmode.refresh_rate;
775 }
776 }
777
778 if( vg.display_refresh_rate < 25 || vg.display_refresh_rate > 300 ){
779 vg.display_refresh_rate = 60;
780 }
781
782 vg_info( "Display refresh rate: %d\n", dispmode.refresh_rate );
783
784 #ifdef _WIN32
785 vg.fps_limit = vg.display_refresh_rate;
786 #else
787 /* request vsync by default on linux to avoid screen tearing.
788 * this does have its own issues with compositing on X11. */
789 vg.fps_limit = 0;
790 #endif
791 }
792
793 VG_STATIC void _vg_terminate(void)
794 {
795 /* Shutdown */
796 _vg_console_write_persistent();
797
798 SDL_AtomicLock( &vg.sl_status );
799 vg.engine_status = k_engine_status_none;
800 SDL_AtomicUnlock( &vg.sl_status );
801
802 _vg_loader_free();
803
804 vg_success( "If you see this it means everything went.. \"well\".....\n" );
805
806 SDL_GL_DeleteContext( vg.gl_context );
807 SDL_Quit();
808 exit(0);
809 }
810
811 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
812 {
813 _vg_process_launch_opts_internal( argc, argv );
814
815 /* Systems init */
816 vg_alloc_quota();
817 _vg_console_init();
818
819 vg_console_reg_var( "fps_limit", &vg.fps_limit, k_var_dtype_i32, 0 );
820 _vg_init_window( window_name );
821
822 vg_async_init();
823
824 SDL_SetRelativeMouseMode(1);
825 vg.thread_id_main = SDL_GetThreadID(NULL);
826
827 /* Opengl-required systems */
828 _vg_ui_init();
829 _vg_loader_init();
830
831 vg.engine_status = k_engine_status_load_internal;
832
833 _vg_opengl_sync_init();
834 vg_loader_start( _vg_load_full );
835 _vg_gameloop();
836 _vg_terminate();
837 }
838
839 VG_STATIC void vg_fatal_error( const char *fmt, ... )
840 {
841 va_list args;
842 va_start( args, fmt );
843 _vg_log_write( stderr, KRED " fatal" KWHT "| " KRED, fmt, args );
844 va_end( args );
845
846 vg_print_backtrace();
847
848 SDL_AtomicLock( &vg.sl_status );
849 vg.engine_status = k_engine_status_crashed;
850 SDL_AtomicUnlock( &vg.sl_status );
851
852 if( vg_thread_purpose() == k_thread_purpose_loader ){
853 longjmp( vg.env_loader_exit, 1 );
854 }
855 else{
856 vg_error( "There is no jump to the error runner thing yet! bai bai\n" );
857 _vg_terminate();
858 }
859 }
860
861 #else /* VG_GAME */
862
863 #include "vg_log.h"
864 VG_STATIC void vg_fatal_error( const char *fmt, ... )
865 {
866 va_list args;
867 va_start( args, fmt );
868 _vg_log_write( stderr, KRED " fatal" KWHT "| " KRED, fmt, args );
869 va_end( args );
870 exit(0);
871 }
872
873 #endif /* VG_GAME */
874
875 /*
876 * Graphic cards will check these to force it to use the GPU
877 */
878 u32 NvOptimusEnablement = 0x00000001;
879 int AmdPowerXpressRequestHighPerformance = 1;
880
881 #endif /* VG_HEADER_H */