4 struct vg_engine vg
= {
6 .time_fixed_delta
= VG_TIMESTEP_FIXED
11 enum engine_status
_vg_engine_status(void)
13 SDL_AtomicLock( &vg
.sl_status
);
14 enum engine_status status
= vg
.engine_status
;
15 SDL_AtomicUnlock( &vg
.sl_status
);
20 enum vg_thread_purpose
vg_thread_purpose(void)
22 SDL_AtomicLock( &vg
.sl_status
);
24 if( vg
.thread_id_main
== SDL_GetThreadID(NULL
) ){
25 SDL_AtomicUnlock( &vg
.sl_status
);
26 return k_thread_purpose_main
;
29 SDL_AtomicUnlock( &vg
.sl_status
);
30 return k_thread_purpose_loader
;
34 static void vg_assert_thread( enum vg_thread_purpose required
)
36 enum vg_thread_purpose purpose
= vg_thread_purpose();
38 if( purpose
!= required
){
39 vg_fatal_error( "thread_purpose must be %u not %u\n", required
, purpose
);
43 static void _vg_opengl_sync_init(void)
45 vg
.sem_loader
= SDL_CreateSemaphore(1);
48 #include "vg_console.h"
49 #include "vg_profiler.h"
53 #include "vg_shader.h"
58 #include "vg_rigidbody_view.h"
59 #include "vg_loader.h"
63 static struct vg_profile vg_prof_update
= {.name
="update()"},
64 vg_prof_render
= {.name
="render()"},
65 vg_prof_swap
= {.name
="swap"};
67 void vg_checkgl( const char *src_info
)
72 while( (err
= glGetError()) != GL_NO_ERROR
){
73 vg_error( "(%s) OpenGL Error: #%d\n", src_info
, err
);
78 vg_fatal_error( "OpenGL Error" );
81 static void async_vg_bake_shaders( void *payload
, u32 size
)
86 void vg_bake_shaders(void)
88 vg_console_reg_cmd( "reload_shaders", vg_shaders_live_recompile
, NULL
);
89 vg_async_call( async_vg_bake_shaders
, NULL
, 0 );
92 void async_internal_complete( void *payload
, u32 size
)
94 vg_success( "Internal async setup complete\n" );
95 SDL_AtomicLock( &vg
.sl_status
);
97 if( vg
.engine_status
== k_engine_status_crashed
){
98 SDL_AtomicUnlock( &vg
.sl_status
);
102 vg
.engine_status
= k_engine_status_running
;
105 SDL_AtomicUnlock( &vg
.sl_status
);
108 #ifdef VG_CUSTOM_SHADERS
109 void vg_auto_shader_register(void); /* created from codegen */
112 static void _vg_load_full( void *data
)
115 vg_tex2d_replace_with_error_async( &vg
.tex_missing
);
116 vg_ui
.tex_bg
= vg
.tex_missing
;
119 vg_loader_step( vg_input_init
, vg_input_free
);
120 vg_loader_step( vg_lines_init
, NULL
);
121 vg_loader_step( vg_rb_view_init
, NULL
);
123 vg_loader_step( vg_audio_init
, vg_audio_free
);
125 vg_loader_step( vg_profiler_init
, NULL
);
128 #ifdef VG_CUSTOM_SHADERS
129 vg_auto_shader_register();
133 vg_async_call( async_internal_complete
, NULL
, 0 );
135 vg_success( "Client loaded in %fs\n", vg
.time_real
);
138 static void _vg_process_events(void)
140 v2_zero( vg
.mouse_wheel
);
141 v2_zero( vg
.mouse_delta
);
148 while( SDL_PollEvent( &event
) ){
149 if( event
.type
== SDL_KEYDOWN
){
150 if( vg_console
.enabled
&&
151 (vg_ui
.focused_control_type
!= k_ui_control_modal
) ){
152 if( event
.key
.keysym
.sym
== SDLK_ESCAPE
||
153 event
.key
.keysym
.scancode
== SDL_SCANCODE_GRAVE
){
154 vg_console
.enabled
= 0;
157 else if( (event
.key
.keysym
.mod
& KMOD_CTRL
) &&
158 event
.key
.keysym
.sym
== SDLK_n
){
159 console_suggest_next();
161 else if( (event
.key
.keysym
.mod
& KMOD_CTRL
) &&
162 event
.key
.keysym
.sym
== SDLK_p
){
163 console_suggest_prev();
166 ui_proc_key( event
.key
.keysym
);
170 if( event
.key
.keysym
.scancode
== SDL_SCANCODE_GRAVE
){
171 vg_console
.enabled
= 1;
174 ui_proc_key( event
.key
.keysym
);
178 else if( event
.type
== SDL_MOUSEWHEEL
){
179 vg
.mouse_wheel
[0] += event
.wheel
.preciseX
;
180 vg
.mouse_wheel
[1] += event
.wheel
.preciseY
;
182 else if( event
.type
== SDL_CONTROLLERDEVICEADDED
||
183 event
.type
== SDL_CONTROLLERDEVICEREMOVED
)
185 vg_input_device_event( &event
);
187 else if( event
.type
== SDL_CONTROLLERAXISMOTION
||
188 event
.type
== SDL_CONTROLLERBUTTONDOWN
||
189 event
.type
== SDL_CONTROLLERBUTTONUP
)
191 vg_input_controller_event( &event
);
193 else if( event
.type
== SDL_MOUSEMOTION
){
194 vg
.mouse_delta
[0] += event
.motion
.xrel
;
195 vg
.mouse_delta
[1] += event
.motion
.yrel
;
197 else if( event
.type
== SDL_WINDOWEVENT
){
198 if( event
.window
.event
== SDL_WINDOWEVENT_SIZE_CHANGED
){
200 SDL_GL_GetDrawableSize( vg
.window
, &w
, &h
);
203 vg_warn( "Got a invalid framebuffer size: "
204 "%dx%d... ignoring\n", w
, h
);
210 vg_framebuffer_resize(w
,h
);
213 else if( event
.window
.event
== SDL_WINDOWEVENT_CLOSE
){
214 vg
.window_should_close
= 1;
217 else if( event
.type
== SDL_TEXTINPUT
){
218 ui_proc_utf8( event
.text
.text
);
222 SDL_GetMouseState( &vg
.mouse_pos
[0], &vg
.mouse_pos
[1] );
225 static void _vg_gameloop_update(void)
227 vg_profile_begin( &vg_prof_update
);
229 vg
.engine_stage
= k_engine_stage_update
;
232 /* Fixed update loop */
233 vg
.engine_stage
= k_engine_stage_update_fixed
;
235 vg
.fixed_iterations
= 0;
236 vg_lines
.enabled
= vg_lines
.render
;
237 vg
.time_fixed_accumulator
+= vg
.time_delta
;
239 while( vg
.time_fixed_accumulator
>= vg
.time_fixed_delta
){
241 vg_lines
.enabled
= 0;
242 vg
.time_fixed_accumulator
-= vg
.time_fixed_delta
;
244 vg
.fixed_iterations
++;
245 if( vg
.fixed_iterations
== 8 ){
249 vg_lines
.enabled
= vg_lines
.render
;
250 vg
.time_fixed_extrapolate
= vg
.time_fixed_accumulator
/ vg
.time_fixed_delta
;
252 vg
.engine_stage
= k_engine_stage_update
;
254 vg_profile_end( &vg_prof_update
);
257 static void vg_settings_gui(void);
258 static void _vg_gameloop_render(void)
260 vg_profile_begin( &vg_prof_render
);
263 vg
.engine_stage
= k_engine_stage_rendering
;
266 vg_profile_end( &vg_prof_render
);
269 vg
.engine_stage
= k_engine_stage_ui
;
272 if( vg_console
.enabled
){
273 vg_ui
.ignore_input_frames
= 10;
275 vg_ui
.ignore_input_frames
= 0;
276 vg_ui
.wants_mouse
= 1;
281 if( vg
.settings_open
)
286 audio_debug_ui( vg
.pv
);
291 int frame_target
= vg
.display_refresh_rate
;
292 if( vg
.fps_limit
> 0 ) frame_target
= vg
.fps_limit
;
294 (struct vg_profile
*[]){
295 &vg_prof_update
,&vg_prof_render
,&vg_prof_swap
}, 3,
296 (1.0f
/(float)frame_target
)*1000.0f
,
297 (ui_rect
){ 4, 4, 250, 0 }, 0, 0
303 "refresh: %d (%.1fms)\n"
305 "iterations: %d (acc: %.3fms%%)\n"
306 "time: real(%.2f) delta(%.2f) rate(%.2f)\n"
307 " extrap(%.2f) frame(%.2f) spin( "PRINTF_U64
" )\n",
308 vg
.window_x
, vg
.window_y
,
309 frame_target
, (1.0f
/(float)frame_target
)*1000.0f
,
312 (vg
.time_fixed_accumulator
/VG_TIMESTEP_FIXED
)*100.0f
,
313 vg
.time_real
, vg
.time_delta
, vg
.time_rate
,
314 vg
.time_fixed_extrapolate
, vg
.time_frame_delta
,
317 ui_text( (ui_rect
){258,4,900,900},perf
,1,0,k_ui_align_left
);
323 static void vg_changevsync(void){
324 if( vg
.vsync
&& (vg
.vsync_feature
!= k_vsync_feature_error
) ){
325 /* turn on vsync if not enabled */
327 enum vsync_feature requested
= k_vsync_feature_enabled
;
328 if( vg
.vsync
< 0 ) requested
= k_vsync_feature_enabled_adaptive
;
330 if( vg
.vsync_feature
!= requested
){
331 vg_info( "Setting swap interval\n" );
333 int swap_interval
= 1;
334 if( requested
== k_vsync_feature_enabled_adaptive
)
337 if( SDL_GL_SetSwapInterval( swap_interval
) == -1 ){
338 if( requested
== k_vsync_feature_enabled
){
339 vg_error( "Vsync is not supported by your system\n" );
340 vg_warn( "You may be overriding it in your"
341 " graphics control panel.\n" );
344 vg_error( "Adaptive Vsync is not supported by your system\n" );
347 vg
.vsync_feature
= k_vsync_feature_error
;
349 /* TODO: Make popup to notify user that this happened */
352 vg_success( "Vsync enabled (%d)\n", requested
);
353 vg
.vsync_feature
= requested
;
358 if( vg
.vsync_feature
!= k_vsync_feature_disabled
){
359 SDL_GL_SetSwapInterval( 0 );
360 vg
.vsync_feature
= k_vsync_feature_disabled
;
365 static int vg_framefilter( double dt
){
366 if( vg
.fps_limit
< 24 ) vg
.fps_limit
= 24;
367 if( vg
.fps_limit
> 300 ) vg
.fps_limit
= 300;
369 double min_frametime
= 1.0/(double)vg
.fps_limit
;
370 if( vg
.time_frame_delta
< min_frametime
){
371 /* TODO: we can use high res nanosleep on Linux here */
372 double sleep_ms
= (min_frametime
-vg
.time_frame_delta
) * 1000.0;
373 u32 ms
= (u32
)floor( sleep_ms
);
376 if( !vg_loader_availible() )
391 static int _vg_crashscreen(void)
394 if( vg_getkey( SDLK_ESCAPE
) )
398 glBindFramebuffer( GL_FRAMEBUFFER
, 0 );
400 glDisable(GL_DEPTH_TEST
);
401 glBlendFunc(GL_ONE_MINUS_DST_ALPHA
, GL_DST_ALPHA
);
402 glBlendEquation(GL_FUNC_ADD
);
404 glClearColor( 0.15f
+ sinf(vg
.time_real
)*0.1f
, 0.0f
, 0.0f
,1.0f
);
405 glClear( GL_COLOR_BUFFER_BIT
);
406 glViewport( 0,0, vg
.window_x
, vg
.window_y
);
415 static void _vg_gameloop(void){
416 //vg.time_fixed_accumulator = 0.75f * (1.0f/60.0f);
418 vg
.time_hp
= SDL_GetPerformanceCounter();
419 vg
.time_hp_last
= vg
.time_hp
;
423 vg
.time_hp
= SDL_GetPerformanceCounter();
424 u64 udt
= vg
.time_hp
- vg
.time_hp_last
;
425 vg
.time_hp_last
= vg
.time_hp
;
427 double dt
= (double)udt
/ (double)SDL_GetPerformanceFrequency();
429 vg
.time_frame_delta
+= dt
;
430 vg_run_async_checked();
432 if( vg_framefilter( dt
) )
437 enum engine_status status
= _vg_engine_status();
438 if( status
== k_engine_status_running
)
439 vg_profile_begin( &vg_prof_swap
);
441 SDL_GL_SwapWindow( vg
.window
);
443 if( status
== k_engine_status_running
)
444 vg_profile_end( &vg_prof_swap
);
446 vg
.time_real
+= vg
.time_frame_delta
;
447 vg
.time_delta
= vg
.time_frame_delta
* vg
.time_rate
;
448 vg
.time
+= vg
.time_delta
;
450 _vg_process_events();
452 if( vg
.window_should_close
)
455 if( status
== k_engine_status_crashed
){
456 if( _vg_crashscreen() )
460 if( status
== k_engine_status_running
){
461 _vg_gameloop_update();
462 _vg_gameloop_render();
469 if( vg
.loader_ring
> 0.01f
){
470 vg_loader_render_ring( vg
.loader_ring
);
471 vg
.loader_ring
-= vg
.time_frame_delta
* 0.5f
;
474 vg
.time_frame_delta
= 0.0;
475 vg
.time_spinning
= 0;
479 static void _vg_process_launch_opts_internal( int argc
, char *argv
[] )
482 while( vg_argp( argc
, argv
) ){
483 if( (arg
= vg_opt_arg( 'w' )) ){
484 vg
.window_x
= atoi( arg
);
487 if( (arg
= vg_opt_arg( 'h' )) ){
488 vg
.window_y
= atoi( arg
);
491 if( (arg
= vg_long_opt_arg( "samples" )) ){
492 vg
.samples
= VG_MAX( 0, VG_MIN( 8, atoi( arg
) ) );
495 if( vg_long_opt( "use-libc-malloc" ) ){
496 vg_mem
.use_libc_malloc
= 1;
499 if( vg_long_opt( "high-performance" ) ){
500 vg
.quality_profile
= k_quality_profile_low
;
507 static void _vg_init_window( const char *window_name
)
509 vg_info( "SDL_INIT\n" );
511 if( SDL_Init( SDL_INIT_VIDEO
) != 0 ){
512 vg_error( "SDL_Init failed: %s\n", SDL_GetError() );
517 SDL_InitSubSystem( SDL_INIT_AUDIO
);
519 SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER
);
521 char *exe_basepath
= SDL_GetBasePath();
522 u32 len
= vg_align8( strlen(exe_basepath
)+1 );
523 char *dest
= vg_linear_alloc( vg_mem
.rtmemory
, len
);
524 strcpy( dest
, exe_basepath
);
525 SDL_free( exe_basepath
);
528 vg_info( "Basepath: %s\n", vg
.base_path
);
530 SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER
, 1 );
531 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION
, 3 );
532 SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION
, 3 );
533 SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK
,
534 SDL_GL_CONTEXT_PROFILE_CORE
);
536 SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR
,
537 SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH
);
539 SDL_GL_SetAttribute( SDL_GL_RED_SIZE
, 8 );
540 SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE
, 8 );
541 SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE
, 8 );
542 SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE
, 8 );
543 SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE
, 0 );
546 * Get monitor information
548 vg_info( "Getting display count\n" );
549 int display_count
= 0,
553 SDL_DisplayMode video_mode
;
554 if( SDL_GetDesktopDisplayMode( display_index
, &video_mode
) ){
555 vg_error( "SDL_GetDesktopDisplayMode failed: %s\n", SDL_GetError() );
560 vg
.display_refresh_rate
= video_mode
.refresh_rate
;
561 vg
.window_x
= video_mode
.w
;
562 vg
.window_y
= video_mode
.h
;
564 if( vg
.screen_mode
== 2 ){
570 SDL_SetHint( "SDL_VIDEO_X11_XINERAMA", "1" );
571 SDL_SetHint( "SDL_VIDEO_X11_XRANDR", "0" );
572 SDL_SetHint( "SDL_VIDEO_X11_XVIDMODE", "0" );
575 u32 flags
= SDL_WINDOW_OPENGL
| SDL_WINDOW_INPUT_GRABBED
|
576 SDL_WINDOW_RESIZABLE
;
578 if( vg
.screen_mode
== 1 )
579 flags
|= SDL_WINDOW_FULLSCREEN
;
580 else if( vg
.screen_mode
== 0 )
581 flags
|= SDL_WINDOW_FULLSCREEN_DESKTOP
;
583 vg_info( "CreateWindow( %d %d %u )\n", vg
.window_x
, vg
.window_y
, flags
);
585 if((vg
.window
= SDL_CreateWindow( window_name
, 0, 0,
586 vg
.window_x
, vg
.window_y
, flags
))){
587 if( vg
.screen_mode
== 2 )
588 SDL_SetWindowPosition( vg
.window
, video_mode
.w
-vg
.window_x
, 0 );
591 vg_error( "SDL_CreateWindow failed: %s", SDL_GetError() );
595 SDL_RaiseWindow( vg
.window
);
596 SDL_SetWindowMinimumSize( vg
.window
, 1280, 720 );
597 SDL_SetWindowMaximumSize( vg
.window
, 4096, 4096 );
599 vg_info( "CreateContext\n" );
602 if( SDL_IsTextInputActive() ) SDL_StopTextInput();
607 if( (vg
.gl_context
= SDL_GL_CreateContext(vg
.window
) )){
608 SDL_GL_GetDrawableSize( vg
.window
, &vg
.window_x
, &vg
.window_y
);
609 vg_success( "Window created (%dx%d)\n", vg
.window_x
, vg
.window_y
);
612 vg_error( "SDL_GL_CreateContext failed: %s\n", SDL_GetError() );
617 if( !gladLoadGLLoader((GLADloadproc
)SDL_GL_GetProcAddress
) ) {
618 vg_error( "Glad Failed to initialize\n" );
619 SDL_GL_DeleteContext( vg
.gl_context
);
624 const unsigned char* glver
= glGetString( GL_VERSION
);
625 vg_success( "Load setup complete, OpenGL version: %s\n", glver
);
627 SDL_GL_SetSwapInterval(0); /* disable vsync while loading */
629 SDL_DisplayMode dispmode
;
630 if( !SDL_GetWindowDisplayMode( vg
.window
, &dispmode
) ){
631 if( dispmode
.refresh_rate
){
632 vg
.display_refresh_rate
= dispmode
.refresh_rate
;
636 if( vg
.display_refresh_rate
< 25 || vg
.display_refresh_rate
> 300 ){
637 vg
.display_refresh_rate
= 60;
640 vg_info( "Display refresh rate: %d\n", dispmode
.refresh_rate
);
641 if( !vg
.fps_limit
) vg
.fps_limit
= vg
.display_refresh_rate
;
644 static void _vg_terminate(void)
647 vg_console_write_persistent();
649 SDL_AtomicLock( &vg
.sl_status
);
650 vg
.engine_status
= k_engine_status_none
;
651 SDL_AtomicUnlock( &vg
.sl_status
);
655 vg_success( "If you see this it means everything went.. \"well\".....\n" );
657 SDL_GL_DeleteContext( vg
.gl_context
);
662 static int cmd_vg_settings_toggle( int argc
, const char *argv
[] );
663 void vg_enter( int argc
, char *argv
[], const char *window_name
)
665 vg_rand_seed( &vg
.rand
, 461 );
666 _vg_process_launch_opts_internal( argc
, argv
);
672 vg_console_reg_var( "vg_fps_limit", &vg
.fps_limit
,
673 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
674 vg_console_reg_var( "vg_vsync", &vg
.vsync
,
675 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
676 vg_console_reg_var( "vg_quality", &vg
.quality_profile
,
677 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
678 vg_console_reg_var( "vg_screen_mode", &vg
.screen_mode
,
679 k_var_dtype_i32
, VG_VAR_PERSISTENT
);
681 vg_console_load_autos();
683 vg_console_reg_cmd( "vg_settings", cmd_vg_settings_toggle
, NULL
);
684 _vg_init_window( window_name
);
687 SDL_SetRelativeMouseMode(1);
689 vg
.thread_id_main
= SDL_GetThreadID(NULL
);
691 /* Opengl-required systems */
695 vg
.engine_status
= k_engine_status_load_internal
;
697 _vg_opengl_sync_init();
698 vg_loader_start( _vg_load_full
, NULL
);
703 void vg_fatal_error( const char *fmt
, ... )
706 va_start( args
, fmt
);
707 _vg_logx_va( stderr
, NULL
, "fatal", KRED
, fmt
, args
);
710 vg_print_backtrace();
712 SDL_AtomicLock( &vg
.sl_status
);
713 vg
.engine_status
= k_engine_status_crashed
;
714 SDL_AtomicUnlock( &vg
.sl_status
);
716 if( vg_thread_purpose() == k_thread_purpose_loader
)
718 longjmp( vg
.env_loader_exit
, 1 );
722 vg_error( "There is no jump to the error runner thing yet! bai bai\n" );
729 * ---------------------------------------------------------------------------
732 #ifdef VG_GAME_SETTINGS
733 extern void vg_game_settings_gui( ui_rect panel
) ;
734 extern void vg_game_settings_init(void);
737 struct ui_enum_opt vg_settings_vsync_enum
[] = {
743 struct ui_enum_opt vg_settings_quality_enum
[] = {
744 { 0, "High Quality" },
746 { 2, "Absolute Minimum" },
749 struct ui_enum_opt vg_settings_screen_mode_enum
[] = {
750 { 0, "Fullscreen (desktop)" },
751 { 1, "Fullscreen (native)" },
752 { 2, "Floating Window" }
755 struct ui_enum_opt vg_settings_dsp_enum
[] = {
761 struct vg_setting_ranged_i32 fps_limit
;
762 struct vg_setting_enum vsync
, quality
, screenmode
, audio_devices
, dsp
;
763 i32 temp_audio_choice
;
765 static vg_settings
= {
766 .fps_limit
= { .label
= "Fps Limit",
767 .min
=24, .max
=300, .actual_value
= &vg
.fps_limit
},
768 .vsync
= { .label
= "Vsync",
769 .actual_value
= &vg
.vsync
,
770 .options
= vg_settings_vsync_enum
, .option_count
= 3 },
771 .quality
= { .label
= "Graphic Quality",
772 .actual_value
= &vg
.quality_profile
,
773 .options
= vg_settings_quality_enum
, .option_count
= 3 },
774 .screenmode
= { .label
= "Type",
775 .actual_value
= &vg
.screen_mode
,
776 .options
= vg_settings_screen_mode_enum
, .option_count
=3 },
777 .audio_devices
= { .label
= "Audio Device",
778 .actual_value
= &vg_settings
.temp_audio_choice
,
779 .options
= NULL
, .option_count
= 0 },
780 .dsp
= { .label
= "Audio effects (reverb etc.)",
781 .actual_value
= &vg_audio
.dsp_enabled
,
782 .options
= vg_settings_dsp_enum
, .option_count
=2 },
785 static void vg_settings_ui_draw_diff( ui_rect orig
){
787 ui_split( orig
, k_ui_axis_v
, -32, 0, l
, r
);
788 ui_text( r
, "*", 1, k_ui_align_middle_center
, ui_colour(k_ui_blue
) );
792 * ------------------------------------------------------------------------- */
794 static void vg_settings_ui_int( char *buf
, u32 len
){
795 for( u32 i
=0, j
=0; i
<len
; i
++ ){
796 if( ((buf
[i
] >= '0') && (buf
[i
] <= '9')) || (buf
[i
] == '\0') )
801 struct ui_textbox_callbacks
static vg_settings_ui_int_callbacks
= {
802 .change
= vg_settings_ui_int
805 static bool vg_settings_ranged_i32_valid( struct vg_setting_ranged_i32
*prop
){
806 if( prop
->new_value
< prop
->min
) return 0;
807 if( prop
->new_value
> prop
->max
) return 0;
811 static bool vg_settings_ranged_i32_diff( struct vg_setting_ranged_i32
*prop
){
812 if( prop
->new_value
!= *prop
->actual_value
) return 1;
816 static bool vg_settings_ui_ranged_i32( struct vg_setting_ranged_i32
*prop
,
819 rect_copy( rect
, orig
);
821 ui_textbox( rect
, prop
->label
, prop
->buf
, sizeof(prop
->buf
),
822 1, 0, &vg_settings_ui_int_callbacks
);
823 prop
->new_value
= atoi( prop
->buf
);
825 if( vg_settings_ranged_i32_diff( prop
) )
826 vg_settings_ui_draw_diff( orig
);
828 bool valid
= vg_settings_ranged_i32_valid( prop
);
831 ui_split( orig
, k_ui_axis_h
, -1, 0, _null
, line
);
834 ui_fill( line
, ui_colour( k_ui_red
) );
840 void ui_settings_ranged_i32_init( struct vg_setting_ranged_i32
*prop
)
843 vg_strnull( &tmp
, prop
->buf
, sizeof(prop
->buf
) );
844 vg_strcati32( &tmp
, *prop
->actual_value
);
845 prop
->new_value
= *prop
->actual_value
;
849 * ------------------------------------------------------------------------- */
851 bool vg_settings_enum_diff( struct vg_setting_enum
*prop
)
853 if( prop
->new_value
!= *prop
->actual_value
) return 1;
857 bool vg_settings_enum( struct vg_setting_enum
*prop
, ui_rect rect
)
860 rect_copy( rect
, orig
);
862 ui_enum( rect
, prop
->label
,
863 prop
->options
, prop
->option_count
, &prop
->new_value
);
865 if( vg_settings_enum_diff( prop
) )
866 vg_settings_ui_draw_diff( orig
);
871 void ui_settings_enum_init( struct vg_setting_enum
*prop
)
873 prop
->new_value
= *prop
->actual_value
;
878 void vg_settings_ui_header( ui_rect inout_panel
, const char *name
)
881 ui_standard_widget( inout_panel
, rect
, 2 );
882 ui_text( rect
, name
, 1, k_ui_align_middle_center
, ui_colour(k_ui_fg
+3) );
886 bool vg_settings_apply_button( ui_rect inout_panel
, bool validated
)
889 ui_px height
= (vg_ui
.font
->glyph_height
+ 18) * k_ui_scale
;
890 ui_split( inout_panel
, k_ui_axis_h
, -height
, k_ui_padding
,
891 inout_panel
, last_row
);
893 const char *string
= "Apply";
895 if( ui_button( last_row
, string
) == 1 )
900 ui_standard_widget( last_row
, rect
, 1 );
901 ui_fill( rect
, ui_colour( k_ui_bg
+1 ) );
902 ui_outline( rect
, -1, ui_colour( k_ui_red
), 0 );
904 ui_rect t
= { 0,0, ui_text_line_width( string
), 14 };
905 ui_rect_center( rect
, t
);
906 ui_text( t
, string
, 1, k_ui_align_left
, ui_colour(k_ui_fg
+3) );
912 static void vg_settings_video_apply(void){
913 if( vg_settings_enum_diff( &vg_settings
.screenmode
) ){
914 vg
.screen_mode
= vg_settings
.screenmode
.new_value
;
916 if( (vg
.screen_mode
== 0) || (vg
.screen_mode
== 1) ){
917 SDL_DisplayMode video_mode
;
918 if( SDL_GetDesktopDisplayMode( 0, &video_mode
) ){
919 vg_error("SDL_GetDesktopDisplayMode failed: %s\n", SDL_GetError());
922 //vg.display_refresh_rate = video_mode.refresh_rate;
923 vg
.window_x
= video_mode
.w
;
924 vg
.window_y
= video_mode
.h
;
926 SDL_SetWindowSize( vg
.window
, vg
.window_x
, vg
.window_y
);
929 if( vg
.screen_mode
== 0 )
930 SDL_SetWindowFullscreen( vg
.window
, SDL_WINDOW_FULLSCREEN_DESKTOP
);
931 if( vg
.screen_mode
== 1 )
932 SDL_SetWindowFullscreen( vg
.window
, SDL_WINDOW_FULLSCREEN
);
933 if( vg
.screen_mode
== 2 ){
934 SDL_SetWindowFullscreen( vg
.window
, 0 );
935 SDL_SetWindowSize( vg
.window
, 1280, 720 );
936 SDL_SetWindowPosition( vg
.window
, 16, 16 );
937 SDL_SetWindowMinimumSize( vg
.window
, 1280, 720 );
938 SDL_SetWindowMaximumSize( vg
.window
, 4096, 4096 );
942 vg
.fps_limit
= vg_settings
.fps_limit
.new_value
;
943 vg
.quality_profile
= vg_settings
.quality
.new_value
;
944 vg
.vsync
= vg_settings
.vsync
.new_value
;
947 static void vg_settings_video_gui( ui_rect panel
){
950 ui_standard_widget( panel
, rq
, 1 );
951 vg_settings_enum( &vg_settings
.quality
, rq
);
955 if( vg
.vsync_feature
== k_vsync_feature_error
){
956 ui_info( panel
, "There was an error activating vsync feature." );
961 vg_settings_ui_header( panel
, "Frame Timing" );
963 ui_standard_widget( panel
, duo
, 1 );
964 ui_split_ratio( duo
, k_ui_axis_v
, 0.5f
, 16, d0
, d1
);
966 vg_settings_enum( &vg_settings
.vsync
, d0
);
967 validated
&= vg_settings_ui_ranged_i32( &vg_settings
.fps_limit
, d1
);
970 ui_standard_widget( panel
, duo
, 10 );
971 int frame_target
= vg
.display_refresh_rate
;
972 if( !vg
.vsync
) frame_target
= vg
.fps_limit
;
974 (struct vg_profile
*[]){
975 &vg_prof_update
,&vg_prof_render
,&vg_prof_swap
}, 3,
976 (1.0f
/(f32
)frame_target
)*1500.0f
,
980 ui_fill( (ui_rect
){ duo
[0], duo
[1]+(duo
[3]*2)/3, duo
[2], 1 },
981 ui_colour(k_ui_fg
) );
984 vg_settings_ui_header( panel
, "Window Specification" );
986 ui_standard_widget( panel
, duo
, 1 );
987 vg_settings_enum( &vg_settings
.screenmode
, duo
);
989 if( vg_settings_apply_button( panel
, validated
) )
990 vg_settings_video_apply();
993 static void vg_settings_audio_apply(void){
994 if( vg_settings_enum_diff( &vg_settings
.audio_devices
) ){
995 if( vg_audio
.sdl_output_device
){
996 vg_info( "Closing audio device %d\n", vg_audio
.sdl_output_device
);
997 SDL_CloseAudioDevice( vg_audio
.sdl_output_device
);
1000 vg_strfree( &vg_audio
.device_choice
);
1002 if( vg_settings
.audio_devices
.new_value
== -1 ){ }
1003 else if( vg_settings
.audio_devices
.new_value
== -2 ){
1004 vg_fatal_error( "Programming error\n" );
1007 struct ui_enum_opt
*selected
= NULL
, *oi
;
1009 for( int i
=0; i
<vg_settings
.audio_devices
.option_count
; i
++ ){
1010 oi
= &vg_settings
.audio_devices
.options
[i
];
1012 if( oi
->value
== vg_settings
.audio_devices
.new_value
){
1018 vg_strnull( &vg_audio
.device_choice
, NULL
, -1 );
1019 vg_strcat( &vg_audio
.device_choice
, oi
->alias
);
1022 vg_audio_device_init();
1023 *vg_settings
.audio_devices
.actual_value
=
1024 vg_settings
.audio_devices
.new_value
;
1028 if( vg_settings_enum_diff( &vg_settings
.dsp
) ){
1029 *vg_settings
.dsp
.actual_value
=
1030 vg_settings
.dsp
.new_value
;
1036 static void vg_settings_audio_gui( ui_rect panel
){
1038 ui_standard_widget( panel
, rq
, 1 );
1039 vg_settings_enum( &vg_settings
.audio_devices
, rq
);
1041 ui_standard_widget( panel
, rq
, 1 );
1042 vg_settings_enum( &vg_settings
.dsp
, rq
);
1044 if( vg_settings_apply_button( panel
, 1 ) )
1045 vg_settings_audio_apply();
1048 void vg_settings_open(void)
1050 vg
.settings_open
= 1;
1052 ui_settings_ranged_i32_init( &vg_settings
.fps_limit
);
1053 ui_settings_enum_init( &vg_settings
.vsync
);
1054 ui_settings_enum_init( &vg_settings
.quality
);
1055 ui_settings_enum_init( &vg_settings
.screenmode
);
1057 /* Create audio options */
1058 int count
= SDL_GetNumAudioDevices( 0 );
1060 struct ui_enum_opt
*options
= malloc( sizeof(struct ui_enum_opt
)*(count
+1) );
1061 vg_settings
.audio_devices
.options
= options
;
1062 vg_settings
.audio_devices
.option_count
= count
+1;
1064 struct ui_enum_opt
*o0
= &options
[0];
1065 o0
->alias
= "OS Default";
1068 for( int i
=0; i
<count
; i
++ ){
1069 struct ui_enum_opt
*oi
= &options
[i
+1];
1071 const char *device_name
= SDL_GetAudioDeviceName( i
, 0 );
1072 int len
= strlen(device_name
);
1074 oi
->alias
= malloc( len
+1 );
1075 memcpy( (void *)oi
->alias
, device_name
, len
+1 );
1079 if( vg_audio
.device_choice
.buffer
){
1080 vg_settings
.temp_audio_choice
= -2;
1082 for( int i
=0; i
<count
; i
++ ){
1083 struct ui_enum_opt
*oi
= &options
[i
+1];
1084 if( !strcmp( oi
->alias
, vg_audio
.device_choice
.buffer
) ){
1085 vg_settings
.temp_audio_choice
= oi
->value
;
1091 vg_settings
.temp_audio_choice
= -1;
1094 ui_settings_enum_init( &vg_settings
.audio_devices
);
1095 ui_settings_enum_init( &vg_settings
.dsp
);
1097 #ifdef VG_GAME_SETTINGS
1098 vg_game_settings_init();
1102 void vg_settings_close(void)
1104 vg
.settings_open
= 0;
1106 struct ui_enum_opt
*options
= vg_settings
.audio_devices
.options
;
1107 for( int i
=1; i
< vg_settings
.audio_devices
.option_count
; i
++ )
1108 free( (void *)options
[i
].alias
);
1109 free( vg_settings
.audio_devices
.options
);
1112 static void vg_settings_gui(void)
1115 ui_rect screen
= { 0, 0, vg
.window_x
, vg
.window_y
};
1116 ui_rect window
= { 0, 0, 1000, 700 };
1117 ui_rect_center( screen
, window
);
1118 vg_ui
.wants_mouse
= 1;
1120 ui_fill( window
, ui_colour( k_ui_bg
+1 ) );
1121 ui_outline( window
, 1, ui_colour( k_ui_bg
+7 ), 0 );
1123 ui_rect title
, panel
;
1124 ui_split( window
, k_ui_axis_h
, 28, 0, title
, panel
);
1125 ui_fill( title
, ui_colour( k_ui_bg
+7 ) );
1126 ui_text( title
, "Settings", 1, k_ui_align_middle_center
,
1127 ui_colourcont(k_ui_bg
+7) );
1129 ui_rect quit_button
;
1130 ui_split( title
, k_ui_axis_v
, title
[2]-title
[3], 2, title
, quit_button
);
1132 if( ui_button_text( quit_button
, "X", 1 ) == 1 ){
1133 vg_settings_close();
1137 ui_rect_pad( panel
, (ui_px
[2]){ 8, 8 } );
1139 const char *opts
[] = { "video", "audio",
1140 #ifdef VG_GAME_SETTINGS
1145 static i32 page
= 0;
1146 ui_tabs( panel
, panel
, opts
, vg_list_size(opts
), &page
);
1149 vg_settings_video_gui( panel
);
1151 else if( page
== 1 )
1152 vg_settings_audio_gui( panel
);
1154 #ifdef VG_GAME_SETTINGS
1155 else if( page
== 2 )
1156 vg_game_settings_gui( panel
);
1160 static int cmd_vg_settings_toggle( int argc
, const char *argv
[] ){
1166 * Graphic cards will check these to force it to use the GPU.
1167 * TODO: explicit declexport. since -flto strips these symbols in release.
1169 u32 NvOptimusEnablement
= 0x00000001;
1170 int AmdPowerXpressRequestHighPerformance
= 1;
1172 #include "vg_async.c"
1173 #include "vg_audio.c"
1174 #include "vg_audio_dsp.c"
1175 #include "vg_audio_synth_bird.c"
1176 #include "vg_binstr.c"
1178 #include "vg_camera.c"
1179 #include "vg_lines.c"
1180 #include "vg_console.c"
1181 #include "vg_imgui.c"
1182 #include "vg_input.c"
1184 #include "vg_loader.c"
1188 #include "vg_mem_pool.c"
1189 #include "vg_mem_queue.c"
1192 #include "vg_perlin.c"
1193 #include "vg_string.c"
1194 #include "vg_profiler.c"
1195 #include "vg_rigidbody_collision.c"
1196 #include "vg_rigidbody_constraints.c"
1197 #include "vg_rigidbody.c"
1198 #include "vg_rigidbody_view.c"
1199 #include "vg_shader.c"
1201 #ifdef VG_CUSTOM_SHADERS
1202 #include "shaders/impl.c"