X-Git-Url: https://harrygodden.com/git/?a=blobdiff_plain;f=vg.h;h=04578f7c81a5c7c53bef107079a6642cb180cf36;hb=7a1b2ba3172f9f296df110997e5b8cca5f10ab68;hp=2eb8e7a4e7bd2a7c39110e60980a7a6cf1dfa291;hpb=6d772a1e69860f4ae3e838e8c5c164754e6533ae;p=vg.git diff --git a/vg.h b/vg.h index 2eb8e7a..04578f7 100644 --- a/vg.h +++ b/vg.h @@ -26,17 +26,17 @@ |IMP| | |.------------- vg_start(void) ---------------' | | | | | | | v -|IMP| | vg_update(void) +|IMP| | vg_pre_update(void) | | | | | | | .-----+. -| | | | | +| | | | | called 0x to 8x | | | | v -|IMP| | '- vg_update_fixed(void) +|IMP| | '- vg_fixed_update(void) | | | | | | | .-' | | | | | | | v -|IMP| | vg_update_post(void) +|IMP| | vg_post_update(void) | | | | | | | v |IMP| | vg_render(void) @@ -52,41 +52,14 @@ #ifndef VG_HEADER_H #define VG_HEADER_H + const char *vg_get_basepath(void); + #include "vg_platform.h" #include "vg_mem.h" - - #ifndef _WIN32 - #include - #endif - -VG_STATIC void vg_print_backtrace(void) -{ -#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 +#endif //#define VG_SYNC_DEBUG #ifdef VG_SYNC_DEBUG @@ -106,40 +80,39 @@ VG_STATIC void vg_print_backtrace(void) #endif /* API */ -VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name ); +static void vg_enter( int argc, char *argv[], const char *window_name ); /* Thread 1 */ -VG_STATIC void vg_preload(void); -VG_STATIC void vg_load(void); +static void vg_preload(void); +static void vg_load(void); /* Main thread */ -VG_STATIC void vg_launch_opt(void); -VG_STATIC void vg_start(void); +static void vg_launch_opt(void); +static void vg_start(void); -VG_STATIC void vg_framebuffer_resize(int w, int h); -VG_STATIC void vg_update(void); -VG_STATIC void vg_update_fixed(void); -VG_STATIC void vg_update_post(void); +static void vg_framebuffer_resize(int w, int h); +static void vg_pre_update(void); +static void vg_fixed_update(void); +static void vg_post_update(void); -VG_STATIC void vg_render(void); -VG_STATIC void vg_ui(void); +static void vg_render(void); +static void vg_gui(void); -struct vg -{ +struct vg{ /* Engine sync */ SDL_Window *window; SDL_GLContext gl_context; + const char *base_path; SDL_sem *sem_loader; /* allows only one loader at a time */ - jmp_buf env_loader_exit; SDL_threadID thread_id_main, thread_id_loader; + void *thread_data; SDL_SpinLock sl_status; - enum engine_status - { + enum engine_status{ k_engine_status_none, k_engine_status_load_internal, k_engine_status_running, @@ -154,10 +127,10 @@ struct vg window_should_close; int display_refresh_rate, - fps_limit; /* 0: use vsync, >0: cap fps to this, no vsync */ + fps_limit, + vsync; - enum vsync_feature - { + enum vsync_feature{ k_vsync_feature_disabled=0, k_vsync_feature_enabled=1, k_vsync_feature_enabled_adaptive=2, @@ -171,6 +144,7 @@ struct vg /* Runtime */ double time, + time_real, time_delta, time_rate, @@ -182,8 +156,7 @@ struct vg int fixed_iterations; - enum engine_stage - { + enum engine_stage{ k_engine_stage_none, k_engine_stage_update, k_engine_stage_update_fixed, @@ -194,16 +167,21 @@ struct vg /* graphics */ m4x4f pv; - enum quality_profile - { + enum quality_profile{ k_quality_profile_high = 0, k_quality_profile_low = 1, } quality_profile; float loader_ring; + GLuint tex_missing; + + vg_rand rand; +} +static vg = { .time_rate = 1.0 }; +const char *vg_get_basepath(void){ + return vg.base_path; } -VG_STATIC vg = { .time_rate = 1.0 }; enum vg_thread_purpose { @@ -214,7 +192,7 @@ enum vg_thread_purpose #include "vg_async.h" -VG_STATIC enum engine_status _vg_engine_status(void) +static enum engine_status _vg_engine_status(void) { SDL_AtomicLock( &vg.sl_status ); enum engine_status status = vg.engine_status; @@ -223,7 +201,7 @@ VG_STATIC enum engine_status _vg_engine_status(void) return status; } -VG_STATIC enum vg_thread_purpose vg_thread_purpose(void) +static enum vg_thread_purpose vg_thread_purpose(void) { SDL_AtomicLock( &vg.sl_status ); @@ -237,32 +215,42 @@ VG_STATIC enum vg_thread_purpose vg_thread_purpose(void) } } -VG_STATIC void _vg_opengl_sync_init(void) +static void vg_assert_thread( enum vg_thread_purpose required ){ + enum vg_thread_purpose purpose = vg_thread_purpose(); + + if( purpose != required ){ + vg_fatal_error( "thread_purpose must be %u not %u\n", required, purpose ); + } +} + +static void _vg_opengl_sync_init(void) { vg.sem_loader = SDL_CreateSemaphore(1); } -VG_STATIC void vg_checkgl( const char *src_info ); +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__) ) #include "vg_console.h" #include "vg_profiler.h" -#include "vg_audio.h" +#ifndef VG_NO_AUDIO + #include "vg_audio.h" +#endif #include "vg_shader.h" #include "vg_tex.h" #include "vg_input.h" -#include "vg_ui.h" +#include "vg_imgui.h" #include "vg_lines.h" #include "vg_loader.h" #include "vg_opt.h" /* Diagnostic */ -VG_STATIC struct vg_profile vg_prof_update = {.name="update()"}, +static struct vg_profile vg_prof_update = {.name="update()"}, vg_prof_render = {.name="render()"}, vg_prof_swap = {.name="swap"}; -VG_STATIC void vg_checkgl( const char *src_info ) +static void vg_checkgl( const char *src_info ) { int fail = 0; @@ -276,12 +264,12 @@ VG_STATIC void vg_checkgl( const char *src_info ) vg_fatal_error( "OpenGL Error" ); } -VG_STATIC void async_vg_bake_shaders( void *payload, u32 size ) +static void async_vg_bake_shaders( void *payload, u32 size ) { vg_shaders_compile(); } -VG_STATIC void vg_bake_shaders(void) +static void vg_bake_shaders(void) { vg_console_reg_cmd( "reload_shaders", vg_shaders_live_recompile, NULL ); vg_async_call( async_vg_bake_shaders, NULL, 0 ); @@ -303,32 +291,66 @@ void async_internal_complete( void *payload, u32 size ) SDL_AtomicUnlock( &vg.sl_status ); } -VG_STATIC void _vg_load_full(void) +static void _vg_load_full( void *data ) { vg_preload(); /* internal */ + vg_tex2d_replace_with_error( &vg.tex_missing ); vg_loader_step( vg_input_init, vg_input_free ); vg_loader_step( vg_lines_init, NULL ); +#ifndef VG_NO_AUDIO vg_loader_step( vg_audio_init, vg_audio_free ); +#endif vg_loader_step( vg_profiler_init, NULL ); vg_async_call( async_internal_complete, NULL, 0 ); /* client */ vg_load(); + + vg_success( "Client loaded in %fs\n", vg.time_real ); } -VG_STATIC void _vg_process_events(void) +static void _vg_process_events(void) { v2_zero( vg.mouse_wheel ); v2_zero( vg.mouse_delta ); + /* Update input */ + vg_process_inputs(); + /* SDL event loop */ SDL_Event event; while( SDL_PollEvent( &event ) ){ if( event.type == SDL_KEYDOWN ){ - console_proc_key( event.key.keysym ); + if( vg_console.enabled && + (vg_ui.focused_control_type != k_ui_control_modal) ){ + if( event.key.keysym.sym == SDLK_ESCAPE || + event.key.keysym.scancode == SDL_SCANCODE_GRAVE ){ + vg_console.enabled = 0; + ui_defocus_all(); + } + else if( (event.key.keysym.mod & KMOD_CTRL) && + event.key.keysym.sym == SDLK_n ){ + _console_suggest_next(); + } + else if( (event.key.keysym.mod & KMOD_CTRL ) && + event.key.keysym.sym == SDLK_p ){ + _console_suggest_prev(); + } + else{ + _ui_proc_key( event.key.keysym ); + } + } + else{ + if( event.key.keysym.scancode == SDL_SCANCODE_GRAVE ){ + vg_console.enabled = 1; + } + else { + _ui_proc_key( event.key.keysym ); + } + } } else if( event.type == SDL_MOUSEWHEEL ){ vg.mouse_wheel[0] += event.wheel.preciseX; @@ -370,23 +392,20 @@ VG_STATIC void _vg_process_events(void) } } else if( event.type == SDL_TEXTINPUT ){ - console_proc_utf8( event.text.text ); + ui_proc_utf8( event.text.text ); } } vg.mouse_pos[0] += vg.mouse_delta[0]; vg.mouse_pos[1] += vg.mouse_delta[1]; - - /* Update input */ - vg_process_inputs(); } -VG_STATIC void _vg_gameloop_update(void) +static void _vg_gameloop_update(void) { vg_profile_begin( &vg_prof_update ); vg.engine_stage = k_engine_stage_update; - vg_update(); + vg_pre_update(); /* Fixed update loop */ vg.engine_stage = k_engine_stage_update_fixed; @@ -396,11 +415,9 @@ VG_STATIC void _vg_gameloop_update(void) vg.time_fixed_accumulator += vg.time_delta; while( vg.time_fixed_accumulator >= VG_TIMESTEP_FIXED ){ - vg_update_fixed(); + vg_fixed_update(); vg_lines.allow_input = 0; - vg.time_fixed_accumulator -= VG_TIMESTEP_FIXED; - //vg.accumulator = VG_MAX( 0.0, vg.accumulator ); vg.fixed_iterations ++; if( vg.fixed_iterations == 8 ){ @@ -411,11 +428,11 @@ VG_STATIC void _vg_gameloop_update(void) vg.time_fixed_extrapolate = vg.time_fixed_accumulator / VG_TIMESTEP_FIXED; vg.engine_stage = k_engine_stage_update; - vg_update_post(); + vg_post_update(); vg_profile_end( &vg_prof_update ); } -VG_STATIC void _vg_gameloop_render(void) +static void _vg_gameloop_render(void) { vg_profile_begin( &vg_prof_render ); @@ -426,24 +443,30 @@ VG_STATIC void _vg_gameloop_render(void) /* ui */ vg.engine_stage = k_engine_stage_ui; { - ui_begin( vg.window_x, vg.window_y ); + ui_prerender(); + if( vg_console.enabled ){ + vg_ui.ignore_input_frames = 10; + vg_gui(); + vg_ui.ignore_input_frames = 0; + vg_ui.wants_mouse = 1; + _vg_console_draw(); + } + else vg_gui(); - /* TODO */ - ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 ); + /* vg tools */ +#ifndef VG_NO_AUDIO + audio_debug_ui( vg.pv ); +#endif + /* profiling */ int frame_target = vg.display_refresh_rate; - - if( vg.fps_limit > 0 ){ - frame_target = vg.fps_limit; - } - + if( vg.fps_limit > 0 ) frame_target = vg.fps_limit; vg_profile_drawn( (struct vg_profile *[]){ &vg_prof_update,&vg_prof_render,&vg_prof_swap}, 3, (1.0f/(float)frame_target)*1000.0f, (ui_rect){ 4, 4, 250, 0 }, 0 ); - if( vg_profiler ){ char perf[256]; @@ -453,48 +476,37 @@ VG_STATIC void _vg_gameloop_render(void) "samples: %d\n" "iterations: %d (acc: %.3fms%%)\n" "time: real(%.2f) delta(%.2f) rate(%.2f)\n" -#ifdef _WIN32 - " extrap(%.2f) frame(%.2f) spin( %llu )\n", -#else - " extrap(%.2f) frame(%.2f) spin( %lu )\n", -#endif + " extrap(%.2f) frame(%.2f) spin( "PRINTF_U64" )\n", vg.window_x, vg.window_y, frame_target, (1.0f/(float)frame_target)*1000.0f, vg.samples, vg.fixed_iterations, (vg.time_fixed_accumulator/VG_TIMESTEP_FIXED)*100.0f, - vg.time, vg.time_delta, vg.time_rate, + vg.time_real, vg.time_delta, vg.time_rate, vg.time_fixed_extrapolate, vg.time_frame_delta, vg.time_spinning ); - ui_text( (ui_rect){258, 4+24+12+12,0,0},perf, 1,0); + ui_text( (ui_rect){258, 4+24+12+12,900,900},perf,1,0,k_ui_align_left); } - - /* FIXME */ - audio_debug_ui( vg.pv ); - vg_ui(); - _vg_console_draw(); - - ui_resolve(); - ui_draw( NULL ); + ui_postrender(); } vg_profile_end( &vg_prof_render ); } -VG_STATIC int vg_framefilter( double dt ) -{ - if( (vg.fps_limit <= 0) && (vg.vsync_feature != k_vsync_feature_error) ){ +static void vg_changevsync(void){ + if( vg.vsync && (vg.vsync_feature != k_vsync_feature_error) ){ /* turn on vsync if not enabled */ enum vsync_feature requested = k_vsync_feature_enabled; - if( vg.fps_limit < 0 ) requested = k_vsync_feature_enabled_adaptive; + if( vg.vsync < 0 ) requested = k_vsync_feature_enabled_adaptive; if( vg.vsync_feature != requested ){ vg_info( "Setting swap interval\n" ); int swap_interval = 1; - if( requested == k_vsync_feature_enabled_adaptive ) swap_interval = -1; + if( requested == k_vsync_feature_enabled_adaptive ) + swap_interval = -1; if( SDL_GL_SetSwapInterval( swap_interval ) == -1 ){ if( requested == k_vsync_feature_enabled ){ @@ -507,25 +519,24 @@ VG_STATIC int vg_framefilter( double dt ) } vg.vsync_feature = k_vsync_feature_error; - vg.fps_limit = vg.display_refresh_rate; - + vg.vsync = 0; /* TODO: Make popup to notify user that this happened */ - return 1; } else{ vg_success( "Vsync enabled (%d)\n", requested ); vg.vsync_feature = requested; } } - - return 0; } - - if( vg.vsync_feature != k_vsync_feature_disabled ){ - SDL_GL_SetSwapInterval( 0 ); - vg.vsync_feature = k_vsync_feature_disabled; + else { + if( vg.vsync_feature != k_vsync_feature_disabled ){ + SDL_GL_SetSwapInterval( 0 ); + vg.vsync_feature = k_vsync_feature_disabled; + } } - +} + +static int vg_framefilter( double dt ){ if( vg.fps_limit < 25 ) vg.fps_limit = 25; if( vg.fps_limit > 300 ) vg.fps_limit = 300; @@ -536,7 +547,10 @@ VG_STATIC int vg_framefilter( double dt ) u32 ms = (u32)floor( sleep_ms ); if( ms ){ - SDL_Delay( ms ); + if( !vg_loader_availible() ) + SDL_Delay(1); + else + SDL_Delay(ms); } else{ vg.time_spinning ++; @@ -548,7 +562,7 @@ VG_STATIC int vg_framefilter( double dt ) return 0; } -VG_STATIC int _vg_crashscreen(void) +static int _vg_crashscreen(void) { #if 0 if( vg_getkey( SDLK_ESCAPE ) ) @@ -561,7 +575,7 @@ VG_STATIC int _vg_crashscreen(void) glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA); glBlendEquation(GL_FUNC_ADD); - glClearColor( 0.15f + sinf(vg.time)*0.1f, 0.0f, 0.0f,1.0f ); + glClearColor( 0.15f + sinf(vg.time_real)*0.1f, 0.0f, 0.0f,1.0f ); glClear( GL_COLOR_BUFFER_BIT ); glViewport( 0,0, vg.window_x, vg.window_y ); @@ -570,8 +584,7 @@ VG_STATIC int _vg_crashscreen(void) return 0; } -VG_STATIC void _vg_gameloop(void) -{ +static void _vg_gameloop(void){ //vg.time_fixed_accumulator = 0.75f * (1.0f/60.0f); vg.time_hp = SDL_GetPerformanceCounter(); @@ -579,7 +592,6 @@ VG_STATIC void _vg_gameloop(void) int post_start = 0; while(1){ - vg.time_hp = SDL_GetPerformanceCounter(); u64 udt = vg.time_hp - vg.time_hp_last; vg.time_hp_last = vg.time_hp; @@ -587,20 +599,23 @@ VG_STATIC void _vg_gameloop(void) double dt = (double)udt / (double)SDL_GetPerformanceFrequency(); vg.time_frame_delta += dt; + vg_run_async_checked(); if( vg_framefilter( dt ) ) continue; + vg_changevsync(); + vg_profile_begin( &vg_prof_swap ); SDL_GL_SwapWindow( vg.window ); vg_profile_end( &vg_prof_swap ); enum engine_status status = _vg_engine_status(); + vg.time_real += vg.time_frame_delta; vg.time_delta = vg.time_frame_delta * vg.time_rate; vg.time += vg.time_delta; - vg_run_async_checked(); _vg_process_events(); if( vg.window_should_close ) @@ -621,8 +636,8 @@ VG_STATIC void _vg_gameloop(void) } if( vg.loader_ring > 0.01f ){ - vg.loader_ring -= vg.time_frame_delta * 0.5f; _vg_loader_render_ring( vg.loader_ring ); + vg.loader_ring -= vg.time_frame_delta * 0.5f; } vg.time_frame_delta = 0.0; @@ -630,7 +645,7 @@ VG_STATIC void _vg_gameloop(void) } } -VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] ) +static void _vg_process_launch_opts_internal( int argc, char *argv[] ) { char *arg; while( vg_argp( argc, argv ) ){ @@ -658,7 +673,7 @@ VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] ) } } -VG_STATIC void _vg_init_window( const char *window_name ) +static void _vg_init_window( const char *window_name ) { vg_info( "SDL_INIT\n" ); @@ -667,9 +682,20 @@ VG_STATIC void _vg_init_window( const char *window_name ) exit(0); } +#ifndef VG_NO_AUDIO SDL_InitSubSystem( SDL_INIT_AUDIO ); +#endif SDL_InitSubSystem( SDL_INIT_GAMECONTROLLER ); + char *exe_basepath = SDL_GetBasePath(); + u32 len = vg_align8( strlen(exe_basepath)+1 ); + char *dest = vg_linear_alloc( vg_mem.rtmemory, len ); + strcpy( dest, exe_basepath ); + SDL_free( exe_basepath ); + vg.base_path = dest; + + vg_info( "Basepath: %s\n", vg.base_path ); + SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 3 ); SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 3 ); @@ -693,11 +719,6 @@ VG_STATIC void _vg_init_window( const char *window_name ) display_index = 0, mode_index = 0; -#ifdef VG_DEVWINDOW - vg.window_x = 1600; - vg.window_y = 1000; -#else - SDL_DisplayMode video_mode; if( SDL_GetDesktopDisplayMode( display_index, &video_mode ) ){ vg_error( "SDL_GetDesktopDisplayMode failed: %s\n", SDL_GetError() ); @@ -708,6 +729,10 @@ VG_STATIC void _vg_init_window( const char *window_name ) vg.display_refresh_rate = video_mode.refresh_rate; vg.window_x = video_mode.w; vg.window_y = video_mode.h; + +#ifdef VG_DEVWINDOW + vg.window_x = 1200; + vg.window_y = 880; #endif #ifndef _WIN32 @@ -725,7 +750,9 @@ VG_STATIC void _vg_init_window( const char *window_name ) #ifdef VG_DEVWINDOW 0, 0, vg.window_x, vg.window_y, SDL_WINDOW_BORDERLESS|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED - ))){} + ))){ + SDL_SetWindowPosition( vg.window, video_mode.w-vg.window_x, 0 ); + } #else 0, 0, vg.window_x, vg.window_y, @@ -751,6 +778,9 @@ VG_STATIC void _vg_init_window( const char *window_name ) vg_info( "CreateContext\n" ); + /* ????? */ + if( SDL_IsTextInputActive() ) SDL_StopTextInput(); + /* * OpenGL loading */ @@ -788,17 +818,15 @@ VG_STATIC void _vg_init_window( const char *window_name ) } vg_info( "Display refresh rate: %d\n", dispmode.refresh_rate ); - -#ifdef _WIN32 vg.fps_limit = vg.display_refresh_rate; + +#if defined(_WIN32) || defined(VG_DEVWINDOW) #else - /* request vsync by default on linux to avoid screen tearing. - * this does have its own issues with compositing on X11. */ - vg.fps_limit = 0; + //vg.vsync = 1; #endif } -VG_STATIC void _vg_terminate(void) +static void _vg_terminate(void) { /* Shutdown */ _vg_console_write_persistent(); @@ -816,8 +844,8 @@ VG_STATIC void _vg_terminate(void) exit(0); } -VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name ) -{ +static void vg_enter( int argc, char *argv[], const char *window_name ){ + vg_rand_seed( &vg.rand, 461 ); _vg_process_launch_opts_internal( argc, argv ); /* Systems init */ @@ -825,11 +853,12 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name ) _vg_console_init(); vg_console_reg_var( "fps_limit", &vg.fps_limit, k_var_dtype_i32, 0 ); + vg_console_reg_var( "vsync", &vg.vsync, k_var_dtype_i32, VG_VAR_PERSISTENT ); _vg_init_window( window_name ); vg_async_init(); - SDL_SetRelativeMouseMode(1); + vg.thread_id_main = SDL_GetThreadID(NULL); /* Opengl-required systems */ @@ -839,16 +868,16 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name ) vg.engine_status = k_engine_status_load_internal; _vg_opengl_sync_init(); - vg_loader_start( _vg_load_full ); + vg_loader_start( _vg_load_full, NULL ); _vg_gameloop(); _vg_terminate(); } -VG_STATIC void vg_fatal_error( const char *fmt, ... ) +static void vg_fatal_error( const char *fmt, ... ) { va_list args; va_start( args, fmt ); - _vg_log_write( stderr, KRED " fatal" KWHT "| " KRED, fmt, args ); + _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args ); va_end( args ); vg_print_backtrace(); @@ -869,11 +898,11 @@ VG_STATIC void vg_fatal_error( const char *fmt, ... ) #else /* VG_GAME */ #include "vg_log.h" -VG_STATIC void vg_fatal_error( const char *fmt, ... ) +static void vg_fatal_error( const char *fmt, ... ) { va_list args; va_start( args, fmt ); - _vg_log_write( stderr, KRED " fatal" KWHT "| " KRED, fmt, args ); + _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args ); va_end( args ); exit(0); } @@ -886,4 +915,6 @@ VG_STATIC void vg_fatal_error( const char *fmt, ... ) u32 NvOptimusEnablement = 0x00000001; int AmdPowerXpressRequestHighPerformance = 1; +#include "vg_log.c" + #endif /* VG_HEADER_H */