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