mc 1.9
[vg.git] / src / vg / vg.h
index cb59cd1bfb3018e7f0f6c80023d428081285ba12..80c31e1a876f887a2c98b57b6c1906c7a86f77a3 100644 (file)
@@ -1,11 +1,12 @@
-// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+/* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */
 
-// TODO: replace as much of this as possible
 #include <stdio.h>
 #include <stdlib.h>
+#include <dirent.h>
 #include <stdint.h>
 #include <string.h>
 #include <stdarg.h>
+#include <ctype.h>
 #include <math.h>
 
 #include "glad/glad.h"
 void vg_register_exit( void( *funcptr )(void), const char *name );
 void vg_exiterr( const char *strErr );
 
-m3x3f vg_pv;
-
 #include "vg/vg_m.h"
 #include "vg/vg_io.h"
 #include "vg/vg_gldiag.h"
 
 #ifndef VG_TOOLS
 
-// Engine globals
+/* Engine globals */
 GLFWwindow* vg_window;
-
-// 1366, 768
-// 1920, 1080
+m3x3f vg_pv;
 
 #ifdef VG_CAPTURE_MODE
 int vg_window_x = 1920;
@@ -48,11 +45,10 @@ v2f vg_mouse;
 v2f vg_mouse_wheel;
 v3f vg_mouse_ws;
 
-float  vg_time;
-float  vg_time_last;
-float  vg_time_delta;
+double vg_time,
+       vg_time_last,
+       vg_time_delta;
 
-// Engine components
 #include "vg/vg_audio.h"
 #include "vg/vg_shader.h"
 #include "vg/vg_lines.h"
@@ -61,10 +57,10 @@ float       vg_time_delta;
 #include "vg/vg_ui.h"
 #include "vg/vg_console.h"
 #include "vg/vg_debug.h"
-#include "vg/vg_steamworks.h"
 
-// Engine main
-// ===========================================================================================================
+#ifdef VG_STEAM
+#include "vg/vg_steamworks.h"
+#endif
 
 #ifndef VG_RELEASE
 void vg_checkgl( const char *src_info )
@@ -88,7 +84,6 @@ void vg_checkgl( const char *src_info )
 void( *vg_on_exit[16] )(void);
 u32 vg_exit_count = 0;
 
-// Add a shutdown step
 void vg_register_exit( void( *funcptr )(void), const char *name )
 {
        vg_info( "exit registered: (%u)'%s'\n", vg_exit_count, name );
@@ -106,7 +101,6 @@ void vg_exit(void)
        vg_info( "done\n" );
 }
 
-// Forcefully exit program after error
 void vg_exiterr( const char *strErr )
 {
        vg_error( "Engine Fatal: %s\n", strErr );
@@ -114,9 +108,6 @@ void vg_exiterr( const char *strErr )
        exit(0);
 }
 
-// Callbacks
-// ---------
-
 void vg_mouse_callback( GLFWwindow* ptrW, double xpos, double ypos )
 {
        vg_mouse[0] = xpos;
@@ -129,27 +120,32 @@ void vg_scroll_callback( GLFWwindow* ptrW, double xoffset, double yoffset )
        vg_mouse_wheel[1] += yoffset;
 }
 
-void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
-{
-       vg_window_x = w;
-       vg_window_y = h;
-}
 
 static void vg_register(void) VG_GAMELOOP;
 static void vg_start(void) VG_GAMELOOP;
 static void vg_update(void) VG_GAMELOOP;
+static void vg_framebuffer_resize(int w, int h) VG_GAMELOOP;
 static void vg_render(void) VG_GAMELOOP;
 static void vg_ui(void) VG_GAMELOOP;
 static void vg_free(void) VG_GAMELOOP;
 
+void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
+{
+       vg_window_x = w;
+       vg_window_y = h;
+
+#ifdef VG_FRAMEBUFFER_RESIZE
+   vg_framebuffer_resize(w,h);
+#endif
+}
+
 static void vg_init( int argc, char *argv[], const char *window_name )
 {
-       // Initialize steamworks
+#ifdef VG_STEAM
        if( !sw_init() )
                return;
+#endif
        
-       // Context creation
-       // ==========================================================================================================================
        glfwInit();
        glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 );
        glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 );
@@ -161,8 +157,11 @@ static void vg_init( int argc, char *argv[], const char *window_name )
 #else
        glfwWindowHint( GLFW_RESIZABLE, GLFW_TRUE );
 #endif
-
-       glfwWindowHint( GLFW_SAMPLES, 4 );
+   glfwWindowHint(GLFW_DOUBLEBUFFER, GLFW_TRUE);
+   
+#if 0
+   glfwWindowHint(GLFW_SAMPLES,4);
+#endif
        
        GLFWmonitor *monitor_primary = glfwGetPrimaryMonitor();
        
@@ -170,21 +169,24 @@ static void vg_init( 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 );
-       glfwWindowHint( GLFW_REFRESH_RATE, mode->refreshRate );
+   
+   int refresh_rate = mode->refreshRate;
 
-       if( !(vg_window = glfwCreateWindow( vg_window_x, vg_window_y, window_name, NULL, NULL)) )
-       {
+   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)) )
                vg_exiterr( "GLFW Failed to initialize" );
-       }
        else
-       {
                vg_register_exit( &glfwTerminate, "glfwTerminate" );
-       }
        
        glfwMakeContextCurrent( vg_window );
        glfwSwapInterval( 1 );
 
-       // Set callbacks
+   glfwSetWindowSizeLimits( vg_window, 800, 600, GLFW_DONT_CARE,GLFW_DONT_CARE);
        glfwSetFramebufferSizeCallback( vg_window, vg_framebuffer_resize_callback );
 
        glfwSetCursorPosCallback( vg_window, vg_mouse_callback );
@@ -192,12 +194,12 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        
        glfwSetCharCallback( vg_window, console_proc_wchar );
        glfwSetKeyCallback( vg_window, console_proc_key );
-       //glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
+#if 0
+       glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
+#endif
 
        if( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) 
-       {
                vg_exiterr( "Glad failed to initialize" );
-       }
 
        const unsigned char* glver = glGetString( GL_VERSION );
        vg_success( "Load setup complete, OpenGL version: %s\n", glver );
@@ -225,7 +227,6 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                
        vg_register();
        vg_register_exit( &vg_free, "vg_free" );
-   vg_register_exit( &sw_free_opengl, "steamworks (opengl)" );
        
        if( vg_shaders_compile() )
        {
@@ -239,13 +240,18 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                
                vg_debugtools_setup();
                
-               // Main gameloop
+      /* 
+       * Main gameloop
+       */
                while( !glfwWindowShouldClose( vg_window ) )
                {
                        v2_copy( (v2f){ 0.0f, 0.0f }, vg_mouse_wheel );
 
                        glfwPollEvents();
+                       
+                       #ifdef VG_STEAM
                        sw_event_loop();
+                       #endif
                        
                        vg_time_last = vg_time;
                        vg_time = glfwGetTime();
@@ -259,7 +265,8 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                        
                        {
                                ui_begin( &ui_global_ctx, vg_window_x, vg_window_y );
-                               ui_set_mouse( &ui_global_ctx, vg_mouse[0], vg_mouse[1], vg_get_button_state( "primary" ) );
+                               ui_set_mouse( &ui_global_ctx, vg_mouse[0], vg_mouse[1], 
+                  vg_get_button_state( "primary" ) );
                                
                                vg_ui();
                                vg_console_draw();
@@ -270,7 +277,6 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                        }
                        
                        glfwSwapBuffers( vg_window );
-                       
                        VG_CHECK_GL();
                }
        }
@@ -278,12 +284,12 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        vg_exit();
 }
 
-// Screen projections
-// ============================================================================================
-
 void vg_projection_update(void)
 {
-       // Do transform local->world
+   /*
+    * Reproject screenspace mouse into world
+    */
+
        vg_mouse_ws[0] = vg_mouse[0];
        vg_mouse_ws[1] = vg_mouse[1];
        vg_mouse_ws[2] = 1.0f;
@@ -298,5 +304,8 @@ void vg_projection_update(void)
 
 #endif
 
+/*
+ * Graphic cards will check these to force it to use the GPU
+ */
 u32 NvOptimusEnablement = 0x00000001;
 int AmdPowerXpressRequestHighPerformance = 1;