frame timing
authorhgn <hgodden00@gmail.com>
Fri, 8 Dec 2023 16:30:41 +0000 (16:30 +0000)
committerhgn <hgodden00@gmail.com>
Fri, 8 Dec 2023 16:30:41 +0000 (16:30 +0000)
vg.h
vg_loader.h

diff --git a/vg.h b/vg.h
index 9f7ebe89f48efaca5200b57ca65a1820570efe42..3ce5a462642ab62d75b89735d330df670239a9c9 100644 (file)
--- 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,
@@ -491,19 +492,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 ){
@@ -516,25 +517,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;
 
@@ -545,7 +545,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 ++;
@@ -587,7 +590,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;
@@ -600,6 +602,8 @@ static void _vg_gameloop(void){
       if( vg_framefilter( dt ) )
          continue;
 
+      vg_changevsync();
+
       vg_profile_begin( &vg_prof_swap );
       SDL_GL_SwapWindow( vg.window );
       vg_profile_end( &vg_prof_swap );
@@ -812,11 +816,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
 }
 
@@ -848,6 +852,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();
index c633ff907f0182f3ef81fcd725e570a72860ff60..5d6d81a210ecbbd92553e6f992607a0714e6b07d 100644 (file)
@@ -161,15 +161,16 @@ static void _vg_render_log(void)
 #endif
 }
 
-static void _vg_loader_render_ring( float opacity )
-{
+static void _vg_loader_render_ring( f32 opacity ){
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBlendEquation(GL_FUNC_ADD);
 
+   opacity *= opacity;
+
    glUseProgram( _shader_loader.id );
        glUniform1f( glGetUniformLocation( _shader_loader.id, "uTime" ), vg.time );
-   float ratio = (float)vg.window_x / (float)vg.window_y;
+   f32 ratio = (f32)vg.window_x / (f32)vg.window_y;
    glUniform1f( glGetUniformLocation( _shader_loader.id, "uRatio"), ratio );
    glUniform1f( glGetUniformLocation( _shader_loader.id, "uOpacity"), opacity );
    glBindVertexArray( vg_loader.vao );