X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg.h;h=04578f7c81a5c7c53bef107079a6642cb180cf36;hb=7a1b2ba3172f9f296df110997e5b8cca5f10ab68;hp=753da33dd706027392d6e5f2828587dd433f4323;hpb=79bf1e02c9af40922b184ccd42533b3608d20364;p=vg.git diff --git a/vg.h b/vg.h index 753da33..04578f7 100644 --- a/vg.h +++ b/vg.h @@ -127,7 +127,8 @@ struct vg{ window_should_close; int display_refresh_rate, - fps_limit; /* 0: use vsync, >0: cap fps to this, no vsync */ + fps_limit, + vsync; enum vsync_feature{ k_vsync_feature_disabled=0, @@ -174,6 +175,8 @@ struct vg{ float loader_ring; GLuint tex_missing; + + vg_rand rand; } static vg = { .time_rate = 1.0 }; const char *vg_get_basepath(void){ @@ -305,6 +308,8 @@ static void _vg_load_full( void *data ) /* client */ vg_load(); + + vg_success( "Client loaded in %fs\n", vg.time_real ); } static void _vg_process_events(void) @@ -489,19 +494,19 @@ static void _vg_gameloop_render(void) vg_profile_end( &vg_prof_render ); } -static int vg_framefilter( double dt ) -{ - if( (vg.fps_limit <= 0) && (vg.vsync_feature != k_vsync_feature_error) ){ +static void vg_changevsync(void){ + if( vg.vsync && (vg.vsync_feature != k_vsync_feature_error) ){ /* turn on vsync if not enabled */ enum vsync_feature requested = k_vsync_feature_enabled; - if( vg.fps_limit < 0 ) requested = k_vsync_feature_enabled_adaptive; + if( vg.vsync < 0 ) requested = k_vsync_feature_enabled_adaptive; if( vg.vsync_feature != requested ){ vg_info( "Setting swap interval\n" ); int swap_interval = 1; - if( requested == k_vsync_feature_enabled_adaptive ) swap_interval = -1; + if( requested == k_vsync_feature_enabled_adaptive ) + swap_interval = -1; if( SDL_GL_SetSwapInterval( swap_interval ) == -1 ){ if( requested == k_vsync_feature_enabled ){ @@ -514,25 +519,24 @@ static int vg_framefilter( double dt ) } vg.vsync_feature = k_vsync_feature_error; - vg.fps_limit = vg.display_refresh_rate; - + vg.vsync = 0; /* TODO: Make popup to notify user that this happened */ - return 1; } else{ vg_success( "Vsync enabled (%d)\n", requested ); vg.vsync_feature = requested; } } - - return 0; } - - if( vg.vsync_feature != k_vsync_feature_disabled ){ - SDL_GL_SetSwapInterval( 0 ); - vg.vsync_feature = k_vsync_feature_disabled; + else { + if( vg.vsync_feature != k_vsync_feature_disabled ){ + SDL_GL_SetSwapInterval( 0 ); + vg.vsync_feature = k_vsync_feature_disabled; + } } - +} + +static int vg_framefilter( double dt ){ if( vg.fps_limit < 25 ) vg.fps_limit = 25; if( vg.fps_limit > 300 ) vg.fps_limit = 300; @@ -543,7 +547,10 @@ static int vg_framefilter( double dt ) u32 ms = (u32)floor( sleep_ms ); if( ms ){ - SDL_Delay( ms ); + if( !vg_loader_availible() ) + SDL_Delay(1); + else + SDL_Delay(ms); } else{ vg.time_spinning ++; @@ -585,7 +592,6 @@ static void _vg_gameloop(void){ int post_start = 0; while(1){ - vg.time_hp = SDL_GetPerformanceCounter(); u64 udt = vg.time_hp - vg.time_hp_last; vg.time_hp_last = vg.time_hp; @@ -593,10 +599,13 @@ static void _vg_gameloop(void){ double dt = (double)udt / (double)SDL_GetPerformanceFrequency(); vg.time_frame_delta += dt; + vg_run_async_checked(); if( vg_framefilter( dt ) ) continue; + vg_changevsync(); + vg_profile_begin( &vg_prof_swap ); SDL_GL_SwapWindow( vg.window ); vg_profile_end( &vg_prof_swap ); @@ -607,7 +616,6 @@ static void _vg_gameloop(void){ vg.time_delta = vg.time_frame_delta * vg.time_rate; vg.time += vg.time_delta; - vg_run_async_checked(); _vg_process_events(); if( vg.window_should_close ) @@ -810,11 +818,11 @@ static void _vg_init_window( const char *window_name ) } vg_info( "Display refresh rate: %d\n", dispmode.refresh_rate ); + vg.fps_limit = vg.display_refresh_rate; #if defined(_WIN32) || defined(VG_DEVWINDOW) - vg.fps_limit = vg.display_refresh_rate; #else - vg.fps_limit = 0; + //vg.vsync = 1; #endif } @@ -836,9 +844,8 @@ static void _vg_terminate(void) exit(0); } -static void vg_enter( int argc, char *argv[], const char *window_name ) -{ - vg_rand_seed( 461 ); +static void vg_enter( int argc, char *argv[], const char *window_name ){ + vg_rand_seed( &vg.rand, 461 ); _vg_process_launch_opts_internal( argc, argv ); /* Systems init */ @@ -846,6 +853,7 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) _vg_console_init(); vg_console_reg_var( "fps_limit", &vg.fps_limit, k_var_dtype_i32, 0 ); + vg_console_reg_var( "vsync", &vg.vsync, k_var_dtype_i32, VG_VAR_PERSISTENT ); _vg_init_window( window_name ); vg_async_init();