sync clean up
authorhgn <hgodden00@gmail.com>
Mon, 28 Nov 2022 22:18:21 +0000 (22:18 +0000)
committerhgn <hgodden00@gmail.com>
Mon, 28 Nov 2022 22:18:21 +0000 (22:18 +0000)
submodules/SDL
vg.h
vg_audio.h
vg_console.h
vg_input.h
vg_loader.h
vg_log.h
vg_m.h
vg_profiler.h
vg_shader.h
vg_ui.h

index 06492c598158cf825a18aececaf7511d7fd04f48..eef4d3c86a653f91b7221c80809ba8ab56f94cf1 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 06492c598158cf825a18aececaf7511d7fd04f48
+Subproject commit eef4d3c86a653f91b7221c80809ba8ab56f94cf1
diff --git a/vg.h b/vg.h
index 806202090074b323137058295832c3a6a91d1669..02c778810680e6be35f49350a6d889d44e19a6c1 100644 (file)
--- a/vg.h
+++ b/vg.h
 |IMP| |        |.------------- vg_start(void) ---------------'
 |   | |        |
 |   | |        v
-|IMP| |   vg_update( int loaded )
+|IMP| |   vg_update(void)
 |   | |        |                                     
 |   | |  .-----+. 
 |   | | |        |
 |   | | |        v
-|IMP| |  '- vg_update_fixed( int loaded )
+|IMP| |  '- vg_update_fixed(void)
 |   | |          |
 |   | |       .-'
 |   | |      |
@@ -77,9 +77,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(int loaded);
-VG_STATIC void vg_update_fixed(int loaded);
-VG_STATIC void vg_update_post(int loaded);
+VG_STATIC void vg_update(void);
+VG_STATIC void vg_update_fixed(void);
+VG_STATIC void vg_update_post(void);
 
 VG_STATIC void vg_render(void);
 VG_STATIC void vg_ui(void);
@@ -128,7 +128,7 @@ struct vg
    SDL_threadID  thread_id_main,
                  thread_id_loader,
                  thread_id_with_opengl_context;
-   int           context_ownership_refcount;
+   int           context_ownership_depth;
 
    int exec_context;
 
@@ -186,95 +186,87 @@ struct vg
 }
 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 );
 
-VG_STATIC void vg_ensure_engine_running(void)
+/*
+ * Checks if the engine is running
+ */
+VG_STATIC void _vg_ensure_engine_running(void)
 {
-   VG_SYNC_LOG( "[%d] Checks if engine is running\n" );
-
    /* Check if the engine is no longer running */
    SDL_AtomicLock( &vg.sl_context );
+   enum engine_status status = vg.engine_status;
+   SDL_AtomicUnlock( &vg.sl_context );
 
-   if( vg.engine_status != k_engine_status_running )
+   if( 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) )
+      while(1) 
       {
-         SDL_SemPost( vg.sem_loader );
+         VG_SYNC_LOG( "[%d] No longer running...\n");
+         SDL_Delay(1000);
       }
-
-      SDL_AtomicUnlock( &vg.sl_context );
-
-      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 );
 }
 
-/*
- * 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)
+VG_STATIC enum vg_thread_purpose vg_thread_purpose(void)
 {
-   VG_SYNC_LOG( "[%d] Starts acquiring sync\n" );
-
-   /* 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;
+      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) )
+   else
    {
-      vg.context_ownership_refcount ++;
       SDL_AtomicUnlock( &vg.sl_context );
-
-      VG_SYNC_LOG( "[%d] We already have sync here\n" );
-      return;
+      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 );
+/*
+ * 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)
+{
+   /* We dont want to do anything if this is the main thread */
 
-   VG_SYNC_LOG( "[%d] Allow exec passed\n" );
+   if( vg_thread_purpose() == k_thread_purpose_loader )
+   {
+      VG_SYNC_LOG( "[%d] vg_acquire_thread_sync()\n" );
+      _vg_ensure_engine_running();
 
-   /* 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 );
+      SDL_AtomicLock( &vg.sl_context );
+      if( vg.context_ownership_depth == 0 )
+      {
+         vg.context_ownership_depth ++;
+         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 );
+         
+         _vg_ensure_engine_running();
 
-   VG_SYNC_LOG( "[%d] Context acquired.\n" );
+         SDL_GL_MakeCurrent( vg.window, vg.gl_context );
+         VG_SYNC_LOG( "[%d] granted\n" );
+      }
+      else
+      {
+         vg.context_ownership_depth ++;
+         VG_SYNC_LOG( "[%d] granted\n" );
+         SDL_AtomicUnlock( &vg.sl_context );
+      }
+   }
 }
 
 /*
@@ -283,37 +275,32 @@ VG_STATIC void vg_acquire_thread_sync(void)
  */
 VG_STATIC void vg_release_thread_sync(void)
 {
-   VG_SYNC_LOG( "[%d] Releases sync\n" );
-
-   SDL_AtomicLock( &vg.sl_context );
-   if( vg.thread_id_main == SDL_GetThreadID(NULL) )
-   {
-      SDL_AtomicUnlock( &vg.sl_context );
-      return;
-   }
-
-   /* signal that we are done */
-   vg.context_ownership_refcount --;
-   if( !vg.context_ownership_refcount )
+   if( vg_thread_purpose() == k_thread_purpose_loader )
    {
-      SDL_AtomicUnlock( &vg.sl_context );
+      VG_SYNC_LOG( "[%d] vg_release_thread_sync()\n" );
 
-      VG_SYNC_LOG( "[%d] Releasing context.\n" );
+      SDL_AtomicLock( &vg.sl_context );
+      vg.context_ownership_depth --;
 
-      SDL_GL_MakeCurrent( NULL, NULL );
-      SDL_SemPost( vg.sem_exec_finished );
+      if( vg.context_ownership_depth == 0 )
+      {
+         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 );
    }
-   else
-      SDL_AtomicUnlock( &vg.sl_context );
 }
 
-VG_STATIC void vg_run_synced_content(void)
+VG_STATIC void _vg_run_synced(void)
 {
    SDL_AtomicLock( &vg.sl_context );
 
    if( vg.exec_context != 0 )
    {
-      VG_SYNC_LOG( "[%d] Allowing content (%d).\n", vg.exec_context );
+      VG_SYNC_LOG( "[%d] _vg_run_synced() (%d).\n", vg.exec_context );
       vg.exec_context = 0;
       SDL_AtomicUnlock( &vg.sl_context );
 
@@ -326,17 +313,20 @@ VG_STATIC void vg_run_synced_content(void)
       SDL_SemWait( vg.sem_exec_finished );
       
       /* check if we killed the engine */
-      vg_ensure_engine_running();
+      _vg_ensure_engine_running();
       
       /* re-engage main thread */
       VG_SYNC_LOG( "[%d] Re-engaging.\n" );
       SDL_GL_MakeCurrent( vg.window, vg.gl_context );
    }
    else
+   {
+      VG_SYNC_LOG( "[%d] Nothing to do.\n" );
       SDL_AtomicUnlock( &vg.sl_context );
+   }
 }
 
-VG_STATIC void vg_opengl_sync_init(void)
+VG_STATIC void _vg_opengl_sync_init(void)
 {
    vg.sem_allow_exec = SDL_CreateSemaphore(0);
    vg.sem_exec_finished = SDL_CreateSemaphore(0);
@@ -393,25 +383,21 @@ VG_STATIC void vg_bake_shaders(void)
    vg_release_thread_sync();
 }
 
-VG_STATIC void vg_load_full(void)
+VG_STATIC void _vg_load_full(void)
 {
    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 );
+   vg_loader_step( vg_audio_init, vg_audio_free );
+   vg_loader_step( vg_profiler_init, NULL );
 
    /* client */
    vg_load();
-
-   vg_acquire_thread_sync();
-   vg.is_loaded = 1;
-   vg_release_thread_sync();
 }
 
-VG_STATIC void vg_process_events(void)
+VG_STATIC void _vg_process_events(void)
 {
    /* Update timers */
    vg.time_real_last = vg.time_real;
@@ -486,12 +472,12 @@ VG_STATIC void vg_process_events(void)
    vg_update_inputs();
 }
 
-VG_STATIC void vg_gameloop_update( int post_start )
+VG_STATIC void _vg_gameloop_update(void)
 {
    vg_profile_begin( &vg_prof_update );
 
    vg.engine_stage = k_engine_stage_update;
-   vg_update( post_start );
+   vg_update();
 
    /* Fixed update loop */
    vg.engine_stage = k_engine_stage_update_fixed;
@@ -501,7 +487,7 @@ VG_STATIC void vg_gameloop_update( int post_start )
    vg_lines.allow_input = 1;
    while( vg.accumulator >= (VG_TIMESTEP_FIXED-0.00125) )
    {
-      vg_update_fixed( post_start );
+      vg_update_fixed();
       vg_lines.allow_input = 0;
 
       vg.accumulator -= VG_TIMESTEP_FIXED;
@@ -516,15 +502,15 @@ VG_STATIC void vg_gameloop_update( int post_start )
    vg_lines.allow_input = 1;
 
    vg.engine_stage = k_engine_stage_update;
-   vg_update_post( post_start );
+   vg_update_post();
    vg_profile_end( &vg_prof_update );
 }
 
-VG_STATIC void vg_gameloop_render( int post_start )
+VG_STATIC void _vg_gameloop_render(void)
 {
    vg_profile_begin( &vg_prof_render );
 
-   if( post_start )
+   if( vg.is_loaded )
    {
       /* render */
       vg.engine_stage = k_engine_stage_rendering;
@@ -565,7 +551,7 @@ VG_STATIC void vg_gameloop_render( int post_start )
 
          audio_debug_ui( vg.pv );
          vg_ui();
-         vg_console_draw();
+         _vg_console_draw();
          
          ui_resolve();
          ui_draw( NULL );
@@ -575,14 +561,14 @@ VG_STATIC void vg_gameloop_render( int post_start )
    vg_profile_end( &vg_prof_render );
 }
 
-VG_STATIC void vg_gameloop(void)
+VG_STATIC void _vg_gameloop(void)
 {
    vg.accumulator = 0.75f * (1.0f/60.0f);
 
    int post_start = 0;
    while(1)
    {
-      vg_process_events();
+      _vg_process_events();
 
       if( vg.window_should_close )
          break;
@@ -601,18 +587,18 @@ VG_STATIC void vg_gameloop(void)
       }
       else
       {
-         vg_loader_render();
+         _vg_loader_render();
       }
 
-      vg_gameloop_update( post_start );
-      vg_gameloop_render( post_start );
+      _vg_gameloop_update();
+      _vg_gameloop_render();
 
       SDL_GL_SwapWindow( vg.window );
-      vg_run_synced_content();
+      _vg_run_synced();
    }
 }
 
-VG_STATIC void vg_process_launch_opts_internal( int argc, char *argv[] )
+VG_STATIC void _vg_process_launch_opts_internal( int argc, char *argv[] )
 {
    char *arg;
    while( vg_argp( argc, argv ) )
@@ -646,7 +632,7 @@ VG_STATIC void vg_process_launch_opts_internal( int argc, char *argv[] )
    }
 }
 
-VG_STATIC void vg_init_window( const char *window_name )
+VG_STATIC void _vg_init_window( const char *window_name )
 {
    if( SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO|SDL_INIT_GAMECONTROLLER) != 0  )
    {
@@ -702,7 +688,7 @@ VG_STATIC void vg_init_window( const char *window_name )
                                      SDL_WINDOWPOS_UNDEFINED,
                                      mode.w, mode.h,
 
-                                     SDL_WINDOW_FULLSCREEN | 
+                                     SDL_WINDOW_FULLSCREEN_DESKTOP | 
                                      SDL_WINDOW_OPENGL |
                                      SDL_WINDOW_INPUT_GRABBED )))
    {
@@ -742,37 +728,35 @@ VG_STATIC void vg_init_window( const char *window_name )
 
 VG_STATIC void vg_enter( int argc, char *argv[], const char *window_name )
 {
-   vg_process_launch_opts_internal( argc, argv );
+   _vg_process_launch_opts_internal( argc, argv );
 
    /* Systems init */
    vg_alloc_quota();
-   vg_log_init();
-   vg_console_init();
-   vg_init_window( window_name );
+   _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();
+   _vg_ui_init();
+   _vg_loader_init();
 
    vg.engine_status = k_engine_status_running;
 
-   vg_opengl_sync_init();
-   vg_loader_start();
-   
-   /* main */
-   vg_gameloop();
+   _vg_opengl_sync_init();
+    vg_loader_start( _vg_load_full );
+   _vg_gameloop();
 
    /* Shutdown */
-   vg_console_write_persistent();
+   _vg_console_write_persistent();
 
    SDL_AtomicLock( &vg.sl_context );
    vg.engine_status = k_engine_status_none;
    SDL_AtomicUnlock( &vg.sl_context );
 
-   vg_loader_free();
+   _vg_loader_free();
 
    vg_success( "If you see this it means everything went.. \"well\".....\n" );
 
@@ -832,47 +816,30 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
    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 );
+      SDL_AtomicLock( &vg.sl_context );
       vg.engine_status = k_engine_status_crashed;
       vg.str_const_engine_err = error;
+      SDL_AtomicUnlock( &vg.sl_context );
 
-      /* 
-       * 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) )
+      /* Notify other thread for curtosey */
+      if( vg_thread_purpose() == k_thread_purpose_main )
       {
+         SDL_AtomicLock( &vg.sl_context );
+
+         if( vg.exec_context != 0 )
+            SDL_SemPost( vg.sem_allow_exec );
+
          SDL_AtomicUnlock( &vg.sl_context );
-         SDL_SemWait( vg.sem_loader );
       }
-      else
-         SDL_AtomicUnlock( &vg.sl_context );
 
-      vg_audio_free(NULL);
+      vg_audio_free();
 
       while(1)
       {
-         vg_process_events();
+         _vg_process_events();
          if( vg.window_should_close )
             break;
 
@@ -889,12 +856,12 @@ VG_STATIC void vg_fatal_exit_loop( const char *error )
          glClear( GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT );
          glViewport( 0,0, vg.window_x, vg.window_y );
 
-         vg_render_log();
+         _vg_render_log();
          SDL_GL_SwapWindow( vg.window );
       }
 
       /* Can now shutdown and EXIT */
-      vg_loader_free();
+      _vg_loader_free();
       SDL_GL_DeleteContext( vg.gl_context );
       SDL_Quit();
       exit(0);
index 46c9e7331a90f2c4ec5534eec9754699a3c5e404..d5ad5d26ec422ea940dd48ab020f84988f17447e 100644 (file)
@@ -3,6 +3,8 @@
 #ifndef VG_AUDIO_H
 #define VG_AUDIO_H
 
+#define VG_GAME
+
 #include "vg/vg.h"
 #include "vg/vg_stdint.h"
 #include "vg/vg_platform.h"
@@ -11,6 +13,7 @@
 #include "vg/vg_ui.h"
 #include "vg/vg_console.h"
 #include "vg/vg_store.h"
+#include "vg/vg_profiler.h"
 
 #include <sys/time.h>
 
@@ -267,7 +270,7 @@ VG_STATIC void vg_audio_init(void)
    vg_success( "Ready\n" );
 }
 
-VG_STATIC void vg_audio_free(void * nothing)
+VG_STATIC void vg_audio_free(void)
 {
    SDL_CloseAudioDevice( vg_audio.sdl_output_device );
 }
@@ -837,6 +840,7 @@ VG_STATIC void audio_require_init( audio_player *player )
    if( player->init )
       return;
 
+   audio_unlock();
    vg_fatal_exit_loop( "Must init audio player before playing! \n" );
 }
 
@@ -845,6 +849,7 @@ VG_STATIC void audio_require_clip_loaded( audio_clip *clip )
    if( clip->data && clip->size )
       return;
 
+   audio_unlock();
    vg_fatal_exit_loop( "Must load audio clip before playing! \n" );
 }
 
index 15c605c64f01c5e0f79842bfadd1ac0cb428ff24..8d35aa286fdce8933e35b39720cd5dfd6534d850 100644 (file)
@@ -68,16 +68,16 @@ struct vg_console
 }
 vg_console;
 
-VG_STATIC void vg_convar_push( struct vg_convar cv );
-VG_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 );
 
-VG_STATIC void vg_console_draw( void );
-void vg_console_println( const char *str );
-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 );
+VG_STATIC void _vg_console_draw( void );
+void           _vg_console_println( const char *str );
+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  vg_execute_console_input( const char *cmd );
 
 /*
  * Console interface
@@ -91,13 +91,13 @@ 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 int vg_console_enabled(void);
+VG_STATIC int _vg_console_enabled(void);
 VG_STATIC void console_proc_key( SDL_Keysym ev );
 
 /*
  * Implementation
  */
-VG_STATIC int vg_console_enabled(void) 
+VG_STATIC int _vg_console_enabled(void) 
 { 
        return vg_console.enabled; 
 }
@@ -119,7 +119,7 @@ VG_STATIC void vg_function_push( struct vg_cmd cmd )
    vg_console.functions[ vg_console.function_count ++ ] = cmd;
 }
 
-VG_STATIC void vg_console_draw( void )
+VG_STATIC void _vg_console_draw( void )
 {
        if( !vg_console.enabled )
                return;
@@ -178,7 +178,7 @@ VG_STATIC void vg_console_draw( void )
    SDL_AtomicUnlock( &log_print_sl );
 }
 
-VG_STATIC int vg_console_list( int argc, char const *argv[] )
+VG_STATIC int _vg_console_list( int argc, char const *argv[] )
 {
        for( int i=0; i<vg_console.function_count; i ++ )
        {
@@ -201,12 +201,12 @@ int _test_break( int argc, const char *argv[] )
    return 0;
 }
 
-VG_STATIC void vg_console_init(void)
+VG_STATIC void _vg_console_init(void)
 {
        vg_function_push( (struct vg_cmd)
    {
                .name = "list",
-               .function = vg_console_list
+               .function = _vg_console_list
        });
 
    vg_function_push( (struct vg_cmd)
@@ -230,7 +230,7 @@ VG_STATIC void vg_console_load_autos(void)
 
                        if( line[0] != 0x00 )
                        {
-                               execute_console_input( line );
+                               vg_execute_console_input( line );
                        }
                }
 
@@ -238,7 +238,7 @@ VG_STATIC void vg_console_load_autos(void)
        }
 }
 
-VG_STATIC void vg_console_write_persistent(void)
+VG_STATIC void _vg_console_write_persistent(void)
 {
        FILE *fp = fopen( "cfg/auto.conf", "w" );
        
@@ -266,12 +266,12 @@ VG_STATIC void vg_console_write_persistent(void)
        fclose( fp );
 }
 
-VG_STATIC void vg_console_free(void)
+VG_STATIC void _vg_console_free(void)
 {
-       vg_console_write_persistent();
+       _vg_console_write_persistent();
 }
 
-VG_STATIC void execute_console_input( const char *cmd )
+VG_STATIC void vg_execute_console_input( const char *cmd )
 {
        char temp[512];
        char const *args[9];
@@ -635,7 +635,7 @@ VG_STATIC void _console_enter(void)
    }
 
    vg_console.history_pos = -1;
-   execute_console_input( vg_console.input );
+   vg_execute_console_input( vg_console.input );
    console_move_cursor( &vg_console.cursor_user, 
                         &vg_console.cursor_pos, -10000, 1 );
    vg_console.input[0] = '\0';
index f97a41af28d161fbf95687319ed57931ac5bf862..d8e398cf8d9edd9e8c995445913307ea1b811a59 100644 (file)
@@ -605,7 +605,7 @@ VG_STATIC void vg_input_init(void)
    vg_release_thread_sync();
 }
 
-VG_STATIC void vg_input_free(void *_)
+VG_STATIC void vg_input_free(void)
 {
    if( vg_input.controller_handle )
    {
index 6d8be1b064f1fc3a2cb391ed8431c054a2c69780..b47d759ec60a1fb676f3fd94ca333b3c90727767 100644 (file)
@@ -12,6 +12,9 @@
 
 #include "common.h"
 
+VG_STATIC void vg_loader_start( void(*pfn)(void) );
+VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) );
+
 static struct vg_shader _shader_loader = 
 {
    .name = "[vg] loader",
@@ -40,12 +43,12 @@ static struct vg_shader _shader_loader =
       "void main()"
       "{"
          "float dither=fract(dot(vec2(171.0,231.0),gl_FragCoord.xy)/71.0)-0.5;"
-         "float grad = 1.0-(aUv.y*0.5+0.5);"
+         "float grad = 1.0-(aUv.y*0.5+0.5)*0.5;"
          "float fmt1 = step( 0.5, grad+dither );"
 
          "vec3 col = 0.5+0.5*sin( uTime + aUv.xyx + vec3(0.0,2.0,4.0) );"
 
-         "FragColor = vec4(col*grad*fmt1,1.0);"
+         "FragColor = vec4(vec3(0.5,0.5,0.5)*grad*fmt1,1.0);"
       "}"
    }
 };
@@ -55,8 +58,7 @@ static struct vg_loader
    /* Shutdown steps */
    struct loader_free_step
    {
-      void (*fn_free)(void *);
-      void *data;
+      void (*fn_free)(void);
    }
    step_buffer[16];
    u32 step_count, step_action;
@@ -65,7 +67,7 @@ static struct vg_loader
 }
 vg_loader;
 
-VG_STATIC void 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 };
@@ -85,7 +87,7 @@ VG_STATIC void vg_loader_init(void)
       vg_fatal_exit_loop( "failed to compile shader" );
 }
 
-VG_STATIC void vg_loader_free(void)
+VG_STATIC void _vg_loader_free(void)
 {
    vg_info( "vg_loader_free\n" );
    glDeleteVertexArrays( 1, &vg_loader.vao );
@@ -97,23 +99,13 @@ VG_STATIC void vg_loader_free(void)
          &vg_loader.step_buffer[vg_loader.step_count -1 -i];
 
       vg_info( " -> %p\n", step->fn_free );
-      step->fn_free( step->data );
+      step->fn_free();
    }
 
    vg_info( "done\n" );
 }
 
-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;
-   if(t < 1.0f/6.0f) return p + (q - p) * 6.0f * t;
-   if(t < 1.0f/2.0f) return q;
-   if(t < 2.0f/3.0f) return p + (q - p) * (2.0f/3.0f - t) * 6.0f;
-   return p;
-}
-
-VG_STATIC void vg_render_log(void)
+VG_STATIC void _vg_render_log(void)
 {
    ui_begin( vg.window_x, vg.window_y );
    SDL_AtomicLock( &log_print_sl );
@@ -147,7 +139,7 @@ VG_STATIC void vg_render_log(void)
    ui_draw( NULL );
 }
 
-VG_STATIC void vg_loader_render(void)
+VG_STATIC void _vg_loader_render(void)
 {
    glViewport( 0,0, vg.window_x, vg.window_y );
    glBindFramebuffer( GL_FRAMEBUFFER, 0 );
@@ -160,52 +152,44 @@ VG_STATIC void vg_loader_render(void)
    glBindVertexArray( vg_loader.vao );
    glDrawArrays( GL_TRIANGLES, 0, 6 );
 
-   vg_render_log();
+   _vg_render_log();
 }
 
 
 VG_STATIC void vg_load_full(void);
 
-VG_STATIC int vg_loader_thread(void * nothing)
+VG_STATIC int _vg_loader_thread(void *pfn)
 {
    SDL_AtomicLock( &vg.sl_context );
    vg.thread_id_loader = SDL_GetThreadID(NULL);
+   VG_SYNC_LOG( "[%d] Loader thread begins\n" );
    SDL_AtomicUnlock( &vg.sl_context );
 
    /* Run client loader */
-   vg_load_full();
+   void (*call_func)(void) = pfn;
+   call_func();
 
    SDL_SemPost( vg.sem_loader );
    vg.thread_id_loader = 0;
 
+   vg_acquire_thread_sync();
+   vg.is_loaded = 1;
+   vg_release_thread_sync();
+
    return 0;
 }
 
-VG_STATIC void vg_loader_start(void)
+VG_STATIC void vg_loader_start( void(*pfn)(void) )
 {
+   vg.is_loaded = 0;
    SDL_SemWait( vg.sem_loader );
-   SDL_CreateThread( vg_loader_thread, "Loader thread", NULL );
-}
-
-/* this is maybe probably unused now */
-VG_STATIC void vg_free_libc_malloced( void *data )
-{
-   free( data );
-}
-
-VG_STATIC void vg_loader_push_free_step( struct loader_free_step step )
-{
-   if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
-      vg_fatal_exit_loop( "Too many free steps" );
-
-   vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
+   SDL_CreateThread( _vg_loader_thread, "Loader thread", pfn );
 }
 
 /*
  * Schedule something to be ran now, freed later. Checks in with engine status
  */
-VG_STATIC void vg_loader_highwater( void( *fn_load )(void), 
-                                 void( *fn_free )(void *), void *data )
+VG_STATIC void vg_loader_step( void( *fn_load )(void), void( *fn_free )(void) )
 {
    if( fn_load )
       fn_load();
@@ -213,24 +197,15 @@ VG_STATIC void vg_loader_highwater( void( *fn_load )(void),
    if( fn_free )
    {
       struct loader_free_step step;
-      step.data = data;
       step.fn_free = fn_free;
 
-      vg_loader_push_free_step( step );
-   }
-   else
-   {
-      if( data )
-      {
-         struct loader_free_step step;
-         step.data = data;
-         step.fn_free = vg_free_libc_malloced;
-
-         vg_loader_push_free_step( step );
-      }
+      if( vg_loader.step_count == vg_list_size(vg_loader.step_buffer) )
+         vg_fatal_exit_loop( "Too many free steps" );
+
+      vg_loader.step_buffer[ vg_loader.step_count ++ ] = step;
    }
 
-   vg_ensure_engine_running();
+   _vg_ensure_engine_running();
 }
 
 #endif /* VG_LOADER_H */
index dfb4fc91f8d4851be6fc34ca06dd4da5e4057144..9f3ae9e0976c0a4bb62bba7dcb9affe2cefc159a 100644 (file)
--- a/vg_log.h
+++ b/vg_log.h
@@ -27,7 +27,7 @@ struct vg_log
 }
 static vg_log;
 
-void vg_console_append_to_log( const char *str )
+void _vg_console_append_to_log( const char *str )
 {
    if( vg_log.buffer_line_count < vg_list_size( vg_log.buffer ) )
       vg_log.buffer_line_count ++;
@@ -49,8 +49,8 @@ void vg_console_append_to_log( const char *str )
       vg_log.buffer_line_current = 0;
 }
 
-VG_STATIC void vg_log_write( FILE *file, const char *prefix, 
-      const char *fmt, va_list args )
+VG_STATIC void _vg_log_write( FILE *file, const char *prefix, 
+                              const char *fmt, va_list args )
 {
 #ifdef VG_GAME
    SDL_AtomicLock( &log_print_sl );
@@ -72,22 +72,22 @@ VG_STATIC void vg_log_write( FILE *file, const char *prefix,
        
        fputs( buffer, file );
    
-   vg_console_append_to_log( buffer );
+   _vg_console_append_to_log( buffer );
 #ifdef VG_GAME
    SDL_AtomicUnlock( &log_print_sl );
 #endif
 }
 
-VG_STATIC void vg_log_init(void)
+VG_STATIC void _vg_log_init(void)
 {
 }
 
 #define VG_LOGX( NAME, PIPE, PFX )           \
-VG_STATIC void NAME(const char *fmt, ...)       \
+VG_STATIC void NAME(const char *fmt, ...)    \
 {                                            \
    va_list args;                             \
    va_start( args, fmt );                    \
-   vg_log_write( PIPE, (PFX), fmt, args );   \
+   _vg_log_write( PIPE, (PFX), fmt, args );   \
    va_end( args );                           \
 }
 
diff --git a/vg_m.h b/vg_m.h
index 7403320df29eb5d3ba2d8a0c5f778172d43c4a2c..703c5e5823b4bc0c459c445a06b069f6ada1dceb 100644 (file)
--- a/vg_m.h
+++ b/vg_m.h
@@ -880,6 +880,21 @@ static inline int box_overlap( boxf a, boxf b )
    ;
 }
 
+static int box_within( boxf greater, boxf lesser )
+{
+   v3f a, b;
+   v3_sub( lesser[0], greater[0], a );
+   v3_sub( lesser[1], greater[1], b );
+
+   if( (a[0] >= 0.0f) && (a[1] >= 0.0f) && (a[2] >= 0.0f) &&
+       (b[0] <= 0.0f) && (b[1] <= 0.0f) && (b[2] <= 0.0f) )
+   {
+      return 1;
+   }
+
+   return 0;
+}
+
 static inline void box_init_inf( boxf box )
 {
    v3_fill( box[0],  INFINITY );
index 784fc287ebf7ea0297dda48276f04a502f21c5ab..7be848aa3ee6937d930dc3f16fb7abd190594017 100644 (file)
@@ -1,6 +1,7 @@
 #ifndef VG_PROFILER_H
 #define VG_PROFILER_H
 
+#define VG_GAME
 #include "vg.h"
 #include "vg_platform.h"
 
@@ -12,7 +13,7 @@ struct vg_profile
 {
    const char *name;
 
-   u32 samples[ VG_PROFILE_SAMPLE_COUNT ];
+   u64 samples[ VG_PROFILE_SAMPLE_COUNT ];
    u32 buffer_count, buffer_current;
 
    enum profile_mode
@@ -40,15 +41,13 @@ VG_STATIC void vg_profile_increment( struct vg_profile *profile )
    if( profile->buffer_current >= VG_PROFILE_SAMPLE_COUNT )
       profile->buffer_current = 0;
 
-   profile->samples[ profile->buffer_current ] = 0.0f;
+   profile->samples[ profile->buffer_current ] = 0;
 }
 
 VG_STATIC void vg_profile_end( struct vg_profile *profile )
 {
-   u64 time_end;
-
-   time_end = SDL_GetPerformanceCounter();
-   u64 delta = profile->start - time_end;
+   u64 time_end = SDL_GetPerformanceCounter(),
+       delta    = time_end - profile->start;
 
    if( profile->mode == k_profile_mode_frame )
    {
@@ -91,7 +90,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
    u32 colours[] = { 0xff0000ff, 0xff00ff00, 0xff00ffff, 0xffff0000,
                      0xffff00ff, 0xffffff00 };
 
-   float rate_mul = 1.0f / (float)SDL_GetPerformanceFrequency();
+   double rate_mul = 1000.0 / (double)SDL_GetPerformanceFrequency();
 
    for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ )
    {
@@ -104,7 +103,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
          if( ptrs[j] < 0 )
             ptrs[j] = VG_PROFILE_SAMPLE_COUNT-1;
 
-         float sample  = (float)profiles[j]->samples[ptrs[j]] * rate_mul,
+         float sample  = (double)profiles[j]->samples[ptrs[j]] * rate_mul,
                px      = (total  / (budget)) * sw,
                wx      = (sample / (budget)) * sw;
 
index 516d1a84c67587c7e44d56fe2853c870de252138..2ee1ef4f457143f7acbd73d92787f5639071d679 100644 (file)
@@ -3,6 +3,7 @@
 #ifndef VG_SHADER_H
 #define VG_SHADER_H
 
+#define VG_GAME
 #include "vg/vg.h"
 #include "vg/vg_platform.h"
 
diff --git a/vg_ui.h b/vg_ui.h
index 0c4b4159bc5636419e86f430fe185aac93410adb..8e73da5a1ab5a208432ba27e359a9b77f6653da2 100644 (file)
--- a/vg_ui.h
+++ b/vg_ui.h
@@ -3,6 +3,7 @@
 #ifndef VG_UI_H
 #define VG_UI_H
 
+#define VG_GAME
 #include "vg/vg.h"
 #include "vg/vg_tex.h"
 #include "vg/vg_shader.h"
@@ -167,7 +168,7 @@ static ui_colourset ui_default_colours = {
        .active = 0xffad9f9e
 };
 
-VG_STATIC void ui_init_context(void)
+VG_STATIC void _vg_ui_init(void)
 {
    if( !vg_shader_compile( &_shader_ui ) ) 
       vg_fatal_exit_loop( "Failed to compile ui shader" );