From: hgn Date: Tue, 25 Oct 2022 02:31:04 +0000 (+0100) Subject: now thats a lot of damage! X-Git-Url: https://harrygodden.com/git/?p=vg.git;a=commitdiff_plain;h=5df88af6730a8b9c4ef698070bb729866ed2e597 now thats a lot of damage! --- diff --git a/src/vg/vg.h b/src/vg/vg.h index 299c9b0..08c18b3 100644 --- a/src/vg/vg.h +++ b/src/vg/vg.h @@ -1,9 +1,27 @@ /* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */ +/* + * Memory model: + * [global (.data)] [temp-stack] [system-stack] [game-heap] + * + * 1. Program starts: .data memory is loaded + * 2. System initialization: + * 2a. the large heap buffer is allocated + * 2b. each engine system is initialized in order, using some of the + * system stack + * 2c. game systems are also put into here + */ + + #ifndef VG_HEADER_H #define VG_HEADER_H #include "vg_platform.h" +#include "vg_mem.h" + +#ifndef _WIN32 +#include +#endif #if defined(VG_SERVER) || defined(VG_TOOLS) @@ -35,10 +53,6 @@ void vg_register_exit( void( *funcptr )(void), const char *name ); #include "vg_steam.h" #endif -#ifndef VG_NON_CLIENT -#include "vg_gldiag.h" -#endif - #ifndef VG_NON_CLIENT struct vg @@ -103,7 +117,7 @@ struct vg const char *gamepad_name; int gamepad_id; } -static vg = { .time_rate = 1.0 }; +VG_STATIC vg = { .time_rate = 1.0 }; struct vg_thread_info { @@ -129,9 +143,9 @@ static VG_THREAD_LOCAL struct vg_thread_info vg_thread_info; #define VG_SYNC_LOG(...) #endif -static void vg_fatal_exit_loop( const char *error ); +VG_STATIC void vg_fatal_exit_loop( const char *error ); -static void vg_ensure_engine_running(void) +VG_STATIC void vg_ensure_engine_running(void) { /* Check if the engine is no longer running */ vg_mutex_lock( &vg.mux_engine_status ); @@ -156,7 +170,7 @@ static void vg_ensure_engine_running(void) * Sync execution so that the OpenGL context is switched onto this thread. * Anything after this call will be in a valid context. */ -static void vg_acquire_thread_sync(void) +VG_STATIC void vg_acquire_thread_sync(void) { /* We dont want to do anything if this is the main thread */ if( vg_thread_info.purpose == k_thread_purpose_main ) @@ -193,7 +207,7 @@ static void vg_acquire_thread_sync(void) * Signify that we are done with the OpenGL context in this thread. * Anything after this call will be in an undefined context. */ -static void vg_release_thread_sync(void) +VG_STATIC void vg_release_thread_sync(void) { if( vg_thread_info.purpose == k_thread_purpose_main ) return; @@ -211,7 +225,7 @@ static void vg_release_thread_sync(void) } } -static void vg_run_synced_content(void) +VG_STATIC void vg_run_synced_content(void) { assert( vg_thread_info.purpose == k_thread_purpose_main ); @@ -243,7 +257,7 @@ static void vg_run_synced_content(void) vg_mutex_unlock( &vg.mux_context ); } -static void vg_opengl_sync_init(void) +VG_STATIC void vg_opengl_sync_init(void) { vg_semaphore_init( &vg.sem_allow_exec, 0 ); vg_semaphore_init( &vg.sem_exec_finished, 0 ); @@ -256,7 +270,7 @@ static void vg_opengl_sync_init(void) vg_thread_info.gl_context_level = 1; } -static void vg_checkgl( const char *src_info ); +VG_STATIC void vg_checkgl( const char *src_info ); #define VG_STRINGIT( X ) #X #define VG_CHECK_GL_ERR() vg_checkgl( __FILE__ ":L" VG_STRINGIT(__LINE__) ) @@ -268,27 +282,26 @@ static void vg_checkgl( const char *src_info ); #include "vg_input.h" #include "vg_ui.h" #include "vg_lines.h" -#include "vg_debug.h" #include "vg_loader.h" #include "vg_opt.h" /* Diagnostic */ -static struct vg_profile vg_prof_update = {.name="update()"}, - vg_prof_render = {.name="render()"}; +VG_STATIC struct vg_profile vg_prof_update = {.name="update()"}, + vg_prof_render = {.name="render()"}; #define VG_GAMELOOP -static void vg_register(void) VG_GAMELOOP; -static void vg_start(void) VG_GAMELOOP; +VG_STATIC void vg_register(void) VG_GAMELOOP; +VG_STATIC void vg_start(void) VG_GAMELOOP; -static void vg_update(int loaded) VG_GAMELOOP; -static void vg_update_fixed(int loaded) VG_GAMELOOP; -static void vg_update_post(int loaded) VG_GAMELOOP; +VG_STATIC void vg_update(int loaded) VG_GAMELOOP; +VG_STATIC void vg_update_fixed(int loaded) VG_GAMELOOP; +VG_STATIC void vg_update_post(int loaded) 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; +VG_STATIC void vg_framebuffer_resize(int w, int h) VG_GAMELOOP; +VG_STATIC void vg_render(void) VG_GAMELOOP; +VG_STATIC void vg_ui(void) VG_GAMELOOP; -static void vg_checkgl( const char *src_info ) +VG_STATIC void vg_checkgl( const char *src_info ) { int fail = 0; @@ -329,39 +342,31 @@ void vg_framebuffer_resize_callback( GLFWwindow *ptrW, int w, int h ) vg_framebuffer_resize(w,h); } -static int vg_bake_shaders(void) +VG_STATIC void vg_bake_shaders(void) { vg_acquire_thread_sync(); +#if 0 vg_function_push( (struct vg_cmd) { .name = "shaders", .function = vg_shaders_live_recompile }); +#endif - if( !vg_shaders_recompile() ) - { - vg_shaders_free(NULL); - vg_release_thread_sync(); - return 0; - } - else - { - vg_release_thread_sync(); - vg_loader_highwater( NULL, vg_shaders_free, NULL ); - return 1; - } + vg_shaders_compile(); + vg_release_thread_sync(); } -void vg_preload(void); -void vg_load(void); -static void vg_load_full(void) +VG_STATIC void vg_preload(void); +VG_STATIC void vg_load(void); +VG_STATIC void vg_load_full(void) { vg_preload(); /* internal */ vg_loader_highwater( vg_gamepad_init, NULL, NULL ); - vg_loader_highwater( vg_lines_init, vg_lines_free, NULL ); + vg_loader_highwater( vg_lines_init, NULL, NULL ); vg_loader_highwater( vg_audio_init, vg_audio_free, NULL ); vg_loader_highwater( vg_profiler_init, NULL, NULL ); @@ -373,7 +378,7 @@ static void vg_load_full(void) vg_release_thread_sync(); } -static void vg_enter( int argc, char *argv[], const char *window_name ) +VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name ) { char *arg; while( vg_argp( argc, argv ) ) @@ -401,7 +406,7 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) glfwWindowHint( GLFW_CONTEXT_VERSION_MAJOR, 3 ); glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 ); glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); - glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_TRUE ); + //glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, GL_FALSE ); glfwWindowHint( GLFW_CONTEXT_RELEASE_BEHAVIOR, GLFW_RELEASE_BEHAVIOR_FLUSH ); glfwWindowHint( GLFW_RESIZABLE, GLFW_FALSE ); @@ -468,9 +473,7 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) glfwSetCharCallback( vg.window, console_proc_wchar ); glfwSetKeyCallback( vg.window, console_proc_key ); -#if 0 - glfwSetInputMode(vg_window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); -#endif + glfwSetInputMode( vg.window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN ); if( !gladLoadGLLoader((GLADloadproc)glfwGetProcAddress) ) { @@ -481,13 +484,11 @@ static void vg_enter( 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(); - - if( !ui_default_init() ) - goto il_exit_ui; - if( !vg_loader_init() ) - goto il_exit_loader; + /* init systems + * -----------------------------------------------------------------------*/ + ui_init_context(); + vg_loader_init(); vg_mutex_init( &vg.mux_engine_status ); vg.engine_status = k_engine_status_running; @@ -516,11 +517,6 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) if( vg.is_loaded ) { -#if 0 - glClearColor( 0.0f,sinf(vg.time*20.0)*0.5f+0.5f,0.0f,1.0f ); - glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT ); -#endif - if( !loaded ) { vg_start(); @@ -580,9 +576,9 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) /* ui */ vg.engine_stage = k_engine_stage_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" ) ); + ui_begin( vg.window_x, vg.window_y ); + ui_set_mouse( vg.mouse[0], vg.mouse[1], + vg_get_button_state( "primary" ) ); vg_profile_drawn( (struct vg_profile *[]){&vg_prof_update,&vg_prof_render}, 2, @@ -606,15 +602,15 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) vg.fixed_iterations, (vg.accumulator/VG_TIMESTEP_FIXED)*100.0f ); - ui_text( &ui_global_ctx, (ui_rect){258, 4+24+12,0,0},perf, 1,0); + ui_text( (ui_rect){258, 4+24+12,0,0},perf, 1,0); } audio_debug_ui( vg.pv ); vg_ui(); vg_console_draw(); - ui_resolve( &ui_global_ctx ); - ui_draw( &ui_global_ctx, NULL ); + ui_resolve(); + ui_draw( NULL ); } } @@ -632,10 +628,7 @@ static void vg_enter( int argc, char *argv[], const char *window_name ) vg_loader_free(); -il_exit_loader: - ui_default_free(); - -il_exit_ui: + vg_success( "If you see this it means everything went.. \"well\".....\n" ); glfwTerminate(); } @@ -643,13 +636,41 @@ il_exit_ui: * Immediately transfer away from calling thread into a safe loop, signal for * others to shutdown, then free everything once the user closes the window. * - * FIXME(bug): glfwWindowShouldClose() never returns 1 in windows via wine, only + * FIXME(bug): glfwWindowShouldClose() never returns 1 in windows via wine, ONLY * when calling the program from outside its normal directory. - * - * A workaround is placed to skip the error loop on windows builds */ -static void vg_fatal_exit_loop( const char *error ) +VG_STATIC void vg_fatal_exit_loop( const char *error ) { + /* + * https://www.gnu.org/software/libc/manual/html_node/Backtraces.html + * thanks gnu <3 + * + * TODO: this on windows? + */ + +#ifndef _WIN32 + + void *array[20]; + char **strings; + int size, i; + + size = backtrace( array, 20 ); + strings = backtrace_symbols( array, size ); + + if( strings != NULL ) + { + vg_error( "---------------- gnu backtrace -------------\n" ); + + for( int i=0; i vg_audio.mem_total ) - vg_fatal_exit_loop( "Audio pool ran out of memory" ); - - void *ptr = vg_audio.mem + vg_audio.mem_current; - vg_audio.mem_current = new_current; - - return ptr; -} - - /* * These functions are called from the main thread and used to prevent bad * access. TODO: They should be no-ops in release builds. */ -static int audio_lock_checker_load(void) +VG_STATIC int audio_lock_checker_load(void) { int value; vg_mutex_lock( &vg_audio.mux_checker ); @@ -193,14 +169,14 @@ static int audio_lock_checker_load(void) return value; } -static void audio_lock_checker_store( int value ) +VG_STATIC void audio_lock_checker_store( int value ) { vg_mutex_lock( &vg_audio.mux_checker ); vg_audio.sync_locked = value; vg_mutex_unlock( &vg_audio.mux_checker ); } -static void audio_require_lock(void) +VG_STATIC void audio_require_lock(void) { if( audio_lock_checker_load() ) return; @@ -209,27 +185,28 @@ static void audio_require_lock(void) abort(); } -static void audio_lock(void) +VG_STATIC void audio_lock(void) { vg_mutex_lock( &vg_audio.mux_sync ); audio_lock_checker_store(1); } -static void audio_unlock(void) +VG_STATIC void audio_unlock(void) { audio_lock_checker_store(0); vg_mutex_unlock( &vg_audio.mux_sync ); } -static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, +VG_STATIC void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frameCount ); -static void vg_audio_init(void) +VG_STATIC void vg_audio_init(void) { vg_mutex_init( &vg_audio.mux_checker ); vg_mutex_init( &vg_audio.mux_sync ); + /* TODO: Move here? */ vg_convar_push( (struct vg_convar){ .name = "debug_audio", .data = &vg_audio.debug_ui, @@ -238,19 +215,15 @@ static void vg_audio_init(void) .persistent = 1 }); - vg_convar_push( (struct vg_convar){ - .name = "debug_audio_clips", - .data = &vg_audio.debug_ui_3d, - .data_type = k_convar_dtype_i32, - .opt_i32 = { .min=0, .max=1, .clamp=1 }, - .persistent = 1 - }); + /* allocate memory */ - u32 decode_region = AUDIO_DECODE_SIZE * SFX_MAX_SYSTEMS; - vg_audio.mem_total = 1024*1024*32; - vg_audio.mem_current = 0; - vg_audio.mem = vg_alloc( vg_audio.mem_total + decode_region ); - vg_audio.decode_mem = &((u8 *)vg_audio.mem)[vg_audio.mem_total]; + /* 32mb fixed */ + vg_audio.audio_pool = + vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32 ); + + /* fixed */ + u32 decode_size = AUDIO_DECODE_SIZE * SFX_MAX_SYSTEMS; + vg_audio.decode_buffer = vg_linear_alloc( vg_mem.rtmemory, decode_size ); /* setup pool */ vg_audio.active_pool_info.base = vg_audio.active_players; @@ -290,13 +263,15 @@ static void vg_audio_init(void) vg_success( "Ready\n" ); } -static void vg_audio_free(void * nothing) +VG_STATIC void vg_audio_free(void * nothing) { ma_device *device = &vg_audio.miniaudio_device; ma_device_uninit( device ); +#if 0 vg_free( vg_audio.mem ); vg_audio.mem = NULL; +#endif } /* @@ -317,7 +292,7 @@ static aatree_ptr audio_alloc_entity_internal(void) return playerid; } -static void audio_entity_free_internal( aatree_ptr id ) +VG_STATIC void audio_entity_free_internal( aatree_ptr id ) { struct active_audio_player *aap = &vg_audio.active_players[ id ]; aap->active = 0; @@ -331,15 +306,15 @@ static void audio_entity_free_internal( aatree_ptr id ) &vg_audio.active_pool_head ); } -static void *audio_entity_vorbis_ptr( aatree_ptr entid ) +VG_STATIC void *audio_entity_vorbis_ptr( aatree_ptr entid ) { - u8 *buf = (u8*)vg_audio.decode_mem, + u8 *buf = (u8*)vg_audio.decode_buffer, *loc = &buf[AUDIO_DECODE_SIZE*entid]; return (void *)loc; } -static void audio_entity_start( audio_entity *src ) +VG_STATIC void audio_entity_start( audio_entity *src ) { aatree_ptr entid = audio_alloc_entity_internal(); if( entid == AATREE_PTR_NIL ) @@ -362,45 +337,39 @@ static void audio_entity_start( audio_entity *src ) src->player->active_entity = entid; } - if( src->info.source->source_mode == k_audio_source_compressed ) - { - /* Setup vorbis decoder */ - struct active_audio_player *aap = &vg_audio.active_players[ entid ]; - - stb_vorbis_alloc alloc = { - .alloc_buffer = (char *)audio_entity_vorbis_ptr( entid ), - .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE - }; + /* Setup vorbis decoder */ + struct active_audio_player *aap = &vg_audio.active_players[ entid ]; + + stb_vorbis_alloc alloc = { + .alloc_buffer = (char *)audio_entity_vorbis_ptr( entid ), + .alloc_buffer_length_in_bytes = AUDIO_DECODE_SIZE + }; - int err; - stb_vorbis *decoder = stb_vorbis_open_memory( - src->info.source->data, src->info.source->len, &err, &alloc ); + int err; + stb_vorbis *decoder = stb_vorbis_open_memory( + src->info.source->data, + src->info.source->size, &err, &alloc ); - if( !decoder ) - { - vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n", - src->info.source->path, err ); + if( !decoder ) + { + vg_error( "stb_vorbis_open_memory failed on '%s' (%d)\n", + src->info.source->path, err ); - audio_entity_free_internal( entid ); - return; - } - else - { - ent->length = stb_vorbis_stream_length_in_samples( decoder ); - } - - aap->vorbis_handle = decoder; + audio_entity_free_internal( entid ); + return; } else { - ent->length = src->info.source->len; + ent->length = stb_vorbis_stream_length_in_samples( decoder ); } + + aap->vorbis_handle = decoder; } /* * Read everything from the queue */ -static void audio_system_enque(void) +VG_STATIC void audio_system_enque(void) { /* Process incoming sound queue */ audio_lock(); @@ -463,7 +432,7 @@ static void audio_system_enque(void) /* * Redistribute sound systems */ -static void audio_system_cleanup(void) +VG_STATIC void audio_system_cleanup(void) { audio_lock(); @@ -490,7 +459,7 @@ static void audio_system_cleanup(void) /* * Get effective volume and pan from this entity */ -static void audio_entity_spacialize( audio_entity *ent, float *vol, float *pan ) +VG_STATIC void audio_entity_spacialize( audio_entity *ent, float *vol, float *pan ) { if( ent->info.vol < 0.01f ) { @@ -520,7 +489,7 @@ static void audio_entity_spacialize( audio_entity *ent, float *vol, float *pan ) } } -static void audio_decode_uncompressed_mono( float *src, u32 count, float *dst ) +VG_STATIC void audio_decode_uncompressed_mono( float *src, u32 count, float *dst ) { for( u32 i=0; istereo */ -static int +VG_STATIC int stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer, int len ) { @@ -565,7 +534,7 @@ stb_vorbis_get_samples_float_interleaved_stereo( stb_vorbis *f, float *buffer, return n; } -static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) +VG_STATIC void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) { vg_profile_begin( &_vg_prof_audio_decode ); @@ -583,24 +552,14 @@ static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) float *dst = &buf[ buffer_pos * 2 ]; - int source_mode = ent->info.source->source_mode; - - if( source_mode == k_audio_source_mono ) - { - float *src = &((float *)ent->info.source->data)[ cursor ]; - audio_decode_uncompressed_mono( src, samples_this_run, dst ); - } - else if( source_mode == k_audio_source_compressed ) - { - int read_samples = stb_vorbis_get_samples_float_interleaved_stereo( - aap->vorbis_handle, - dst, - samples_this_run ); + int read_samples = stb_vorbis_get_samples_float_interleaved_stereo( + aap->vorbis_handle, + dst, + samples_this_run ); - if( read_samples != samples_this_run ) - { - vg_warn( "Invalid samples read (%s)\n", ent->info.source->path ); - } + if( read_samples != samples_this_run ) + { + vg_warn( "Invalid samples read (%s)\n", ent->info.source->path ); } cursor += samples_this_run; @@ -608,11 +567,7 @@ static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) if( (ent->info.flags & AUDIO_FLAG_LOOP) && remaining ) { - if( source_mode == k_audio_source_compressed ) - { - stb_vorbis_seek_start( aap->vorbis_handle ); - } - + stb_vorbis_seek_start( aap->vorbis_handle ); cursor = 0; continue; } @@ -633,7 +588,7 @@ static void audio_entity_get_samples( aatree_ptr id, u32 count, float *buf ) vg_profile_end( &_vg_prof_audio_decode ); } -static void audio_entity_mix( aatree_ptr id, float *buffer, +VG_STATIC void audio_entity_mix( aatree_ptr id, float *buffer, u32 frame_count ) { audio_entity *ent = &vg_audio.active_players[id].ent; @@ -687,7 +642,7 @@ static void audio_entity_mix( aatree_ptr id, float *buffer, /* * callback from miniaudio.h interface */ -static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, +VG_STATIC void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, const void *pInput, ma_uint32 frame_count ) { struct timespec time_start, time_end; @@ -729,108 +684,26 @@ static void audio_mixer_callback( ma_device *pDevice, void *pOutBuf, audio_unlock(); } -/* Decompress entire vorbis stream into buffer */ -static float *audio_decompress_vorbis( const unsigned char *data, int len, - int channels, u32 *samples ) -{ - int err; - stb_vorbis *pv = stb_vorbis_open_memory( data, len, &err, NULL ); - - if( !pv ) - { - vg_error( "stb_vorbis_open_memory() failed with error code: %i\n", err ); - return NULL; - } - - u32 length_samples = stb_vorbis_stream_length_in_samples( pv ); - - vg_info( "decompress_vorbis: %u samples (%.2fs), %.1fkb\n", - length_samples, - (float)length_samples / (44100.0f*(float)channels), - (float)(length_samples*4*channels) / 1024.0f ); - - float *buffer = audio_alloc( length_samples * channels * sizeof(float) ); - if( !buffer ) - { - stb_vorbis_close( pv ); - vg_error( "Failed to allocated memory for audio\n" ); - return NULL; - } - - 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 ); - length_samples = read_samples; - } - - stb_vorbis_close( pv ); - *samples = length_samples; - return buffer; -} - -static int audio_clip_load( audio_clip *clip ) +VG_STATIC void audio_clip_load( audio_clip *clip ) { - /* Load and decompress */ - i64 file_len; - void *filedata = vg_asset_read_s( clip->path, &file_len ); - - if( !filedata ) - { - vg_error( "OGG load failed (%s)\n", clip->path ); - return 0; - } - - if( clip->source_mode == k_audio_source_mono ) - { - u32 samples = 0; - float *sound = audio_decompress_vorbis( filedata, file_len, 1, &samples ); - clip->data = sound; - clip->len = samples; - - float seconds = (float)samples / 44100.0f, - mb = (float)(samples*4) / (1024.0f*1024.0f); - - vg_info( "Loaded audio clip[mono] '%s' (%.1fs, %.1fmb)\n", - clip->path, seconds, mb ); - } - else if( clip->source_mode == k_audio_source_compressed ) - { - void *data = audio_alloc( file_len ); - memcpy( data, filedata, file_len ); - - clip->data = data; - clip->len = file_len; + clip->data = vg_file_read( vg_audio.audio_pool, clip->path ); + clip->size = vg_file_size( vg_audio.audio_pool ); - float mb = (float)(file_len) / (1024.0f*1024.0f); - vg_info( "Loaded audio clip[compressed] '%s' (%.1fmb)\n", - clip->path, mb ); - } - else - { - /* ... */ - - clip->data = NULL; - clip->len = 0; - - vg_error( "Unkown source mode (%u)\n", clip->source_mode ); - return 0; - } + if( !clip->data ) + vg_fatal_exit_loop( "Audio failed to load" ); - return 1; + float mb = (float)(clip->size) / (1024.0f*1024.0f); + vg_info( "Loaded audio clip '%s' (%.1fmb)\n", clip->path, mb ); } -static void audio_clip_loadn( audio_clip *arr, int count ) +VG_STATIC void audio_clip_loadn( audio_clip *arr, int count ) { for( int i=0; iplayer = sys; } -static void audio_require_init( audio_player *player ) +VG_STATIC void audio_require_init( audio_player *player ) { if( player->init ) return; @@ -860,9 +733,9 @@ static void audio_require_init( audio_player *player ) vg_fatal_exit_loop( "Must init audio player before playing! \n" ); } -static void audio_require_clip_loaded( audio_clip *clip ) +VG_STATIC void audio_require_clip_loaded( audio_clip *clip ) { - if( clip->data ) + if( clip->data && clip->size ) return; vg_fatal_exit_loop( "Must load audio clip before playing! \n" ); @@ -870,7 +743,7 @@ static void audio_require_clip_loaded( audio_clip *clip ) /* Play a clip using player. If its already playing something, it will * fadeout quickly and start the next sound */ -static void audio_player_playclip( audio_player *player, audio_clip *clip ) +VG_STATIC void audio_player_playclip( audio_player *player, audio_clip *clip ) { audio_require_lock(); audio_require_init( player ); @@ -881,14 +754,14 @@ static void audio_player_playclip( audio_player *player, audio_clip *clip ) } #if 0 -static void audio_player_playoneshot( audio_player *player, audio_clip *clip ) +VG_STATIC void audio_player_playoneshot( audio_player *player, audio_clip *clip ) { audio_require_lock(); audio_require_init( player ); } #endif -static void audio_play_oneshot( audio_clip *clip, float volume ) +VG_STATIC void audio_play_oneshot( audio_clip *clip, float volume ) { audio_require_lock(); audio_require_clip_loaded( clip ); @@ -908,7 +781,7 @@ static void audio_play_oneshot( audio_clip *clip, float volume ) ent->player = NULL; } -static void audio_player_init( audio_player *player ) +VG_STATIC void audio_player_init( audio_player *player ) { player->active_entity = AATREE_PTR_NIL; player->init = 1; @@ -922,7 +795,7 @@ static void audio_player_init( audio_player *player ) * Safety enforced Get/set attributes */ -static int audio_player_is_playing( audio_player *sys ) +VG_STATIC int audio_player_is_playing( audio_player *sys ) { audio_require_lock(); @@ -932,43 +805,43 @@ static int audio_player_is_playing( audio_player *sys ) return 0; } -static void audio_player_set_position( audio_player *sys, v3f pos ) +VG_STATIC void audio_player_set_position( audio_player *sys, v3f pos ) { audio_require_lock(); v3_copy( pos, sys->info.world_position ); } -static void audio_player_set_vol( audio_player *sys, float vol ) +VG_STATIC void audio_player_set_vol( audio_player *sys, float vol ) { audio_require_lock(); sys->info.vol = vol; } -static float audio_player_get_vol( audio_player *sys ) +VG_STATIC float audio_player_get_vol( audio_player *sys ) { audio_require_lock(); return sys->info.vol; } -static void audio_player_set_pan( audio_player *sys, float pan ) +VG_STATIC void audio_player_set_pan( audio_player *sys, float pan ) { audio_require_lock(); sys->info.pan = pan; } -static float audio_player_get_pan( audio_player *sys ) +VG_STATIC float audio_player_get_pan( audio_player *sys ) { audio_require_lock(); return sys->info.pan; } -static void audio_player_set_flags( audio_player *sys, u32 flags ) +VG_STATIC void audio_player_set_flags( audio_player *sys, u32 flags ) { audio_require_lock(); sys->info.flags = flags; } -static u32 audio_player_get_flags( audio_player *sys ) +VG_STATIC u32 audio_player_get_flags( audio_player *sys ) { audio_require_lock(); return sys->info.flags; @@ -979,7 +852,7 @@ static u32 audio_player_get_flags( audio_player *sys ) * Debugging */ -static void audio_debug_ui( m4x4f mtx_pv ) +VG_STATIC void audio_debug_ui( m4x4f mtx_pv ) { if( !vg_audio.debug_ui ) return; @@ -1026,52 +899,51 @@ static void audio_debug_ui( m4x4f mtx_pv ) char perf[128]; /* Draw UI */ - ui_global_ctx.cursor[0] = 258; - ui_global_ctx.cursor[1] = VG_PROFILE_SAMPLE_COUNT*2+8+24+12; - ui_global_ctx.cursor[2] = 150; - ui_global_ctx.cursor[3] = 12; + vg_uictx.cursor[0] = 258; + vg_uictx.cursor[1] = VG_PROFILE_SAMPLE_COUNT*2+8+24+12; + vg_uictx.cursor[2] = 150; + vg_uictx.cursor[3] = 12; - float usage = (float)vg_audio.mem_current / (1024.0f*1024.0f), - total = (float)vg_audio.mem_total / (1024.0f*1024.0f), - percent = (usage/total) * 100.0f; + float mb1 = 1024.0f*1024.0f, + usage = vg_linear_get_cur( vg_audio.audio_pool ) / mb1, + total = vg_linear_get_capacity( vg_audio.audio_pool ) / mb1, + percent = (usage/total) * 100.0f; + snprintf( perf, 127, "Mem: %.1f/%.1fmb (%.1f%%)\n", usage, total, percent ); - ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 ); - ui_global_ctx.cursor[1] += 20; + ui_text( vg_uictx.cursor, perf, 1, 0 ); + vg_uictx.cursor[1] += 20; ui_rect overlap_buffer[ SFX_MAX_SYSTEMS ]; u32 overlap_length = 0; - if( !vg_audio.debug_ui_3d ) - return; - /* Draw audio stack */ for( int i=0; icursor / (float)inf->length) * w; /* cursor */ - ui_global_ctx.cursor[2] = 2; - ui_global_ctx.cursor[0] = c; - ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0xffffffff ); + vg_uictx.cursor[2] = 2; + vg_uictx.cursor[0] = c; + ui_fill_rect( vg_uictx.cursor, 0xffffffff ); - ui_global_ctx.cursor[0] = baseline + 2; - ui_global_ctx.cursor[1] += 2; + vg_uictx.cursor[0] = baseline + 2; + vg_uictx.cursor[1] += 2; snprintf( perf, 127, "%s %.1f%%", infos[i].name, infos[i].vol ); - ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 ); + ui_text( vg_uictx.cursor, perf, 1, 0 ); if( inf->flags & AUDIO_FLAG_SPACIAL_3D ) { @@ -1112,7 +984,7 @@ static void audio_debug_ui( m4x4f mtx_pv ) wr[1] += 18; } - ui_text( &ui_global_ctx, wr, perf, 1, 0 ); + ui_text( wr, perf, 1, 0 ); ui_rect_copy( wr, overlap_buffer[ overlap_length ++ ] ); } @@ -1120,8 +992,8 @@ static void audio_debug_ui( m4x4f mtx_pv ) projected_behind: - ui_end_down( &ui_global_ctx ); - ui_global_ctx.cursor[1] += 1; + ui_end_down(); + vg_uictx.cursor[1] += 1; } } diff --git a/src/vg/vg_console.h b/src/vg/vg_console.h index fc2775c..62e3118 100644 --- a/src/vg/vg_console.h +++ b/src/vg/vg_console.h @@ -6,6 +6,9 @@ #include "vg/vg_ui.h" #include "vg/vg_log.h" +typedef struct vg_convar vg_convar; +typedef struct vg_cmd vg_cmd; + struct vg_console { struct vg_convar @@ -40,17 +43,16 @@ struct vg_console int persistent; /* Should this var be stored to cfg/auto.conf? */ } - *convars; + convars[ 32 ]; struct vg_cmd { int (*function)( int argc, char const *argv[] ); const char *name; } - *functions; + functions[ 32 ]; - u32 convar_count, convar_cap, - function_count, function_cap; + u32 convar_count, function_count; char input[96]; int cursor_user, cursor_pos, string_length; @@ -59,69 +61,63 @@ struct vg_console int history_last, history_pos, history_count; int enabled; - int scale; } -vg_console = { .scale = 1 }; +vg_console; -static void vg_convar_push( struct vg_convar cv ); -static void vg_function_push( struct vg_cmd cmd ); +VG_STATIC void vg_convar_push( struct vg_convar cv ); +VG_STATIC void vg_function_push( struct vg_cmd cmd ); -static void vg_console_draw( void ); +VG_STATIC void vg_console_draw( void ); void vg_console_println( const char *str ); -static int vg_console_list( int argc, char const *argv[] ); -static void vg_console_init(void); -static void vg_console_write_persistent(void); -static void vg_console_free(void); -static void execute_console_input( const char *cmd ); +VG_STATIC int vg_console_list( int argc, char const *argv[] ); +VG_STATIC void vg_console_init(void); +VG_STATIC void vg_console_write_persistent(void); +VG_STATIC void vg_console_free(void); +VG_STATIC void execute_console_input( const char *cmd ); /* * 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 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_wchar( GLFWwindow* ptrW, u32 uWchar ); -static int vg_console_enabled(void); +VG_STATIC void console_make_selection( int* start, int* end ); +VG_STATIC void console_move_cursor( int* cursor0, int* cursor1, + int dir, int snap_together ); +VG_STATIC int console_makeroom( int datastart, int length ); +VG_STATIC int console_delete_char( int direction ); +VG_STATIC void console_to_clipboard(void); +VG_STATIC void console_clipboard_paste(void); +VG_STATIC void console_put_char( char c ); +VG_STATIC void console_history_get( char* buf, int entry_num ); +VG_STATIC void console_proc_key( GLFWwindow* ptrW, int key, + int scancode, int action, int mods ); +VG_STATIC void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar ); +VG_STATIC int vg_console_enabled(void); /* * Implementation */ -static int vg_console_enabled(void) +VG_STATIC int vg_console_enabled(void) { return vg_console.enabled; } -static void vg_convar_push( struct vg_convar cv ) +VG_STATIC void vg_convar_push( vg_convar cv ) { - vg_info( "Console variable '%s' registered\n", cv.name ); - vg_console.convars = buffer_reserve( vg_console.convars, - vg_console.convar_count, - &vg_console.convar_cap, 1, - sizeof( struct vg_convar ) ); + if( vg_console.convar_count > vg_list_size(vg_console.convars) ) + vg_fatal_exit_loop( "Too many convars registered" ); + vg_info( "Console variable '%s' registered\n", cv.name ); vg_console.convars[ vg_console.convar_count ++ ] = cv; } -static void vg_function_push( struct vg_cmd cmd ) +VG_STATIC void vg_function_push( struct vg_cmd cmd ) { - vg_info( "Console command '%s' registered\n", cmd.name ); - vg_console.functions = buffer_reserve( vg_console.functions, - vg_console.function_count, - &vg_console.function_cap, 1, - sizeof( struct vg_cmd ) ); + if( vg_console.function_count > vg_list_size(vg_console.functions) ) + vg_fatal_exit_loop( "Too many functions registered" ); vg_console.functions[ vg_console.function_count ++ ] = cmd; } -static void vg_console_draw( void ) +VG_STATIC void vg_console_draw( void ) { if( !vg_console.enabled ) return; @@ -132,17 +128,17 @@ static void vg_console_draw( void ) int const fh = 14; int console_lines = VG_MIN( 16, vg_log.buffer_line_count ); - ui_global_ctx.cursor[0] = 0; - ui_global_ctx.cursor[1] = 0; - ui_global_ctx.cursor[3] = 16*fh*vg_console.scale; - ui_fill_x( &ui_global_ctx ); + vg_uictx.cursor[0] = 0; + vg_uictx.cursor[1] = 0; + vg_uictx.cursor[3] = 16*fh; + ui_fill_x(); - ui_new_node( &ui_global_ctx ); + ui_new_node(); { - ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 ); + ui_fill_rect( vg_uictx.cursor, 0x77333333 ); - ui_global_ctx.cursor[3] = fh*vg_console.scale; - ui_align_bottom( &ui_global_ctx ); + vg_uictx.cursor[3] = fh; + ui_align_bottom(); for( int i=0; iname ); } - vg_info( "* snowsound\n" ); - for( int i=0; i\n" ); - return 0; -} - -static int _test_break( int argc, const char *argv[] ) +int _test_break( int argc, const char *argv[] ) { vg_fatal_exit_loop( "Test crash from main, after loading (console)" ); return 0; } -static void vg_console_init(void) +VG_STATIC void vg_console_init(void) { - 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 }, - .update = NULL - }); - vg_function_push( (struct vg_cmd) { .name = "list", .function = vg_console_list }); - vg_function_push( (struct vg_cmd) - { - .name = "chartest", - .function = vg_console_chartest - }); - vg_function_push( (struct vg_cmd) { .name = "crash", @@ -252,7 +213,7 @@ static void vg_console_init(void) }); } -static void vg_console_load_autos(void) +VG_STATIC void vg_console_load_autos(void) { /* Read and exec persistent commands */ FILE *fp = fopen( "cfg/auto.conf", "r" ); @@ -274,7 +235,7 @@ static void vg_console_load_autos(void) } } -static void vg_console_write_persistent(void) +VG_STATIC void vg_console_write_persistent(void) { FILE *fp = fopen( "cfg/auto.conf", "w" ); @@ -302,15 +263,12 @@ static void vg_console_write_persistent(void) fclose( fp ); } -static void vg_console_free(void) +VG_STATIC void vg_console_free(void) { vg_console_write_persistent(); - - vg_free( vg_console.convars ); - vg_free( vg_console.functions ); } -static void execute_console_input( const char *cmd ) +VG_STATIC void execute_console_input( const char *cmd ) { char temp[512]; char const *args[9]; @@ -378,7 +336,8 @@ static void execute_console_input( const char *cmd ) 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 ): + vg_minf( vg_maxf( data_float, cv->opt_f32.min), + cv->opt_f32.max ): data_float; break; } @@ -425,13 +384,13 @@ static void execute_console_input( const char *cmd ) /* * Console Interface */ -static void console_make_selection( int* start, int* end ) +VG_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, +VG_STATIC void console_move_cursor( int* cursor0, int* cursor1, int dir, int snap_together ) { *cursor0 = VG_MAX( 0, vg_console.cursor_user + dir ); @@ -444,7 +403,7 @@ static void console_move_cursor( int* cursor0, int* cursor1, *cursor1 = *cursor0; } -static int console_makeroom( int datastart, int length ) +VG_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; @@ -462,7 +421,7 @@ static int console_makeroom( int datastart, int length ) return VG_MIN( length, vg_list_size( vg_console.input )-datastart ); } -static int console_delete_char( int direction ) +VG_STATIC int console_delete_char( int direction ) { int start, end; console_make_selection( &start, &end ); @@ -486,7 +445,7 @@ static int console_delete_char( int direction ) return start; } -static void console_to_clipboard(void) +VG_STATIC void console_to_clipboard(void) { int start, end; console_make_selection( &start, &end ); @@ -500,7 +459,7 @@ static void console_to_clipboard(void) } } -static void console_clipboard_paste(void) +VG_STATIC void console_clipboard_paste(void) { int datastart = console_delete_char(0); const char* clipboard = glfwGetClipboardString(NULL); @@ -513,7 +472,7 @@ static void console_clipboard_paste(void) &vg_console.cursor_pos, cpylength, 1 ); } -static void console_put_char( char c ) +VG_STATIC void console_put_char( char c ) { if( !vg_console.enabled ) return; @@ -526,7 +485,7 @@ static void console_put_char( char c ) console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, 1, 1 ); } -static void console_history_get( char* buf, int entry_num ) +VG_STATIC void console_history_get( char* buf, int entry_num ) { if( !vg_console.history_count ) return; @@ -538,13 +497,13 @@ static void console_history_get( char* buf, int entry_num ) } /* Receed secondary cursor */ -static void _console_left_select(void) +VG_STATIC void _console_left_select(void) { console_move_cursor( &vg_console.cursor_user, NULL, -1, 0 ); } /* Match and receed both cursors */ -static void _console_left(void) +VG_STATIC void _console_left(void) { int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1; @@ -552,12 +511,12 @@ static void _console_left(void) &vg_console.cursor_pos, -cursor_diff, 1 ); } -static void _console_right_select(void) +VG_STATIC void _console_right_select(void) { console_move_cursor( &vg_console.cursor_user, NULL, 1, 0 ); } -static void _console_right(void) +VG_STATIC void _console_right(void) { int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1; @@ -565,7 +524,7 @@ static void _console_right(void) &vg_console.cursor_pos, +cursor_diff, 1 ); } -static void _console_down(void) +VG_STATIC void _console_down(void) { vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 ); console_history_get( vg_console.input, vg_console.history_pos ); @@ -575,7 +534,7 @@ static void _console_down(void) vg_list_size( vg_console.input ), 1 ); } -static void _console_up(void) +VG_STATIC void _console_up(void) { vg_console.history_pos = VG_MAX ( @@ -597,55 +556,55 @@ static void _console_up(void) vg_list_size( vg_console.input ), 1); } -static void _console_backspace(void) +VG_STATIC void _console_backspace(void) { vg_console.cursor_user = console_delete_char( -1 ); vg_console.cursor_pos = vg_console.cursor_user; } -static void _console_delete(void) +VG_STATIC void _console_delete(void) { vg_console.cursor_user = console_delete_char( 1 ); vg_console.cursor_pos = vg_console.cursor_user; } -static void _console_home_select(void) +VG_STATIC void _console_home_select(void) { console_move_cursor( &vg_console.cursor_user, NULL, -10000, 0 ); } -static void _console_home(void) +VG_STATIC void _console_home(void) { console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, -10000, 1 ); } -static void _console_end_select(void) +VG_STATIC void _console_end_select(void) { console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0 ); } -static void _console_end(void) +VG_STATIC void _console_end(void) { console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, vg_list_size( vg_console.input ), 1 ); } -static void _console_select_all(void) +VG_STATIC void _console_select_all(void) { console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0); console_move_cursor( &vg_console.cursor_pos, NULL, -10000, 0); } -static void _console_cut(void) +VG_STATIC void _console_cut(void) { console_to_clipboard(); vg_console.cursor_user = console_delete_char(0); vg_console.cursor_pos = vg_console.cursor_user; } -static void _console_enter(void) +VG_STATIC void _console_enter(void) { if( !strlen( vg_console.input ) ) return; @@ -671,7 +630,7 @@ static void _console_enter(void) vg_console.input[0] = '\0'; } -static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, +VG_STATIC void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int action, int mods ) { if( !action ) @@ -733,7 +692,7 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, } /* Handle an OS based input of UTF32 character from the keyboard or such */ -static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar ) +VG_STATIC void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar ) { if( uWchar <= 0x7F && (char)uWchar != 0x60) { diff --git a/src/vg/vg_debug.h b/src/vg/vg_debug.h deleted file mode 100644 index 67557b7..0000000 --- a/src/vg/vg_debug.h +++ /dev/null @@ -1,106 +0,0 @@ -#if 0 -int debug_sfx = 0; -struct vg_convar debug_cvars[] = -{ - { .name = "debug_sfx", .data = &debug_sfx, .data_type = k_convar_dtype_i32 } -}; - -static void sfx_internal_debug_overlay(void) -{ - if( !debug_sfx ) - return; - - // Grab values - struct sound_info - { - float signal; - const char *name; - u32 cursor, flags; - - u32 buffer_length, clip_start, clip_end; - } - infos[ SFX_MAX_SYSTEMS ]; - int num_systems; - - char perf[128]; - - sfx_lock(); - num_systems = sfx_sys_len; - - for( int i = 0; i < sfx_sys_len; i ++ ) - { - sfx_system *sys = sfx_sys + i; - struct sound_info *snd = &infos[ i ]; - - //snd->signal = sys->signal_average; - snd->name = sys->name; - snd->cursor = sys->cur; - snd->flags = sys->flags; - snd->clip_start = sys->clip_start; - snd->clip_end = sys->clip_end; - snd->buffer_length = sys->buffer_length; - } - strcpy( perf, sfx_performance_info ); - sfx_unlock(); - - // UI part - // ======== - ui_global_ctx.cursor[0] = 10; - ui_global_ctx.cursor[1] = 10; - ui_global_ctx.cursor[2] = 150; - ui_global_ctx.cursor[3] = 12; - ui_text( &ui_global_ctx, ui_global_ctx.cursor, perf, 1, 0 ); - ui_global_ctx.cursor[1] += 20; - - - // Draw audio stack - for( int i = 0; i < num_systems; i ++ ) - { - ui_global_ctx.cursor[2] = 150; - ui_global_ctx.cursor[3] = 12; - - u32 alpha = (infos[i].flags & SFX_FLAG_PERSISTENT)? 0xff000000: 0x22000000; - - ui_new_node( &ui_global_ctx ); - { - ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x00333333 | alpha ); - - ui_px baseline = ui_global_ctx.cursor[0]; - ui_px width_block_1 = (ui_px)(((float)infos[i].clip_start / (float)infos[i].buffer_length) * 150.0f); - ui_px width_block_2 = (ui_px)(((float)(infos[i].buffer_length-infos[i].clip_end) / (float)infos[i].buffer_length) * 150.0f ); - - ui_global_ctx.cursor[2] = width_block_1; - ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77ffffff ); - - ui_global_ctx.cursor[2] = width_block_2; - ui_align_right( &ui_global_ctx ); - ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77ffffff ); - - // Cursor - - ui_global_ctx.cursor[2] = 2; - ui_global_ctx.cursor[0] = baseline + (ui_px)(((float)infos[i].cursor / (float)infos[i].buffer_length) * 150.0f); - ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0xffffffff ); - - ui_global_ctx.cursor[0] = baseline + 2; - ui_global_ctx.cursor[1] += 2; - ui_text( &ui_global_ctx, ui_global_ctx.cursor, infos[i].name, 1, 0 ); - } - ui_end_down( &ui_global_ctx ); - ui_global_ctx.cursor[1] += 1; - } -} - -static void vg_debugtools_draw(void) -{ - sfx_internal_debug_overlay(); -} - -static void vg_debugtools_setup(void) -{ - for( int i = 0; i < vg_list_size( debug_cvars ); i ++ ) - { - vg_convar_push( debug_cvars[i] ); - } -} -#endif diff --git a/src/vg/vg_gldiag.h b/src/vg/vg_gldiag.h deleted file mode 100644 index 99a7059..0000000 --- a/src/vg/vg_gldiag.h +++ /dev/null @@ -1,43 +0,0 @@ -#define VG_GLDIAG_GL_GET_STRING( o, x ) fprintf( o, #x ": %s\n", glGetString( x ) ); - -static void vg_check_gl_integerv( FILE *log, GLenum name, const char *stringname ) -{ - GLint data; - glGetIntegerv( name, &data ); - fprintf( log, "%s:%d\n", stringname, data ); -} - -#define VG_GLDIAG_GL_INTEGERV( o, x ) vg_check_gl_integerv( o, x, #x ); - -static int vg_run_gfx_diagnostics(void) -{ -#if 0 - FILE *log = fopen( "gldiag.txt", "w" ); - if( log ) - { - VG_GLDIAG_GL_GET_STRING( log, GL_VERSION ) - VG_GLDIAG_GL_GET_STRING( log, GL_VENDOR ) - VG_GLDIAG_GL_GET_STRING( log, GL_RENDERER ) - VG_GLDIAG_GL_GET_STRING( log, GL_SHADING_LANGUAGE_VERSION ) - - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_3D_TEXTURE_SIZE ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_ARRAY_TEXTURE_LAYERS ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_CLIP_DISTANCES ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_COLOR_TEXTURE_SAMPLES ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_COMBINED_UNIFORM_BLOCKS ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_DRAW_BUFFERS ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_ELEMENTS_INDICES ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_ELEMENTS_VERTICES ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_FRAGMENT_UNIFORM_BLOCKS ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_RENDERBUFFER_SIZE ) - VG_GLDIAG_GL_INTEGERV( log, GL_MAX_UNIFORM_BLOCK_SIZE ) - VG_GLDIAG_GL_INTEGERV( log, GL_NUM_EXTENSIONS ) - VG_GLDIAG_GL_INTEGERV( log, GL_PACK_ALIGNMENT ) - - fclose( log ); - } -#endif - - return 0; -} diff --git a/src/vg/vg_input.h b/src/vg/vg_input.h index 7bfd6f4..ac2cce6 100644 --- a/src/vg/vg_input.h +++ b/src/vg/vg_input.h @@ -5,14 +5,14 @@ #include "common.h" #include "vg/vg_loader.h" -static inline float vg_get_axis( const char *axis ); -static inline int vg_get_button( const char *button ); +VG_STATIC inline float vg_get_axis( const char *axis ); +VG_STATIC inline int vg_get_button( const char *button ); /* * Cannot be used in fixed update */ -static inline int vg_get_button_down( const char *button ); -static inline int vg_get_button_up( const char *button ); +VG_STATIC inline int vg_get_button_down( const char *button ); +VG_STATIC inline int vg_get_button_up( const char *button ); enum vg_button_state { @@ -30,7 +30,7 @@ enum EInputMode } vg_input_mode; -static struct axis_binding +VG_STATIC struct axis_binding { const char *name; union @@ -45,7 +45,7 @@ static struct axis_binding } vg_axis_binds[]; -static struct button_binding +VG_STATIC struct button_binding { const char *name; int bind; @@ -60,14 +60,14 @@ vg_controller_binds[]; #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wreturn-type" -static inline float vg_get_axis( const char *axis ) +VG_STATIC 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 ) +VG_STATIC struct button_binding *vg_get_button_ptr( const char *button ) { for( int i=0; i +#include /* - * FIle I/O + * File I/O */ -static i64 vg_file_size( FILE *fileptr ) + +#define VG_FILE_IO_CHUNK_SIZE 1024*256 + +VG_STATIC void vg_file_print_invalid( FILE *fp ) { - fseek( fileptr, 0, SEEK_END ); - i64 fsize = ftell( fileptr ); - fseek( fileptr, 0, SEEK_SET ); - - return fsize; + if( feof( fp )) + { + vg_error( "mdl_open: header too short\n" ); + } + else + { + if( ferror( fp )) + vg_error( "mdl_open: %s\n", strerror(errno) ); + else + vg_error( "mdl_open: unkown failure\n" ); + + } } -static void *vg_disk_open_read( const char *path, int reserve_end, i64 *size ) +/* read entire binary file */ +VG_STATIC void *vg_file_read( void *lin_alloc, const char *path ) { FILE *f = fopen( path, "rb" ); if( f ) { - i64 fsize = vg_file_size( f ); - void *buf = vg_alloc( fsize + reserve_end ); - - /* Invalid / corrupt read */ - if( fread( buf, 1, fsize, f ) != fsize ) + void *buffer = vg_linear_alloc( lin_alloc, 0 ); + u64 current = 0; + + /* read in chunks */ + for( u32 i=0; 1; i++ ) { - vg_free( buf ); - buf = NULL; + buffer = vg_linear_extend( lin_alloc, buffer, VG_FILE_IO_CHUNK_SIZE ); + + u64 l = fread( buffer + current, 1, VG_FILE_IO_CHUNK_SIZE, f ); + current += l; + + if( l != VG_FILE_IO_CHUNK_SIZE ) + { + if( feof( f ) ) + { + break; + } + else + { + if( ferror( f ) ) + { + fclose(f); + vg_fatal_exit_loop( "read error" ); + } + else + { + fclose(f); + vg_fatal_exit_loop( "unknown error codition" ); + } + } + } } - - *size = fsize; - + + buffer = vg_linear_resize( lin_alloc, buffer, current ); fclose( f ); - return buf; + + return buffer; } else { + vg_error( "vg_disk_open_read: %s\n", strerror(errno) ); return NULL; } } -static char *vg_disk_load_text( const char *path, i64 *size ) +/* get the size of the file just loaded */ +VG_STATIC u32 vg_linear_last_size( void *allocator ); /* ? */ +VG_STATIC u32 vg_file_size( void *lin_alloc ) { - char *buf; - i64 fsize; - - if( (buf = (char *)vg_disk_open_read( path, 1, &fsize )) ) - { - buf[ fsize ] = 0x00; - *size = fsize +1; - - return buf; - } - - return NULL; + return vg_linear_last_size( lin_alloc ); } -static void *vg_asset_read_s( const char *path, i64 *size ) +/* read entire file and append a null on the end */ +VG_STATIC char *vg_file_read_text( void *lin_alloc, const char *path ) { - return vg_disk_open_read( path, 0, size ); -} + char *str = vg_file_read( lin_alloc, path ); -static void *vg_asset_read( const char *path ) -{ - i64 size; - return vg_disk_open_read( path, 0, &size ); -} + if( !str ) + return NULL; -static char *vg_textasset_read_s( const char *path, i64 *size ) -{ - return vg_disk_load_text( path, size ); -} + /* include null terminator */ + str = vg_linear_extend( lin_alloc, str, 1 ); + str[ vg_file_size(lin_alloc) ] = '\0'; -static char *vg_textasset_read( const char *name ) -{ - i64 size; - return vg_disk_load_text( name, &size ); + return str; } -static int vg_asset_write( const char *path, void *data, i64 size ) + +VG_STATIC int vg_asset_write( const char *path, void *data, i64 size ) { FILE *f = fopen( path, "wb" ); if( f ) diff --git a/src/vg/vg_lines.h b/src/vg/vg_lines.h index 40e2886..58e0e14 100644 --- a/src/vg/vg_lines.h +++ b/src/vg/vg_lines.h @@ -5,13 +5,7 @@ #include "vg/vg.h" -static int debug_lines_enable = 0; - -#ifdef VG_3D - typedef v3f line_co; -#else - typedef v2f line_co; -#endif +typedef v3f line_co; static struct vg_shader _shader_lines = { @@ -22,24 +16,15 @@ static struct vg_shader _shader_lines = .orig_file = NULL, .static_src = -#ifdef VG_3D "uniform mat4 uPv;" "layout (location=0) in vec3 a_co;" -#else - "uniform mat3 uPv;" - "layout (location=0) in vec2 a_co;" -#endif "layout (location=1) in vec4 a_colour;" "" "out vec4 s_colour;" "" "void main()" "{" -#ifdef VG_3D " vec4 vert_pos = uPv * vec4( a_co, 1.0 );" -#else - " vec4 vert_pos = vec4( uPv * vec3( a_co, 1.0 ), 1.0 );" -#endif " s_colour = a_colour;" " gl_Position = vert_pos;" "}" @@ -60,32 +45,29 @@ static struct vg_shader _shader_lines = } }; + struct { - struct vg_lines_vert - { -#ifdef VG_3D + u32 draw; + + struct vg_lines_vert + { v3f co; -#else - v2f co; -#endif + u32 colour; + } + *vertex_buffer; - u32 colour; - } - *buffer; - GLuint vao, vbo; - u32 draw_idx, cap, buffer_size; } -vg_lines; +static vg_lines; -static void vg_lines_init(void) +VG_STATIC void vg_lines_init(void) { vg_info( "vg_lines_init\n" ); vg_convar_push( (struct vg_convar){ - .name = "debug_lines", - .data = &debug_lines_enable, + .name = "vg_lines", + .data = &vg_lines.draw, .data_type = k_convar_dtype_i32, .opt_i32 = { .min=0, .max=1, .clamp=1 }, .persistent = 1 @@ -100,18 +82,19 @@ static void vg_lines_init(void) glBindVertexArray( vg_lines.vao ); glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); - vg_lines.cap = 50000; - vg_lines.buffer_size = vg_lines.cap * sizeof( struct vg_lines_vert ); + u32 size = 50000 * sizeof( struct vg_lines_vert ); + + vg_lines.vertex_buffer = + vg_create_linear_allocator( vg_mem.rtmemory, size ); - glBufferData( GL_ARRAY_BUFFER, vg_lines.buffer_size, - NULL, GL_DYNAMIC_DRAW ); + glBufferData( GL_ARRAY_BUFFER, size, NULL, GL_DYNAMIC_DRAW ); glBindVertexArray( vg_lines.vao ); VG_CHECK_GL_ERR(); /* Pointers */ glVertexAttribPointer( 0, - sizeof( vg_lines.buffer[0].co ) / sizeof(float), + 3, GL_FLOAT, GL_FALSE, sizeof( struct vg_lines_vert ), @@ -128,88 +111,57 @@ static void vg_lines_init(void) (void*)(offsetof( struct vg_lines_vert, colour )) ); glEnableVertexAttribArray( 1 ); - + VG_CHECK_GL_ERR(); - - /* Alloc RAM */ - vg_lines.buffer = vg_alloc( vg_lines.buffer_size ); vg_success( "done\n" ); } vg_release_thread_sync(); } -static void vg_lines_free(void *nothing) -{ - glDeleteVertexArrays( 1, &vg_lines.vao ); - glDeleteBuffers( 1, &vg_lines.vbo ); - vg_free( vg_lines.buffer ); -} - -static void vg_lines_drawall( float* projection ) +VG_STATIC void vg_lines_drawall( float* projection ) { glUseProgram( _shader_lines.id ); -#ifdef VG_3D glUniformMatrix4fv -#else - glUniformMatrix3fv -#endif ( glGetUniformLocation( _shader_lines.id, "uPv" ), 1, GL_FALSE, projection ); glBindVertexArray( vg_lines.vao ); glBindBuffer( GL_ARRAY_BUFFER, vg_lines.vbo ); + + u32 bufusage = vg_linear_get_cur(vg_lines.vertex_buffer); - glBufferSubData( GL_ARRAY_BUFFER, 0, vg_lines.draw_idx * - sizeof(struct vg_lines_vert), vg_lines.buffer ); + glBufferSubData( GL_ARRAY_BUFFER, 0, bufusage, vg_lines.vertex_buffer ); glEnable( GL_BLEND ); glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA ); glBlendEquation( GL_FUNC_ADD ); - if( debug_lines_enable ) - glDrawArrays( GL_LINES, 0, vg_lines.draw_idx ); + if( vg_lines.draw ) + glDrawArrays( GL_LINES, 0, bufusage / sizeof(struct vg_lines_vert) ); glDisable( GL_BLEND ); - vg_lines.draw_idx = 0; + vg_linear_clear( vg_lines.vertex_buffer ); } -static void vg_line2( line_co from, line_co to, u32 fc, u32 tc ) +VG_STATIC void vg_line2( line_co from, line_co to, u32 fc, u32 tc ) { - struct vg_lines_vert *v = &vg_lines.buffer[vg_lines.draw_idx]; + u32 size = 2 * sizeof(struct vg_lines_vert); + struct vg_lines_vert *v = vg_linear_alloc( vg_lines.vertex_buffer, size ); -#ifdef VG_3D v3_copy( from, v[0].co ); v3_copy( to, v[1].co ); -#else - v2_copy( from, v[0].co ); - v2_copy( to, v[1].co ); -#endif v[0].colour = fc; v[1].colour = tc; - - vg_lines.draw_idx += 2; } -static void vg_line( line_co from, line_co to, u32 colour ) +VG_STATIC void vg_line( line_co from, line_co to, u32 colour ) { vg_line2( from, to, colour, colour ); } -static void vg_line_box( line_co min, line_co max, u32 colour ) -{ -#ifdef VG_3D - /* TODO... */ -#else - vg_line( min, (v2f){min[0],max[1]}, colour ); - vg_line( (v2f){min[0],max[1]}, max, colour ); - vg_line( max, (v2f){max[0],min[1]}, colour ); - vg_line( (v2f){max[0],min[1]}, min, colour ); -#endif -} - -static void vg_line_boxf( boxf box, u32 colour ) +VG_STATIC void vg_line_boxf( boxf box, u32 colour ) { v3f p000, p001, p010, p011, p100, p101, p110, p111; @@ -239,7 +191,7 @@ static void vg_line_boxf( boxf box, u32 colour ) vg_line( p111, p011, colour ); } -static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ) +VG_STATIC void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ) { v3f p000, p001, p010, p011, p100, p101, p110, p111; @@ -281,7 +233,7 @@ static void vg_line_boxf_transformed( m4x3f m, boxf box, u32 colour ) vg_line( p100, p010, colour ); } -static void vg_line_cross(v3f pos,u32 colour, float scale) +VG_STATIC void vg_line_cross(v3f pos,u32 colour, float scale) { v3f p0, p1; v3_add( (v3f){ scale,0.0f,0.0f}, pos, p0 ); @@ -295,7 +247,7 @@ static void vg_line_cross(v3f pos,u32 colour, float scale) vg_line( p0, p1, colour ); } -static void vg_line_pt3( v3f pt, float size, u32 colour ) +VG_STATIC void vg_line_pt3( v3f pt, float size, u32 colour ) { boxf box = { diff --git a/src/vg/vg_loader.h b/src/vg/vg_loader.h index c7522b0..23d35b9 100644 --- a/src/vg/vg_loader.h +++ b/src/vg/vg_loader.h @@ -58,14 +58,14 @@ static struct vg_loader void (*fn_free)(void *); void *data; } - *step_buffer; - u32 step_count, step_cap, step_action; + step_buffer[16]; + u32 step_count, step_action; GLuint vao, vbo; } vg_loader; -static int vg_loader_init(void) +VG_STATIC void vg_loader_init(void) { float quad[] = { 0.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f }; @@ -76,23 +76,16 @@ static int vg_loader_init(void) glBindBuffer( GL_ARRAY_BUFFER, vg_loader.vbo ); glBufferData( GL_ARRAY_BUFFER, sizeof(quad), quad, GL_STATIC_DRAW ); glBindVertexArray( vg_loader.vao ); - glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, - sizeof(float)*2, (void*)0 ); + glVertexAttribPointer( 0, 2, GL_FLOAT, GL_FALSE, sizeof(float)*2, (void*)0 ); glEnableVertexAttribArray( 0 ); VG_CHECK_GL_ERR(); if( !vg_shader_compile( &_shader_loader ) ) - { - glDeleteVertexArrays( 1, &vg_loader.vao ); - glDeleteBuffers( 1, &vg_loader.vbo ); - return 0; - } - - return 1; + vg_fatal_exit_loop( "failed to compile shader" ); } -static void vg_loader_free(void) +VG_STATIC void vg_loader_free(void) { vg_info( "vg_loader_free\n" ); glDeleteVertexArrays( 1, &vg_loader.vao ); @@ -107,11 +100,10 @@ static void vg_loader_free(void) step->fn_free( step->data ); } - vg_free( vg_loader.step_buffer ); vg_info( "done\n" ); } -static float hue_to_rgb( float p, float q, float t ) +VG_STATIC float hue_to_rgb( float p, float q, float t ) { if(t < 0.0f) t += 1.0f; if(t > 1.0f) t -= 1.0f; @@ -121,9 +113,9 @@ static float hue_to_rgb( float p, float q, float t ) return p; } -static void vg_render_log(void) +VG_STATIC void vg_render_log(void) { - ui_begin( &ui_global_ctx, vg.window_x, vg.window_y ); + ui_begin( vg.window_x, vg.window_y ); int const fh = 14; int lines_screen_max = ((vg.window_y/fh)-2), @@ -132,10 +124,10 @@ static void vg_render_log(void) int ptr = vg_log.buffer_line_current; - ui_global_ctx.cursor[0] = 0; - ui_global_ctx.cursor[1] = lines_to_draw*fh; - ui_global_ctx.cursor[3] = fh; - ui_fill_x( &ui_global_ctx ); + vg_uictx.cursor[0] = 0; + vg_uictx.cursor[1] = lines_to_draw*fh; + vg_uictx.cursor[3] = fh; + ui_fill_x(); for( int i=0; i + +typedef struct vg_linear_allocator vg_linear_allocator; + +struct +{ + void *rtmemory, + *scratch; +} +static vg_mem; + +#pragma pack(push,1) +struct vg_linear_allocator +{ + u32 size, cur; + + /* allows temporarily extendable buffers */ + void *last_alloc; + u32 last_alloc_size; +}; +#pragma pack(pop) + +/* + * TODO: Fallback on libc + * TODO: 8 byte alignment + */ + +VG_STATIC void vg_error(const char *fmt, ...); + +/* allocate something from a linear allocator */ +__attribute__((warn_unused_result)) +VG_STATIC void *vg_linear_alloc( void *allocator, u32 size ) +{ + if( allocator == NULL ) + vg_fatal_exit_loop( "Null allocator" ); + + vg_linear_allocator *allocptr = allocator; + allocptr --; + + if( allocptr->cur + size > allocptr->size ) + { + vg_error( "%u(current) + %u(alloc) > %u(max)\n", + allocptr->cur, size, allocptr->size ); + + vg_fatal_exit_loop( "Linear allocator out of memory" ); + } + + void *data = allocator + allocptr->cur; + allocptr->cur += size; + allocptr->last_alloc = data; + allocptr->last_alloc_size = size; + + return data; +} + +/* resize latest block of memory from linear */ +__attribute__((warn_unused_result)) +VG_STATIC void *vg_linear_resize( void *allocator, void *data, u32 newsize ) +{ + vg_linear_allocator *allocptr = allocator; + allocptr --; + + if( allocptr->last_alloc == data ) + { + allocptr->cur -= allocptr->last_alloc_size; + return vg_linear_alloc( allocator, newsize ); + } + else + { + vg_fatal_exit_loop( "Cannot resize this buffer anymore" ); + } + + return NULL; +} + +VG_STATIC void vg_linear_del( void *allocator, void *data ) +{ + void *ignore = vg_linear_resize( allocator, data, 0 ); + + vg_linear_allocator *allocptr = allocator; + allocptr --; + + allocptr->last_alloc = NULL; + allocptr->last_alloc_size = 0; +} + +/* extend latest block of memory from linear */ +__attribute__((warn_unused_result)) +VG_STATIC void *vg_linear_extend( void *allocator, void *data, u32 extra ) +{ + vg_linear_allocator *allocptr = allocator; + allocptr --; + + return vg_linear_resize( allocator, data, allocptr->last_alloc_size+extra ); +} + +/* get the current usage of allocator */ +VG_STATIC u32 vg_linear_get_cur( void *allocator ) +{ + vg_linear_allocator *allocptr = allocator; + allocptr --; + + return allocptr->cur; +} + +/* get the capacity of allocator */ +VG_STATIC u32 vg_linear_get_capacity( void *allocator ) +{ + vg_linear_allocator *allocptr = allocator; + allocptr --; + + return allocptr->size; +} + +/* get the size of the last allocated thing */ +VG_STATIC u32 vg_linear_last_size( void *allocator ) +{ + vg_linear_allocator *allocptr = allocator; + allocptr --; + + return allocptr->last_alloc_size; +} + +/* yeet all memory from linear allocator */ +VG_STATIC void vg_linear_clear( void *allocator ) +{ + vg_linear_allocator *allocptr = allocator; + allocptr --; + + allocptr->last_alloc = NULL; + allocptr->last_alloc_size = 0; + allocptr->cur = 0; +} + +/* allocate a FIXED SIZE linear allocator */ +VG_STATIC void *vg_create_linear_allocator( void *lin_alloc, u32 size ) +{ + u32 total = size + sizeof(vg_linear_allocator); + vg_linear_allocator *allocptr; + + if( lin_alloc == NULL ) + { + static int allow_once = 1; + + if( allow_once ) + { + allocptr = malloc( total ); + + if( allocptr == NULL ) + vg_fatal_exit_loop( "Create linear: Malloc failed" ); + + allow_once = 0; + } + else + vg_fatal_exit_loop( "Shouldnt call this twice!" ); + } + else + { + vg_linear_allocator *parent = lin_alloc; + parent --; + + allocptr = vg_linear_alloc( lin_alloc, total ); + +#if 0 + parent->last_alloc = allocptr+1; + parent->last_alloc_size = total; +#else + parent->last_alloc = NULL; + parent->last_alloc_size = total; +#endif + } + + allocptr->size = size; + allocptr->cur = 0; + allocptr->last_alloc = NULL; + allocptr->last_alloc_size = 0; + + void *data = allocptr+1; + return data; +} + +/* request all the memory we need in advance */ +VG_STATIC void vg_prealloc_quota( u32 size ) +{ + size = VG_MAX( size, 20*1024*1024 ); + vg_mem.rtmemory = vg_create_linear_allocator( NULL, size ); + vg_mem.scratch = vg_create_linear_allocator( vg_mem.rtmemory, 10*1024*1024 ); +} + +#endif /* VG_MEM_H */ diff --git a/src/vg/vg_module.h b/src/vg/vg_module.h deleted file mode 100644 index 15dfd47..0000000 --- a/src/vg/vg_module.h +++ /dev/null @@ -1,29 +0,0 @@ -#ifndef VG_MODULE_H -#define VG_MODULE_H - -#include "vg_shader.h" - -struct vg_module -{ - const char *name; - - struct - { - struct vg_shader *shaders; - u32 shader_count; - - vg_tex2d *textures; - u32 texture_count; - } - resources; - - int (*fn_load)(void); - void (*fn_free)(void); -}; - -static int vg_module_load( struct vg_module *m ) -{ - -} - -#endif /* VG_MODULE_H */ diff --git a/src/vg/vg_platform.h b/src/vg/vg_platform.h index b71a01d..f1c5488 100644 --- a/src/vg/vg_platform.h +++ b/src/vg/vg_platform.h @@ -29,6 +29,10 @@ struct vg_achievement const char *name; }; +#ifndef VG_STATIC +#define VG_STATIC static +#endif + #define vg_static_assert _Static_assert #define vg_list_size( A ) (sizeof(A)/sizeof(A[0])) #define VG_MUST_USE_RESULT __attribute__((warn_unused_result)) @@ -65,7 +69,7 @@ struct vg_achievement #endif -static void vg_strncpy( const char *src, char *dst, u32 len ) +VG_STATIC void vg_strncpy( const char *src, char *dst, u32 len ) { for( u32 i=0; i - -#define VG_ZERO_NEW_MEM - -static void vg_fatal_exit_loop( const char *error ); -static void *vg_alloc( size_t size ) -{ - if( size == 0 ) - return NULL; - - void *ptr = malloc( size ); - - if( !ptr ) - { - vg_fatal_exit_loop( "Out of memory" ); - } - -#ifdef VG_ZERO_NEW_MEM - u8 *bytes = ptr; - for( u32 i=0; i #include #include @@ -166,24 +112,24 @@ void free( void *ptr ); #include #include -static int vg_thread_run( void *pfunc, void *data ); -static void vg_thread_exit(void); -static void vg_set_thread_name( const char *name ); -static int vg_semaphore_init( vg_semaphore *sem, u32 value ); -static int vg_semaphore_trywait( vg_semaphore *sem ); -static int vg_semaphore_wait( vg_semaphore *sem ); -static int vg_semaphore_post( vg_semaphore *sem ); -static void vg_semaphore_free( vg_semaphore *sem ); -static int vg_mutex_init( vg_mutex *mutex ); -static int vg_mutex_lock( vg_mutex *mutex ); -static int vg_mutex_unlock( vg_mutex *mutex ); -static void vg_mutex_free( vg_mutex *mutex ); -static void vg_sleep_ms( long msec ); -static double vg_time_diff( vg_timespec start, vg_timespec end ); +VG_STATIC int vg_thread_run( void *pfunc, void *data ); +VG_STATIC void vg_thread_exit(void); +VG_STATIC void vg_set_thread_name( const char *name ); +VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value ); +VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem ); +VG_STATIC int vg_semaphore_wait( vg_semaphore *sem ); +VG_STATIC int vg_semaphore_post( vg_semaphore *sem ); +VG_STATIC void vg_semaphore_free( vg_semaphore *sem ); +VG_STATIC int vg_mutex_init( vg_mutex *mutex ); +VG_STATIC int vg_mutex_lock( vg_mutex *mutex ); +VG_STATIC int vg_mutex_unlock( vg_mutex *mutex ); +VG_STATIC void vg_mutex_free( vg_mutex *mutex ); +VG_STATIC void vg_sleep_ms( long msec ); +VG_STATIC double vg_time_diff( vg_timespec start, vg_timespec end ); #ifdef _WIN32_NO -static int vg_thread_run( void *pfunc, void *data ) +VG_STATIC int vg_thread_run( void *pfunc, void *data ) { HANDLE hThread = CreateThread ( @@ -210,31 +156,31 @@ static int vg_thread_run( void *pfunc, void *data ) } } -static void vg_thread_exit(void) +VG_STATIC void vg_thread_exit(void) { ExitThread(0); } -static void vg_set_thread_name( const char *name ) +VG_STATIC void vg_set_thread_name( const char *name ) { /* I believe this is a meaningless concept in windows */ } -static int vg_semaphore_init( vg_semaphore *sem, u32 value ); -static int vg_semaphore_trywait( vg_semaphore *sem ); -static int vg_semaphore_wait( vg_semaphore *sem ); -static int vg_semaphore_post( vg_semaphore *sem ); -static void vg_semaphore_free( vg_semaphore *sem ); -static int vg_mutex_init( vg_mutex *mutex ); -static int vg_mutex_lock( vg_mutex *mutex ); -static int vg_mutex_unlock( vg_mutex *mutex ); -static void vg_mutex_free( vg_mutex *mutex ); -static void vg_sleep_ms( long msec ); -static double vg_time_diff( vg_timespec start, vg_timespec end ); +VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value ); +VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem ); +VG_STATIC int vg_semaphore_wait( vg_semaphore *sem ); +VG_STATIC int vg_semaphore_post( vg_semaphore *sem ); +VG_STATIC void vg_semaphore_free( vg_semaphore *sem ); +VG_STATIC int vg_mutex_init( vg_mutex *mutex ); +VG_STATIC int vg_mutex_lock( vg_mutex *mutex ); +VG_STATIC int vg_mutex_unlock( vg_mutex *mutex ); +VG_STATIC void vg_mutex_free( vg_mutex *mutex ); +VG_STATIC void vg_sleep_ms( long msec ); +VG_STATIC double vg_time_diff( vg_timespec start, vg_timespec end ); #else -static int vg_thread_run( void *pfunc, void *data ) +VG_STATIC int vg_thread_run( void *pfunc, void *data ) { pthread_t hThread; if( pthread_create( &hThread, NULL, pfunc, data ) ) @@ -249,12 +195,12 @@ static int vg_thread_run( void *pfunc, void *data ) } -static void vg_thread_exit(void) +VG_STATIC void vg_thread_exit(void) { pthread_exit(NULL); } -static void vg_set_thread_name( const char *name ) +VG_STATIC void vg_set_thread_name( const char *name ) { /* not defined but links?? */ #if 0 @@ -262,38 +208,38 @@ static void vg_set_thread_name( const char *name ) #endif } -static int vg_semaphore_init( vg_semaphore *sem, u32 value ) +VG_STATIC int vg_semaphore_init( vg_semaphore *sem, u32 value ) { return !sem_init( sem, 0, value ); } -static int vg_semaphore_trywait( vg_semaphore *sem ) +VG_STATIC int vg_semaphore_trywait( vg_semaphore *sem ) { return !sem_trywait( sem ); } -static int vg_semaphore_wait( vg_semaphore *sem ) +VG_STATIC int vg_semaphore_wait( vg_semaphore *sem ) { return !sem_wait( sem ); } -static int vg_semaphore_post( vg_semaphore *sem ) +VG_STATIC int vg_semaphore_post( vg_semaphore *sem ) { return !sem_post( sem ); } -static void vg_semaphore_free( vg_semaphore *sem ) +VG_STATIC void vg_semaphore_free( vg_semaphore *sem ) { sem_destroy( sem ); } -static int vg_mutex_init( vg_mutex *mutex ) +VG_STATIC int vg_mutex_init( vg_mutex *mutex ) { memset( mutex, 0, sizeof(vg_mutex) ); return 1; } -static int vg_mutex_lock( vg_mutex *mutex ) +VG_STATIC int vg_mutex_lock( vg_mutex *mutex ) { if( !pthread_mutex_lock( mutex ) ) return 1; @@ -301,7 +247,7 @@ static int vg_mutex_lock( vg_mutex *mutex ) return 0; } -static int vg_mutex_unlock( vg_mutex *mutex ) +VG_STATIC int vg_mutex_unlock( vg_mutex *mutex ) { if( !pthread_mutex_unlock( mutex ) ) return 1; @@ -309,12 +255,12 @@ static int vg_mutex_unlock( vg_mutex *mutex ) return 0; } -static void vg_mutex_free( vg_mutex *mutex ) +VG_STATIC void vg_mutex_free( vg_mutex *mutex ) { } -static void vg_sleep_ms( long msec ) +VG_STATIC void vg_sleep_ms( long msec ) { struct timespec ts; @@ -324,7 +270,7 @@ static void vg_sleep_ms( long msec ) } /* diff two timespecs in MS */ -static double vg_time_diff( struct timespec start, struct timespec end ) +VG_STATIC double vg_time_diff( struct timespec start, struct timespec end ) { double elapsed = 1000.0*end.tv_sec + 1e-6*end.tv_nsec - (1000.0*start.tv_sec + 1e-6*start.tv_nsec); @@ -337,22 +283,4 @@ static double vg_time_diff( struct timespec start, struct timespec end ) #define VG_MIN( A, B ) ((A)<(B)?(A):(B)) #define VG_MAX( A, B ) ((A)>(B)?(A):(B)) -static void *buffer_reserve( void *buffer, u32 count, u32 *cap, u32 amount, - size_t emsize ) -{ - if( count+amount > *cap ) - { - *cap = VG_MAX( (*cap)*2, (*cap)+amount ); - return vg_realloc( buffer, (*cap) * emsize ); - } - - return buffer; -} - -static void *buffer_fix( void *buffer, u32 count, u32 *cap, size_t emsize ) -{ - *cap = count; - return vg_realloc( buffer, (*cap) * emsize ); -} - #endif diff --git a/src/vg/vg_profiler.h b/src/vg/vg_profiler.h index fdd26a7..fded330 100644 --- a/src/vg/vg_profiler.h +++ b/src/vg/vg_profiler.h @@ -24,12 +24,12 @@ struct vg_profile struct timespec start; }; -static void vg_profile_begin( struct vg_profile *profile ) +VG_STATIC void vg_profile_begin( struct vg_profile *profile ) { clock_gettime( CLOCK_REALTIME, &profile->start ); } -static void vg_profile_increment( struct vg_profile *profile ) +VG_STATIC void vg_profile_increment( struct vg_profile *profile ) { profile->buffer_current ++; @@ -42,7 +42,7 @@ static void vg_profile_increment( struct vg_profile *profile ) profile->samples[ profile->buffer_current ] = 0.0f; } -static void vg_profile_end( struct vg_profile *profile ) +VG_STATIC void vg_profile_end( struct vg_profile *profile ) { struct timespec time_end; @@ -61,7 +61,7 @@ static void vg_profile_end( struct vg_profile *profile ) } } -static void vg_profile_drawn( struct vg_profile **profiles, u32 count, +VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count, float budget, ui_rect panel, u32 colour_offset ) { if( !vg_profiler ) @@ -76,7 +76,7 @@ static void vg_profile_drawn( struct vg_profile **profiles, u32 count, float sh = panel[3] / VG_PROFILE_SAMPLE_COUNT, sw = panel[2]; - ui_fill_rect( &ui_global_ctx, panel, 0xa0000000 ); + ui_fill_rect( panel, 0xa0000000 ); assert( count <= 8 ); float avgs[8]; @@ -110,7 +110,7 @@ static void vg_profile_drawn( struct vg_profile **profiles, u32 count, wx, sh }; u32 colour = colours[ (j+colour_offset) % vg_list_size(colours) ]; - ui_fill_rect( &ui_global_ctx, block, colour ); + ui_fill_rect( block, colour ); total += sample; avgs[j] += sample; @@ -125,15 +125,15 @@ static void vg_profile_drawn( struct vg_profile **profiles, u32 count, avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)), profiles[i]->name ); - ui_text( &ui_global_ctx, (ui_rect){ panel[0] + panel[2] + 4, - panel[1] + i * 14, 0, 0 }, - infbuf, - 1, - k_text_align_left ); + ui_text( (ui_rect){ panel[0] + panel[2] + 4, + panel[1] + i * 14, 0, 0 }, + infbuf, + 1, + k_text_align_left ); } } -static void vg_profiler_init(void) +VG_STATIC void vg_profiler_init(void) { vg_convar_push( (struct vg_convar){ .name = "vg_profiler", diff --git a/src/vg/vg_shader.h b/src/vg/vg_shader.h index 06f455d..516d1a8 100644 --- a/src/vg/vg_shader.h +++ b/src/vg/vg_shader.h @@ -6,36 +6,42 @@ #include "vg/vg.h" #include "vg/vg_platform.h" +#if 0 #define STB_INCLUDE_IMPLEMENTATION #define STB_INCLUDE_LINE_GLSL -#define STB_MALLOC vg_alloc -#define STB_FREE vg_free +#define STB_MALLOC vg_alloc +#define STB_FREE vg_free #define STB_REALLOC vg_realloc #include "stb/stb_include.h" +#endif const char *vg_shader_gl_ver = "#version 330 core\n"; -static -struct vg_shader -{ - GLuint id; - const char *name; +typedef struct vg_shader vg_shader; - struct vg_subshader +struct +{ + struct vg_shader { - const char *orig_file, - *static_src; - } - vs, fs; + GLuint id; + const char *name; + + struct vg_subshader + { + const char *orig_file, + *static_src; + } + vs, fs; - void (*link)(void); - int compiled; + void (*link)(void); + int compiled; + } + * shaders[32]; + u32 count; } -** vg_shaders_active = NULL; -static u32 vg_shader_count, - vg_shader_cap; +static vg_shaders; -static GLuint vg_shader_subshader( const char *src, GLint gliShaderType ) +VG_STATIC GLuint vg_shader_subshader( const char *src, GLint gliShaderType ) { GLint shader = glCreateShader( gliShaderType ); @@ -64,7 +70,7 @@ static GLuint vg_shader_subshader( const char *src, GLint gliShaderType ) return shader; } -static int vg_shader_compile( struct vg_shader *shader ) +VG_STATIC int vg_shader_compile( struct vg_shader *shader ) { vg_info( "Compile shader '%s'\n", shader->name ); @@ -94,6 +100,9 @@ static int vg_shader_compile( struct vg_shader *shader ) } else { + vg_fatal_exit_loop( "Unimplemented" ); + +#if 0 char error[260]; char path[260]; strcpy( path, shader->vs.orig_file ); @@ -114,16 +123,19 @@ static int vg_shader_compile( struct vg_shader *shader ) svs = avs; sfs = afs; +#endif } vert = vg_shader_subshader( svs, GL_VERTEX_SHADER ); frag = vg_shader_subshader( sfs, GL_FRAGMENT_SHADER ); +#if 0 if( !static_src ) { - vg_free( avs ); - vg_free( afs ); + free( avs ); + free( afs ); } +#endif if( !vert || !frag ) return 0; @@ -160,7 +172,7 @@ static int vg_shader_compile( struct vg_shader *shader ) return 1; } -static void vg_free_shader( struct vg_shader *shader ) +VG_STATIC void vg_free_shader( struct vg_shader *shader ) { if( shader->compiled ) { @@ -169,55 +181,39 @@ static void vg_free_shader( struct vg_shader *shader ) } } -static void vg_shaders_free(void *nothing) -{ - for( int i=0; icompiled ) - glDeleteProgram( shader->id ); - } - - vg_free( vg_shaders_active ); -} - -static int vg_shaders_recompile(void) +VG_STATIC void vg_shaders_compile(void) { vg_info( "Compiling shaders\n" ); - for( int i=0; icompiled = 0; shader->id = 0; /* TODO: make this an error shader */ - vg_shaders_active = buffer_reserve( vg_shaders_active, vg_shader_count, - &vg_shader_cap, 1, - sizeof( struct vg_shader * ) ); - - vg_shaders_active[ vg_shader_count ++ ] = shader; + vg_shaders.shaders[ vg_shaders.count ++ ] = shader; } #endif /* VG_SHADER_H */ diff --git a/src/vg/vg_shader2.h b/src/vg/vg_shader2.h deleted file mode 100644 index 7201dac..0000000 --- a/src/vg/vg_shader2.h +++ /dev/null @@ -1,13 +0,0 @@ -struct vg_subshader -{ - const char *static_src, - *orig_file; - - void (*uniform_register)(void); -}; - -struct vg_shader -{ - struct vg_subshader vs, fs; -}; - diff --git a/src/vg/vg_steam.h b/src/vg/vg_steam.h index ab135bc..740ffda 100644 --- a/src/vg/vg_steam.h +++ b/src/vg/vg_steam.h @@ -3,6 +3,10 @@ #include "vg.h" +/* + * TODO: Combine interfaces and stuff here instead of having them in client code + */ + #if defined(__linux__) || defined(__APPLE__) /* * The 32-bit version of gcc has the alignment requirement for u64 and double @@ -522,6 +526,78 @@ int SteamAPI_ManualDispatch_GetAPICallResult( HSteamPipe hSteamPipe, void SteamAPI_ReleaseCurrentThreadMemory(void); +static void steamworks_process_api_call( HSteamPipe pipe, + CallbackMsg_t *callback ) +{ + SteamAPICallCompleted_t *pCallCompleted = + (SteamAPICallCompleted_t *)callback->m_pubParam; + + int bFailed; + + void *temp = alloca( pCallCompleted->m_cubParam ); + + if( SteamAPI_ManualDispatch_GetAPICallResult( + pipe, + pCallCompleted->m_hAsyncCall, + temp, + pCallCompleted->m_cubParam, + pCallCompleted->m_iCallback, + &bFailed ) + ) + { + /* + * Dispatch the call result to the registered handler(s) for the + * call identified by pCallCompleted->m_hAsyncCall + */ + + vg_info( "steamworks_event::api_call_completed( %lu )\n", + pCallCompleted->m_hAsyncCall ); + + int j=0; + for( int i=0; im_hAsyncCall ) + { + steam_async_trackers[j ++] = steam_async_trackers[i]; + } + else + { + steam_async *pasync = &steam_async_trackers[j]; + pasync->p_handler( temp, pasync->data ); + } + } + + if( steam_async_track_count == j ) + { + vg_error( "No tracker was register for API call\n" ); + } + + steam_async_track_count = j; + } + else + { +#if 0 + typedef enum ESteamAPICallFailure + { + k_ESteamAPICallFailureNone = -1, + k_ESteamAPICallFailureSteamGone = 0, + k_ESteamAPICallFailureNetworkFailure = 1, + k_ESteamAPICallFailureInvalidHandle = 2, + k_ESteamAPICallFailureMismatchedCallback = 3, + } + + ESteamAPICallFailure; + ESteamAPICallFailure fail_why = + SteamAPI_ISteamUtils_GetAPICallFailureReason( + steam_api_classes.utils, pCallCompleted->m_hAsyncCall ); + + vg_error( "steamworks_event: error getting call result on" + "%lu (code %d)\n", + pCallCompleted->m_hAsyncCall, fail_why ); +#endif + } +} + static void steamworks_event_loop( HSteamPipe pipe ) { SteamAPI_ManualDispatch_RunFrame( pipe ); @@ -534,74 +610,7 @@ static void steamworks_event_loop( HSteamPipe pipe ) /* Check for dispatching API call results */ if( callback.m_iCallback == k_iSteamAPICallCompleted ) { - SteamAPICallCompleted_t *pCallCompleted = - (SteamAPICallCompleted_t *)callback.m_pubParam; - - void *pTmpCallResult = vg_alloc( pCallCompleted->m_cubParam ); - int bFailed; - - if( SteamAPI_ManualDispatch_GetAPICallResult( - pipe, - pCallCompleted->m_hAsyncCall, - pTmpCallResult, - pCallCompleted->m_cubParam, - pCallCompleted->m_iCallback, - &bFailed ) - ) - { - /* - * Dispatch the call result to the registered handler(s) for the - * call identified by pCallCompleted->m_hAsyncCall - */ - - vg_info( "steamworks_event::api_call_completed( %lu )\n", - pCallCompleted->m_hAsyncCall ); - - int j=0; - for( int i=0; im_hAsyncCall ) - { - steam_async_trackers[j ++] = steam_async_trackers[i]; - } - else - { - steam_async *pasync = &steam_async_trackers[j]; - pasync->p_handler( pTmpCallResult, pasync->data ); - } - } - - if( steam_async_track_count == j ) - { - vg_error( "No tracker was register for API call\n" ); - } - - steam_async_track_count = j; - } - else - { -#if 0 - typedef enum ESteamAPICallFailure - { - k_ESteamAPICallFailureNone = -1, - k_ESteamAPICallFailureSteamGone = 0, - k_ESteamAPICallFailureNetworkFailure = 1, - k_ESteamAPICallFailureInvalidHandle = 2, - k_ESteamAPICallFailureMismatchedCallback = 3, - } - - ESteamAPICallFailure; - ESteamAPICallFailure fail_why = - SteamAPI_ISteamUtils_GetAPICallFailureReason( - steam_api_classes.utils, pCallCompleted->m_hAsyncCall ); - - vg_error( "steamworks_event: error getting call result on" - "%lu (code %d)\n", - pCallCompleted->m_hAsyncCall, fail_why ); -#endif - } - - vg_free( pTmpCallResult ); + steamworks_process_api_call( pipe, &callback ); } else { diff --git a/src/vg/vg_steam_auth.h b/src/vg/vg_steam_auth.h index 1875999..8a6c400 100644 --- a/src/vg/vg_steam_auth.h +++ b/src/vg/vg_steam_auth.h @@ -114,25 +114,24 @@ static u8 vg_char_base16( char c ) static int vg_load_steam_symetric_key( const char *path, u8 *buf ) { - i64 len; - char *text_src = vg_textasset_read_s( path, &len ); + vg_linear_clear( vg_mem.scratch ); + char *src = vg_file_read( vg_mem.scratch, path ); - if( text_src ) + if( src ) { - if( len < k_nSteamEncryptedAppTicketSymmetricKeyLen ) + u32 size = vg_file_size(vg_mem.rtmemory); + if( size < k_nSteamEncryptedAppTicketSymmetricKeyLen ) { vg_error( "Application key was invalid size\n" ); - vg_free( text_src ); return 0; } for( int i=0; iflags & VG_TEXTURE_ALLOCATED_INTERNAL) ) { @@ -77,28 +77,44 @@ static inline void vg_tex2d_clamp(void) glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); } -static GLuint vg_tex2d_rgba( const char *path ) +VG_STATIC GLuint vg_tex2d_rgba( const char *path ) { - i64 length; - u8 *src_data = vg_asset_read_s( path, &length ); - GLuint texture_name; glGenTextures( 1, &texture_name ); glBindTexture( GL_TEXTURE_2D, texture_name ); - - if( src_data ) - { + + vg_linear_clear( vg_mem.scratch ); + void *file = vg_file_read( vg_mem.scratch, path ); + + if( file ) + { 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 ); - - vg_free( tex_buffer ); - vg_free( src_data ); - } - else - { + + u32 size = vg_file_size( vg_mem.scratch ); + u8 *tex_buffer = qoi_decode( file, size, &info, 4 ); + + if( tex_buffer ) + { + vg_info( "Texture decoded: [%u %u] %s\n", + info.width, info.height, path ); + + glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, info.width, info.height, + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_buffer ); + + /* TODO: pass through linear_alloc function */ + QOI_FREE(tex_buffer); + } + else + { + vg_error( "File size: %u\n", size ); + goto temp_error; + } + } + else + { +temp_error: + vg_error( "Loading texture failed (%s)\n", path ); + u32 tex_err[4] = { 0xffff00ff, @@ -107,15 +123,14 @@ static GLuint vg_tex2d_rgba( const char *path ) 0xffff00ff }; - 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 ); - } - + 0, GL_RGBA, GL_UNSIGNED_BYTE, tex_err ); + } + return texture_name; } -static void vg_tex2d_init( vg_tex2d *textures[], int num ) +VG_STATIC void vg_tex2d_init( vg_tex2d *textures[], int num ) { for( int i=0; ivao ); - glDeleteBuffers( 1, &ctx->vbo ); - glDeleteBuffers( 1, &ctx->ebo ); - - vg_free( ctx->verts ); - vg_free( ctx->indices ); -} + if( !vg_shader_compile( &_shader_ui ) ) + vg_fatal_exit_loop( "Failed to compile ui shader" ); -static int ui_init_context( ui_ctx *ctx, int index_buffer_size ) -{ - u32 vertex_buffer_size = (index_buffer_size+(index_buffer_size/2)); + /* + * Vertex buffer + * ---------------------------------------- + */ + u32 index_buffer_size = 20000, + vertex_buffer_size = (index_buffer_size+(index_buffer_size/2)); /* Generate the buffer we are gonna be drawing to */ - glGenVertexArrays( 1, &ctx->vao ); - glGenBuffers( 1, &ctx->vbo ); - glGenBuffers( 1, &ctx->ebo ); + glGenVertexArrays( 1, &vg_uictx.vao ); + glGenBuffers( 1, &vg_uictx.vbo ); + glGenBuffers( 1, &vg_uictx.ebo ); - glBindVertexArray( ctx->vao ); - glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo ); + glBindVertexArray( vg_uictx.vao ); + glBindBuffer( GL_ARRAY_BUFFER, vg_uictx.vbo ); glBufferData( GL_ARRAY_BUFFER, vertex_buffer_size * sizeof( struct ui_vert ), NULL, GL_DYNAMIC_DRAW ); - glBindVertexArray( ctx->vao ); + glBindVertexArray( vg_uictx.vao ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_uictx.ebo ); glBufferData( GL_ELEMENT_ARRAY_BUFFER, index_buffer_size * sizeof( u16 ), NULL, GL_DYNAMIC_DRAW ); @@ -234,42 +224,26 @@ static int ui_init_context( ui_ctx *ctx, int index_buffer_size ) VG_CHECK_GL_ERR(); /* Alloc RAM default context */ - ctx->verts = (struct ui_vert *)vg_alloc( - vertex_buffer_size * sizeof(struct ui_vert) ); - if( !ctx->verts ) - goto il_fail_alloc_verts; - - ctx->indices = (u16*)vg_alloc( index_buffer_size * sizeof(u16) ); - if( !ctx->indices ) - goto il_fail_alloc_indices; - - if( !ctx->colours ) - ctx->colours = &ui_default_colours; + u32 vert_size = vertex_buffer_size*sizeof(struct ui_vert), + inds_size = index_buffer_size *sizeof(u16); + + vg_uictx.vertex_buffer = + vg_create_linear_allocator(vg_mem.rtmemory,vert_size); - return 1; + vg_uictx.indice_buffer = + vg_create_linear_allocator(vg_mem.rtmemory,inds_size); - vg_free( ctx->indices ); - ctx->indices = NULL; -il_fail_alloc_indices: - vg_free( ctx->verts ); - ctx->verts = NULL; -il_fail_alloc_verts: - glDeleteBuffers( 1, &ctx->ebo ); - glDeleteBuffers( 1, &ctx->vbo ); - glDeleteVertexArrays( 1, &ctx->vao ); - VG_CHECK_GL_ERR(); - return 0; -} - -static int ui_default_init(void) -{ + /* font + * ----------------------------------------------------- + */ + /* Load default font */ u32 compressed[] = { #include "vg/vg_pxfont.h" }; u32 pixels = 0, total = 256*256, data = 0; - u8 *image = vg_alloc( total ); + u8 image[256*256]; while( pixels < total ) { @@ -289,57 +263,37 @@ static int 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 ); - - vg_free( image ); + GL_RED, GL_UNSIGNED_BYTE, image ); VG_CHECK_GL_ERR(); - vg_tex2d_clamp(); vg_tex2d_nearest(); - - if( !ui_init_context( &ui_global_ctx, 20000 ) ) goto il_generic_fail; - if( !vg_shader_compile( &_shader_ui ) ) goto il_shader_fail; - - return 1; - -il_shader_fail: -il_generic_fail: - glDeleteTextures( 1, &ui_glyph_texture ); - vg_free( image ); - return 0; -} - -static void ui_default_free(void) -{ - vg_free_shader( &_shader_ui ); - glDeleteTextures( 1, &ui_glyph_texture ); - ui_context_vg_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_rect rect, u32 colour, ui_px uv[4] ); -static void ui_draw( ui_ctx *ctx, m3x3f view_override ) +VG_STATIC void ui_draw( m3x3f view_override ) { - u32 num_indices_normal = ctx->num_indices; + u32 num_indices_normal = vg_uictx.num_indices; /* Append images to back of buffer */ - for( int i = 0; i < ctx->image_count; i ++ ) + for( int i = 0; i < vg_uictx.image_count; i ++ ) { - ui_fill_rect_uv( ctx, ctx->images[i].rc, - 0xffffffff, (ui_px[4]){0,0,128,128} ); + ui_fill_rect_uv( vg_uictx.images[i].rc, 0xffffffff, + (ui_px[4]){0,0,128,128} ); } - glBindVertexArray( ctx->vao ); + glBindVertexArray( vg_uictx.vao ); - glBindBuffer( GL_ARRAY_BUFFER, ctx->vbo ); + glBindBuffer( GL_ARRAY_BUFFER, vg_uictx.vbo ); glBufferSubData( GL_ARRAY_BUFFER, 0, - ctx->num_verts * sizeof( struct ui_vert ), ctx->verts ); + vg_uictx.num_verts * sizeof( struct ui_vert ), + vg_uictx.vertex_buffer ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, ctx->ebo ); + glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, vg_uictx.ebo ); glBufferSubData( GL_ELEMENT_ARRAY_BUFFER, 0, - ctx->num_indices * sizeof( u16 ), ctx->indices ); + vg_uictx.num_indices * sizeof( u16 ), + vg_uictx.indice_buffer ); glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); @@ -370,9 +324,9 @@ static void ui_draw( ui_ctx *ctx, m3x3f view_override ) GL_UNSIGNED_SHORT, (void*)(0) ); /* Draw image elements */ - for( int i = 0; i < ctx->image_count; i ++ ) + for( int i = 0; i < vg_uictx.image_count; i ++ ) { - struct ui_image *img = &ctx->images[i]; + struct ui_image *img = &vg_uictx.images[i]; glBindTexture( GL_TEXTURE_2D, img->image ); glDrawElements( GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, @@ -385,7 +339,7 @@ static void ui_draw( ui_ctx *ctx, m3x3f view_override ) /* * Rect controls */ -static void ui_rect_copy( ui_rect src, ui_rect dst ) +VG_STATIC void ui_rect_copy( ui_rect src, ui_rect dst ) { dst[0] = src[0]; dst[1] = src[1]; @@ -393,7 +347,7 @@ static void ui_rect_copy( ui_rect src, ui_rect dst ) dst[3] = src[3]; } -static void ui_rect_pad( ui_rect rect, ui_px pad ) +VG_STATIC void ui_rect_pad( ui_rect rect, ui_px pad ) { rect[0] += pad; rect[1] += pad; @@ -404,29 +358,29 @@ static void ui_rect_pad( ui_rect rect, ui_px pad ) /* * Stack control */ -static struct ui_qnode *ui_current( ui_ctx *ctx ) +static struct ui_qnode *ui_current(void) { - return &ctx->stack[ ctx->stack_count-1 ]; + return &vg_uictx.stack[ vg_uictx.stack_count-1 ]; } -static void ui_new_node( ui_ctx *ctx ) +VG_STATIC void ui_new_node(void) { - if( ctx->stack_count == vg_list_size( ctx->stack ) ) + if( vg_uictx.stack_count == vg_list_size( vg_uictx.stack ) ) { vg_error( "[UI] Stack overflow while creating box!" ); return; } - struct ui_qnode *parent = &ctx->stack[ ctx->stack_count-1 ]; - struct ui_qnode *node = &ctx->stack[ ctx->stack_count++ ]; - ui_rect_copy( ctx->cursor, node->rect ); + struct ui_qnode *parent = &vg_uictx.stack[ vg_uictx.stack_count-1 ]; + struct ui_qnode *node = &vg_uictx.stack[ vg_uictx.stack_count++ ]; + ui_rect_copy( vg_uictx.cursor, node->rect ); 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( vg_uictx.mouse[0] >= node->rect[0] && + vg_uictx.mouse[0] < node->rect[0]+node->rect[2] && + vg_uictx.mouse[1] >= node->rect[1] && + vg_uictx.mouse[1] < node->rect[1]+node->rect[3] ) node->mouse_over = 1; else node->mouse_over = 0; @@ -437,67 +391,67 @@ static void ui_new_node( ui_ctx *ctx ) } } -static int ui_hasmouse( ui_ctx *ctx ) +static int ui_hasmouse(void) { - struct ui_qnode *node = ui_current( ctx ); - return (node->mouse_over && (node->capture_id == ctx->capture_mouse_id)); + struct ui_qnode *node = ui_current(); + return (node->mouse_over && (node->capture_id == vg_uictx.capture_mouse_id)); } -static void ui_end( ui_ctx *ctx ) +VG_STATIC void ui_end(void) { - struct ui_qnode *node = &ctx->stack[ --ctx->stack_count ]; - ui_rect_copy( node->rect, ctx->cursor ); + struct ui_qnode *node = &vg_uictx.stack[ --vg_uictx.stack_count ]; + ui_rect_copy( node->rect, vg_uictx.cursor ); } -static void ui_end_down( ui_ctx *ctx ) +VG_STATIC void ui_end_down(void) { - ui_px height = ui_current( ctx )->rect[3]; - ui_end( ctx ); - ctx->cursor[1] += height; + ui_px height = ui_current()->rect[3]; + ui_end(); + vg_uictx.cursor[1] += height; } -static void ui_end_right( ui_ctx *ctx ) +VG_STATIC void ui_end_right(void) { - ui_px width = ui_current( ctx )->rect[2]; - ui_end( ctx ); - ctx->cursor[0] += width; + ui_px width = ui_current()->rect[2]; + ui_end(); + vg_uictx.cursor[0] += width; } -static void ui_fill_y( ui_ctx *ctx ) +VG_STATIC void ui_fill_y(void) { - struct ui_qnode *node = ui_current( ctx ); - ctx->cursor[3] = node->rect[3] - (ctx->cursor[1]-node->rect[1]); + struct ui_qnode *node = ui_current(); + vg_uictx.cursor[3] = node->rect[3] - (vg_uictx.cursor[1]-node->rect[1]); } -static void ui_fill_x( ui_ctx *ctx ) +VG_STATIC void ui_fill_x(void) { - struct ui_qnode *node = ui_current( ctx ); - ctx->cursor[2] = node->rect[2] - (ctx->cursor[0]-node->rect[0]); + struct ui_qnode *node = ui_current(); + vg_uictx.cursor[2] = node->rect[2] - (vg_uictx.cursor[0]-node->rect[0]); } -static void ui_align_bottom( ui_ctx *ctx ) +VG_STATIC void ui_align_bottom(void) { - struct ui_qnode *node = ui_current( ctx ); - ctx->cursor[1] = node->rect[1] + node->rect[3] - ctx->cursor[3]; + struct ui_qnode *node = ui_current(); + vg_uictx.cursor[1] = node->rect[1] + node->rect[3] - vg_uictx.cursor[3]; } -static void ui_align_right( ui_ctx *ctx ) +VG_STATIC void ui_align_right(void) { - struct ui_qnode *node = ui_current( ctx ); - ctx->cursor[0] = node->rect[0] + node->rect[2] - ctx->cursor[2]; + struct ui_qnode *node = ui_current(); + vg_uictx.cursor[0] = node->rect[0] + node->rect[2] - vg_uictx.cursor[2]; } -static void ui_align_top( ui_ctx *ctx ) +VG_STATIC void ui_align_top(void) { - ctx->cursor[1] = ui_current( ctx )->rect[1]; + vg_uictx.cursor[1] = ui_current()->rect[1]; } -static void ui_align_left( ui_ctx *ctx ) +VG_STATIC void ui_align_left(void) { - ctx->cursor[0] = ui_current( ctx )->rect[0]; + vg_uictx.cursor[0] = ui_current()->rect[0]; } -static void ui_clamp_rect( ui_rect parent, ui_rect dest ) +VG_STATIC void ui_clamp_rect( ui_rect parent, ui_rect dest ) { dest[0] = vg_min( parent[0] + parent[2] - dest[2], dest[0] ); dest[1] = vg_min( parent[1] + parent[3] - dest[3], dest[1] ); @@ -505,42 +459,43 @@ static void ui_clamp_rect( ui_rect parent, ui_rect dest ) dest[1] = vg_max( parent[1], dest[1] ); } -static void ui_capture_mouse( ui_ctx *ctx, u32 id ) +VG_STATIC void ui_capture_mouse( u32 id ) { - struct ui_qnode *node = &ctx->stack[ ctx->stack_count-1 ]; + struct ui_qnode *node = &vg_uictx.stack[ vg_uictx.stack_count-1 ]; node->capture_id = id; - if( !ctx->capture_lock && node->mouse_over ) + if( !vg_uictx.capture_lock && node->mouse_over ) { - ctx->capture_mouse_id = id; + vg_uictx.capture_mouse_id = id; } } -static int ui_want_mouse( ui_ctx *ctx ) +static int ui_want_mouse(void) { - return ctx->capture_mouse_id == 0? 0: 1; + return vg_uictx.capture_mouse_id == 0? 0: 1; } -static void ui_set_clip( ui_ctx *ctx, ui_rect clip ) +VG_STATIC void ui_set_clip( ui_rect clip ) { - ctx->clipping[0] = clip[0]; - ctx->clipping[1] = clip[1]; - ctx->clipping[2] = clip[0] + clip[2]; - ctx->clipping[3] = clip[1] + clip[3]; + vg_uictx.clipping[0] = clip[0]; + vg_uictx.clipping[1] = clip[1]; + vg_uictx.clipping[2] = clip[0] + clip[2]; + vg_uictx.clipping[3] = clip[1] + clip[3]; } -static void ui_release_clip( ui_ctx *ctx ) +VG_STATIC void ui_release_clip(void) { - ctx->clipping[0] = -32000; - ctx->clipping[1] = -32000; - ctx->clipping[2] = 32000; - ctx->clipping[3] = 32000; + vg_uictx.clipping[0] = -32000; + vg_uictx.clipping[1] = -32000; + vg_uictx.clipping[2] = 32000; + vg_uictx.clipping[3] = 32000; } -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_rect rect, u32 colour, ui_px uv[4] ) { - struct ui_vert *vertices = &ctx->verts[ ctx->num_verts ]; + struct ui_vert *vertices = &vg_uictx.vertex_buffer[ vg_uictx.num_verts ]; + u16 *indices = &vg_uictx.indice_buffer[ vg_uictx.num_indices ]; + vertices[0].co[0] = rect[0]; vertices[0].co[1] = rect[1]; vertices[0].uv[0] = uv[0]; @@ -561,13 +516,12 @@ static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, vertices[3].uv[0] = uv[0]; vertices[3].uv[1] = uv[3]; vertices[3].colour = colour; - u16 ind_start = ctx->num_verts; - u16 *indices = &ctx->indices[ ctx->num_indices ]; + u16 ind_start = vg_uictx.num_verts; - ui_rect_copy( ctx->clipping, vertices[0].clip ); - ui_rect_copy( ctx->clipping, vertices[1].clip ); - ui_rect_copy( ctx->clipping, vertices[2].clip ); - ui_rect_copy( ctx->clipping, vertices[3].clip ); + ui_rect_copy( vg_uictx.clipping, vertices[0].clip ); + ui_rect_copy( vg_uictx.clipping, vertices[1].clip ); + ui_rect_copy( vg_uictx.clipping, vertices[2].clip ); + ui_rect_copy( vg_uictx.clipping, vertices[3].clip ); indices[0] = ind_start+0; indices[1] = ind_start+2; @@ -577,15 +531,15 @@ static struct ui_vert *ui_fill_rect_uv( ui_ctx *ctx, ui_rect rect, indices[4] = ind_start+3; indices[5] = ind_start+2; - ctx->num_indices += 6; - ctx->num_verts += 4; + vg_uictx.num_indices += 6; + vg_uictx.num_verts += 4; return vertices; } -static struct ui_vert *ui_fill_rect( ui_ctx *ctx, ui_rect rect, u32 colour ) +static struct ui_vert *ui_fill_rect( ui_rect rect, u32 colour ) { - return ui_fill_rect_uv( ctx, rect, colour, (ui_px[4]){ 4,4, 4,4 } ); + return ui_fill_rect_uv( rect, colour, (ui_px[4]){ 4,4, 4,4 } ); } static ui_px ui_text_line_offset( const char *str, ui_px scale, @@ -610,7 +564,7 @@ static ui_px ui_text_line_offset( const char *str, ui_px scale, return (-length * scale*8) / 2; } -static void ui_text( ui_ctx *ctx, ui_px pos[2], +VG_STATIC void ui_text( ui_px pos[2], const char *str, ui_px scale, enum ui_text_align align ) { ui_rect text_cursor; @@ -642,7 +596,7 @@ static void ui_text( ui_ctx *ctx, ui_px pos[2], glyph_base[0] *= 8; glyph_base[1] *= 8; - ui_fill_rect_uv( ctx, text_cursor, current_colour, (ui_px[4]) + ui_fill_rect_uv( text_cursor, current_colour, (ui_px[4]) { glyph_base[0]+2, glyph_base[1]+1, @@ -701,49 +655,49 @@ static void ui_text( ui_ctx *ctx, ui_px pos[2], /* * API Control */ -static void ui_begin( ui_ctx *ctx, ui_px res_x, ui_px res_y ) +VG_STATIC void ui_begin( ui_px res_x, ui_px res_y ) { - ctx->cursor[0] = 0; - ctx->cursor[1] = 0; - ctx->cursor[2] = res_x; - ctx->cursor[3] = res_y; + vg_uictx.cursor[0] = 0; + vg_uictx.cursor[1] = 0; + vg_uictx.cursor[2] = res_x; + vg_uictx.cursor[3] = res_y; - ui_rect_copy( ctx->cursor, ctx->stack[0].rect ); - ctx->stack[0].mouse_over = 1; + ui_rect_copy( vg_uictx.cursor, vg_uictx.stack[0].rect ); + vg_uictx.stack[0].mouse_over = 1; - ctx->stack_count = 1; + vg_uictx.stack_count = 1; - ctx->num_verts = 0; - ctx->num_indices = 0; + vg_uictx.num_verts = 0; + vg_uictx.num_indices = 0; - ui_release_clip( ctx ); + ui_release_clip(); - if( ctx->click_state == 0 ) - ctx->capture_mouse_id = 0; + if( vg_uictx.click_state == 0 ) + vg_uictx.capture_mouse_id = 0; - ctx->image_count = 0; + vg_uictx.image_count = 0; } -static void ui_resolve( ui_ctx *ctx ) +VG_STATIC void ui_resolve(void) { - if( ctx->stack_count-1 ) + if( vg_uictx.stack_count-1 ) { vg_error( "[UI] Mismatched node create/drestroy!" ); return; } - if( ctx->click_state == 3 || ctx->click_state == 0 ) + if( vg_uictx.click_state == 3 || vg_uictx.click_state == 0 ) { - ctx->capture_lock = 0; + vg_uictx.capture_lock = 0; } } -static void ui_set_mouse( ui_ctx *ctx, int x, int y, int click_state ) +VG_STATIC void ui_set_mouse( int x, int y, int click_state ) { - ctx->mouse[0] = x; - ctx->mouse[1] = y; + vg_uictx.mouse[0] = x; + vg_uictx.mouse[1] = y; - ctx->click_state = click_state; + vg_uictx.click_state = click_state; } /* @@ -766,87 +720,87 @@ enum button_state k_button_hold }; -static int ui_button( ui_ctx *ctx ) +static int ui_button(void) { - u32 uid = ctx->control_id ++; + u32 uid = vg_uictx.control_id ++; - ui_new_node( ctx ); + ui_new_node(); { - ui_capture_mouse( ctx, uid ); + ui_capture_mouse( uid ); - if( ui_hasmouse(ctx) ) + if( ui_hasmouse() ) { - ui_fill_rect( ctx, ctx->cursor, ctx->colours->hover ); + ui_fill_rect( vg_uictx.cursor, vg_uictx.colours->hover ); - if( ctx->click_state == 1 ) + if( vg_uictx.click_state == 1 ) { - ctx->capture_lock = 1; + vg_uictx.capture_lock = 1; return k_button_start_click; } - else if( ctx->capture_lock && ctx->click_state == 3 ) + else if( vg_uictx.capture_lock && vg_uictx.click_state == 3 ) return k_button_click; - else if( ctx->capture_lock && ctx->click_state == 2 ) + else if( vg_uictx.capture_lock && vg_uictx.click_state == 2 ) return k_button_hold; return k_button_click; } else - ui_fill_rect( ctx, ctx->cursor, ctx->colours->main ); + ui_fill_rect( vg_uictx.cursor, vg_uictx.colours->main ); } return k_button_released; } -static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) +static int ui_window( struct ui_window *window, u32 control_group ) { if( window->drag ) { - window->transform[0] = ctx->mouse[0]+window->drag_offset[0]; - window->transform[1] = ctx->mouse[1]+window->drag_offset[1]; + window->transform[0] = vg_uictx.mouse[0]+window->drag_offset[0]; + window->transform[1] = vg_uictx.mouse[1]+window->drag_offset[1]; - ui_clamp_rect( ctx->stack[0].rect, window->transform ); + ui_clamp_rect( vg_uictx.stack[0].rect, window->transform ); - if( ctx->click_state == 0 || ctx->click_state == 3 ) + if( vg_uictx.click_state == 0 || vg_uictx.click_state == 3 ) { window->drag = 0; } } - ui_rect_copy( window->transform, ctx->cursor ); + ui_rect_copy( window->transform, vg_uictx.cursor ); - ui_new_node( ctx ); + ui_new_node(); { - ui_capture_mouse( ctx, ctx->control_id ++ ); + ui_capture_mouse( vg_uictx.control_id ++ ); /* Drag bar */ - ctx->cursor[3] = 25; - ui_new_node( ctx ); + vg_uictx.cursor[3] = 25; + ui_new_node(); { - ui_capture_mouse( ctx, ctx->control_id ++ ); + ui_capture_mouse( vg_uictx.control_id ++ ); - struct ui_vert *drag_bar = ui_fill_rect( ctx, ctx->cursor, 0xff555555 ); + struct ui_vert *drag_bar = ui_fill_rect( vg_uictx.cursor, 0xff555555 ); /* title.. */ - ctx->cursor[0] += 2; - ctx->cursor[1] += 2; - ui_text( ctx, ctx->cursor, window->title, 2, 0 ); + vg_uictx.cursor[0] += 2; + vg_uictx.cursor[1] += 2; + ui_text( vg_uictx.cursor, window->title, 2, 0 ); /* Close button */ - ctx->cursor[3] = 25; - ctx->cursor[2] = 25; - ui_align_right( ctx ); - ui_align_top( ctx ); - ui_rect_pad( ctx->cursor, 4 ); + vg_uictx.cursor[3] = 25; + vg_uictx.cursor[2] = 25; + ui_align_right(); + ui_align_top(); + ui_rect_pad( vg_uictx.cursor, 4 ); - if( ui_button( ctx ) ) + if( ui_button() ) { vg_info( "Click clacked\n" ); } - ctx->cursor[0] += 2; - ui_text( ctx, ctx->cursor, "x", 2, 0 ); - ui_end( ctx ); + vg_uictx.cursor[0] += 2; + ui_text( vg_uictx.cursor, "x", 2, 0 ); + ui_end(); - if( ui_hasmouse( ctx ) ) + if( ui_hasmouse() ) { drag_bar[0].colour = 0xff777777; drag_bar[1].colour = 0xff777777; @@ -854,23 +808,23 @@ static int ui_window( ui_ctx *ctx, struct ui_window *window, u32 control_group ) drag_bar[3].colour = 0xff777777; /* start drag */ - if( ctx->click_state == 1 ) + if( vg_uictx.click_state == 1 ) { window->drag = 1; - window->drag_offset[0] = window->transform[0]-ctx->mouse[0]; - window->drag_offset[1] = window->transform[1]-ctx->mouse[1]; + window->drag_offset[0] = window->transform[0]-vg_uictx.mouse[0]; + window->drag_offset[1] = window->transform[1]-vg_uictx.mouse[1]; } } } - ui_end_down( ctx ); + ui_end_down(); } return 1; } -static void ui_push_image( ui_ctx *ctx, ui_rect rc, GLuint image ) +VG_STATIC void ui_push_image( ui_rect rc, GLuint image ) { - struct ui_image *img = &ctx->images[ ctx->image_count ++ ]; + struct ui_image *img = &vg_uictx.images[ vg_uictx.image_count ++ ]; ui_rect_copy( rc, img->rc ); img->image = image; } @@ -893,99 +847,69 @@ struct ui_checkbox int *data; }; -static void ui_slider( ui_ctx *ctx, struct ui_slider *slider ) +VG_STATIC void ui_slider( struct ui_slider *slider ) { - ui_new_node( ctx ); + ui_new_node(); - ui_px slider_start = ctx->cursor[0]; + ui_px slider_start = vg_uictx.cursor[0]; - float const ftotal = ctx->cursor[2], + float const ftotal = vg_uictx.cursor[2], fwidth = ftotal*0.25f, fmove = ftotal - fwidth, fstart = fwidth*0.5f, frange = slider->max-slider->min, fpos = (*slider->data - slider->min) / frange; - ui_fill_rect( ctx, ctx->cursor, 0xff111111 ); - ctx->cursor[2] = fwidth; - ctx->cursor[0] = slider_start + fpos * fmove; + ui_fill_rect( vg_uictx.cursor, 0xff111111 ); + vg_uictx.cursor[2] = fwidth; + vg_uictx.cursor[0] = slider_start + fpos * fmove; - u32 uid = ctx->control_id ++; - int status = ui_button( ctx ); - if( ctx->capture_lock && (ctx->capture_mouse_id == uid)) + u32 uid = vg_uictx.control_id ++; + int status = ui_button(); + if( vg_uictx.capture_lock && (vg_uictx.capture_mouse_id == uid)) { - float ui_new = ctx->mouse[0], + float ui_new = vg_uictx.mouse[0], local = ui_new - (slider_start + fstart), zo = vg_clampf(local / fmove,0.0f,1.0f); *slider->data = vg_lerpf( slider->min, slider->max, zo ); } - ctx->cursor[0] += 4; - ctx->cursor[1] += 4; + vg_uictx.cursor[0] += 4; + vg_uictx.cursor[1] += 4; char buf[12]; snprintf( buf, 12, "%.2f", *slider->data ); - ui_text( ctx, ctx->cursor, buf, 1, 0 ); - ui_end_down( ctx ); - ui_end_down( ctx ); + ui_text( vg_uictx.cursor, buf, 1, 0 ); + ui_end_down(); + ui_end_down(); } -static void ui_slider_vector( ui_ctx *ctx, struct ui_slider_vector *slider ) +VG_STATIC void ui_slider_vector( struct ui_slider_vector *slider ) { for( int i=0; ilen; i++ ) { slider->sub[i].data = &slider->data[i]; slider->sub[i].min = slider->min; slider->sub[i].max = slider->max; - ui_slider( ctx, &slider->sub[i] ); + ui_slider( &slider->sub[i] ); } } -static void ui_checkbox( ui_ctx *ctx, struct ui_checkbox *cb ) +VG_STATIC void ui_checkbox( struct ui_checkbox *cb ) { - if( ui_button(ctx) == k_button_click ) + if( ui_button() == k_button_click ) *cb->data ^= 0x1; - ui_new_node(ctx); - ui_rect_pad( ctx->cursor, 4 ); + ui_new_node(); + ui_rect_pad( vg_uictx.cursor, 4 ); if( *cb->data ) - ui_fill_rect( ctx, ctx->cursor, 0xff00e052 ); + ui_fill_rect( vg_uictx.cursor, 0xff00e052 ); else - ui_fill_rect( ctx, ctx->cursor, 0xff0052e0 ); - - ui_end(ctx); - ui_end_down(ctx); -} - -/* 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 ) -#define gui_hasmouse(...) ui_hasmouse( &ui_global_ctx, __VA_ARGS__) -#define gui_end() ui_end( &ui_global_ctx ) -#define gui_end_down() ui_end_down( &ui_global_ctx ) -#define gui_end_right() ui_end_right( &ui_global_ctx ) -#define gui_fill_y() ui_fill_y( &ui_global_ctx) -#define gui_fill_x() ui_fill_x( &ui_global_ctx) -#define gui_align_bottom() ui_align_bottom( &ui_global_ctx ) -#define gui_align_right() ui_align_right( &ui_global_ctx ) -#define gui_align_top() ui_align_top( &ui_global_ctx ) -#define gui_align_left() ui_align_left( &ui_global_ctx ) -#define gui_clamp_rect(...) ui_clamp_rect( &ui_global_ctx, __VA_ARGS__) -#define gui_capture_mouse(...) ui_capture_mouse( &ui_global_ctx, __VA_ARGS__) -#define gui_set_clip(...) ui_set_clip( &ui_global_ctx, __VA_ARGS__) -#define gui_release_clip() ui_release_clip( &ui_global_ctx ) -#define gui_fill_rect_uv(...) ui_fill_rect_uv( &ui_global_ctx, __VA_ARGS__) -#define gui_fill_rect(...) ui_fill_rect( &ui_global_ctx, __VA_ARGS__) -#define gui_text(...) ui_text( &ui_global_ctx, __VA_ARGS__) -#define gui_begin(...) ui_begin( &ui_global_ctx, __VA_ARGS__) -#define gui_resolve(...) ui_resolve( &ui_global_ctx, __VA_ARGS__) -#define gui_set_mouse(...) ui_set_mouse( &ui_global_ctx, __VA_ARGS__) -#define gui_button(...) ui_button( &ui_global_ctx, __VA_ARGS__) -#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_reset_colours(...) ui_reset_colours( &ui_global_ctx ) + ui_fill_rect( vg_uictx.cursor, 0xff0052e0 ); + + ui_end(); + ui_end_down(); +} #endif /* VG_UI_H */