-/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
/*
|IMP| | |.------------- vg_start(void) ---------------'
| | | |
| | | v
-|IMP| | vg_update( int loaded )
+|IMP| | vg_pre_update(void)
| | | |
| | | .-----+.
-| | | | |
+| | | | | called 0x to 8x
| | | | v
-|IMP| | '- vg_update_fixed( int loaded )
+|IMP| | '- vg_fixed_update(void)
| | | |
| | | .-'
| | | |
| | | v
-|IMP| | vg_update_post(void)
+|IMP| | vg_post_update(void)
| | | |
| | | v
|IMP| | vg_render(void)
|IMP| | vg_ui(void)
| | | |
| | '----'
-'___'
-
- .-.
-| ? |
-| | .-------------------------------------.
-|API| | vg_fatal_exit_loop( const char *err ) |
-| | '-------------------------------------'
-| | |
-| | .------+.
-| | | |
-| | | v
-|IMP| '- vg_framebuffer_resize(void)
'___'
*/
-#ifndef VG_STATIC
- #define VG_STATIC
-#endif
-
-/* API */
-VG_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);
-
-/* Main thread */
-VG_STATIC void vg_launch_opt(void);
-VG_STATIC void vg_start(void);
-
-VG_STATIC void vg_framebuffer_resize(int w, int h);
-VG_STATIC void vg_update(int loaded);
-VG_STATIC void vg_update_fixed(int loaded);
-VG_STATIC void vg_update_post(int loaded);
-
-VG_STATIC void vg_render(void);
-VG_STATIC void vg_ui(void);
-
#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 <execinfo.h>
- #endif
#ifdef VG_GAME
#include "dep/glad/glad.h"
- #include "submodules/SDL/include/SDL.h"
+ #include "dep/sdl/include/SDL.h"
#include "vg_stdint.h"
void vg_register_exit( void( *funcptr )(void), const char *name );
#include "vg_m.h"
#include "vg_io.h"
#include "vg_log.h"
+#ifndef VG_NO_STEAM
#include "vg_steam.h"
+#endif
//#define VG_SYNC_DEBUG
#ifdef VG_SYNC_DEBUG
#define VG_SYNC_LOG(...)
#endif
-struct vg
-{
+/* API */
+static void vg_enter( int argc, char *argv[], const char *window_name );
+
+/* Thread 1 */
+static void vg_preload(void);
+static void vg_load(void);
+
+/* Main thread */
+static void vg_launch_opt(void);
+static void vg_start(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);
+
+static void vg_render(void);
+static void vg_gui(void);
+
+struct vg{
/* Engine sync */
SDL_Window *window;
SDL_GLContext gl_context;
+ const char *base_path;
- SDL_SpinLock sl_context;
- SDL_sem *sem_allow_exec,
- *sem_exec_finished,
- *sem_loader;
+ SDL_sem *sem_loader; /* allows only one loader at a time */
+ jmp_buf env_loader_exit;
SDL_threadID thread_id_main,
- thread_id_loader,
- thread_id_with_opengl_context;
- int context_ownership_refcount;
+ thread_id_loader;
+ void *thread_data;
- int exec_context;
-
- enum engine_status
- {
+ SDL_SpinLock sl_status;
+ enum engine_status{
k_engine_status_none,
+ k_engine_status_load_internal,
k_engine_status_running,
k_engine_status_crashed
}
engine_status;
- const char *str_const_engine_err;
- int is_loaded;
/* Window information */
int window_x,
samples,
window_should_close;
- float refresh_rate;
+ int display_refresh_rate,
+ fps_limit; /* 0: use vsync, >0: cap fps to this, no vsync */
+
+ enum vsync_feature{
+ k_vsync_feature_disabled=0,
+ k_vsync_feature_enabled=1,
+ k_vsync_feature_enabled_adaptive=2,
+ k_vsync_feature_error=3
+ }
+ vsync_feature;
double mouse_pos[2];
v2f mouse_delta,
/* Runtime */
double time,
- time_delta,
- frame_delta,
time_real,
- time_real_last,
+ time_delta,
time_rate,
- accumulator;
+
+ time_fixed_accumulator,
+ time_fixed_extrapolate,
+ time_frame_delta;
+
+ u64 time_hp, time_hp_last, time_spinning;
int fixed_iterations;
- enum engine_stage
- {
+ enum engine_stage{
k_engine_stage_none,
k_engine_stage_update,
k_engine_stage_update_fixed,
/* graphics */
m4x4f pv;
- enum quality_profile
- {
+ enum quality_profile{
k_quality_profile_high = 0,
k_quality_profile_low = 1,
}
quality_profile;
+
+ float loader_ring;
+}
+static vg = { .time_rate = 1.0 };
+const char *vg_get_basepath(void){
+ return vg.base_path;
}
-VG_STATIC vg = { .time_rate = 1.0 };
-struct vg_thread_info
+enum vg_thread_purpose
{
- enum vg_thread_purpose
- {
- k_thread_purpose_nothing,
- k_thread_purpose_main,
- k_thread_purpose_loader
- }
- purpose;
-
- int gl_context_level;
+ k_thread_purpose_nothing,
+ k_thread_purpose_main,
+ k_thread_purpose_loader
};
-VG_STATIC void vg_fatal_exit_loop( const char *error );
+#include "vg_async.h"
-VG_STATIC void vg_ensure_engine_running(void)
+static enum engine_status _vg_engine_status(void)
{
- VG_SYNC_LOG( "[%d] Checks if engine is running\n" );
-
- /* Check if the engine is no longer running */
- SDL_AtomicLock( &vg.sl_context );
-
- if( vg.engine_status != k_engine_status_running )
- {
- VG_SYNC_LOG( "[%d] Engine is no longer running\n");
-
- /* Safe to disregard loader thread from this point on, elswhere */
- if( vg.thread_id_loader == SDL_GetThreadID(NULL) )
- {
- SDL_SemPost( vg.sem_loader );
- }
-
- SDL_AtomicUnlock( &vg.sl_context );
+ SDL_AtomicLock( &vg.sl_status );
+ enum engine_status status = vg.engine_status;
+ SDL_AtomicUnlock( &vg.sl_status );
- VG_SYNC_LOG( "[%d] is just going to hang around and wait to die\n");
- while(1) SDL_Delay(1000);
- }
-
- SDL_AtomicUnlock( &vg.sl_context );
+ return status;
}
-/*
- * Sync execution so that the OpenGL context is switched onto this thread.
- * Anything after this call will be in a valid context.
- */
-VG_STATIC void vg_acquire_thread_sync(void)
+static enum vg_thread_purpose vg_thread_purpose(void)
{
- VG_SYNC_LOG( "[%d] Starts acquiring sync\n" );
+ SDL_AtomicLock( &vg.sl_status );
- /* We dont want to do anything if this is the main thread */
- SDL_AtomicLock( &vg.sl_context );
- if( vg.thread_id_main == SDL_GetThreadID(NULL) )
- {
- SDL_AtomicUnlock( &vg.sl_context );
- return;
+ if( vg.thread_id_main == SDL_GetThreadID(NULL) ){
+ SDL_AtomicUnlock( &vg.sl_status );
+ return k_thread_purpose_main;
}
- SDL_AtomicUnlock( &vg.sl_context );
-
- vg_ensure_engine_running();
-
- /* Check if thread already has the context */
- SDL_AtomicLock( &vg.sl_context );
- if( vg.thread_id_with_opengl_context == SDL_GetThreadID(NULL) )
- {
- vg.context_ownership_refcount ++;
- SDL_AtomicUnlock( &vg.sl_context );
-
- VG_SYNC_LOG( "[%d] We already have sync here\n" );
- return;
+ else{
+ SDL_AtomicUnlock( &vg.sl_status );
+ return k_thread_purpose_loader;
}
-
- VG_SYNC_LOG( "[%d] Signal to sync.\n" );
- vg.exec_context = 1;
- SDL_AtomicUnlock( &vg.sl_context );
-
- /* wait until told we can go */
- VG_SYNC_LOG( "[%d] Waiting to acuire sync.\n" );
-
- SDL_SemWait( vg.sem_allow_exec );
- SDL_GL_MakeCurrent( vg.window, vg.gl_context );
-
- VG_SYNC_LOG( "[%d] Allow exec passed\n" );
-
- /* context now valid to work in while we hold up main thread */
- SDL_AtomicLock( &vg.sl_context );
- vg.context_ownership_refcount ++;
- SDL_AtomicUnlock( &vg.sl_context );
-
- VG_SYNC_LOG( "[%d] Context acquired.\n" );
}
-/*
- * Signify that we are done with the OpenGL context in this thread.
- * Anything after this call will be in an undefined context.
- */
-VG_STATIC void vg_release_thread_sync(void)
-{
- VG_SYNC_LOG( "[%d] Releases sync\n" );
+static void vg_assert_thread( enum vg_thread_purpose required ){
+ enum vg_thread_purpose purpose = vg_thread_purpose();
- SDL_AtomicLock( &vg.sl_context );
- if( vg.thread_id_main == SDL_GetThreadID(NULL) )
- {
- SDL_AtomicUnlock( &vg.sl_context );
- return;
+ if( purpose != required ){
+ vg_fatal_error( "thread_purpose must be %u not %u\n", required, purpose );
}
-
- /* signal that we are done */
- vg.context_ownership_refcount --;
- if( !vg.context_ownership_refcount )
- {
- SDL_AtomicUnlock( &vg.sl_context );
-
- VG_SYNC_LOG( "[%d] Releasing context.\n" );
-
- SDL_GL_MakeCurrent( NULL, NULL );
- SDL_SemPost( vg.sem_exec_finished );
- }
- else
- SDL_AtomicUnlock( &vg.sl_context );
}
-VG_STATIC void vg_run_synced_content(void)
+static void _vg_opengl_sync_init(void)
{
- SDL_AtomicLock( &vg.sl_context );
-
- if( vg.exec_context != 0 )
- {
- VG_SYNC_LOG( "[%d] Allowing content (%d).\n", vg.exec_context );
- vg.exec_context = 0;
- SDL_AtomicUnlock( &vg.sl_context );
-
- /* allow operations to go */
- SDL_GL_MakeCurrent( NULL, NULL );
- SDL_SemPost( vg.sem_allow_exec );
-
- /* wait for operations to complete */
- VG_SYNC_LOG( "[%d] Waiting for content.\n" );
- SDL_SemWait( vg.sem_exec_finished );
-
- /* check if we killed the engine */
- vg_ensure_engine_running();
-
- /* re-engage main thread */
- VG_SYNC_LOG( "[%d] Re-engaging.\n" );
- SDL_GL_MakeCurrent( vg.window, vg.gl_context );
- }
- else
- SDL_AtomicUnlock( &vg.sl_context );
-}
-
-VG_STATIC void vg_opengl_sync_init(void)
-{
- vg.sem_allow_exec = SDL_CreateSemaphore(0);
- vg.sem_exec_finished = SDL_CreateSemaphore(0);
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()"},
- vg_prof_render = {.name="render()"};
+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;
GLenum err;
- while( (err = glGetError()) != GL_NO_ERROR )
- {
+ while( (err = glGetError()) != GL_NO_ERROR ){
vg_error( "(%s) OpenGL Error: #%d\n", src_info, err );
fail = 1;
}
if( fail )
- vg_fatal_exit_loop( "OpenGL Error" );
+ vg_fatal_error( "OpenGL Error" );
}
-VG_STATIC void vg_bake_shaders(void)
+static void async_vg_bake_shaders( void *payload, u32 size )
{
- vg_acquire_thread_sync();
+ vg_shaders_compile();
+}
-#if 0
- vg_function_push( (struct vg_cmd)
- {
- .name = "shaders",
- .function = vg_shaders_live_recompile
- });
-#endif
+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 );
+}
- vg_shaders_compile();
- vg_release_thread_sync();
+void async_internal_complete( void *payload, u32 size )
+{
+ vg_success( "Internal async setup complete\n" );
+ SDL_AtomicLock( &vg.sl_status );
+
+ if( vg.engine_status == k_engine_status_crashed ){
+ SDL_AtomicUnlock( &vg.sl_status );
+ return;
+ }
+ else{
+ vg.engine_status = k_engine_status_running;
+ }
+
+ SDL_AtomicUnlock( &vg.sl_status );
}
-VG_STATIC void vg_load_full(void)
+static void _vg_load_full( void *data )
{
vg_preload();
/* internal */
- vg_loader_highwater( vg_input_init, vg_input_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 );
+ 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_acquire_thread_sync();
- vg.is_loaded = 1;
- vg_release_thread_sync();
}
-VG_STATIC void vg_process_events(void)
+static void _vg_process_events(void)
{
- /* Update timers */
- vg.time_real_last = vg.time_real;
- vg.time_real = (double)SDL_GetTicks64() / 1000.0;
- vg.frame_delta = vg.time_real-vg.time_real_last;
-
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 );
+ while( SDL_PollEvent( &event ) ){
+ if( event.type == SDL_KEYDOWN ){
+ 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 )
- {
+ else if( event.type == SDL_MOUSEWHEEL ){
vg.mouse_wheel[0] += event.wheel.preciseX;
vg.mouse_wheel[1] += event.wheel.preciseY;
}
+ else if( event.type == SDL_CONTROLLERDEVICEADDED ||
+ event.type == SDL_CONTROLLERDEVICEREMOVED )
+ {
+ vg_input_device_event( &event );
+ }
else if( event.type == SDL_CONTROLLERAXISMOTION ||
event.type == SDL_CONTROLLERBUTTONDOWN ||
- event.type == SDL_CONTROLLERBUTTONUP ||
- event.type == SDL_CONTROLLERDEVICEADDED ||
- event.type == SDL_CONTROLLERDEVICEREMOVED
- )
+ event.type == SDL_CONTROLLERBUTTONUP )
{
vg_input_controller_event( &event );
}
- else if( event.type == SDL_MOUSEMOTION )
- {
+ else if( event.type == SDL_MOUSEMOTION ){
vg.mouse_delta[0] += event.motion.xrel;
vg.mouse_delta[1] += event.motion.yrel;
}
- else if( event.type == SDL_WINDOWEVENT )
- {
- if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED )
- {
+ else if( event.type == SDL_WINDOWEVENT ){
+ if( event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED ){
int w, h;
SDL_GL_GetDrawableSize( vg.window, &w, &h );
- if( !w || !h )
- {
+ if( !w || !h ){
vg_warn( "Got a invalid framebuffer size: "
"%dx%d... ignoring\n", w, h );
}
- else
- {
+ else{
vg.window_x = w;
vg.window_y = h;
vg_framebuffer_resize(w,h);
}
}
- else if( event.window.event == SDL_WINDOWEVENT_CLOSE )
- {
+ else if( event.window.event == SDL_WINDOWEVENT_CLOSE ){
vg.window_should_close = 1;
}
}
- else if( event.type == SDL_TEXTINPUT )
- {
- console_proc_utf8( event.text.text );
+ else if( event.type == SDL_TEXTINPUT ){
+ 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_update_inputs();
}
-VG_STATIC void vg_gameloop_update( int post_start )
+static void _vg_gameloop_update(void)
{
vg_profile_begin( &vg_prof_update );
vg.engine_stage = k_engine_stage_update;
- vg_update( post_start );
+ vg_pre_update();
/* Fixed update loop */
vg.engine_stage = k_engine_stage_update_fixed;
- vg.accumulator += vg.time_delta;
vg.fixed_iterations = 0;
vg_lines.allow_input = 1;
- while( vg.accumulator >= (VG_TIMESTEP_FIXED-0.00125) )
- {
- vg_update_fixed( post_start );
- vg_lines.allow_input = 0;
+ vg.time_fixed_accumulator += vg.time_delta;
- vg.accumulator -= VG_TIMESTEP_FIXED;
- vg.accumulator = VG_MAX( 0.0, vg.accumulator );
+ while( vg.time_fixed_accumulator >= VG_TIMESTEP_FIXED ){
+ vg_fixed_update();
+ vg_lines.allow_input = 0;
+ vg.time_fixed_accumulator -= VG_TIMESTEP_FIXED;
vg.fixed_iterations ++;
- if( vg.fixed_iterations == 8 )
- {
+ if( vg.fixed_iterations == 8 ){
break;
}
}
vg_lines.allow_input = 1;
+ vg.time_fixed_extrapolate = vg.time_fixed_accumulator / VG_TIMESTEP_FIXED;
vg.engine_stage = k_engine_stage_update;
- vg_update_post( post_start );
+ vg_post_update();
vg_profile_end( &vg_prof_update );
}
-VG_STATIC void vg_gameloop_render( int post_start )
+static void _vg_gameloop_render(void)
{
vg_profile_begin( &vg_prof_render );
- if( post_start )
+ /* render */
+ vg.engine_stage = k_engine_stage_rendering;
+ vg_render();
+
+ /* ui */
+ vg.engine_stage = k_engine_stage_ui;
{
- /* render */
- vg.engine_stage = k_engine_stage_rendering;
- vg_render();
+ 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();
- /* ui */
- vg.engine_stage = k_engine_stage_ui;
- {
- ui_begin( vg.window_x, vg.window_y );
+ /* vg tools */
+#ifndef VG_NO_AUDIO
+ audio_debug_ui( vg.pv );
+#endif
- /* TODO */
- ui_set_mouse( vg.mouse_pos[0], vg.mouse_pos[1], 0 );
+ /* profiling */
+ int frame_target = vg.display_refresh_rate;
+ 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];
- vg_profile_drawn(
- (struct vg_profile *[]){&vg_prof_update,&vg_prof_render}, 2,
- (1.0f/(float)vg.refresh_rate)*1000.0f,
- (ui_rect){ 4, 4, 250, 0 }, 0
- );
-
- if( vg_profiler )
- {
-
- char perf[128];
-
- snprintf( perf, 127,
- "x: %d y: %d\n"
- "refresh: %.1f (%.1fms)\n"
- "samples: %d\n"
- "iterations: %d (acc: %.3fms%%)\n",
- vg.window_x, vg.window_y,
- vg.refresh_rate, (1.0f/vg.refresh_rate)*1000.0f,
- vg.samples,
- vg.fixed_iterations,
- (vg.accumulator/VG_TIMESTEP_FIXED)*100.0f );
-
- ui_text( (ui_rect){258, 4+24+12,0,0},perf, 1,0);
+ snprintf( perf, 255,
+ "x: %d y: %d\n"
+ "refresh: %d (%.1fms)\n"
+ "samples: %d\n"
+ "iterations: %d (acc: %.3fms%%)\n"
+ "time: real(%.2f) delta(%.2f) rate(%.2f)\n"
+ " 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_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,900,900},perf,1,0,k_ui_align_left);
+ }
+ ui_postrender();
+ }
+
+ vg_profile_end( &vg_prof_render );
+}
+
+static int vg_framefilter( double dt )
+{
+ if( (vg.fps_limit <= 0) && (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_feature != requested ){
+ vg_info( "Setting swap interval\n" );
+
+ int 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 ){
+ vg_error( "Vsync is not supported by your system\n" );
+ vg_warn( "You may be overriding it in your"
+ " graphics control panel.\n" );
+ }
+ else{
+ vg_error( "Adaptive Vsync is not supported by your system\n" );
+ }
+
+ vg.vsync_feature = k_vsync_feature_error;
+ vg.fps_limit = vg.display_refresh_rate;
+
+ /* TODO: Make popup to notify user that this happened */
+ return 1;
}
+ else{
+ vg_success( "Vsync enabled (%d)\n", requested );
+ vg.vsync_feature = requested;
+ }
+ }
- audio_debug_ui( vg.pv );
- vg_ui();
- vg_console_draw();
-
- ui_resolve();
- ui_draw( NULL );
+ return 0;
+ }
+
+ if( vg.vsync_feature != k_vsync_feature_disabled ){
+ SDL_GL_SetSwapInterval( 0 );
+ vg.vsync_feature = k_vsync_feature_disabled;
+ }
+
+ if( vg.fps_limit < 25 ) vg.fps_limit = 25;
+ if( vg.fps_limit > 300 ) vg.fps_limit = 300;
+
+ double min_frametime = 1.0/(double)vg.fps_limit;
+ if( vg.time_frame_delta < min_frametime ){
+ /* TODO: we can use high res nanosleep on Linux here */
+ double sleep_ms = (min_frametime-vg.time_frame_delta) * 1000.0;
+ u32 ms = (u32)floor( sleep_ms );
+
+ if( ms ){
+ SDL_Delay( ms );
}
+ else{
+ vg.time_spinning ++;
+ }
+
+ return 1;
}
- vg_profile_end( &vg_prof_render );
+ return 0;
}
-VG_STATIC void vg_gameloop(void)
+static int _vg_crashscreen(void)
{
- vg.accumulator = 0.75f * (1.0f/60.0f);
+#if 0
+ if( vg_getkey( SDLK_ESCAPE ) )
+ return 1;
+#endif
+
+ glBindFramebuffer( GL_FRAMEBUFFER, 0 );
+ glEnable(GL_BLEND);
+ glDisable(GL_DEPTH_TEST);
+ glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
+ glBlendEquation(GL_FUNC_ADD);
+
+ 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 );
+
+ _vg_render_log();
+
+ return 0;
+}
+
+static void _vg_gameloop(void){
+ //vg.time_fixed_accumulator = 0.75f * (1.0f/60.0f);
+
+ vg.time_hp = SDL_GetPerformanceCounter();
+ vg.time_hp_last = vg.time_hp;
int post_start = 0;
- while(1)
- {
- vg_process_events();
+ while(1){
+
+ vg.time_hp = SDL_GetPerformanceCounter();
+ u64 udt = vg.time_hp - vg.time_hp_last;
+ vg.time_hp_last = vg.time_hp;
- if( vg.window_should_close )
- break;
+ double dt = (double)udt / (double)SDL_GetPerformanceFrequency();
+
+ vg.time_frame_delta += dt;
+
+ if( vg_framefilter( dt ) )
+ continue;
- /* scaled time */
- vg.time_delta = vg.frame_delta * vg.time_rate;
+ 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;
-
- if( vg.is_loaded )
- {
- if( !post_start )
- {
- vg_start();
- post_start = 1;
- }
+
+ vg_run_async_checked();
+ _vg_process_events();
+
+ if( vg.window_should_close )
+ break;
+
+ if( status == k_engine_status_crashed ){
+ if( _vg_crashscreen() )
+ break;
}
- else
- {
- vg_loader_render();
+ else{
+ if( status == k_engine_status_running ){
+ _vg_gameloop_update();
+ _vg_gameloop_render();
+ }
+ else{
+ _vg_loader_render();
+ }
}
- vg_gameloop_update( post_start );
- vg_gameloop_render( post_start );
+ if( vg.loader_ring > 0.01f ){
+ _vg_loader_render_ring( vg.loader_ring );
+ vg.loader_ring -= vg.time_frame_delta * 0.5f;
+ }
- SDL_GL_SwapWindow( vg.window );
- vg_run_synced_content();
+ vg.time_frame_delta = 0.0;
+ vg.time_spinning = 0;
}
}
-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 ) )
- {
- if( (arg = vg_opt_arg( 'w' )) )
- {
+ while( vg_argp( argc, argv ) ){
+ if( (arg = vg_opt_arg( 'w' )) ){
vg.window_x = atoi( arg );
}
- if( (arg = vg_opt_arg( 'h' )) )
- {
+ if( (arg = vg_opt_arg( 'h' )) ){
vg.window_y = atoi( arg );
}
- if( (arg = vg_long_opt_arg( "samples" )) )
- {
+ if( (arg = vg_long_opt_arg( "samples" )) ){
vg.samples = VG_MAX( 0, VG_MIN( 8, atoi( arg ) ) );
}
- if( vg_long_opt( "use-libc-malloc" ) )
- {
- vg_mem.use_libc_malloc = atoi( arg );
+ if( vg_long_opt( "use-libc-malloc" ) ){
+ vg_mem.use_libc_malloc = 1;
}
- if( vg_long_opt( "high-performance" ) )
- {
+ if( vg_long_opt( "high-performance" ) ){
vg.quality_profile = k_quality_profile_low;
}
}
}
-VG_STATIC void vg_init_window( const char *window_name )
+static void _vg_init_window( const char *window_name )
{
- if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_GAMECONTROLLER) != 0 )
- {
+ vg_info( "SDL_INIT\n" );
+
+ if( SDL_Init( SDL_INIT_VIDEO ) != 0 ){
vg_error( "SDL_Init failed: %s\n", SDL_GetError() );
exit(0);
}
- SDL_GL_SetSwapInterval( 1 );
+#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 );
SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE );
- SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 1 );
+ SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
+ SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH );
+
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
+ SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 0 );
- if( vg.samples > 1 )
- {
- SDL_GL_SetAttribute( SDL_GL_MULTISAMPLEBUFFERS, 1 );
- SDL_GL_SetAttribute( SDL_GL_MULTISAMPLESAMPLES, vg.samples );
- }
-
- SDL_GL_SetAttribute( SDL_GL_CONTEXT_RELEASE_BEHAVIOR,
- SDL_GL_CONTEXT_RELEASE_BEHAVIOR_FLUSH );
-
/*
* Get monitor information
*/
- int display_count = 0, display_index = 0, mode_index = 0;
- if( (display_count = SDL_GetNumVideoDisplays()) < 1 )
- {
- vg_error( "SDL_GetNumVideoDisplays returned: %i\n", display_count );
+ vg_info( "Getting display count\n" );
+ int display_count = 0,
+ display_index = 0,
+ mode_index = 0;
+
+ SDL_DisplayMode video_mode;
+ if( SDL_GetDesktopDisplayMode( display_index, &video_mode ) ){
+ vg_error( "SDL_GetDesktopDisplayMode failed: %s\n", SDL_GetError() );
+ SDL_Quit();
exit(0);
}
- /* TODO: Allow chosing the modes at startup */
- SDL_DisplayMode mode = { SDL_PIXELFORMAT_UNKNOWN, 0, 0, 0, 0 };
- if( SDL_GetDisplayMode( display_index, mode_index, &mode ) != 0 )
- {
- vg_error( "SDL_GetDisplayMode failed: %s", SDL_GetError() );
- exit(0);
- }
+ 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
- vg.refresh_rate = mode.refresh_rate;
+#ifndef _WIN32
+ SDL_SetHint( "SDL_VIDEO_X11_XINERAMA", "1" );
+ SDL_SetHint( "SDL_VIDEO_X11_XRANDR", "0" );
+ SDL_SetHint( "SDL_VIDEO_X11_XVIDMODE", "0" );
+#endif
+
+ vg_info( "CreateWindow( %d %d @%dhz )\n", vg.window_x, vg.window_y,
+ vg.display_refresh_rate );
/* TODO: Allow selecting closest video mode from launch opts */
if((vg.window = SDL_CreateWindow( window_name,
- SDL_WINDOWPOS_UNDEFINED,
- SDL_WINDOWPOS_UNDEFINED,
- mode.w, mode.h,
- SDL_WINDOW_FULLSCREEN |
+#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,
+
+ SDL_WINDOW_FULLSCREEN_DESKTOP |
SDL_WINDOW_OPENGL |
- SDL_WINDOW_INPUT_GRABBED )))
+ SDL_WINDOW_INPUT_GRABBED
+ )))
{
+ if( SDL_SetWindowDisplayMode( vg.window, &video_mode ) ){
+ vg_error( "SDL_SetWindowDisplayMode failed: %s", SDL_GetError() );
+ SDL_Quit();
+ exit(0);
+ }
}
- else
- {
+#endif
+ else{
vg_error( "SDL_CreateWindow failed: %s", SDL_GetError() );
exit(0);
}
+ SDL_RaiseWindow( vg.window );
+
+ vg_info( "CreateContext\n" );
+
+ /* ????? */
+ if( SDL_IsTextInputActive() ) SDL_StopTextInput();
+
/*
* OpenGL loading
*/
- if( (vg.gl_context = SDL_GL_CreateContext(vg.window) ))
- {
+ if( (vg.gl_context = SDL_GL_CreateContext(vg.window) )){
SDL_GL_GetDrawableSize( vg.window, &vg.window_x, &vg.window_y );
vg_success( "Window created (%dx%d)\n", vg.window_x, vg.window_y );
}
- else
- {
+ else{
vg_error( "SDL_GL_CreateContext failed: %s\n", SDL_GetError() );
SDL_Quit();
exit(0);
}
- if( !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) )
- {
+ if( !gladLoadGLLoader((GLADloadproc)SDL_GL_GetProcAddress) ) {
vg_error( "Glad Failed to initialize\n" );
SDL_GL_DeleteContext( vg.gl_context );
SDL_Quit();
const unsigned char* glver = glGetString( GL_VERSION );
vg_success( "Load setup complete, OpenGL version: %s\n", glver );
-}
-VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
-{
- vg_process_launch_opts_internal( argc, argv );
+ SDL_GL_SetSwapInterval(0); /* disable vsync while loading */
- /* Systems init */
- vg_alloc_quota();
- vg_log_init();
- vg_console_init();
- vg_init_window( window_name );
-
- SDL_SetRelativeMouseMode(1);
- vg.thread_id_main = SDL_GetThreadID(NULL);
-
- /* Opengl-required systems */
- ui_init_context();
- vg_loader_init();
+ SDL_DisplayMode dispmode;
+ if( !SDL_GetWindowDisplayMode( vg.window, &dispmode ) ){
+ if( dispmode.refresh_rate ){
+ vg.display_refresh_rate = dispmode.refresh_rate;
+ }
+ }
- vg.engine_status = k_engine_status_running;
+ if( vg.display_refresh_rate < 25 || vg.display_refresh_rate > 300 ){
+ vg.display_refresh_rate = 60;
+ }
- vg_opengl_sync_init();
- vg_loader_start();
-
- /* main */
- vg_gameloop();
+ vg_info( "Display refresh rate: %d\n", dispmode.refresh_rate );
+#if defined(_WIN32) || defined(VG_DEVWINDOW)
+ vg.fps_limit = vg.display_refresh_rate;
+#else
+ vg.fps_limit = 0;
+#endif
+}
+
+static void _vg_terminate(void)
+{
/* Shutdown */
- vg_console_write_persistent();
+ _vg_console_write_persistent();
- SDL_AtomicLock( &vg.sl_context );
+ SDL_AtomicLock( &vg.sl_status );
vg.engine_status = k_engine_status_none;
- SDL_AtomicUnlock( &vg.sl_context );
+ SDL_AtomicUnlock( &vg.sl_status );
- vg_loader_free();
+ _vg_loader_free();
vg_success( "If you see this it means everything went.. \"well\".....\n" );
SDL_GL_DeleteContext( vg.gl_context );
SDL_Quit();
+ exit(0);
}
-/*
- * 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
- * when calling the program from outside its normal directory.
- */
-VG_STATIC void vg_fatal_exit_loop( const char *error )
+static void vg_enter( int argc, char *argv[], const char *window_name )
{
- /*
- * https://www.gnu.org/software/libc/manual/html_node/Backtraces.html
- * thanks gnu <3
- *
- * TODO: this on windows?
- */
+ vg_rand_seed( 461 );
+ _vg_process_launch_opts_internal( argc, argv );
-#ifndef _WIN32
-
- void *array[20];
- char **strings;
- int size, i;
-
- size = backtrace( array, 20 );
- strings = backtrace_symbols( array, size );
+ /* Systems init */
+ vg_alloc_quota();
+ _vg_console_init();
+
+ vg_console_reg_var( "fps_limit", &vg.fps_limit, k_var_dtype_i32, 0 );
+ _vg_init_window( window_name );
- if( strings != NULL )
- {
- vg_error( "---------------- gnu backtrace -------------\n" );
+ vg_async_init();
+ SDL_SetRelativeMouseMode(1);
- for( int i=0; i<size; i++ )
- vg_info( "%s\n", strings[i] );
+ vg.thread_id_main = SDL_GetThreadID(NULL);
+
+ /* Opengl-required systems */
+ _vg_ui_init();
+ _vg_loader_init();
- vg_error( "---------------- gnu backtrace -------------\n" );
- }
+ vg.engine_status = k_engine_status_load_internal;
- free( strings );
+ _vg_opengl_sync_init();
+ vg_loader_start( _vg_load_full, NULL );
+ _vg_gameloop();
+ _vg_terminate();
+}
-#endif
+static void vg_fatal_error( const char *fmt, ... )
+{
+ va_list args;
+ va_start( args, fmt );
+ _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args );
+ va_end( args );
- vg_error( "Fatal error: %s\n", error );
+ vg_print_backtrace();
- SDL_AtomicLock( &vg.sl_context );
- if( vg.engine_status == k_engine_status_none )
- {
- SDL_AtomicUnlock( &vg.sl_context );
+ SDL_AtomicLock( &vg.sl_status );
+ vg.engine_status = k_engine_status_crashed;
+ SDL_AtomicUnlock( &vg.sl_status );
- /* TODO: Correct shutdown before other systems */
- exit(0);
+ if( vg_thread_purpose() == k_thread_purpose_loader ){
+ longjmp( vg.env_loader_exit, 1 );
}
- else
- {
- SDL_AtomicUnlock( &vg.sl_context );
-
- /*
- * if main
- * if loader running
- * wait until loader checks in, it will die
- * else
- * pass immediately
- * else
- * if have context
- * pass immediately
- * else
- * wait for main to get to us, it will never be used again
- *
- * undefined behaviour:
- * fatal_exit_loop is called in both threads, preventing an appropriate
- * reaction to the crash. This *should* be made
- * obvious by the assertion
- */
- vg_acquire_thread_sync();
-
- SDL_AtomicUnlock( &vg.sl_context );
- vg.engine_status = k_engine_status_crashed;
- vg.str_const_engine_err = error;
-
- /*
- * Wait for loader to finish what it was doing, if it was running.
- * Then we can continue in our nice error screen
- */
- if( vg.thread_id_main == SDL_GetThreadID(NULL) )
- {
- SDL_AtomicUnlock( &vg.sl_context );
- SDL_SemWait( vg.sem_loader );
- }
- else
- SDL_AtomicUnlock( &vg.sl_context );
-
- vg_audio_free(NULL);
-
- while(1)
- {
- vg_process_events();
- if( vg.window_should_close )
- break;
-
- if( vg_getkey( SDLK_ESCAPE ) )
- break;
-
- glBindFramebuffer( GL_FRAMEBUFFER, 0 );
- glEnable(GL_BLEND);
- glDisable(GL_DEPTH_TEST);
- glBlendFunc(GL_ONE_MINUS_DST_ALPHA, GL_DST_ALPHA);
- glBlendEquation(GL_FUNC_ADD);
-
- glClearColor( 0.15f + sinf(vg.time_real)*0.1f, 0.0f, 0.0f,1.0f );
- glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
- glViewport( 0,0, vg.window_x, vg.window_y );
-
- vg_render_log();
- SDL_GL_SwapWindow( vg.window );
- }
-
- /* Can now shutdown and EXIT */
- vg_loader_free();
- SDL_GL_DeleteContext( vg.gl_context );
- SDL_Quit();
- exit(0);
+ else{
+ vg_error( "There is no jump to the error runner thing yet! bai bai\n" );
+ _vg_terminate();
}
}
#else /* VG_GAME */
-VG_STATIC void vg_fatal_exit_loop( const char *error )
+#include "vg_log.h"
+static void vg_fatal_error( const char *fmt, ... )
{
- vg_error( "Fatal error: %s\n", error );
+ va_list args;
+ va_start( args, fmt );
+ _vg_logx_va( stderr, NULL, "fatal", KRED, fmt, args );
+ va_end( args );
exit(0);
}
u32 NvOptimusEnablement = 0x00000001;
int AmdPowerXpressRequestHighPerformance = 1;
+#include "vg_log.c"
+
#endif /* VG_HEADER_H */