settings menu & texsheet
[vg.git] / vg.h
diff --git a/vg.h b/vg.h
index a89efaf7569fc2f19d5ed55f999f7a6245079b21..919f6f3b878049b68f55afb749eddf904d7c37c1 100644 (file)
--- a/vg.h
+++ b/vg.h
   #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_pre_update(void);
-VG_STATIC void vg_fixed_update(void);
-VG_STATIC void vg_post_update(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_gui(void);
+static void vg_render(void);
+static void vg_gui(void);
+
+enum quality_profile{
+   k_quality_profile_high = 0,
+   k_quality_profile_low = 1,
+   k_quality_profile_min = 2
+};
 
 struct vg{
    /* Engine sync */
@@ -127,7 +133,12 @@ struct vg{
        window_should_close;
 
    int display_refresh_rate,
-       fps_limit;             /* 0: use vsync, >0: cap fps to this, no vsync */
+       fps_limit,
+       vsync,
+       screen_mode,
+       display_index;
+
+   int settings_open;
 
    enum vsync_feature{
       k_vsync_feature_disabled=0,
@@ -137,7 +148,7 @@ struct vg{
    }
    vsync_feature;
 
-   double mouse_pos[2];
+   i32 mouse_pos[2];
    v2f mouse_delta,
        mouse_wheel;
 
@@ -165,16 +176,20 @@ struct vg{
    engine_stage;
 
    /* graphics */
+#ifdef VG_3D
    m4x4f pv;
-   enum quality_profile{
-      k_quality_profile_high = 0,
-      k_quality_profile_low = 1,
-   }
-   quality_profile;
+#else
+   m3x3f pv;
+#endif
+
+   i32 quality_profile;
 
    float loader_ring;
+   GLuint tex_missing;
+
+   vg_rand rand;
 }
-VG_STATIC vg = { .time_rate = 1.0 };
+static vg = { .time_rate = 1.0 };
 const char *vg_get_basepath(void){
    return vg.base_path;
 }
@@ -188,7 +203,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;
@@ -197,7 +212,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 );
 
@@ -211,7 +226,7 @@ VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
    }
 }
 
-VG_STATIC void vg_assert_thread( enum vg_thread_purpose required ){
+static void vg_assert_thread( enum vg_thread_purpose required ){
    enum vg_thread_purpose purpose = vg_thread_purpose();
 
    if( purpose != required ){
@@ -219,12 +234,12 @@ VG_STATIC void vg_assert_thread( enum vg_thread_purpose required ){
    }
 }
 
-VG_STATIC void _vg_opengl_sync_init(void)
+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__) )
 
@@ -240,13 +255,14 @@ VG_STATIC void vg_checkgl( const char *src_info );
 #include "vg_lines.h"
 #include "vg_loader.h"
 #include "vg_opt.h"
+#include "vg_settings_menu.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;
 
@@ -260,12 +276,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 );
@@ -287,9 +303,10 @@ void async_internal_complete( void *payload, u32 size )
    SDL_AtomicUnlock( &vg.sl_status );
 }
 
-VG_STATIC void _vg_load_full( void *data )
-{
+static void _vg_load_full( void *data ){
    vg_preload();
+   vg_tex2d_replace_with_error( &vg.tex_missing );
+   vg_ui.tex_bg = vg.tex_missing;
 
    /* internal */
    vg_loader_step( vg_input_init, vg_input_free );
@@ -303,9 +320,11 @@ VG_STATIC void _vg_load_full( void *data )
 
    /* 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 );
@@ -389,11 +408,10 @@ VG_STATIC void _vg_process_events(void)
       }
    }
 
-   vg.mouse_pos[0] += vg.mouse_delta[0];
-   vg.mouse_pos[1] += vg.mouse_delta[1];
+   SDL_GetMouseState( &vg.mouse_pos[0], &vg.mouse_pos[1] );
 }
 
-VG_STATIC void _vg_gameloop_update(void)
+static void _vg_gameloop_update(void)
 {
    vg_profile_begin( &vg_prof_update );
 
@@ -425,7 +443,7 @@ VG_STATIC void _vg_gameloop_update(void)
    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 );
 
@@ -446,21 +464,24 @@ VG_STATIC void _vg_gameloop_render(void)
       }
       else vg_gui();
 
+      if( vg.settings_open )
+         vg_settings_gui();
+
       /* 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;
-      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 ){
+         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, 0
+         );
          char perf[256];
          
          snprintf( perf, 255, 
@@ -487,19 +508,33 @@ VG_STATIC void _vg_gameloop_render(void)
    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 aaaaaaaaaaaaaaaaa( ui_rect r ){
+   int frame_target = vg.display_refresh_rate;
+   if( !vg.vsync ) frame_target = vg.fps_limit;
+
+   vg_profile_drawn( 
+         (struct vg_profile *[]){
+            &vg_prof_update,&vg_prof_render,&vg_prof_swap}, 3,
+         (1.0f/(f32)frame_target)*1500.0f, 
+         r, 0, 1
+   );
+
+   ui_fill( (ui_rect){ r[0], r[1] + (r[3]*2)/3, r[2], 1 }, ui_colour(k_ui_fg) );
+}
+
+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 ){
@@ -512,26 +547,25 @@ 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;
+      }
    }
-   
-   if( vg.fps_limit < 25 ) vg.fps_limit = 25;
+}
+
+static int vg_framefilter( double dt ){
+   if( vg.fps_limit < 24 ) vg.fps_limit = 24;
    if( vg.fps_limit > 300 ) vg.fps_limit = 300;
 
    double min_frametime = 1.0/(double)vg.fps_limit;
@@ -541,7 +575,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 ++;
@@ -553,7 +590,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 ) )
@@ -575,7 +612,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();
@@ -583,7 +620,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;
@@ -591,10 +627,13 @@ 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 );
@@ -605,7 +644,6 @@ VG_STATIC void _vg_gameloop(void){
       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 )
@@ -635,7 +673,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 ) ){
@@ -663,7 +701,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" );
 
@@ -721,8 +759,8 @@ VG_STATIC void _vg_init_window( const char *window_name )
    vg.window_y = video_mode.h;
 
 #ifdef VG_DEVWINDOW
-   vg.window_x = 1200;
-   vg.window_y = 880;
+   vg.window_x = 1280;
+   vg.window_y = 720;
 #endif
 
 #ifndef _WIN32
@@ -742,6 +780,7 @@ VG_STATIC void _vg_init_window( const char *window_name )
          SDL_WINDOW_BORDERLESS|SDL_WINDOW_OPENGL|SDL_WINDOW_INPUT_GRABBED
       ))){
       SDL_SetWindowPosition( vg.window, video_mode.w-vg.window_x, 0 );
+      vg.screen_mode = 2;
    }
 #else
                                      0, 0,
@@ -749,7 +788,8 @@ VG_STATIC void _vg_init_window( const char *window_name )
 
                                      SDL_WINDOW_FULLSCREEN_DESKTOP | 
                                      SDL_WINDOW_OPENGL |
-                                     SDL_WINDOW_INPUT_GRABBED 
+                                     SDL_WINDOW_INPUT_GRABBED |
+                                     SDL_WINDOW_RESIZABLE
                                      )))
    {
       if( SDL_SetWindowDisplayMode( vg.window, &video_mode ) ){
@@ -765,6 +805,8 @@ VG_STATIC void _vg_init_window( const char *window_name )
    }
 
    SDL_RaiseWindow( vg.window );
+   SDL_SetWindowMinimumSize( vg.window, 1280, 720 );
+   SDL_SetWindowMaximumSize( vg.window, 4096, 4096 );
 
    vg_info( "CreateContext\n" );
 
@@ -808,15 +850,15 @@ VG_STATIC void _vg_init_window( const char *window_name )
    }
 
    vg_info( "Display refresh rate: %d\n", dispmode.refresh_rate );
+   vg.fps_limit = vg.display_refresh_rate;
 
 #if defined(_WIN32) || defined(VG_DEVWINDOW)
-   vg.fps_limit = vg.display_refresh_rate;
 #else
-   vg.fps_limit = 0;
+   //vg.vsync = 1;
 #endif
 }
 
-VG_STATIC void _vg_terminate(void)
+static void _vg_terminate(void)
 {
    /* Shutdown */
    _vg_console_write_persistent();
@@ -834,9 +876,8 @@ VG_STATIC void _vg_terminate(void)
    exit(0);
 }
 
-VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
-{
-   vg_rand_seed( 461 );
+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 */
@@ -844,6 +885,8 @@ 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_console_reg_cmd( "vg_settings", cmd_vg_settings_toggle, NULL );
    _vg_init_window( window_name );
 
    vg_async_init();
@@ -863,7 +906,7 @@ VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
    _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 );
@@ -888,13 +931,13 @@ 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_logx_va( stderr, NULL, "fatal", KRED, fmt, args );
    va_end( args );
-   exit(0);
+   exit(1);
 }
 
 #endif /* VG_GAME */