Platform and OS stability stuff
[vg.git] / src / vg / vg.h
index e6f4001469395ee47869c591a2075de60f185885..f3ed6991690edd23e57b12697a910e2b5ebe557d 100644 (file)
 
 #ifndef VG_SERVER
 #include "../../dep/glad/glad.h"
+
+#define GLFW_INCLUDE_GLCOREARB
+
+#ifdef _WIN32
+  #define GLFW_DLL
+#endif
+
 #include "../../dep/glfw/glfw3.h"
 #endif
 
@@ -36,13 +43,9 @@ void vg_register_exit( void( *funcptr )(void), const char *name );
 
 m4x4f vg_pv;
 
-#ifdef VG_CAPTURE_MODE
-int vg_window_x = 1920;
-int vg_window_y = 1080;
-#else
-int vg_window_x = 1366;
-int vg_window_y = 768;
-#endif
+int vg_window_x = 0;
+int vg_window_y = 0;
+int vg_samples = 0;
 
 v2f vg_mouse;
 v2f vg_mouse_wheel;
@@ -248,6 +251,7 @@ static void vg_checkgl( const char *src_info );
 #include "vg_lines.h"
 #include "vg_debug.h"
 #include "vg_loader.h"
+#include "vg_opt.h"
 
 #define VG_GAMELOOP
 static void vg_register(void) VG_GAMELOOP;
@@ -286,6 +290,12 @@ void vg_scroll_callback( GLFWwindow* ptrW, double xoffset, double yoffset )
 
 void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
 {
+   if( !w || !h )
+   {
+      vg_warn( "Got a invalid framebuffer size: %dx%d... ignoring\n", w, h );
+      return;
+   }
+
    vg_window_x = w;
    vg_window_y = h;
 
@@ -331,6 +341,25 @@ static void vg_load_full(void)
 
 static void vg_enter( int argc, char *argv[], const char *window_name )
 {
+   char *arg;
+   while( vg_argp( argc, argv ) )
+   {
+      if( (arg = vg_opt_arg( 'w' )) )
+      {
+         vg_window_x = atoi( arg );
+      }
+
+      if( (arg = vg_opt_arg( 'h' )) )
+      {
+         vg_window_y = atoi( arg );
+      }
+
+      if( (arg = vg_long_opt_arg( "samples" )) )
+      {
+         vg_samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
+      }
+   }
+
    vg_log_init();
    vg_console_init();
 
@@ -339,13 +368,12 @@ static void vg_enter( int argc, char *argv[], const char *window_name )
    glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
    glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
    glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
+   glfwWindowHint( GLFW_CONTEXT_RELEASE_BEHAVIOR, GLFW_RELEASE_BEHAVIOR_FLUSH );
    
-   glfwWindowHint( GLFW_RESIZABLE, GLFW_TRUE );
+   glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
    glfwWindowHint( GLFW_DOUBLEBUFFER, GLFW_TRUE );
    
-#if 0
-   glfwWindowHint(GLFW_SAMPLES,4);
-#endif
+   glfwWindowHint( GLFW_SAMPLES, vg_samples );
    
    GLFWmonitor *monitor_primary = glfwGetPrimaryMonitor();
    
@@ -353,20 +381,48 @@ static void vg_enter( int argc, char *argv[], const char *window_name )
    glfwWindowHint( GLFW_RED_BITS, mode->redBits );
    glfwWindowHint( GLFW_GREEN_BITS, mode->greenBits );
    glfwWindowHint( GLFW_BLUE_BITS, mode->blueBits );
-   
-   /* This is set like this because of an OS issue */
-   int refresh_rate = mode->refreshRate;
-   if( refresh_rate < 28 || refresh_rate >= 144 )
-      refresh_rate = 60;
-   glfwWindowHint( GLFW_REFRESH_RATE, refresh_rate );
-
-   if( !(vg.window = glfwCreateWindow( vg_window_x, vg_window_y, 
-                                       window_name, NULL, NULL)) )
+
+   /* TODO? */
+   glfwWindowHint( GLFW_REFRESH_RATE, 60 );
+
+   if( !vg_window_x )
+      vg_window_x = mode->width;
+
+   if( !vg_window_y )
+      vg_window_y = mode->height;
+
+
+   if( (vg.window = glfwCreateWindow(  vg_window_x, vg_window_y,
+                                       window_name, monitor_primary, NULL)) )
+   {
+      glfwGetFramebufferSize( vg.window, &vg_window_x, &vg_window_y );
+      vg_success( "Window created (%dx%d)\n", vg_window_x, vg_window_y );
+   }
+   else
    {
       vg_error( "GLFW Failed to initialize\n" );
       return;
    }
    
+   /* We need 3.1.2 for correct VSync on windows */
+   {
+      int vmaj, vmin, vrev;
+      glfwGetVersion( &vmaj, &vmin, &vrev );
+
+      if( vmaj < 3 || 
+         (vmaj == 3 && vmin  < 1) || 
+         (vmaj == 3 && vmin == 1 && vrev < 2 ) )
+      {
+         vg_error( "GLFW out of date (%d.%d.%d); (3.1.2 is required)\n",
+                    vmaj, vmin, vrev );
+
+         glfwTerminate();
+         return;
+      }
+      
+      vg_success( "GLFW Version %d.%d.%d\n", vmaj, vmin, vrev );
+   }
+   
    glfwMakeContextCurrent( vg.window );
    glfwSwapInterval( 1 );
 
@@ -549,11 +605,10 @@ static void vg_fatal_exit_loop( const char *error )
 
          glfwPollEvents();
 
-         vg_time = glfwGetTime();
-         vg_time_delta = vg_minf( vg_time - vg_time_last, 0.1f );
-      
-         glClearColor( sinf(vg_time*20.0)*0.5f+0.5f, 0.0f, 0.0f,1.0f );
+         glClearColor( 0.15f + sinf(glfwGetTime())*0.1f, 0.0f, 0.0f,1.0f );
          glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
+         glViewport( 0,0, vg_window_x, vg_window_y );
+
          vg_render_log();
 
          glfwSwapBuffers( vg.window );
@@ -568,6 +623,14 @@ static void vg_fatal_exit_loop( const char *error )
    }
 }
 
+#else
+
+static void vg_fatal_exit_loop( const char *error )
+{
+   vg_error( "Fatal error: %s\n", error );
+   exit(0);
+}
+
 #endif
 
 /*