cross compile build script
[fishladder.git] / vg / vg.h
diff --git a/vg/vg.h b/vg/vg.h
index 6b4cd233a5365a1ecbdfa66b4a4374773c515e6d..575bf036eeabf667aa322983555a3a2dee818830 100644 (file)
--- a/vg/vg.h
+++ b/vg/vg.h
 #include <stdint.h>
 #include <string.h>
 #include <stdarg.h>
+#include <ctype.h>
+#include <math.h>
 
 #include "gl/glad/glad.h"
 #include "gl/glfw3.h"
 
 #define STB_DS_IMPLEMENTATION
 #include "stb/stb_ds.h"
+//#define STB_IMAGE_IMPLEMENTATION
+//#include "stb/stb_image.h"
 
-#include "cglm/include/cglm/cglm.h"
+#define QOI_IMPLEMENTATION
+#include "phoboslab/qoi.h"
+
+#include "vg/vg_platform.h"
 
 void vg_register_exit( void( *funcptr )(void), const char *name );
 void vg_exiterr( const char *strErr );
 
-#include "vg/vg_platform.h"
-#include "vg/vg_io.h"
-#include "vg/vg_audio.h"
-#include "vg/vg_shader.h"
+m3x3f vg_pv;
 
-#include "steam/steamworks_thin.h"
+#include "vg/vg_m.h"
+#include "vg/vg_io.h"
+#include "vg/vg_gldiag.h"
 
-static inline float vg_get_axis( const char *axis ) __attribute__((unused));
-static inline int vg_get_button( const char *button ) __attribute__((unused));
-static inline int vg_get_button_down( const char *button ) __attribute__((unused));
-static inline int vg_get_button_up( const char *button ) __attribute__((unused));
+#ifndef VG_TOOLS
 
-// Globals
+// Engine globals
 GLFWwindow* vg_window;
-int vg_window_x = 1280;
-int vg_window_y = 720;
 
-float vg_mouse_x;
-float vg_mouse_y;
+// 1366, 768
+// 1920, 1080
+
+#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
+
+v2f vg_mouse;
+v2f vg_mouse_wheel;
+v3f vg_mouse_ws;
 
 float  vg_time;
 float  vg_time_last;
 float  vg_time_delta;
 
-// Input
-// ===========================================================================================================
-GLFWgamepadstate vg_gamepad;
-int                    vg_gamepad_ready = 0;
-const char *vg_gamepad_name = NULL;
-int                    vg_gamepad_id;
-
-enum EInputMode
-{
-       k_EInputMode_pc,
-       k_EInputMode_gamepad
-}
-vg_input_mode;
-
-static struct axis_binding
-{
-       const char *name;
-       union
-       {
-               int positive;
-               int bind;
-       };
-       int negative;
-       
-       float value;
-}
-vg_axis_binds[];
-
-static struct button_binding
-{
-       const char *name;
-       int bind;
-       
-       int value; int prev;
-}
-vg_button_binds[];
-
-#include "vg/config.h"
-
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wreturn-type"
-
-static inline float vg_get_axis( const char *axis )
-{
-       for( int i = 0; i < vg_list_size( vg_axis_binds ); i ++ )
-       {
-               if( !strcmp( axis, vg_axis_binds[i].name ) )
-               {
-                       return vg_axis_binds[i].value;
-               }
-       }
-}
-
-static inline struct button_binding *vg_get_button_ptr( const char *button )
-{
-       for( int i = 0; i < vg_list_size( vg_button_binds ); i ++ )
-       {
-               if( !strcmp( button, vg_button_binds[i].name ) )
-               {
-                       return vg_button_binds + i;
-               }
-       }
-}
-#pragma GCC diagnostic pop
-
-static inline int vg_get_button( const char *button )
-{
-       return vg_get_button_ptr( button )->value;
-}
-
-static inline int vg_get_button_down( const char *button )
-{
-       struct button_binding *bind = vg_get_button_ptr( button );
-       return bind->value & (bind->value ^ bind->prev);
-}
-
-static inline int vg_get_button_up( const char *button )
-{
-       struct button_binding *bind = vg_get_button_ptr( button );
-       return bind->prev & (bind->value ^ bind->prev);
-}
-
-static inline int key_is_keyboard( int const id )
-{
-       static_assert( GLFW_MOUSE_BUTTON_LAST < GLFW_KEY_SPACE, "GLFW: Mouse has too many buttons" );
-       return id > GLFW_MOUSE_BUTTON_LAST;
-}
-
-// Mouse AND Keyboard get button press
-int get_button_cross_device( int const id )
-{
-       if( key_is_keyboard( id ) )
-       {
-               return glfwGetKey( vg_window, id );
-       }
-       else
-       {
-               return glfwGetMouseButton( vg_window, id ) == GLFW_PRESS;
-       }
-}
+// Engine components
+#include "vg/vg_audio.h"
+#include "vg/vg_shader.h"
+#include "vg/vg_lines.h"
+#include "vg/vg_tex.h"
+#include "vg/vg_input.h"
+#include "vg/vg_ui.h"
+#include "vg/vg_console.h"
+#include "vg/vg_debug.h"
 
-void vg_update_inputs(void)
-{
-       // Update button inputs
-       for( int i = 0; i < vg_list_size( vg_button_binds ); i ++ )
-       {
-               struct button_binding *binding = vg_button_binds + i;
-               binding->prev = binding->value;
-       
-               if( vg_input_mode == k_EInputMode_pc )
-               {
-                       binding->value = get_button_cross_device( binding->bind );
-               }
-               else
-               {
-                       binding->value = vg_gamepad.buttons[ binding->bind ];
-               }
-       }
-       
-       // Update axis inputs
-       for( int i = 0; i < vg_list_size( vg_axis_binds ); i ++ )
-       {
-               struct axis_binding *binding = vg_axis_binds + i;
-               
-               if( vg_input_mode == k_EInputMode_pc )
-               {
-                       binding->value  = get_button_cross_device( binding->positive );
-                       binding->value -= get_button_cross_device( binding->negative );
-               }
-               else
-               {
-                       binding->value = vg_gamepad.axes[ binding->bind ];
-               }
-       }
-}
+#ifdef VG_STEAM
+#include "vg/vg_steamworks.h"
+#endif
 
 // Engine main
 // ===========================================================================================================
@@ -234,13 +125,14 @@ void vg_exiterr( const char *strErr )
 
 void vg_mouse_callback( GLFWwindow* ptrW, double xpos, double ypos )
 {
-       vg_mouse_x = xpos;
-       vg_mouse_y = ypos;
+       vg_mouse[0] = xpos;
+       vg_mouse[1] = ypos;
 }
 
 void vg_scroll_callback( GLFWwindow* ptrW, double xoffset, double yoffset )
 {
-       
+       vg_mouse_wheel[0] += xoffset;
+       vg_mouse_wheel[1] += yoffset;
 }
 
 void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
@@ -249,6 +141,7 @@ void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h )
        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_render(void) VG_GAMELOOP;
@@ -259,14 +152,8 @@ static void vg_init( int argc, char *argv[], const char *window_name )
 {
 #ifdef VG_STEAM
        // Initialize steamworks
-       if( !sw_init( 1218140U ) )
-       {
-               vg_exiterr( "Steamworks failed to initialize" );
-       }
-       else
-       {
-               vg_register_exit( &sw_SteamAPI_Shutdown, "SteamAPI" );
-       }
+       if( !sw_init() )
+               return;
 #endif
        
        // Context creation
@@ -277,6 +164,12 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE );
        glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE );
        
+#ifdef VG_CAPTURE_MODE
+       glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE );
+#else
+       glfwWindowHint( GLFW_RESIZABLE, GLFW_TRUE );
+#endif
+
        glfwWindowHint( GLFW_SAMPLES, 4 );
        
        GLFWmonitor *monitor_primary = glfwGetPrimaryMonitor();
@@ -305,8 +198,8 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        glfwSetCursorPosCallback( vg_window, vg_mouse_callback );
        glfwSetScrollCallback( vg_window, vg_scroll_callback );
        
-       //glfwSetCharCallback( vg_window, console_proc_wchar );
-       //glfwSetKeyCallback( vg_window, console_proc_key );
+       glfwSetCharCallback( vg_window, console_proc_wchar );
+       glfwSetKeyCallback( vg_window, console_proc_key );
        //glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
 
        if( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) 
@@ -317,6 +210,8 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        const unsigned char* glver = glGetString( GL_VERSION );
        vg_success( "Load setup complete, OpenGL version: %s\n", glver );
        
+       vg_run_gfx_diagnostics();
+       
        for( int id = 0; id <= GLFW_JOYSTICK_LAST; id ++ )
        {
                if( glfwJoystickIsGamepad( id ) )
@@ -327,37 +222,62 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                        vg_gamepad_ready = 1;
                        vg_gamepad_id = id;
                        
-                       return;
+                       break;
                }
        }
        
-       vg_audio_init();
-       vg_register_exit( &vg_audio_free, "vg_audio_free" );
+       vg_lines_init();
+       vg_register_exit( &vg_lines_free, "vg_lines_free" );
+       ui_default_init();
+       vg_register_exit( &ui_default_free, "UI" );
+               
+       vg_register();
+       vg_register_exit( &vg_free, "vg_free" );
        
-       vg_start();
        if( vg_shaders_compile() )
        {
+               vg_start();
+       
+               vg_console_init();
+               vg_register_exit( &vg_console_free, "Console" );
+       
+               vg_audio_init();
+               vg_register_exit( &vg_audio_free, "vg_audio_free" );
+               
+               vg_debugtools_setup();
+               
                // Main gameloop
                while( !glfwWindowShouldClose( vg_window ) )
                {
+                       v2_copy( (v2f){ 0.0f, 0.0f }, vg_mouse_wheel );
+
                        glfwPollEvents();
                        
                        #ifdef VG_STEAM
-                       sw_RunSteamEventLoop();
+                       sw_event_loop();
                        #endif
                        
                        vg_time_last = vg_time;
                        vg_time = glfwGetTime();
-                       vg_time_delta = vg_min( vg_time - vg_time_last, 0.1f );
+                       vg_time_delta = vg_minf( vg_time - vg_time_last, 0.1f );
                        
                        vg_update_inputs();
-                       
                        vg_update();
+                       vg_render();
                        
-                       // Update mashed projections etc
+                       vg_lines_drawall((float*)vg_pv);
                        
-                       vg_render();
-                       vg_ui();
+                       {
+                               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" ) );
+                               
+                               vg_ui();
+                               vg_console_draw();
+                               vg_debugtools_draw();
+                               
+                               ui_resolve( &ui_global_ctx );
+                               ui_draw( &ui_global_ctx, NULL );
+                       }
                        
                        glfwSwapBuffers( vg_window );
                        
@@ -365,9 +285,28 @@ static void vg_init( int argc, char *argv[], const char *window_name )
                }
        }
        
-       vg_free();
        vg_exit();
 }
 
+// Screen projections
+// ============================================================================================
+
+void vg_projection_update(void)
+{
+       // Do transform local->world
+       vg_mouse_ws[0] = vg_mouse[0];
+       vg_mouse_ws[1] = vg_mouse[1];
+       vg_mouse_ws[2] = 1.0f;
+       
+       vg_mouse_ws[0] =   (2.0f * vg_mouse_ws[0]) / ((float)vg_window_x) - 1.0f;
+       vg_mouse_ws[1] = -((2.0f * vg_mouse_ws[1]) / ((float)vg_window_y) - 1.0f);
+       
+       m3x3f inverse;
+       m3x3_inv( vg_pv, inverse ); 
+       m3x3_mulv( inverse, vg_mouse_ws, vg_mouse_ws );
+}
+
+#endif
+
 u32 NvOptimusEnablement = 0x00000001;
 int AmdPowerXpressRequestHighPerformance = 1;