From 95c9a820474d4974ee394f2fcfae82fbfa367dbd Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 14 May 2022 04:15:30 +0100 Subject: [PATCH] mc 1.9 --- dep/dr_soft/miniaudio_impl.c | 3 + src/{vg/template_game.c => template/main.c} | 22 +- src/template/vg_config.h | 18 ++ src/vg/vg.h | 105 +++++---- src/vg/vg_audio.h | 227 ++++++------------ src/vg/vg_console.h | 203 ++++++++++------ src/vg/vg_input.h | 36 ++- src/vg/vg_io.h | 37 +-- src/vg/vg_m.h | 63 +++-- src/vg/vg_platform.h | 37 ++- src/vg/vg_shader.h | 20 +- src/vg/vg_tex.h | 11 +- src/vg/vg_ui.h | 243 ++++++++------------ vg_compiler.sh | 46 ++-- 14 files changed, 530 insertions(+), 541 deletions(-) rename src/{vg/template_game.c => template/main.c} (52%) create mode 100644 src/template/vg_config.h diff --git a/dep/dr_soft/miniaudio_impl.c b/dep/dr_soft/miniaudio_impl.c index afcddad..fa30328 100644 --- a/dep/dr_soft/miniaudio_impl.c +++ b/dep/dr_soft/miniaudio_impl.c @@ -1,2 +1,5 @@ +#define MA_NO_GENERATION +#define MA_NO_DECODING +#define MA_NO_ENCODING #define MINIAUDIO_IMPLEMENTATION #include "miniaudio.h" diff --git a/src/vg/template_game.c b/src/template/main.c similarity index 52% rename from src/vg/template_game.c rename to src/template/main.c index 15b26e9..644c759 100644 --- a/src/vg/template_game.c +++ b/src/template/main.c @@ -2,11 +2,10 @@ #include "vg/vg.h" -m3x3f m_projection; -m3x3f m_view; -m3x3f m_mdl; - -int main( int argc, char *argv[] ) { vg_init( argc, argv, "FishLadder" ); } +int main( int argc, char *argv[] ) +{ + vg_init( argc, argv, "Voyager Game Engine" ); +} void vg_register(void){} void vg_start(void){} @@ -23,5 +22,16 @@ void vg_render(void) // Rest of drawing code... } -void vg_ui(void){} +void vg_ui(void) +{ + gui_text( + (ui_px [2]){ vg_window_x / 2, vg_window_y / 2 }, + "Voyager Game Engine", 2, k_text_align_center + ); + + gui_text( + (ui_px [2]){ vg_window_x / 2, vg_window_y / 2 + 30 }, + "Template project file", 1, k_text_align_center + ); +} void vg_free(void){} diff --git a/src/template/vg_config.h b/src/template/vg_config.h new file mode 100644 index 0000000..e1e5ab5 --- /dev/null +++ b/src/template/vg_config.h @@ -0,0 +1,18 @@ +// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +#define VG_CONFIG + +static struct button_binding vg_button_binds[] = +{ + { .name = "primary", .bind = GLFW_MOUSE_BUTTON_LEFT }, + { .name = "secondary", .bind = GLFW_MOUSE_BUTTON_RIGHT } +}; + +static struct axis_binding vg_axis_binds[] = +{ + { .name = "horizontal", .axis = GLFW_GAMEPAD_AXIS_LEFT_X }, + { .name = "vertical", .axis = GLFW_GAMEPAD_AXIS_LEFT_Y } +}; + +static struct vg_achievement vg_achievements[] = +{ +}; diff --git a/src/vg/vg.h b/src/vg/vg.h index cb59cd1..80c31e1 100644 --- a/src/vg/vg.h +++ b/src/vg/vg.h @@ -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 #include +#include #include #include #include +#include #include #include "glad/glad.h" @@ -22,19 +23,15 @@ 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; diff --git a/src/vg/vg_audio.h b/src/vg/vg_audio.h index acbba43..6ef64eb 100644 --- a/src/vg/vg_audio.h +++ b/src/vg/vg_audio.h @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */ #define MA_NO_GENERATION #define MA_NO_DECODING @@ -9,7 +9,6 @@ #include "stb/stb_vorbis.h" #define SFX_MAX_SYSTEMS 32 -//#define SFX_FLAG_ONESHOT 0x1 #define SFX_FLAG_STEREO 0x2 #define SFX_FLAG_REPEAT 0x4 #define SFX_FLAG_PERSISTENT 0x8 @@ -30,34 +29,34 @@ struct sfx_system sfx_system *persisitent_source; int in_queue; - // Source buffer start + /* Source buffer start */ float *source, *replacement; u32 clip_start, clip_end, buffer_length; - // Modifiers + /* Modifiers */ sfx_vol_control *vol_src; float vol, cvol; - // Info + /* Info */ u32 ch, end, cur; u32 flags; - // Effects + /* Effects */ u32 fadeout, fadeout_current; - // Diagnostic + /* Diagnostic */ const char *name; }; -// Set of up to 8 sound effects packed into one +/* Set of up to 8 sound effects packed into one */ typedef struct sfx_set sfx_set; struct sfx_set { float *main; char *sources; - u32 segments[32]; //from->to,from->to ... + u32 segments[32]; /* from->to,from->to ... */ u32 numsegments; u32 ch; u32 flags; @@ -66,26 +65,27 @@ struct sfx_set ma_device g_aud_device; ma_device_config g_aud_dconfig; -// Thread 1 - audio engine ( spawned from miniaudio.h ) -// ====================================================== +/* + * Thread 1 - audio engine ( spawned from miniaudio.h ) + * ====================================================== + */ sfx_system sfx_sys[SFX_MAX_SYSTEMS]; int sfx_sys_len = 0; -// Thread 0 - Critical transfer section -// ====================================================== -MUTEX_TYPE sfx_mux_t01; // Resources share: 0 & 1 +/* + * Thread 0 - Critical transfer section + * ====================================================== + */ +MUTEX_TYPE sfx_mux_t01; /* Resources share: 0 & 1 */ -sfx_system *sfx_q[SFX_MAX_SYSTEMS]; // Stuff changed -int sfx_q_len = 0; // How much +sfx_system *sfx_q[SFX_MAX_SYSTEMS]; /* Stuff changed */ +int sfx_q_len = 0; -// x / 2 -// ====================================================== - -// g_vol_master is never directly acessed by users float g_master_volume = 1.f; -// Decompress entire vorbis stream into buffer -static float *sfx_vorbis_stream( const unsigned char *data, int len, int channels, u32 *samples ) +/* Decompress entire vorbis stream into buffer */ +static float *sfx_vorbis_stream( const unsigned char *data, int len, + int channels, u32 *samples ) { int err; stb_vorbis *pv = stb_vorbis_open_memory( data, len, &err, NULL ); @@ -97,7 +97,7 @@ static float *sfx_vorbis_stream( const unsigned char *data, int len, int channel } u32 length_samples = stb_vorbis_stream_length_in_samples( pv ); - float *buffer = (float *)malloc( length_samples * channels * sizeof( float )); + float *buffer = (float *)malloc( length_samples * channels * sizeof(float)); if( !buffer ) { @@ -106,10 +106,13 @@ static float *sfx_vorbis_stream( const unsigned char *data, int len, int channel return NULL; } - int read_samples = stb_vorbis_get_samples_float_interleaved( pv, channels, buffer, length_samples * channels ); + int read_samples = stb_vorbis_get_samples_float_interleaved( + pv, channels, buffer, length_samples * channels ); + if( read_samples != length_samples ) { - vg_warn( "| warning: sample count mismatch. Expected %u got %i\n", length_samples, read_samples ); + vg_warn( "| warning: sample count mismatch. Expected %u got %i\n", + length_samples, read_samples ); length_samples = read_samples; } @@ -136,97 +139,9 @@ static float *sfx_vorbis( const char *strFileName, int channels, u32 *samples ) } } -typedef struct sfx_bgload sfx_bgload_t; -struct sfx_bgload -{ - char *path; - u32 channels; - - float *buffer; - u32 samples; - - void *user; - - void(*OnComplete)(sfx_bgload_t *inf); -}; - -// Thread worker for background load job -void *sfx_vorbis_a_t( void *_inf ) -{ - sfx_bgload_t *info = _inf; - - // Load the ogg clip - info->buffer = sfx_vorbis( info->path, info->channels, &info->samples ); - info->OnComplete( info ); - - return NULL; -} - -// Asynchronous resource load -int sfx_vorbis_a( const char *path, int channels, void(*OnComplete)(sfx_bgload_t *inf), void *user ) -{ - vg_info( "background job started for: %s\n", path ); - - sfx_bgload_t *params = malloc( sizeof( sfx_bgload_t ) ); - params->path = malloc( strlen( path ) + 1 ); - strcpy( params->path, path ); - params->OnComplete = OnComplete; - params->user = user; - params->channels = channels; - - return vg_thread_run( sfx_vorbis_a_t, params ); -} - -// Asynchronous load-to-system callback -struct sfx_vorbis_a_to_inf -{ - sfx_system *sys; - u32 flags; -}; - -#define SFX_A_FLAG_AUTOSTART 0x1 -#define SFX_A_FLAG_AUTOFREE 0x2 - -/* -static int sfx_save( sfx_system *sys ); - -// Asynchronous load-to-system callback -void sfx_vorbis_a_to_c( sfx_bgload_t *loadinf ) -{ - struct sfx_vorbis_a_to_inf *inf = loadinf->user; - - // Mark buffer for deallocation if autofree is set - if( inf->flags & SFX_A_FLAG_AUTOFREE ) - inf->sys->replacement = loadinf->buffer; - else - inf->sys->source = loadinf->buffer; - - inf->sys->end = loadinf->samples; - - if( inf->flags & SFX_A_FLAG_AUTOSTART ) - sfx_save( inf->sys ); - - free( loadinf->path ); - free( loadinf ); - free( inf ); -} - -// Asynchronous vorbis load into audio system -void sfx_vorbis_a_to( sfx_system *sys, const char *strFileName, int channels, u32 flags ) -{ - struct sfx_vorbis_a_to_inf *inf = malloc( sizeof( struct sfx_vorbis_a_to_inf ) ); - inf->flags = flags; - inf->sys = sys; - - sys->ch = channels; - - if( !sfx_vorbis_a( strFileName, channels, sfx_vorbis_a_to_c, inf ) ) - free( inf ); -}*/ - -// 0 -// ====================================================== - +/* + * thread 0 / client code + */ static int sfx_begin_edit( sfx_system *sys ) { MUTEX_LOCK( sfx_mux_t01 ); @@ -246,12 +161,11 @@ static void sfx_end_edit( sfx_system *sys ) MUTEX_UNLOCK( sfx_mux_t01 ); } -// Mark change to be uploaded to queue system +/* Mark change to be uploaded to queue system */ static int sfx_push( sfx_system *sys ) { if( !sys->in_queue ) { - // Mark change in queue sfx_q[ sfx_q_len ++ ] = sys; sys->in_queue = 1; } @@ -261,7 +175,7 @@ static int sfx_push( sfx_system *sys ) return 1; } -// Edit a volume float, has to be function wrapped because of mutex +/* Edit a volume float, has to be function wrapped because of mutex */ static void sfx_vol_fset( sfx_vol_control *src, float to ) { MUTEX_LOCK( sfx_mux_t01 ); @@ -271,7 +185,7 @@ static void sfx_vol_fset( sfx_vol_control *src, float to ) MUTEX_UNLOCK( sfx_mux_t01 ); } -// thread-safe get volume value +/* thread-safe get volume value */ static float sfx_vol_fget( sfx_vol_control *src ) { float val; @@ -285,7 +199,7 @@ static float sfx_vol_fget( sfx_vol_control *src ) return val; } -// thread-safe set master volume +/* thread-safe set master volume */ static void sfx_set_master( float to ) { MUTEX_LOCK( sfx_mux_t01 ); @@ -295,7 +209,7 @@ static void sfx_set_master( float to ) MUTEX_UNLOCK( sfx_mux_t01 ); } -// thread-safe get master volume +/* thread-safe get master volume */ static float sfx_get_master(void) { float val; @@ -311,7 +225,6 @@ static float sfx_get_master(void) void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount ); -// Miniaudio.h init static void vg_audio_init(void) { g_aud_dconfig = ma_device_config_init( ma_device_type_playback ); @@ -338,29 +251,30 @@ static void vg_audio_init(void) } } -// Shutdown audio device static void vg_audio_free(void) { ma_device_uninit( &g_aud_device ); } -// 1 -// ====================================================== +/* + * thread 1 + */ -// Create and return slot for a sound static sfx_system *sfx_alloc(void) { if( sfx_sys_len >= SFX_MAX_SYSTEMS ) return NULL; - // A conditional is done against this in localization step, - // Needs to be initialized. + /* + * A conditional is done against this in localization step, + * Needs to be initialized. + */ sfx_sys[ sfx_sys_len ].source = NULL; return sfx_sys + (sfx_sys_len++); } -// Fetch samples into pcf +/* Fetch samples into pcf */ static void audio_mixer_getsamples( float *pcf, float *source, u32 cur, u32 ch ) { if( ch == 2 ) @@ -375,10 +289,13 @@ static void audio_mixer_getsamples( float *pcf, float *source, u32 cur, u32 ch ) } } -// miniaudio.h interface -void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount ) +/* + * callback from miniaudio.h interface + */ +void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, + const void *pInput, ma_uint32 frameCount ) { - // Process incoming sound queue + /* Process incoming sound queue */ MUTEX_LOCK( sfx_mux_t01 ); while( sfx_q_len --> 0 ) @@ -388,30 +305,26 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput src->in_queue = 0; - // Copy clone = sfx_alloc(); *clone = *src; - // Links need to exist on persistent sounds + /* Links need to exist on persistent sounds */ clone->persisitent_source = src->flags & SFX_FLAG_PERSISTENT? src: NULL; } sfx_q_len = 0; - // Volume modifiers + /* Volume modifiers */ for( int i = 0; i < sfx_sys_len; i ++ ) { sfx_system *sys = sfx_sys + i; - // Apply persistent volume if linked + /* Apply persistent volume if linked */ if( sys->flags & SFX_FLAG_PERSISTENT ) { sys->vol = sys->persisitent_source->vol * g_master_volume; - // Persistent triggers - // ------------------- - - // Fadeout effect ( + remove ) + /* Fadeout effect ( + remove ) */ if( sys->persisitent_source->fadeout ) { sys->fadeout_current = sys->persisitent_source->fadeout_current; @@ -422,7 +335,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput } } - // Apply volume slider if it has one linked + /* Apply volume slider if it has one linked */ if( sys->vol_src ) sys->cvol = sys->vol * sys->vol_src->val; else @@ -431,7 +344,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput MUTEX_UNLOCK( sfx_mux_t01 ); - // Clear buffer + /* Clear buffer */ float *pOut32F = (float *)pOutBuf; for( int i = 0; i < frameCount * 2; i ++ ){ pOut32F[i] = 0.f; @@ -453,7 +366,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput if( sys->fadeout ) { - // Force this system to be removed now + /* Force this system to be removed now */ if( sys->fadeout_current == 0 ) { sys->flags &= 0x00000000; @@ -468,7 +381,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput { audio_mixer_getsamples( pcf, sys->source, cursor, sys->ch ); - float vol = sys->vol; + float vol = sys->cvol; if( sys->fadeout ) { @@ -504,7 +417,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput } } - // Redistribute sound systems + /* Redistribute sound systems */ MUTEX_LOCK( sfx_mux_t01 ); u32 idx = 0, wr = 0; @@ -512,7 +425,7 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput { sfx_system *src = sfx_sys + idx; - // Keep only if cursor is before end or repeating + /* Keep only if cursor is before end or repeating */ if( src->cur < src->end || (src->flags & SFX_FLAG_REPEAT) ) { sfx_sys[ wr ++ ] = sfx_sys[ idx ]; @@ -527,9 +440,12 @@ void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput (void)pInput; } -// Load strings into sfx_set's memory -// String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0" -static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAsync ) +/* + * Load strings into sfx_set's memory + * String layout: "sounda.ogg\0soundb.ogg\0soundc.ogg\0\0" + */ +static void sfx_set_strings( sfx_set *dest, char *strSources, + u32 flags, int bAsync ) { dest->ch = (flags & SFX_FLAG_STEREO)? 2: 1; @@ -558,7 +474,8 @@ static void sfx_set_strings( sfx_set *dest, char *strSources, u32 flags, int bAs if( nbuf ) { dest->main = nbuf; - memcpy( dest->main + (total-samples)*dest->ch, sound, samples*dest->ch*sizeof(float) ); + memcpy( dest->main + (total-samples)*dest->ch, + sound, samples*dest->ch*sizeof(float) ); free( sound ); dest->segments[ dest->numsegments*2+0 ] = total-samples; @@ -595,7 +512,7 @@ static void sfx_set_play( sfx_set *source, sfx_system *sys, int id ) sys->end = source->segments[ id*2 + 1 ]; sys->ch = source->ch; - // Diagnostics + /* for diagnostics */ sys->clip_start = sys->cur; sys->clip_end = sys->end; sys->buffer_length = source->segments[ (source->numsegments-1)*2 + 1 ]; @@ -604,8 +521,9 @@ static void sfx_set_play( sfx_set *source, sfx_system *sys, int id ) } } -// Pick a random sound from the buffer and play it into system -static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, int min_id, int max_id ) +/* Pick a random sound from the buffer and play it into system */ +static void sfx_set_playrnd( sfx_set *source, sfx_system *sys, + int min_id, int max_id ) { if( !source->numsegments ) return; @@ -632,7 +550,6 @@ static void sfx_system_fadeout( sfx_system *sys, u32 length_samples ) } } -// Free set resources static void sfx_set_free( sfx_set *set ) { free( set->main ); diff --git a/src/vg/vg_console.h b/src/vg/vg_console.h index e8ca2f0..304de0f 100644 --- a/src/vg/vg_console.h +++ b/src/vg/vg_console.h @@ -1,10 +1,11 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ struct vg_console { struct vg_convar { void *data; + void (*update)(void); const char *name; enum vg_convar_dtype @@ -20,10 +21,18 @@ struct vg_console struct { int min, max, clamp; - } opt_i32; + } + opt_i32; + + struct + { + float min, max; + int clamp; + } + opt_f32; }; - int persistent; // Should be stored to cfg/auto.conf + int persistent; /* Should this var be stored to cfg/auto.conf? */ } *convars; @@ -48,10 +57,6 @@ struct vg_console } vg_console = { .scale = 1 }; -// Declerations -// ------------ - -// Registration static void vg_convar_push( struct vg_convar cv ); static void vg_function_push( struct vg_cmd cmd ); @@ -63,24 +68,26 @@ static void vg_console_write_persistent(void); static void vg_console_free(void); static void execute_console_input( const char *cmd ); -// Console interface -// ----------------- - +/* + * Console interface + */ static void console_make_selection( int* start, int* end ); -static void console_move_cursor( int* cursor0, int* cursor1, int dir, int snap_together ); +static void console_move_cursor( int* cursor0, int* cursor1, + int dir, int snap_together ); static int console_makeroom( int datastart, int length ); static int console_delete_char( int direction ); static void console_to_clipboard(void); static void console_clipboard_paste(void); static void console_put_char( char c ); static void console_history_get( char* buf, int entry_num ); -static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int action, int mods ); +static void console_proc_key( GLFWwindow* ptrW, int key, + int scancode, int action, int mods ); static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar ); static int vg_console_enabled(void); -// ========================================================================================================= -// Implementation - +/* + * Implementation + */ static int vg_console_enabled(void) { return vg_console.enabled; @@ -107,7 +114,8 @@ static void vg_console_draw( void ) ui_global_ctx.cursor[0] = 0; ui_global_ctx.cursor[1] = 0; - ui_global_ctx.cursor[3] = vg_list_size( vg_console.lines )*fh*vg_console.scale; + ui_global_ctx.cursor[3] = + vg_list_size( vg_console.lines )*fh*vg_console.scale; ui_fill_x( &ui_global_ctx ); ui_new_node( &ui_global_ctx ); @@ -122,7 +130,8 @@ static void vg_console_draw( void ) if( ptr < 0 ) ptr = vg_list_size( vg_console.lines )-1; - ui_text( &ui_global_ctx, ui_global_ctx.cursor, vg_console.lines[ptr], vg_console.scale, 0 ); + ui_text( &ui_global_ctx, ui_global_ctx.cursor, + vg_console.lines[ptr], vg_console.scale, 0 ); ui_global_ctx.cursor[1] -= fh*vg_console.scale; ptr --; @@ -137,13 +146,15 @@ static void vg_console_draw( void ) { ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 ); - ui_text( &ui_global_ctx, ui_global_ctx.cursor, vg_console.input, vg_console.scale, 0 ); + ui_text( &ui_global_ctx, ui_global_ctx.cursor, + vg_console.input, vg_console.scale, 0 ); int start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ), end = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user ); ui_global_ctx.cursor[0] = (start * UI_GLYPH_SPACING_X * vg_console.scale); - ui_global_ctx.cursor[2] = (start == end? 0.5f: (float)(end-start)) * (float)UI_GLYPH_SPACING_X * (float)vg_console.scale; + ui_global_ctx.cursor[2] = (start == end? 0.5f: (float)(end-start)) + * (float)UI_GLYPH_SPACING_X * (float)vg_console.scale; ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x66ffffff ); } @@ -193,8 +204,11 @@ static void vg_console_init(void) vg_log_callback = vg_console_println; vg_convar_push( (struct vg_convar) - { .name = "console_scale", .data = &vg_console.scale, .data_type = k_convar_dtype_i32, - .opt_i32 = { .clamp = 1, .min = 1, .max = 7 } } ); + { .name = "console_scale", .data = &vg_console.scale, + .data_type = k_convar_dtype_i32, + .opt_i32 = { .clamp = 1, .min = 1, .max = 7 }, + .update = NULL + }); vg_function_push( (struct vg_cmd){ .name = "list", @@ -206,7 +220,7 @@ static void vg_console_init(void) .function = vg_console_chartest }); - // Read and exec persistent commands + /* Read and exec persistent commands */ FILE *fp = fopen( "cfg/auto.conf", "r" ); if( fp ) { @@ -270,7 +284,7 @@ static void execute_console_input( const char *cmd ) int in_token = 0; - // Split string into tokens + /* Split string into tokens */ for( int i = 0; i < vg_list_size( temp ); i ++ ) { if( cmd[i] ) @@ -305,13 +319,14 @@ static void execute_console_input( const char *cmd ) return; int data_int; + float data_float; for( int i = 0; i < arrlen( vg_console.convars ); i ++ ) { struct vg_convar *cv = &vg_console.convars[ i ]; if( !strcmp( cv->name, args[0] ) ) { - // Cvar Matched, try get value + /* Cvar Matched, try get value */ if( arg_count >= 2 ) { switch( cv->data_type ) @@ -320,19 +335,36 @@ static void execute_console_input( const char *cmd ) case k_convar_dtype_i32: data_int = atoi( args[1] ); - *((int *)cv->data) = cv->opt_i32.clamp? VG_MIN( VG_MAX(data_int, cv->opt_i32.min), cv->opt_i32.max ): data_int; + + *((int *)cv->data) = cv->opt_i32.clamp? + VG_MIN( VG_MAX(data_int, cv->opt_i32.min), cv->opt_i32.max ): + data_int; break; - case k_convar_dtype_f32: *((float *)cv->data) = atof( temp ); break; + case k_convar_dtype_f32: + data_float = atof( args[1] ); + *((float *)cv->data) = cv->opt_f32.clamp? + vg_minf( vg_maxf( data_float, cv->opt_f32.min), cv->opt_f32.max ): + data_float; + break; } + + if( cv->update ) + cv->update(); } else { switch( cv->data_type ) { - case k_convar_dtype_i32: vg_info( "= %d\n", *((int *)cv->data) ); break; - case k_convar_dtype_u32: vg_info( "= %u\n", *((u32 *)cv->data) ); break; - case k_convar_dtype_f32: vg_info( "= %.4f\n", *((float *)cv->data) ); break; + case k_convar_dtype_i32: + vg_info( "= %d\n", *((int *)cv->data) ); + break; + case k_convar_dtype_u32: + vg_info( "= %u\n", *((u32 *)cv->data) ); + break; + case k_convar_dtype_f32: + vg_info( "= %.4f\n", *((float *)cv->data) ); + break; } } @@ -340,34 +372,40 @@ static void execute_console_input( const char *cmd ) } } - // Try commands + /* + * Find and excecute command + */ for( int i = 0; i < arrlen( vg_console.functions ); i ++ ) { struct vg_cmd *cmd = &vg_console.functions[ i ]; if( !strcmp( cmd->name, args[0] ) ) { - // Call function cmd->function( arg_count-1, args+1 ); return; } } - vg_error( "No command/variable named '%s'. Use 'list' to view all\n", args[0] ); + vg_error( "No command/var named '%s'. Use 'list' to view all\n", args[0] ); } -// ============================================================================================================================= -// Console interface - +/* + * Console Interface + */ static void console_make_selection( int* start, int* end ) { *start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ); *end = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user ); } -static void console_move_cursor( int* cursor0, int* cursor1, int dir, int snap_together ) +static void console_move_cursor( int* cursor0, int* cursor1, + int dir, int snap_together ) { *cursor0 = VG_MAX( 0, vg_console.cursor_user + dir ); - *cursor0 = VG_MIN( VG_MIN( vg_list_size( vg_console.input ), strlen( vg_console.input )), *cursor0 ); + *cursor0 = + VG_MIN( + VG_MIN( vg_list_size( vg_console.input ), strlen( vg_console.input )), + *cursor0 ); + if( snap_together ) *cursor1 = *cursor0; } @@ -376,11 +414,14 @@ static int console_makeroom( int datastart, int length ) { int move_to = VG_MIN( datastart+length, vg_list_size( vg_console.input ) ); int move_amount = strlen( vg_console.input )-datastart; - int move_end = VG_MIN( move_to+move_amount, vg_list_size( vg_console.input ) ); + int move_end = + VG_MIN( move_to+move_amount, vg_list_size( vg_console.input ) ); move_amount = move_end-move_to; if( move_amount ) - memmove( &vg_console.input[ move_to ], &vg_console.input[ datastart ], move_end-move_to ); + memmove( &vg_console.input[ move_to ], + &vg_console.input[ datastart ], + move_end-move_to ); vg_console.input[ move_end ] = '\0'; @@ -392,20 +433,22 @@ static int console_delete_char( int direction ) int start, end; console_make_selection( &start, &end ); - // There is no selection + /* There is no selection */ if( !(end-start) ) { if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_console.input ) ); else if( direction == -1 ) start = VG_MAX( start-1, 0 ); } - // Still no selction, no need to do anything + /* Still no selction, no need to do anything */ if( !(end-start) ) return start; - // Copy the end->terminator to start + /* Copy the end->terminator to start */ int remaining_length = strlen( vg_console.input )+1-end; - memmove( &vg_console.input[ start ], &vg_console.input[ end ], remaining_length ); + memmove( &vg_console.input[ start ], + &vg_console.input[ end ], + remaining_length ); return start; } @@ -432,8 +475,8 @@ static void console_clipboard_paste(void) int cpylength = console_makeroom(datastart, length); memcpy( vg_console.input + datastart, clipboard, cpylength); - - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, cpylength, 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, cpylength, 1 ); } static void console_put_char( char c ) @@ -454,11 +497,14 @@ static void console_history_get( char* buf, int entry_num ) if( !vg_console.history_count ) return; - int pick = (vg_console.history_last - VG_MIN( entry_num, vg_console.history_count -1 )) % vg_list_size( vg_console.history ); + int offset = VG_MIN( entry_num, vg_console.history_count -1 ), + pick = (vg_console.history_last - offset) % + vg_list_size( vg_console.history ); strcpy( buf, vg_console.history[ pick ] ); } -static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int action, int mods ) +static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, + int action, int mods ) { if( action ) { @@ -475,24 +521,26 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio if( key == GLFW_KEY_LEFT ) { - if( mods & GLFW_MOD_SHIFT ) // Receed secondary cursor + if( mods & GLFW_MOD_SHIFT ) /* Receed secondary cursor */ { console_move_cursor( &vg_console.cursor_user, NULL, -1, 0 ); } - else // Match and receed both cursors + else /* Match and receed both cursors */ { - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -cursor_diff, 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, -cursor_diff, 1 ); } } - else if( key == GLFW_KEY_RIGHT ) // Advance secondary cursor + else if( key == GLFW_KEY_RIGHT ) /* Advance secondary cursor */ { if( mods & GLFW_MOD_SHIFT ) { console_move_cursor( &vg_console.cursor_user, NULL, 1, 0 ); } - else // Match and advance both cursors + else /* Match and advance both cursors */ { - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, +cursor_diff, 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, +cursor_diff, 1 ); } } else if( key == GLFW_KEY_DOWN ) @@ -502,7 +550,8 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio { vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 ); console_history_get( vg_console.input, vg_console.history_pos ); - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 ); } } else if( key == GLFW_KEY_UP ) @@ -525,49 +574,54 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio ); console_history_get( vg_console.input, vg_console.history_pos ); - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, + vg_list_size( vg_console.input ), 1); } } - else if( key == GLFW_KEY_BACKSPACE ) // Lookback delete + else if( key == GLFW_KEY_BACKSPACE ) /* Lookback delete */ { vg_console.cursor_user = console_delete_char( -1 ); vg_console.cursor_pos = vg_console.cursor_user; } - else if( key == GLFW_KEY_DELETE ) // Lookforward delete + else if( key == GLFW_KEY_DELETE ) /* Lookforward delete */ { vg_console.cursor_user = console_delete_char( 1 ); vg_console.cursor_pos = vg_console.cursor_user; } - else if( key == GLFW_KEY_HOME ) // Home key + else if( key == GLFW_KEY_HOME ) /* Home key */ { if( mods & GLFW_MOD_SHIFT ) console_move_cursor( &vg_console.cursor_user, NULL, -10000, 0 ); else - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -10000, 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, -10000, 1 ); } - else if( key == GLFW_KEY_END ) // End key + else if( key == GLFW_KEY_END ) /* End key */ { if( mods & GLFW_MOD_SHIFT ) console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0 ); else - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, + vg_list_size( vg_console.input ), 1 ); } else if( key == GLFW_KEY_A ) { - if( mods & GLFW_MOD_CONTROL ) // Select all + if( mods & GLFW_MOD_CONTROL ) /* Select all */ { console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0); console_move_cursor( &vg_console.cursor_pos, NULL, -10000, 0); } } - else if( key == GLFW_KEY_C ) // Copy + else if( key == GLFW_KEY_C ) /* Copy */ { if( mods & GLFW_MOD_CONTROL ) { console_to_clipboard(); } } - else if( key == GLFW_KEY_X ) // Cut + else if( key == GLFW_KEY_X ) /* Cut */ { if( mods & GLFW_MOD_CONTROL ) { @@ -576,7 +630,7 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio vg_console.cursor_pos = vg_console.cursor_user; } } - else if( key == GLFW_KEY_V ) // Paste + else if( key == GLFW_KEY_V ) /* Paste */ { if( mods & GLFW_MOD_CONTROL ) { @@ -590,25 +644,30 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int actio vg_info( "%s\n", vg_console.input ); - if( strcmp( vg_console.input, vg_console.history[ vg_console.history_last ]) ) + if( strcmp( vg_console.input, + vg_console.history[ vg_console.history_last ]) ) { - vg_console.history_last = ( vg_console.history_last + 1) % vg_list_size(vg_console.history ); - vg_console.history_count = VG_MIN( vg_list_size( vg_console.history ), vg_console.history_count + 1 ); - strcpy( vg_console.history[ vg_console.history_last ], vg_console.input ); + vg_console.history_last = ( vg_console.history_last + 1) % + vg_list_size(vg_console.history ); + vg_console.history_count = + VG_MIN( vg_list_size( vg_console.history ), + vg_console.history_count + 1 ); + strcpy( vg_console.history[ vg_console.history_last ], + vg_console.input ); } vg_console.history_pos = -1; execute_console_input( vg_console.input ); - console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -10000, 1 ); + console_move_cursor( &vg_console.cursor_user, + &vg_console.cursor_pos, -10000, 1 ); vg_console.input[0] = '\0'; } } } -// Handle an OS based input of UTF32 character from the keyboard or such +/* Handle an OS based input of UTF32 character from the keyboard or such */ static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar ) { - //LOG_INFO("Recieved wchar: %u\n", uWchar); if( uWchar <= 0x7F && (char)uWchar != 0x60) { console_put_char((char)uWchar); diff --git a/src/vg/vg_input.h b/src/vg/vg_input.h index 8c43550..4818d52 100644 --- a/src/vg/vg_input.h +++ b/src/vg/vg_input.h @@ -1,9 +1,9 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ -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)); +static inline float vg_get_axis( const char *axis ); +static inline int vg_get_button( const char *button ); +static inline int vg_get_button_down( const char *button ); +static inline int vg_get_button_up( const char *button ); enum vg_button_state { @@ -13,13 +13,12 @@ enum vg_button_state k_button_state_none = 0 }; -// Input -// =========================================================================================================== GLFWgamepadstate vg_gamepad; int vg_gamepad_ready = 0; const char *vg_gamepad_name = NULL; int vg_gamepad_id; +/* TODO: Fix this... */ enum EInputMode { k_EInputMode_pc, @@ -34,6 +33,7 @@ static struct axis_binding { int positive; int bind; + int axis; }; int negative; @@ -68,9 +68,9 @@ static inline float vg_get_axis( const char *axis ) static inline struct button_binding *vg_get_button_ptr( const char *button ) { - for( int i = 0; i < vg_list_size( vg_button_binds ); i ++ ) + for( int i=0; i 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; - } } void vg_update_inputs(void) { - // Update button inputs + /* Update button inputs */ for( int i = 0; i < vg_list_size( vg_button_binds ); i ++ ) { struct button_binding *binding = vg_button_binds + i; @@ -142,7 +138,7 @@ void vg_update_inputs(void) } } - // Update axis inputs + /* Update axis inputs */ for( int i = 0; i < vg_list_size( vg_axis_binds ); i ++ ) { struct axis_binding *binding = vg_axis_binds + i; diff --git a/src/vg/vg_io.h b/src/vg/vg_io.h index afd134f..376dfda 100644 --- a/src/vg/vg_io.h +++ b/src/vg/vg_io.h @@ -1,7 +1,4 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved - -// Coloured logging -// =========================================================================================================== +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ #define KNRM "\x1B[0m" #define KRED "\x1B[31m" @@ -14,7 +11,8 @@ void (*vg_log_callback)( const char *str ) = NULL; -static void vg_log_write( FILE *file, const char *prefix, const char *fmt, va_list args ) +static void vg_log_write( FILE *file, const char *prefix, + const char *fmt, va_list args ) { char buffer[512]; int i, j; @@ -36,18 +34,23 @@ static void vg_log_write( FILE *file, const char *prefix, const char *fmt, va_li vg_log_callback( buffer ); } -static void vg_success( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KGRN "success" KWHT "| " KGRN), fmt, args ); va_end( args ); } -static void vg_info( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KNRM " info" KWHT "| " KNRM), fmt, args ); va_end( args ); } -static void vg_warn( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stdout, (KYEL " warn" KWHT "| " KYEL), fmt, args ); va_end( args ); } -static void vg_error( const char *fmt, ... ) - { va_list args; va_start( args, fmt ); vg_log_write( stderr, (KRED " error" KWHT "| " KRED), fmt, args ); va_end( args ); } +#define VG_LOGX( NAME, PIPE, PFX ) \ +static void NAME(const char *fmt, ...) \ +{ \ + va_list args; \ + va_start( args, fmt ); \ + vg_log_write( PIPE, (PFX), fmt, args ); \ + va_end( args ); \ +} -// FILE IO -// =========================================================================================================== +VG_LOGX( vg_success, stdout, (KGRN "success" KWHT "| " KGRN) ) +VG_LOGX( vg_info, stdout, (KNRM " info" KWHT "| " KNRM) ) +VG_LOGX( vg_warn, stdout, (KYEL " warn" KWHT "| " KYEL) ) +VG_LOGX( vg_error, stderr, (KRED " error" KWHT "| " KRED) ) +/* + * FIle I/O + */ static i64 vg_file_size( FILE *fileptr ) { fseek( fileptr, 0, SEEK_END ); @@ -57,7 +60,7 @@ static i64 vg_file_size( FILE *fileptr ) return fsize; } -static void *vg_disk_open_read( const char *path, int const reserve_end, i64 *size ) +static void *vg_disk_open_read( const char *path, int reserve_end, i64 *size ) { FILE *f = fopen( path, "rb" ); if( f ) @@ -67,7 +70,7 @@ static void *vg_disk_open_read( const char *path, int const reserve_end, i64 *si if( buf ) { - // Invalid / corrupt read + /* Invalid / corrupt read */ if( fread( buf, 1, fsize, f ) != fsize ) { free( buf ); diff --git a/src/vg/vg_m.h b/src/vg/vg_m.h index 577138f..392eb2f 100644 --- a/src/vg/vg_m.h +++ b/src/vg/vg_m.h @@ -1,12 +1,8 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved - -// Util -// ================================================================================================================== +/* Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved */ #define VG_PIf 3.14159265358979323846264338327950288f #define VG_TAUf 6.28318530717958647692528676655900576f -// Simple min/max replacements static inline float vg_minf( float a, float b ) { return a < b? a: b; @@ -25,7 +21,6 @@ static inline float vg_clampf( float a, float min, float max ) #define VG_MIN( A, B ) ((A)<(B)?(A):(B)) #define VG_MAX( A, B ) ((A)>(B)?(A):(B)) -// Hopefully deprecate this!! static inline int vg_min( int a, int b ) { return a < b? a: b; @@ -36,15 +31,14 @@ static inline int vg_max( int a, int b ) return a > b? a: b; } -// Convert degrees to radians static inline float vg_rad( float deg ) { return deg * VG_PIf / 180.0f; } -// Vector 2 -// ================================================================================================================== - +/* + * Vector 3 + */ static inline void v2_copy( v2f a, v2f b ) { b[0] = a[0]; b[1] = a[1]; @@ -107,7 +101,6 @@ static inline void v2_divs( v2f a, float s, v2f d ) d[0] = a[0]/s; d[1] = a[1]/s; } - static inline void v2_mul( v2f a, v2f b, v2f d ) { d[0] = a[0]*b[0]; @@ -159,9 +152,9 @@ static inline void v2_lerp( v2f a, v2f b, float t, v2f d ) d[1] = a[1] + t*(b[1]-a[1]); } -// Vector 3 -// ================================================================================================================== - +/* + * Vector 3 + */ static inline void v3_zero( v3f a ) { a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; @@ -289,9 +282,9 @@ static inline void v3_fill( v3f a, float v ) a[2] = v; } -// Vector 4 -// ================================================================================================================== - +/* + * Vector 4 + */ static inline void v4_copy( v4f a, v4f b ) { b[0] = a[0]; b[1] = a[1]; b[2] = a[2]; b[3] = a[3]; @@ -302,8 +295,9 @@ static inline void v4_zero( v4f a ) a[0] = 0.f; a[1] = 0.f; a[2] = 0.f; a[3] = 0.f; } -// Matrix 2x2 -// =========================================================================================================== +/* + * Matrix 2x2 + */ #define M2X2_INDENTIY {{1.0f, 0.0f, }, \ { 0.0f, 1.0f, }} @@ -336,8 +330,9 @@ static inline void m2x2_create_rotation( m2x2f a, float theta ) a[1][1] = c; } -// Matrix 3x3 -//====================================================================================================== +/* + * Matrix 3x3 + */ #define M3X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\ { 0.0f, 1.0f, 0.0f, },\ @@ -438,7 +433,8 @@ static inline void m3x3_mulv( m3x3f m, v3f v, v3f d ) v3_copy( res, d ); } -static inline void m3x3_projection( m3x3f dst, float const left, float const right, float const bottom, float const top ) +static inline void m3x3_projection( m3x3f dst, + float const left, float const right, float const bottom, float const top ) { float rl, tb; @@ -489,8 +485,9 @@ static inline void m3x3_rotate( m3x3f m, float angle ) m[1][2] = m02 * -s + m12 * c; } -// Matrix 4x3 -// ================================================================================================================== +/* + * Matrix 4x3 + */ #define M4X3_IDENTITY {{1.0f, 0.0f, 0.0f, },\ { 0.0f, 1.0f, 0.0f, },\ @@ -555,8 +552,9 @@ static inline void m4x3_mulv( m4x3f m, v3f v, v3f d ) v3_copy( res, d ); } -// Affine transforms -// ==================================================================================================================== +/* + * Affine transforms + */ static inline void m4x3_translate( m4x3f m, v3f v ) { @@ -620,7 +618,6 @@ static inline void m4x3_rotate_z( m4x3f m, float angle ) m4x3_mul( m, t, m ); } -// Warning: These functions are unoptimized.. static inline void m4x3_expand_aabb_point( m4x3f m, boxf box, v3f point ) { v3f v; @@ -661,10 +658,11 @@ static inline void m4x3_transform_aabb( m4x3f m, boxf box ) m4x3_expand_aabb_point( m, box, (v3f){ b[0], b[1], b[2] } ); } -// Planes (double precision) -// ================================================================================================================== - -static inline void tri_to_plane( double a[3], double b[3], double c[3], double p[4] ) +/* + * Planes (double precision) + */ +static inline void tri_to_plane( double a[3], double b[3], + double c[3], double p[4] ) { double edge0[3]; double edge1[3]; @@ -690,7 +688,8 @@ static inline void tri_to_plane( double a[3], double b[3], double c[3], double p p[2] = p[2] / l; } -static inline int plane_intersect( double a[4], double b[4], double c[4], double p[4] ) +static inline int plane_intersect( double a[4], double b[4], + double c[4], double p[4] ) { double const epsilon = 1e-8f; diff --git a/src/vg/vg_platform.h b/src/vg/vg_platform.h index b1986b9..02681d0 100644 --- a/src/vg/vg_platform.h +++ b/src/vg/vg_platform.h @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ typedef uint8_t u8; typedef uint16_t u16; @@ -32,19 +32,17 @@ struct vg_achievement }; #define vg_static_assert _Static_assert - #define vg_list_size( A ) (sizeof(A)/sizeof(A[0])) -// THREADING -// ================================================================================================================== - -// Pthred emulation for windows +/* Pthred emulation for windows */ #ifdef _WIN32 #include #define MUTEX_TYPE HANDLE #define MUTEX_INITIALIZER NULL #define MUTEX_SETUP(x) (x) = CreateMutex(NULL, FALSE, NULL) - #define MUTEX_CLEANUP(x) (CloseHandle(x)) //TODO: Why is this defined but never used? + + /* TODO: Why is this defined but never used? */ + #define MUTEX_CLEANUP(x) (CloseHandle(x)) #define MUTEX_LOCK(x) emulate_pthread_mutex_lock(&(x)) #define MUTEX_UNLOCK(x) (ReleaseMutex(x)) @@ -53,7 +51,8 @@ struct vg_achievement if( *mx == NULL ) /* static initializer? */ { HANDLE p = CreateMutex( NULL, FALSE, NULL ); - if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL ) != NULL ) + if( InterlockedCompareExchangePointer( (PVOID*)mx, (PVOID)p, NULL ) + != NULL ) CloseHandle(p); } @@ -71,21 +70,22 @@ struct vg_achievement int vg_thread_run( void *pfunc, void *data ) { #ifdef _WIN32 - HANDLE hThread = CreateThread ( - NULL, // Thread attributes - 0, // Stack size (0 = use default) - pfunc, // Thread start address - data, // Parameter to pass to the thread - 0, // Creation flags - NULL // Thread id + NULL, /* Thread attributes */ + 0, /* Stack size (0 = use default) */ + pfunc, /* Thread start address */ + data, /* Parameter to pass to the thread */ + 0, /* Creation flags */ + NULL /* Thread id */ ); if ( hThread == NULL ) { - // Thread creation failed. - // More details can be retrieved by calling GetLastError() + /* + * Thread creation failed. + * More details can be retrieved by calling GetLastError() + */ return 1; } else @@ -93,9 +93,7 @@ int vg_thread_run( void *pfunc, void *data ) CloseHandle( hThread ); return 0; } - #else - pthread_t hThread; if( pthread_create( &hThread, NULL, pfunc, data ) ) { @@ -106,6 +104,5 @@ int vg_thread_run( void *pfunc, void *data ) pthread_detach( hThread ); return 0; } - #endif } diff --git a/src/vg/vg_shader.h b/src/vg/vg_shader.h index ff8a5f0..da61186 100644 --- a/src/vg/vg_shader.h +++ b/src/vg/vg_shader.h @@ -1,10 +1,11 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ const char *vg_shader_gl_ver = "#version 330 core\n"; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wreturn-type" -static inline int static_str_index( const char *list[], int len, const char *str ) +static inline int static_str_index( const char *list[], int len, + const char *str ) { for( int i = 0; i < len; i ++ ) { @@ -22,9 +23,11 @@ static inline int static_str_index( const char *list[], int len, const char *str #define SHADER_NAME( NAME ) (NAME##_static_shader.program) #define SHADER_USE( NAME ) glUseProgram( NAME##_static_shader.program ) -#define SHADER_UNIFORM( NAME, U ) NAME##_shader_uniforms[ STR_STATIC_INDEX( NAME##_shader_names, U ) ] +#define SHADER_UNIFORM( NAME, U ) \ + NAME##_shader_uniforms[ STR_STATIC_INDEX( NAME##_shader_names, U ) ] #define SHADER_UNIFORM_NAME( NAME, UID ) NAME##_shader_names[ UID ] -#define STR_STATIC_INDEX( LIST, STR ) static_str_index( LIST, vg_list_size(LIST), STR ) +#define STR_STATIC_INDEX( LIST, STR ) \ + static_str_index( LIST, vg_list_size(LIST), STR ) #define UNIFORMS(...) __VA_ARGS__ @@ -114,7 +117,7 @@ static int vg_shader_compile( struct vg_shader *shader ) if( geo ) glDeleteShader( geo ); - // Check for link errors + /* Check for link errors */ char infoLog[ 512 ]; int success_link = 1; @@ -128,9 +131,12 @@ static int vg_shader_compile( struct vg_shader *shader ) return 0; } - // Complete linkeage + /* Complete linkeage */ for( int i = 0; i < shader->uniform_count; i ++ ) - shader->uniforms[ i ] = glGetUniformLocation( shader->program, shader->uniform_names[i] ); + { + shader->uniforms[ i ] = + glGetUniformLocation( shader->program, shader->uniform_names[i] ); + } shader->status |= SHADER_STATUS_COMPILED; return 1; diff --git a/src/vg/vg_tex.h b/src/vg/vg_tex.h index 4c14540..33e074e 100644 --- a/src/vg/vg_tex.h +++ b/src/vg/vg_tex.h @@ -1,4 +1,4 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ #define VG_TEXTURE_NO_MIP 0x1 #define VG_TEXTURE_REPEAT 0x2 @@ -42,7 +42,8 @@ static inline void vg_tex2d_nearest(void) static inline void vg_tex2d_linear_mipmap(void) { - glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR ); + glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, + GL_LINEAR_MIPMAP_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); } @@ -72,7 +73,8 @@ static GLuint vg_tex2d_rgba( const char *path ) qoi_desc info; u8 *tex_buffer = qoi_decode( src_data, length, &info, 4 ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); free( tex_buffer ); free( src_data ); @@ -88,7 +90,8 @@ static GLuint vg_tex2d_rgba( const char *path ) }; vg_error( "Loading texture failed (%s)\n", path ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, 2, 2, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); } return texture_name; diff --git a/src/vg/vg_ui.h b/src/vg/vg_ui.h index f6fa895..f7f8a1c 100644 --- a/src/vg/vg_ui.h +++ b/src/vg/vg_ui.h @@ -1,12 +1,11 @@ -// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved +/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ SHADER_DEFINE( shader_ui, - // VERTEX - "layout (location=0) in vec2 a_co;" // i16, i16, .. ? - "layout (location=1) in vec2 a_uv;" // i16, i16 - "layout (location=2) in vec4 a_colour;" // u32 - "layout (location=3) in vec4 a_clip;" // i16, i16, i16, i16 + "layout (location=0) in vec2 a_co;" + "layout (location=1) in vec2 a_uv;" + "layout (location=2) in vec4 a_colour;" + "layout (location=3) in vec4 a_clip;" "uniform mat3 uPv;" "" "out vec2 aTexCoords;" @@ -24,7 +23,7 @@ SHADER_DEFINE( shader_ui, "aClip = a_clip;" "}", - // FRAGMENT + /* Fragment */ "uniform sampler2D uTexGlyphs;" "out vec4 FragColor;" "" @@ -36,7 +35,11 @@ SHADER_DEFINE( shader_ui, "" "void main()" "{" - "float clip_blend = step( aWsp.x, aClip.z ) * step( aWsp.y, aClip.w ) * step( aClip.x, aWsp.x ) * step( aClip.y, aWsp.y );" + "float clip_blend = step( aWsp.x, aClip.z ) *" + "step( aWsp.y, aClip.w ) *" + "step( aClip.x, aWsp.x ) *" + "step( aClip.y, aWsp.y ); " + "vec4 glyph = vec4(1.0,1.0,1.0,1.0);" "if( aColour.a == 0.0 )" @@ -55,16 +58,13 @@ SHADER_DEFINE( shader_ui, UNIFORMS({ "uPv", "uTexGlyphs" }) ) -// Types -// =========================================================================================================== - typedef i16 ui_px; typedef u32 ui_colour; typedef ui_px ui_rect[4]; typedef struct ui_ctx ui_ctx; typedef struct ui_colourset ui_colourset; -// Relative to cursor p0 +/* Relative to cursor p0 */ enum ui_text_align { k_text_align_left = 0, @@ -105,10 +105,10 @@ struct ui_ctx #pragma pack(push,1) struct ui_vert { - ui_px co[2]; //32 4 - u8 uv[2]; //16 2 - u32 colour; //32 4 - ui_rect clip; //64 8 + ui_px co[2]; + u8 uv[2]; + u32 colour; + ui_rect clip; } *verts; #pragma pack(pop) @@ -125,9 +125,9 @@ struct ui_ctx u32 id_base; int glyph_base; - // User input + /* User input */ ui_px mouse[2]; - int click_state; // 0: released, 1: on down, 2: pressed, 3: on release + int click_state; /* 0: released, 1: on down, 2: pressed, 3: on release */ ui_colourset *colours; @@ -144,9 +144,6 @@ struct ui_ctx int image_count; }; -// Globals -// =========================================================================================================== - #define UI_GLYPH_SPACING_X 9 static GLuint ui_glyph_texture = 0; @@ -158,14 +155,11 @@ static ui_colourset ui_default_colours = { static ui_ctx ui_global_ctx; -// Initialization -// =========================================================================================================== - static void ui_init_context( ui_ctx *ctx, int index_buffer_size ) { u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2)); - // Generate the buffer we are gonna be drawing to + /* Generate the buffer we are gonna be drawing to */ { glGenVertexArrays(1, &ctx->vao); glGenBuffers( 1, &ctx->vbo ); @@ -174,34 +168,42 @@ static void ui_init_context( ui_ctx *ctx, int index_buffer_size ) glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo ); - glBufferData( GL_ARRAY_BUFFER, vertex_buffer_size * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW ); + glBufferData( GL_ARRAY_BUFFER, + vertex_buffer_size * sizeof( struct ui_vert ), + NULL, GL_DYNAMIC_DRAW ); glBindVertexArray( ctx->vao ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); + glBufferData( GL_ELEMENT_ARRAY_BUFFER, + index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); u32 const stride = sizeof( struct ui_vert ); - // XY - glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, co ) ); + /* XY */ + glVertexAttribPointer( 0, 2, GL_SHORT, GL_FALSE, + stride, (void *)offsetof( struct ui_vert, co ) ); glEnableVertexAttribArray( 0 ); - // UV - glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, stride, (void *)offsetof( struct ui_vert, uv ) ); + /* UV */ + glVertexAttribPointer( 1, 2, GL_UNSIGNED_BYTE, GL_FALSE, + stride, (void *)offsetof( struct ui_vert, uv ) ); glEnableVertexAttribArray( 1 ); - // COLOUR - glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, (void *)offsetof( struct ui_vert, colour ) ); + /* COLOUR */ + glVertexAttribPointer( 2, 4, GL_UNSIGNED_BYTE, GL_TRUE, stride, + (void *)offsetof( struct ui_vert, colour ) ); glEnableVertexAttribArray( 2 ); - // CLIPPING - glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, (void *)offsetof( struct ui_vert, clip ) ); + /* CLIPPING */ + glVertexAttribPointer( 3, 4, GL_SHORT, GL_FALSE, stride, + (void *)offsetof( struct ui_vert, clip ) ); glEnableVertexAttribArray( 3 ); } - // Initialize default context + /* Initialize default context */ { - ctx->verts = (struct ui_vert *)malloc( vertex_buffer_size * sizeof(struct ui_vert) ); + ctx->verts = (struct ui_vert *)malloc( + vertex_buffer_size * sizeof(struct ui_vert) ); ctx->indices = (u16*)malloc( index_buffer_size * sizeof(u16) ); if( !ctx->colours ) @@ -221,7 +223,7 @@ static void ui_context_free( ui_ctx *ctx ) static void ui_default_init(void) { - // Load default font + /* Load default font */ u32 compressed[] = { #include "vg/vg_pxfont.h" }; @@ -247,14 +249,14 @@ static void ui_default_init(void) glGenTextures( 1, &ui_glyph_texture ); glBindTexture( GL_TEXTURE_2D, ui_glyph_texture ); - glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, GL_RED, GL_UNSIGNED_BYTE, image ); + glTexImage2D( GL_TEXTURE_2D, 0, GL_R8, 256, 256, 0, + GL_RED, GL_UNSIGNED_BYTE, image ); vg_tex2d_clamp(); vg_tex2d_nearest(); free( image ); - // Setup OpenGL memory SHADER_INIT( shader_ui ); ui_init_context( &ui_global_ctx, 20000 ); } @@ -265,22 +267,29 @@ static void ui_default_free(void) ui_context_free( &ui_global_ctx ); } -static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, ui_px uv[4] ); +static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, + u32 colour, ui_px uv[4] ); + static void ui_draw( ui_ctx *ctx, m3x3f view_override ) { u32 num_indices_normal = ctx->num_indices; - // Append images to back of buffer + /* Append images to back of buffer */ for( int i = 0; i < ctx->image_count; i ++ ) - ui_fill_rect_uv( ctx, ctx->images[i].rc, 0xffffffff, (ui_px[4]){0,0,128,128} ); + { + ui_fill_rect_uv( ctx, ctx->images[i].rc, + 0xffffffff, (ui_px[4]){0,0,128,128} ); + } glBindVertexArray( ctx->vao ); glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo ); - glBufferSubData( GL_ARRAY_BUFFER, 0, ctx->num_verts * sizeof( struct ui_vert ), ctx->verts ); + glBufferSubData( GL_ARRAY_BUFFER, 0, + ctx->num_verts * sizeof( struct ui_vert ), ctx->verts ); glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo ); - glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, ctx->num_indices * sizeof( u16 ), ctx->indices ); + glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, + ctx->num_indices * sizeof( u16 ), ctx->indices ); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -295,32 +304,36 @@ static void ui_draw( ui_ctx *ctx, m3x3f view_override ) view_override = view; m3x3_translate( view, (v3f){ -1.0f, 1.0f, 0.0f } ); - m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), -1.0f/((float)vg_window_y*0.5f), 1.0f } ); + m3x3_scale( view, (v3f){ 1.0f/((float)vg_window_x*0.5f), + -1.0f/((float)vg_window_y*0.5f), 1.0f } ); } - glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, GL_FALSE, (float *)view_override ); + glUniformMatrix3fv( SHADER_UNIFORM( shader_ui, "uPv" ), 1, + GL_FALSE, (float *)view_override ); glActiveTexture( GL_TEXTURE0 ); glBindTexture( GL_TEXTURE_2D, ui_glyph_texture ); glUniform1i( SHADER_UNIFORM( shader_ui, "uTexGlyphs" ), 0 ); - glDrawElements( GL_TRIANGLES, num_indices_normal, GL_UNSIGNED_SHORT, (void*)(0) ); + glDrawElements( GL_TRIANGLES, num_indices_normal, + GL_UNSIGNED_SHORT, (void*)(0) ); - // Draw image elements + /* Draw image elements */ for( int i = 0; i < ctx->image_count; i ++ ) { struct ui_image *img = &ctx->images[i]; glBindTexture( GL_TEXTURE_2D, img->image ); - glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, (void*)( (num_indices_normal + 6*i)*sizeof(u16) ) ); + glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, + (void*)( (num_indices_normal + 6*i)*sizeof(u16) ) ); } glDisable(GL_BLEND); } -// Rect controls -// =========================================================================================================== - +/* + * Rect controls + */ static void ui_rect_copy( ui_rect src, ui_rect dst ) { dst[0] = src[0]; @@ -337,9 +350,9 @@ static void ui_rect_pad( ui_rect rect, ui_px pad ) rect[3] -= pad*2; } -// Stack control -// =========================================================================================================== - +/* + * Stack control + */ static struct ui_qnode *ui_current( ui_ctx *ctx ) { return &ctx->stack[ ctx->stack_count-1 ]; @@ -356,8 +369,10 @@ static void ui_new_node( ui_ctx *ctx ) if( parent->mouse_over ) { - if( ctx->mouse[0] >= node->rect[0] && ctx->mouse[0] < node->rect[0]+node->rect[2] && - ctx->mouse[1] >= node->rect[1] && ctx->mouse[1] < node->rect[1]+node->rect[3] ) + if( ctx->mouse[0] >= node->rect[0] && + ctx->mouse[0] < node->rect[0]+node->rect[2] && + ctx->mouse[1] >= node->rect[1] && + ctx->mouse[1] < node->rect[1]+node->rect[3] ) node->mouse_over = 1; else node->mouse_over = 0; @@ -406,7 +421,6 @@ static void ui_fill_x( ui_ctx *ctx ) ctx->cursor[2] = node->rect[2] - (ctx->cursor[0]-node->rect[0]); } -// Alignment: | [] | -> | []| static void ui_align_bottom( ui_ctx *ctx ) { struct ui_qnode *node = ui_current( ctx ); @@ -476,10 +490,8 @@ static void ui_release_clip( ui_ctx *ctx ) ctx->clipping[3] = 32000; } -// Drawing -// =========================================================================================================== - -static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, u32 colour, ui_px uv[4] ) +static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, + u32 colour, ui_px uv[4] ) { struct ui_vert *vertices = &ctx->verts[ ctx->num_verts ]; vertices[0].co[0] = rect[0]; @@ -529,7 +541,8 @@ static struct ui_vert *ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour ) return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } ); } -static ui_px ui_text_line_offset( const char *str, ui_px scale, enum ui_text_align align ) +static ui_px ui_text_line_offset( const char *str, ui_px scale, + enum ui_text_align align ) { if( align == k_text_align_left ) return 0; @@ -550,7 +563,8 @@ static ui_px ui_text_line_offset( const char *str, ui_px scale, enum ui_text_ali return (-length * scale*8) / 2; } -static void ui_text( ui_ctx *ctx, ui_px pos[2], const char *str, ui_px scale, enum ui_text_align align ) +static void ui_text( ui_ctx *ctx, ui_px pos[2], + const char *str, ui_px scale, enum ui_text_align align ) { ui_rect text_cursor; u32 current_colour = 0x00ffffff; @@ -581,7 +595,13 @@ static void ui_text( ui_ctx *ctx, ui_px pos[2], const char *str, ui_px scale, en glyph_base[0] *= 8; glyph_base[1] *= 8; - ui_fill_rect_uv( ctx, text_cursor, current_colour, (ui_px[4]){glyph_base[0]+2,glyph_base[1]+1,glyph_base[0]+6,glyph_base[1]+8} ); + ui_fill_rect_uv( ctx, text_cursor, current_colour, (ui_px[4]) + { + glyph_base[0]+2, + glyph_base[1]+1, + glyph_base[0]+6, + glyph_base[1]+8 + }); } else if( c == '\x1B' ) { @@ -631,9 +651,9 @@ static void ui_text( ui_ctx *ctx, ui_px pos[2], const char *str, ui_px scale, en } } -// API control -// ==================================================================== - +/* + * API Control + */ static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y ) { ctx->cursor[0] = 0; @@ -668,9 +688,6 @@ static void ui_resolve( ui_ctx *ctx ) } } -// User Input piping -// ==================================================================== - static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state ) { ctx->mouse[0] = x; @@ -679,9 +696,9 @@ static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state ) ctx->click_state = click_state; } -// High level controls -// ==================================================================== - +/* + * High level controls + */ struct ui_window { const char *title; @@ -749,7 +766,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) { ui_capture_mouse( ctx, __COUNTER__ ); - // Drag bar + /* Drag bar */ ctx->cursor[3] = 25; ui_new_node( ctx ); { @@ -757,12 +774,12 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, 0xff555555 ); - // title.. + /* title.. */ ctx->cursor[0] += 2; ctx->cursor[1] += 2; ui_text( ctx, ctx->cursor, window->title, 2, 0 ); - // Close button + /* Close button */ ctx->cursor[3] = 25; ctx->cursor[2] = 25; ui_align_right( ctx ); @@ -784,7 +801,7 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) drag_bar[2].colour = 0xff777777; drag_bar[3].colour = 0xff777777; - // start drag + /* start drag */ if( ctx->click_state == 1 ) { window->drag = 1; @@ -799,71 +816,6 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) return 1; } -struct ui_scrollbar -{ - int drag; - ui_px drag_offset; - - ui_px py; - ui_px bar_height; - ui_px view_height; -}; - -static void ui_scrollbar( ui_ctx *ctx, struct ui_scrollbar *scrollbar, u32 id ) -{ - scrollbar->view_height = ctx->cursor[3]; - - if( scrollbar->drag ) - { - scrollbar->py = ctx->mouse[1]+scrollbar->drag_offset; - scrollbar->py = VG_MAX( scrollbar->py, 0 ); - scrollbar->py = VG_MIN( scrollbar->py, ctx->cursor[3] - scrollbar->bar_height ); - - if( ctx->click_state == 0 || ctx->click_state == 3 ) - scrollbar->drag = 0; - } - - ui_new_node( ctx ); - { - ui_fill_rect( ctx, ctx->cursor, ctx->colours->background ); - ui_capture_mouse( ctx, id ); - - ctx->cursor[1] += scrollbar->py; - ctx->cursor[3] = scrollbar->bar_height; - - ui_new_node( ctx ); - { - ui_capture_mouse( ctx, __COUNTER__ ); - struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, ctx->colours->bar ); - - if( ui_hasmouse( ctx ) || scrollbar->drag ) - { - drag_bar[0].colour = ctx->colours->bar_hover; - drag_bar[1].colour = ctx->colours->bar_hover; - drag_bar[2].colour = ctx->colours->bar_hover; - drag_bar[3].colour = ctx->colours->bar_hover; - - // start drag - if( ctx->click_state == 1 ) - { - scrollbar->drag = 1; - scrollbar->drag_offset = scrollbar->py - ctx->mouse[1]; - } - } - } - ui_end_down( ctx ); - } - ui_end( ctx ); -} - -static ui_px ui_calculate_content_scroll( struct ui_scrollbar *scrollbar, ui_px content ) -{ - float overlap = vg_maxf( 0.0f, (float)(content - scrollbar->view_height) ); - - float range = scrollbar->view_height - scrollbar->bar_height; - return ((float)scrollbar->py / range) * overlap; -} - static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image ) { struct ui_image *img = &ctx->images[ ctx->image_count ++ ]; @@ -871,7 +823,7 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image ) img->image = image; } -// Shortnames +/* Shortnames */ #define gui_draw(...) ui_draw( &ui_global_ctx, __VA_ARGS__) #define gui_current(...) ui_current( &ui_global_ctx, __VA_ARGS__) #define gui_new_node() ui_new_node( &ui_global_ctx ) @@ -900,5 +852,4 @@ static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image ) #define gui_window(...) ui_window( &ui_global_ctx, __VA_ARGS__) #define gui_want_mouse() ui_want_mouse( &ui_global_ctx ) #define gui_push_image(...) ui_push_image( &ui_global_ctx, __VA_ARGS__ ) -#define gui_scrollbar(...) ui_scrollbar( &ui_global_ctx, __VA_ARGS__) #define gui_reset_colours(...) ui_reset_colours( &ui_global_ctx ) diff --git a/vg_compiler.sh b/vg_compiler.sh index b9ee1ab..2573a40 100755 --- a/vg_compiler.sh +++ b/vg_compiler.sh @@ -49,20 +49,13 @@ checkfatal(){ fi } -if [[ -f vg.conf ]]; then - source vg.conf -else - error "Directory is not a VG project" - exit 1 -fi - # Platforms # =========================================== target_os_windows(){ target_ext=".exe" target_compiler="i686-w64-mingw32-gcc" - target_libs="-lglfw3 -lopengl32 -lm -mwindows" + target_libs="-lglfw3 -lopengl32 -lm -mwindows $vg_root/dep/steam/steam_api.dll" target_dir="build.win32" target_steam_api="steam_api.dll" target_miniaudio="$vg_root/dep/dr_soft/miniaudio_win32.o" @@ -76,19 +69,24 @@ target_os_windows(){ target_os_linux(){ target_ext="" target_compiler="gcc" - target_libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl -lsteam_api" + target_libs="-lGL -lglfw -lX11 -lXxf86vm -lXrandr -lm -lpthread -lXi -ldl" + + if [ "$opt_steam" != "" ]; then + target_libs="$target_libs -lsteam_api" + fi + target_dir="build.linux" target_steam_api="libsteam_api.so" target_miniaudio="$vg_root/dep/dr_soft/miniaudio_linux.o" if [ $opt_release = true ]; then - target_opts="-O3" + target_opts="-O0" else - target_opts="-fsanitize=address -ggdb3" + target_opts="-O0 -fsanitize=address -ggdb3 -fno-omit-frame-pointer" fi } precompile_x(){ - cmd="$target_compiler -O3 -Wall -c $1 -o $2.o" + cmd="$target_compiler -O3 -Wall -Wstrict-aliasing=3 -c $1 -o $2.o" logit " $cmd" $cmd @@ -98,7 +96,7 @@ precompile_x(){ } compile_x(){ - paths="-I. -L$vg_root/dep/glfw -L$vg_root/dep/steam -I$vg_root/dep -I$vg_root/src" + paths="-I. -L./ -L$vg_root/dep/glfw -L$vg_root/dep/steam -I$vg_root/dep -I$vg_root/src" setup="$target_compiler $opt_gcc -Wall -Wstrict-aliasing=3 -Wno-unused-function $paths" targets="$1 $vg_root/dep/glad/glad.c $target_miniaudio -o $2$target_ext" final="$target_libs -Wl,-rpath=./ $opt_steam" @@ -196,7 +194,7 @@ recompile_miniaudio(){ options=rptlwaq -longopts=release,build-linux,build-windows,steam,play,build-tools,assets,full,miniaudio +longopts=release,build-linux,build-windows,steam,play,build-tools,assets,full,miniaudio,template parsed=$(getopt --options=$options --longoptions=$longopts --name "vgc" -- "$@") if [ $? -ne 0 ]; then @@ -251,6 +249,19 @@ while true; do ;; --steam) opt_steam="-DVG_STEAM" + shift; + ;; + --template) + + if [ -f "main.c" ]; then error "main.c already exists"; + elif [ -f "vg_config.h" ]; then error "vg_config.h already exists"; + elif [ -f "vg.conf" ]; then error "vg.conf already exists"; + else + cp $vg_root/src/template/main.c main.c + cp $vg_root/src/template/vg_config.h vg_config.h + cp $vg_root/src/template/vg.conf vg.conf + fi + shift; ;; --) @@ -264,6 +275,13 @@ while true; do esac done +if [[ -f vg.conf ]]; then + source vg.conf +else + error "Directory is not a VG project" + exit 1 +fi + detect_os(){ if [[ "$OSTYPE" != "linux-gnu"* ]]; then host_os=win32 -- 2.25.1