compiler options
[vg.git] / vg.h
diff --git a/vg.h b/vg.h
index 6aeadc9a0032a8c678d2a052bc750f134afae1e8..a89efaf7569fc2f19d5ed55f999f7a6245079b21 100644 (file)
--- a/vg.h
+++ b/vg.h
 |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)
 
   #include "vg_platform.h"
   #include "vg_mem.h"
-
-  #ifndef _WIN32
-    #include <execinfo.h>
-  #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<size; i++ )
-         vg_info( "%s\n", strings[i] );
-
-      vg_error( "---------------- gnu backtrace -------------\n" );
-   }
-
-   free( strings );
-
-#endif
-}
   
   #ifdef VG_GAME
     #include "dep/glad/glad.h"
@@ -96,7 +67,9 @@ VG_STATIC void vg_print_backtrace(void)
     #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
@@ -118,9 +91,9 @@ 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(void);
-VG_STATIC void vg_update_fixed(void);
-VG_STATIC void vg_update_post(void);
+VG_STATIC void vg_pre_update(void);
+VG_STATIC void vg_fixed_update(void);
+VG_STATIC void vg_post_update(void);
 
 VG_STATIC void vg_render(void);
 VG_STATIC void vg_gui(void);
@@ -170,6 +143,7 @@ struct vg{
 
    /* Runtime */
    double time,
+          time_real,
           time_delta,
           time_rate,
 
@@ -237,6 +211,14 @@ VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
    }
 }
 
+VG_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 );
+   }
+}
+
 VG_STATIC void _vg_opengl_sync_init(void)
 {
    vg.sem_loader = SDL_CreateSemaphore(1);
@@ -248,11 +230,12 @@ VG_STATIC void vg_checkgl( const char *src_info );
 
 #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"
@@ -311,7 +294,9 @@ VG_STATIC void _vg_load_full( void *data )
    /* internal */
    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 );
@@ -332,9 +317,10 @@ VG_STATIC void _vg_process_events(void)
    SDL_Event event;
    while( SDL_PollEvent( &event ) ){
       if( event.type == SDL_KEYDOWN ){
-         if( vg_console.enabled ){
+         if( vg_console.enabled && 
+               (vg_ui.focused_control_type != k_ui_control_modal) ){
             if( event.key.keysym.sym == SDLK_ESCAPE ||
-                event.key.keysym.sym == SDLK_BACKQUOTE ){
+                event.key.keysym.scancode == SDL_SCANCODE_GRAVE ){
                vg_console.enabled = 0;
                ui_defocus_all();
             }
@@ -351,7 +337,7 @@ VG_STATIC void _vg_process_events(void)
             }
          }
          else{
-            if( event.key.keysym.sym == SDLK_BACKQUOTE ){
+            if( event.key.keysym.scancode == SDL_SCANCODE_GRAVE ){
                vg_console.enabled = 1;
             }
             else {
@@ -412,7 +398,7 @@ VG_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;
@@ -422,11 +408,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 ){
@@ -437,7 +421,7 @@ 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 );
 }
 
@@ -463,28 +447,19 @@ VG_STATIC void _vg_gameloop_render(void)
       else vg_gui();
 
       /* vg tools */
+#ifndef VG_NO_AUDIO
       audio_debug_ui( vg.pv );
+#endif
 
-      ui_postrender();
-#if 0
-      ui_begin( vg.window_x, vg.window_y );
-
-      /* 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;
-      }
-      
+      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];
          
@@ -494,29 +469,19 @@ 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 */
-      vg_gui();
-      
-      ui_resolve();
-      ui_draw( NULL );
-#endif
+      ui_postrender();
    }
 
    vg_profile_end( &vg_prof_render );
@@ -601,7 +566,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 );
 
@@ -610,8 +575,7 @@ VG_STATIC int _vg_crashscreen(void)
    return 0;
 }
 
-VG_STATIC void _vg_gameloop(void)
-{
+VG_STATIC void _vg_gameloop(void){
    //vg.time_fixed_accumulator = 0.75f * (1.0f/60.0f);
 
    vg.time_hp = SDL_GetPerformanceCounter();
@@ -637,6 +601,7 @@ VG_STATIC void _vg_gameloop(void)
 
       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;
 
@@ -661,8 +626,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;
@@ -707,7 +672,9 @@ 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();
@@ -742,11 +709,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() );
@@ -757,6 +719,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
@@ -774,7 +740,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,
@@ -899,7 +867,7 @@ VG_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();
@@ -924,7 +892,7 @@ VG_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);
 }
@@ -937,4 +905,6 @@ VG_STATIC void vg_fatal_error( const char *fmt, ... )
 u32 NvOptimusEnablement = 0x00000001;
 int AmdPowerXpressRequestHighPerformance = 1;
 
+#include "vg_log.c"
+
 #endif /* VG_HEADER_H */