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