adwadwa
[vg.git] / vg.h
diff --git a/vg.h b/vg.h
index 02c778810680e6be35f49350a6d889d44e19a6c1..64b6f6fc16dcf3c797d93c357cef6d5d4dd6f526 100644 (file)
--- a/vg.h
+++ b/vg.h
@@ -371,13 +371,11 @@ VG_STATIC void vg_bake_shaders(void)
 {
    vg_acquire_thread_sync();
 
-#if 0
    vg_function_push( (struct vg_cmd)
    {
-      .name = "shaders",
+      .name = "reload_shaders",
       .function = vg_shaders_live_recompile
    });
-#endif
 
    vg_shaders_compile();
    vg_release_thread_sync();
@@ -549,6 +547,7 @@ VG_STATIC void _vg_gameloop_render(void)
             ui_text( (ui_rect){258, 4+24+12,0,0},perf, 1,0);
          }
 
+         /* FIXME */
          audio_debug_ui( vg.pv );
          vg_ui();
          _vg_console_draw();
@@ -620,7 +619,7 @@ VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] )
 
       if(  vg_long_opt( "use-libc-malloc" ) )
       {
-         vg_mem.use_libc_malloc = atoi( arg );
+         vg_mem.use_libc_malloc = 1;
       }
 
       if( vg_long_opt( "high-performance" ) )
@@ -634,64 +633,90 @@ VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] )
 
 VG_STATIC void _vg_init_window( const char *window_name )
 {
-   if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_GAMECONTROLLER) != 0  )
-   {
+   vg_info( "SDL_INIT\n" );
+
+   if( SDL_Init( SDL_INIT_VIDEO ) != 0  ){
       vg_error( "SDL_Init failed: %s\n", SDL_GetError() );
       exit(0);
    }
 
-   SDL_GL_SetSwapInterval( 1 );
+   SDL_InitSubSystem( SDL_INIT_AUDIO );
+   SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER );
+
    SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 );
    SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, 
                            SDL_GL_CONTEXT_PROFILE_CORE );
 
-   SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1 );
+   SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
+                           SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH );
+   
    SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
    SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
+   SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0 );
 
-   if( vg.samples > 1 )
-   {
-      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
-      SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, vg.samples );
-   }
-
-   SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
-                           SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH );
-   
    /* 
     * Get monitor information 
     */
+   vg_info( "Getting display count\n" );
    int display_count = 0, display_index = 0, mode_index = 0;
-   if( (display_count = SDL_GetNumVideoDisplays()) < 1 )
-   {
+   if( (display_count = SDL_GetNumVideoDisplays()) < 1 ){
       vg_error( "SDL_GetNumVideoDisplays returned: %i\n", display_count );
       exit(0);
    }
 
+
+
+
    /* TODO: Allow chosing the modes at startup */
-   SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
-   if( SDL_GetDisplayMode( display_index, mode_index, &mode ) != 0 )
+   vg_info( "Getting default display mode\n" );
+   SDL_DisplayMode vm_ideal = { 0, 0, 0, 0, 0 };
+   if( SDL_GetDisplayMode( display_index, mode_index, &vm_ideal ) != 0 )
    {
       vg_error( "SDL_GetDisplayMode failed: %s", SDL_GetError() );
       exit(0);
    }
 
-   vg.refresh_rate = mode.refresh_rate;
+   if( vg.window_x )
+      vm_ideal.w = vg.window_x;
+
+   if( vg.window_y )
+      vm_ideal.h = vg.window_y;
+
+
+   SDL_DisplayMode vm_best;
+   if( !SDL_GetClosestDisplayMode( display_index, &vm_ideal, &vm_best ) )
+   {
+      vg_error( "SDL_GetClosestDisplayMode failed: %s", SDL_GetError() );
+      exit(0);
+   }
+
+   vg.refresh_rate = vm_best.refresh_rate;
+   vg.window_x = vm_best.w;
+   vg.window_y = vm_best.h;
+
+   vg_info( "CreateWindow( %d %d @%.2fhz )\n", vg.window_x, vg.window_y,
+                                               vg.refresh_rate );
 
    /* TODO: Allow selecting closest video mode from launch opts */
    if((vg.window = SDL_CreateWindow( window_name,
                                      SDL_WINDOWPOS_UNDEFINED,
                                      SDL_WINDOWPOS_UNDEFINED,
-                                     mode.w, mode.h,
+                                     vg.window_x, vg.window_y,
 
                                      SDL_WINDOW_FULLSCREEN_DESKTOP | 
                                      SDL_WINDOW_OPENGL |
                                      SDL_WINDOW_INPUT_GRABBED )))
    {
+      if( SDL_SetWindowDisplayMode( vg.window, &vm_best ) )
+      {
+         vg_error( "SDL_SetWindowDisplayMode failed: %s", SDL_GetError() );
+         SDL_Quit();
+         exit(0);
+      }
    }
    else
    {
@@ -699,6 +724,8 @@ VG_STATIC void _vg_init_window( const char *window_name )
       exit(0);
    }
 
+   vg_info( "CreateContext\n" );
+
    /* 
     * OpenGL loading 
     */
@@ -724,6 +751,33 @@ VG_STATIC void _vg_init_window( const char *window_name )
 
    const unsigned char* glver = glGetString( GL_VERSION );
    vg_success( "Load setup complete, OpenGL version: %s\n", glver );
+
+   vg_info( "Setting swap interval\n" );
+
+   if( SDL_GL_SetSwapInterval( -1 ) == -1 )
+   {
+      vg_warn( "Adaptive Vsync not supported\n" );
+
+      if( SDL_GL_SetSwapInterval( 1 ) == -1 )
+      {
+         vg_fatal_exit_loop( "Cannot enable Vsync! You might be overriding it"
+                             " in your graphics control panel.\n" );
+      }
+      else
+         vg_success( "Using vsync\n" );
+   }
+   else
+      vg_success( "Using adaptive Vsync\n" );
+
+   SDL_DisplayMode dispmode;
+   if( !SDL_GetWindowDisplayMode( vg.window, &dispmode ) )
+   {
+      if( dispmode.refresh_rate )
+      {
+         vg.refresh_rate = dispmode.refresh_rate;
+         vg_info( "Refresh rate: %d\n", dispmode.refresh_rate );
+      }
+   }
 }
 
 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
@@ -764,22 +818,8 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
    SDL_Quit();
 }
 
-/*
- * Immediately transfer away from calling thread into a safe loop, signal for 
- * others to shutdown, then free everything once the user closes the window.
- *
- * FIXME(bug): glfwWindowShouldClose() never returns 1 in windows via wine, ONLY
- *             when calling the program from outside its normal directory.
- */
-VG_STATIC void vg_fatal_exit_loop( const char *error )
+void vg_print_backtrace(void)
 {
-   /* 
-    *    https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
-    *    thanks gnu <3
-    *
-    *    TODO: this on windows?
-    */
-
 #ifndef _WIN32
 
    void *array[20];
@@ -802,7 +842,25 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
    free( strings );
 
 #endif
+}
+
+/*
+ * Immediately transfer away from calling thread into a safe loop, signal for 
+ * others to shutdown, then free everything once the user closes the window.
+ *
+ * FIXME(bug): glfwWindowShouldClose() never returns 1 in windows via wine, ONLY
+ *             when calling the program from outside its normal directory.
+ */
+VG_STATIC void vg_fatal_exit_loop( const char *error )
+{
+   /* 
+    *    https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
+    *    thanks gnu <3
+    *
+    *    TODO: this on windows?
+    */
 
+   vg_print_backtrace();
    vg_error( "Fatal error: %s\n", error );
 
    SDL_AtomicLock( &vg.sl_context );
@@ -853,7 +911,7 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
          glBlendEquation(GL_FUNC_ADD);
 
          glClearColor( 0.15f + sinf(vg.time_real)*0.1f, 0.0f, 0.0f,1.0f );
-         glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+         glClear( GL_COLOR_BUFFER_BIT );
          glViewport( 0,0, vg.window_x, vg.window_y );
 
          _vg_render_log();