+VG_STATIC int vg_framefilter( double dt )
+{
+ if( (vg.fps_limit <= 0) && (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_feature != requested ){
+ vg_info( "Setting swap interval\n" );
+
+ int 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 ){
+ vg_error( "Vsync is not supported by your system\n" );
+ vg_warn( "You may be overriding it in your"
+ " graphics control panel.\n" );
+ }
+ else{
+ vg_error( "Adaptive Vsync is not supported by your system\n" );
+ }
+
+ vg.vsync_feature = k_vsync_feature_error;
+ vg.fps_limit = vg.display_refresh_rate;
+
+ /* 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;
+ }
+
+ if( vg.fps_limit < 25 ) vg.fps_limit = 25;
+ if( vg.fps_limit > 300 ) vg.fps_limit = 300;
+
+ double min_frametime = 1.0/(double)vg.fps_limit;
+ if( vg.time_frame_delta < min_frametime ){
+ /* TODO: we can use high res nanosleep on Linux here */
+ double sleep_ms = (min_frametime-vg.time_frame_delta) * 1000.0;
+ u32 ms = (u32)floor( sleep_ms );
+
+ if( ms ){
+ SDL_Delay( ms );
+ }
+ else{
+ vg.time_spinning ++;
+ }
+
+ return 1;
+ }
+
+ return 0;
+}
+