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