dusting
authorhgn <hgodden00@gmail.com>
Thu, 13 Apr 2023 04:51:30 +0000 (05:51 +0100)
committerhgn <hgodden00@gmail.com>
Thu, 13 Apr 2023 04:51:30 +0000 (05:51 +0100)
vg.h
vg_audio.h
vg_console.h
vg_input.h
vg_lines.h
vg_profiler.h

diff --git a/vg.h b/vg.h
index 40767a2a0746cf20fce277d0eb8d658f7c936eb0..3377b3147a8813e57ac596603d4146569331730e 100644 (file)
--- a/vg.h
+++ b/vg.h
@@ -370,12 +370,7 @@ VG_STATIC void vg_checkgl( const char *src_info )
 VG_STATIC void vg_bake_shaders(void)
 {
    vg_acquire_thread_sync();
-
-   vg_function_push( (struct vg_cmd)
-   {
-      .name = "reload_shaders",
-      .function = vg_shaders_live_recompile
-   });
+   vg_console_reg_cmd( "reload_shaders", vg_shaders_live_recompile, NULL );
 
    vg_shaders_compile();
    vg_release_thread_sync();
index 3ba7093030c57547c25be468f2bf5097fa479f13..cf6651ed6930e4777c3dff5d18aeef3affe955b6 100644 (file)
 #define AUDIO_FLAG_NO_DOPPLER 0x2
 #define AUDIO_FLAG_SPACIAL_3D 0x4
 #define AUDIO_FLAG_AUTO_START 0x8
-
-/* Vorbis will ALWAYS use the maximum amount of channels it can */
-//#define AUDIO_FLAG_MONO       0x100 NOTE: This is the default, so its not used
-//#define AUDIO_FLAG_STEREO     0x200
-//#define AUDIO_FLAG_VORBIS     0x400
-//#define AUDIO_FLAG_BIRD_SYNTH 0x800
-
 #define AUDIO_FLAG_FORMAT     0x1E00
 
 enum audio_format
@@ -266,32 +259,14 @@ VG_STATIC void audio_mixer_callback( void *user, u8 *stream, int frame_count );
 VG_STATIC void vg_audio_init(void)
 {
    /* TODO: Move here? */
-   vg_var_push( (struct vg_var){
-      .name = "debug_audio",
-      .data = &vg_audio.debug_ui,
-      .data_type = k_var_dtype_i32,
-      .opt_i32 = { .min=0, .max=1, .clamp=1 },
-      .persistent = 1
-   });
-
-   vg_var_push( (struct vg_var){
-      .name = "debug_dsp",
-      .data = &vg_audio.debug_dsp,
-      .data_type = k_var_dtype_i32,
-      .opt_i32 = { .min=0, .max=1, .clamp=1 },
-      .persistent = 1
-   });
-
-   vg_var_push( (struct vg_var){
-      .name = "volume",
-      .data = &vg_audio.external_global_volume,
-      .data_type = k_var_dtype_f32,
-      .opt_f32 = { .min=0.0f, .max=2.0f, .clamp=1 },
-      .persistent = 1
-   });
+   vg_console_reg_var( "debug_audio", &vg_audio.debug_ui, 
+                        k_var_dtype_i32, VG_VAR_CHEAT );
+   vg_console_reg_var( "debug_dsp", &vg_audio.debug_dsp,
+                        k_var_dtype_i32, VG_VAR_CHEAT );
+   vg_console_reg_var( "volume", &vg_audio.external_global_volume,
+                        k_var_dtype_f32, VG_VAR_PERSISTENT );
 
    /* allocate memory */
-
    /* 32mb fixed */
    vg_audio.audio_pool = 
       vg_create_linear_allocator( vg_mem.rtmemory, 1024*1024*32, 
index 26c6a6d20c00414eef330fa18f7500b4caf52dd9..9f2cba4eb647f03e2cdc867e7b2f4e9acefaa863 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2021-2022 Harry Godden (hgn) - All Rights Reserved */
+/* Copyright (C) 2021-2023 Harry Godden (hgn) - All Rights Reserved */
 
 #ifndef VG_CONSOLE_H
 #define VG_CONSOLE_H
 #include "vg/vg_ui.h"
 #include "vg/vg_log.h"
 
-#define VG_VAR_F32_PERSISTENT( NAME )  \
-   vg_var_push( (struct vg_var){       \
-      .name = #NAME,                   \
-      .data = &NAME,                   \
-      .data_type = k_var_dtype_f32,    \
-      .persistent = 1                  \
-   });
-
-#define VG_VAR_F32( NAME )             \
-   vg_var_push( (struct vg_var){       \
-      .name = #NAME,                   \
-      .data = &NAME,                   \
-      .data_type = k_var_dtype_f32,    \
-   });
-
-#define VG_VAR_I32_PERSISTENT( NAME )  \
-   vg_var_push( (struct vg_var){       \
-      .name = #NAME,                   \
-      .data = &NAME,                   \
-      .data_type = k_var_dtype_i32,    \
-      .persistent = 1                  \
-   });
-
-#define VG_VAR_I32( NAME )             \
-   vg_var_push( (struct vg_var){       \
-      .name = #NAME,                   \
-      .data = &NAME,                   \
-      .data_type = k_var_dtype_i32,    \
-   });
+#define VG_VAR_F32( NAME, ... ) \
+   { u32 flags=0x00; __VA_ARGS__ ;\
+   vg_console_reg_var( #NAME, &NAME, k_var_dtype_f32, flags ); }
+
+#define VG_VAR_I32( NAME, ... ) \
+   { u32 flags=0x00; __VA_ARGS__ ;\
+   vg_console_reg_var( #NAME, &NAME, k_var_dtype_i32, flags ); }
+
+#define VG_VAR_PERSISTENT 0x1
+#define VG_VAR_CHEAT      0x2
 
 typedef struct vg_var vg_var;
 typedef struct vg_cmd vg_cmd;
 
-struct vg_console
-{
-       struct vg_var
-       {
+struct vg_console{
+       struct vg_var{
                void *data;
                const char *name;
                
-               enum vg_var_dtype
-               {
+               enum vg_var_dtype{
                        k_var_dtype_i32,
                        k_var_dtype_u32,
                        k_var_dtype_f32
                } 
                data_type;
-               
-               union
-               {
-                       struct
-                       {
-                               int min, max, clamp;
-                       } 
-         opt_i32;
-
-         struct
-         {
-            float min, max;
-            int clamp;
-         }
-         opt_f32;
-               };
 
-               int persistent; /* Should this var be stored to cfg/auto.conf? */
+      u32 flags;
        } 
        vars[ 128 ];
        
-       struct vg_cmd
-       {
+       struct vg_cmd{
                int  (*function)( int argc, char const *argv[] );
       void (*poll_suggest)( int argc, char const *argv[] );
                const char *name;
        }
        functions[ 32 ];
 
-   struct 
-   {
+   struct {
       const char *str;
       int len;
 
@@ -112,8 +72,6 @@ struct vg_console
 }
 vg_console;
 
-VG_STATIC void  vg_var_push( struct vg_var 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 );
@@ -146,24 +104,40 @@ VG_STATIC int _vg_console_enabled(void)
        return vg_console.enabled; 
 }
 
-VG_STATIC void vg_var_push( vg_var cv )
+VG_STATIC 
+void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type, 
+                         u32 flags )
 {
    if( vg_console.var_count > vg_list_size(vg_console.vars) )
       vg_fatal_exit_loop( "Too many vars registered" );
 
-   vg_info( "Console variable '%s' registered\n", cv.name );
-   vg_console.vars[ vg_console.var_count ++ ] = cv;
+   vg_var *var = &vg_console.vars[ vg_console.var_count ++ ];
+   var->name = alias;
+   var->data = ptr;
+   var->data_type = type;
+   var->flags = flags;
+
+   vg_info( "Console variable '%s' registered\n", alias );
 }
 
-VG_STATIC void vg_function_push( struct vg_cmd cmd )
+VG_STATIC 
+void vg_console_reg_cmd( const char *alias,
+                         int (*function)(int argc, const char *argv[]),
+                         void (*poll_suggest)(int argc, const char *argv[]) )
 {
    if( vg_console.function_count > vg_list_size(vg_console.functions) )
       vg_fatal_exit_loop( "Too many functions registered" );
 
-   vg_console.functions[ vg_console.function_count ++ ] = cmd;
+   vg_cmd *cmd = &vg_console.functions[ vg_console.function_count ++ ];
+
+   cmd->function = function;
+   cmd->poll_suggest = poll_suggest;
+   cmd->name = alias;
+
+   vg_info( "Console function '%s' registered\n", alias );
 }
 
-VG_STATIC void _vg_console_draw( void )
+VG_STATIC void _vg_console_draw(void)
 {
        if( !vg_console.enabled )
                return;
@@ -190,8 +164,7 @@ VG_STATIC void _vg_console_draw( void )
                vg_uictx.cursor[3] = fh;
                ui_align_bottom();
 
-               for( int i=0; i<console_lines; i ++ )
-               {
+               for( int i=0; i<console_lines; i ++ ){
                        ptr --;
 
                        if( ptr < 0 )
@@ -227,8 +200,7 @@ VG_STATIC void _vg_console_draw( void )
 
 
    /* suggestions */
-   if( vg_console.suggestion_count )
-   {
+   if( vg_console.suggestion_count ){
       vg_uictx.cursor[0] += UI_GLYPH_SPACING_X * vg_console.suggestion_pastepos;
       vg_uictx.cursor[1] += 2;
       vg_uictx.cursor[3] = vg_console.suggestion_count * fh;
@@ -239,8 +211,7 @@ VG_STATIC void _vg_console_draw( void )
          ui_fill_rect( vg_uictx.cursor, 0x77040404 );
 
          vg_uictx.cursor[3] = fh;
-         for( int i=0; i<vg_console.suggestion_count; i ++ )
-         {
+         for( int i=0; i<vg_console.suggestion_count; i ++ ){
             if( i == vg_console.suggestion_select )
                ui_fill_rect( vg_uictx.cursor, 0x66a0e508 );
 
@@ -256,14 +227,12 @@ VG_STATIC void _vg_console_draw( void )
 
 VG_STATIC int _vg_console_list( int argc, char const *argv[] )
 {
-       for( int i=0; i<vg_console.function_count; i ++ )
-       {
+       for( int i=0; i<vg_console.function_count; i ++ ){
                struct vg_cmd *cmd = &vg_console.functions[ i ];
                vg_info( "* %s\n", cmd->name );
        }
        
-       for( int i=0; i<vg_console.var_count; i ++ )
-       {
+       for( int i=0; i<vg_console.var_count; i ++ ){
                struct vg_var *cv = &vg_console.vars[ i ];
                vg_info( "%s\n", cv->name );
        }
@@ -277,55 +246,56 @@ int _test_break( int argc, const char *argv[] )
    return 0;
 }
 
-VG_STATIC void _vg_console_init(void)
+int _vg_console_exec( int argc, const char *argv[] )
 {
-       vg_function_push( (struct vg_cmd)
-   {
-               .name = "list",
-               .function = _vg_console_list
-       });
+   if( argc < 1 ) return 0;
 
-   vg_function_push( (struct vg_cmd)
-   {
-      .name = "crash",
-      .function = _test_break
-   });
-}
+   char path[256];
+   strcpy( path, "cfg/" );
+   strncat( path, argv[0], 250 );
 
-VG_STATIC void vg_console_load_autos(void)
-{
-       /* Read and exec persistent commands */
-       FILE *fp = fopen( "cfg/auto.conf", "r" );
-       if( fp )
-       {
+       FILE *fp = fopen( path, "r" );
+       if( fp ){
                char line[256];
 
-               while( fgets( line, sizeof( line ), fp ) )
-               {
+               while( fgets( line, sizeof( line ), fp ) ){
                        line[ strcspn( line, "\r\n#" ) ] = 0x00;
 
-                       if( line[0] != 0x00 )
-                       {
+                       if( line[0] != 0x00 ){
                                vg_execute_console_input( line );
                        }
                }
 
                fclose( fp );
        }
+   else{
+      vg_error( "Could not open '%s'\n", path );
+   }
+
+   return 0;
+}
+
+VG_STATIC void _vg_console_init(void)
+{
+   vg_console_reg_cmd( "list", _vg_console_list, NULL );
+   vg_console_reg_cmd( "crash", _test_break, NULL );
+   vg_console_reg_cmd( "exec", _vg_console_exec, NULL );
+}
+
+VG_STATIC void vg_console_load_autos(void)
+{
+   _vg_console_exec( 1, (const char *[]){ "auto.conf" } );
 }
 
 VG_STATIC void _vg_console_write_persistent(void)
 {
        FILE *fp = fopen( "cfg/auto.conf", "w" );
        
-       for( int i=0; i<vg_console.var_count; i ++ )
-       {
+       for( int i=0; i<vg_console.var_count; i ++ ){
                struct vg_var *cv = &vg_console.vars[i];
 
-               if( cv->persistent )
-               {
-                       switch( cv->data_type )
-                       {
+               if( cv->flags & VG_VAR_PERSISTENT ){
+                       switch( cv->data_type ){
                                case k_var_dtype_i32:
                                        fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
                                break;
@@ -358,12 +328,9 @@ VG_STATIC int vg_console_tokenize( const char *src, char *dst,
        int arg_count = 0,
        in_token = 0;
        
-       for( int i=0; 1; i ++ )
-       {
-               if( src[i] )
-               {
-                       if( src[i] == ' ' || src[i] == '\t' )
-                       {
+       for( int i=0;; i ++ ){
+               if( src[i] ){
+                       if( src[i] == ' ' || src[i] == '\t' ){
             if( in_token )
                dst[i] = '\0';
 
@@ -372,19 +339,16 @@ VG_STATIC int vg_console_tokenize( const char *src, char *dst,
                                if( arg_count == 8 )
                                        break;
                        }
-                       else
-                       {
+                       else{
                                dst[i] = src[i];
                                
-                               if( !in_token )
-                               {
+                               if( !in_token ){
                                        args[ arg_count ++ ] = &dst[i];
                                        in_token = 1;
                                }
                        }
                }
-               else
-               {
+               else{
                        dst[i] = '\0';
                        break;
                }
@@ -395,11 +359,9 @@ VG_STATIC int vg_console_tokenize( const char *src, char *dst,
 
 VG_STATIC vg_var *vg_console_match_var( const char *kw )
 {
-       for( int i=0; i<vg_console.var_count; i ++ )
-       {
+       for( int i=0; i<vg_console.var_count; i ++ ){
                struct vg_var *cv = &vg_console.vars[ i ];
-               if( !strcmp( cv->name, kw ) )
-               {
+               if( !strcmp( cv->name, kw ) ){
          return cv;
       }
    }
@@ -409,11 +371,9 @@ VG_STATIC vg_var *vg_console_match_var( const char *kw )
 
 VG_STATIC vg_cmd *vg_console_match_cmd( const char *kw )
 {
-       for( int i=0; i<vg_console.function_count; i ++ )
-       {
+       for( int i=0; i<vg_console.function_count; i ++ ){
                struct vg_cmd *cmd = &vg_console.functions[ i ];
-               if( !strcmp( cmd->name, kw ) )
-               {
+               if( !strcmp( cmd->name, kw ) ){
          return cmd;
       }
    }
@@ -429,40 +389,27 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
        
        if( arg_count == 0 )
                return;
-       
-       int data_int;
-   float data_float;
 
    vg_var *cv = vg_console_match_var( args[0] );
    vg_cmd *fn = vg_console_match_cmd( args[0] );
 
    assert( !(cv && fn) );
 
-   if( cv )
-   {
+   if( cv ){
       /* Cvar Matched, try get value */
-      if( arg_count >= 2 )
-      {
+      if( arg_count >= 2 ){
          if( (cv->data_type == k_var_dtype_u32) ||
              (cv->data_type == k_var_dtype_i32) )
          {
-            data_int = atoi( args[1] ); 
-            
-            *((int *)cv->data) = cv->opt_i32.clamp? 
-             VG_MIN( VG_MAX(data_int, cv->opt_i32.min), cv->opt_i32.max ): 
-             data_int;
+            int *ptr = cv->data;
+            *ptr = atoi( args[1] ); 
          }
-         else if( cv->data_type == k_var_dtype_f32 )
-         {
-            data_float = atof( args[1] );
-            *((float *)cv->data) = cv->opt_f32.clamp?
-               vg_minf( vg_maxf( data_float, cv->opt_f32.min), 
-                        cv->opt_f32.max ):
-               data_float;
+         else if( cv->data_type == k_var_dtype_f32 ){
+            float *ptr = cv->data;
+            *ptr = atof( args[1] );
          }
       }
-      else
-      {
+      else{
          if( cv->data_type == k_var_dtype_i32 )
             vg_info( "= %d\n", *((int *)cv->data) ); 
          else if( cv->data_type == k_var_dtype_u32 )
@@ -473,8 +420,7 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
    
       return;
        }
-   else if( fn )
-   {
+   else if( fn ){
       fn->function( arg_count-1, args+1 );
       return;
    }
@@ -498,20 +444,17 @@ u32 str_lev_distance( const char *s1, const char *s2 )
       costs[k] = k;
 
    u32 i = 0;
-   for( u32 i=0; i<m; i++ )
-   {
+   for( u32 i=0; i<m; i++ ){
       costs[0] = i+1;
 
       u32 corner = i;
 
-      for( u32 j=0; j<n; j++ )
-      {
+      for( u32 j=0; j<n; j++ ){
          u32 upper = costs[j+1];
 
          if( s1[i] == s2[j] )
             costs[ j+1 ] = corner;
-         else
-         {
+         else{
             u32 t = (upper < corner)? upper: corner;
             costs[j+1] = ((costs[j] < t)? costs[j]: t) + 1;
          }
@@ -531,14 +474,11 @@ u32 str_lcs( const char *s1, const char *s2 )
    int suff[32][32],
        result = 0;
 
-   for( int i=0; i<=m; i++ )
-   {
-      for( int j=0; j<=n; j++ )
-      {
+   for( int i=0; i<=m; i++ ){
+      for( int j=0; j<=n; j++ ){
          if( i == 0 || j == 0 )
             suff[i][j] = 0;
-         else if( s1[i-1] == s2[j-1] )
-         {
+         else if( s1[i-1] == s2[j-1] ){
             suff[i][j] = suff[i-1][j-1] + 1;
             result = VG_MAX( result, suff[i][j] );
          }
@@ -571,8 +511,7 @@ VG_STATIC void console_suggest_score_text( const char *str, const char *input,
          best_pos = j;
    
    /* insert if good score */
-   if( best_pos < vg_list_size( vg_console.suggestions ) )
-   {
+   if( best_pos < vg_list_size( vg_console.suggestions ) ){
       int start = VG_MIN( vg_console.suggestion_count, 
                           vg_list_size( vg_console.suggestions )-1 );
       for( int j=start; j>best_pos; j -- )
@@ -622,22 +561,18 @@ VG_STATIC void console_update_suggestions(void)
    vg_console.suggestion_pastepos = args[token_count-1]-temp;
 
    /* Score all our commands and cvars */
-   if( token_count == 1 )
-   {
-      for( int i=0; i<vg_console.var_count; i++ )
-      {
+   if( token_count == 1 ){
+      for( int i=0; i<vg_console.var_count; i++ ){
          vg_var *cvar = &vg_console.vars[i];
          console_suggest_score_text( cvar->name, args[0], 1 );
       }
 
-      for( int i=0; i<vg_console.function_count; i++ )
-      {
+      for( int i=0; i<vg_console.function_count; i++ ){
          vg_cmd *cmd = &vg_console.functions[i];
          console_suggest_score_text( cmd->name, args[0], 1 );
       }
    }
-   else
-   {
+   else{
       vg_cmd *cmd = vg_console_match_cmd( args[0] );
       vg_var *var = vg_console_match_var( args[0] );
 
@@ -649,8 +584,7 @@ VG_STATIC void console_update_suggestions(void)
    }
 
    /* some post processing */
-   for( int i=0; i<vg_console.suggestion_count; i++ )
-   {
+   for( int i=0; i<vg_console.suggestion_count; i++ ){
       vg_console.suggestion_maxlen = VG_MAX( vg_console.suggestion_maxlen,
                                              vg_console.suggestions[i].len );
 
@@ -709,8 +643,7 @@ VG_STATIC int console_delete_char( int direction )
        console_make_selection( &start, &end );
        
        /* There is no selection */
-       if( !(end-start) )
-       {
+       if( !(end-start) ){
                if( direction == 1 ) end = VG_MIN( end+1, strlen( vg_console.input ) );
                else if( direction == -1 ) start = VG_MAX( start-1, 0 );
        }
@@ -733,8 +666,7 @@ VG_STATIC void console_to_clipboard(void)
        console_make_selection( &start, &end );
        char buffer[512];
        
-       if( end-start )
-       {
+       if( end-start ){
                memcpy( buffer, &vg_console.input[ start ], end-start );
                buffer[ end-start ] = 0x00;
       SDL_SetClipboardText( buffer );
@@ -934,14 +866,12 @@ VG_STATIC void _console_fetch_suggestion(void)
 {
    char *target = &vg_console.input[ vg_console.suggestion_pastepos ];
 
-   if( vg_console.suggestion_select == -1 )
-   {
+   if( vg_console.suggestion_select == -1 ){
       strcpy( target, vg_console.input_copy );
       console_move_cursor( &vg_console.cursor_user, 
                            &vg_console.cursor_pos, 10000, 1 );
    }
-   else
-   {
+   else{
       strncpy( target,
             vg_console.suggestions[ vg_console.suggestion_select ].str,
             vg_list_size( vg_console.input )-1 );
@@ -954,8 +884,7 @@ VG_STATIC void _console_fetch_suggestion(void)
 
 VG_STATIC void _console_suggest_store_normal(void)
 {
-   if( vg_console.suggestion_select == -1 )
-   {
+   if( vg_console.suggestion_select == -1 ){
       char *target = &vg_console.input[ vg_console.suggestion_pastepos ];
       strcpy( vg_console.input_copy, target );
    }
@@ -963,8 +892,7 @@ VG_STATIC void _console_suggest_store_normal(void)
 
 VG_STATIC void _console_suggest_next(void)
 {
-   if( vg_console.suggestion_count )
-   {
+   if( vg_console.suggestion_count ){
       _console_suggest_store_normal();
 
       vg_console.suggestion_select ++;
@@ -978,8 +906,7 @@ VG_STATIC void _console_suggest_next(void)
 
 VG_STATIC void _console_suggest_prev(void)
 {
-   if( vg_console.suggestion_count )
-   {
+   if( vg_console.suggestion_count ){
       _console_suggest_store_normal();
 
       vg_console.suggestion_select --;
@@ -997,8 +924,7 @@ VG_STATIC void _console_suggest_prev(void)
 VG_STATIC void console_proc_key( SDL_Keysym ev )
 {
    /* Open / close console */
-   if( ev.sym == SDLK_BACKQUOTE )
-   {
+   if( ev.sym == SDLK_BACKQUOTE ){
       vg_console.enabled = !vg_console.enabled;
 
       if( vg_console.enabled )
@@ -1050,22 +976,17 @@ VG_STATIC void console_proc_key( SDL_Keysym ev )
    if( ev.mod & KMOD_ALT )
       mod |= KMOD_ALT;
 
-   for( int i=0; i<vg_list_size( mappings ); i++ )
-   {
+   for( int i=0; i<vg_list_size( mappings ); i++ ){
       struct console_mapping *mapping = &mappings[i];
 
-      if( mapping->key == ev.sym )
-      {
-         if( mapping->mod == 0 )
-         {
-            if( mod == 0 )
-            {
+      if( mapping->key == ev.sym ){
+         if( mapping->mod == 0 ){
+            if( mod == 0 ){
                mapping->handler();
                return;
             }
          }
-         else if( (mod & mapping->mod) == mapping->mod )
-         {
+         else if( (mod & mapping->mod) == mapping->mod ){
             mapping->handler();
             return;
          }
@@ -1080,8 +1001,7 @@ VG_STATIC void console_proc_utf8( const char *text )
 {
    const char *ptr = text;
 
-   while( *ptr )
-   {
+   while( *ptr ){
       if( *ptr != '`' ) 
          console_put_char( *ptr );
       ptr ++;
index af0ac6882d9ff285cdbe6e95ba91d71a930553a8..8a75b70a37988e77741e0e98b4869e2fbd4f50c7 100644 (file)
@@ -13,23 +13,19 @@ VG_STATIC inline int vg_get_button( const char *button );
  */
 VG_STATIC inline int vg_get_button_down( const char *button );
 VG_STATIC inline int vg_get_button_up( const char *button );
+VG_STATIC float controller_deadzone = 0.05f;
 
-VG_STATIC float g_controller_deadzone = 0.05f;
-
-enum vg_button_state
-{
+enum vg_button_state{
        k_button_state_down = 1,
        k_button_state_up = 3,
        k_button_state_pressed = 2,
        k_button_state_none = 0
 };
 
-struct input_binding
-{
+struct input_binding{
        const char *name;
 
-   enum input_type
-   {
+   enum input_type{
       k_input_type_button,
       k_input_type_axis,
       k_input_type_axis_norm,
@@ -42,10 +38,8 @@ struct input_binding
    }
    type;
 
-   union
-   {
-      struct input_axis
-      {
+   union{
+      struct input_axis{
          SDL_GameControllerAxis gamepad_axis;
          SDL_Keycode            keyboard_positive,
                                 keyboard_negative;
@@ -55,8 +49,7 @@ struct input_binding
       }
       axis;
 
-      struct
-      {
+      struct{
          SDL_GameControllerButton gamepad_id;
          SDL_Keycode              keyboard_id;
          int                      mouse_id;
@@ -127,8 +120,7 @@ VG_STATIC struct input_binding *vg_get_named_input( const char *name )
    if( name[0] == '+' || name[0] == '-' )
       name ++;
 
-   for( u32 i=0; i<vg_input.named_input_count; i++ )
-   {
+   for( u32 i=0; i<vg_input.named_input_count; i++ ){
       struct input_binding *bind = &vg_input.named_inputs[i];
       if( !strcmp( bind->name, name ) )
          return bind;
@@ -193,23 +185,19 @@ VG_STATIC const char *vg_input_to_str( u32 input, enum input_type input_type )
    if( input == -1 )
       return NULL;
 
-   if( input_type == k_input_type_keyboard_key )
-   {
-      if( (input >= SDLK_a) && (input <= SDLK_z) )
-      {
+   if( input_type == k_input_type_keyboard_key ){
+      if( (input >= SDLK_a) && (input <= SDLK_z) ){
          return &"a\0b\0c\0d\0e\0f\0g\0h\0i\0j\0k\0l\0m\0n\0o\0p\0"
                  "q\0r\0s\0t\0u\0v\0w\0x\0y\0z\0"[(input-SDLK_a)*2];
       }
 
-      if( (input >= SDLK_0) && (input <= SDLK_9) )
-      {
+      if( (input >= SDLK_0) && (input <= SDLK_9) ){
          return &"0\0" "1\0" "2\0" "3\0" "4\0" 
                  "5\0" "6\0" "7\0" "8\0" "9\0"[(input-SDLK_0)*2];
       }
    }
 
-   for( int i=0; i<vg_list_size(vg_all_bindable_inputs); i++ )
-   {
+   for( int i=0; i<vg_list_size(vg_all_bindable_inputs); i++ ){
       struct input_en *desc = &vg_all_bindable_inputs[i];
 
       if( (desc->type == input_type) && (desc->id == input) )
@@ -221,43 +209,36 @@ VG_STATIC const char *vg_input_to_str( u32 input, enum input_type input_type )
 
 VG_STATIC enum input_type vg_str_to_input( const char *str, u32 *input )
 {
-   if( !str )
-   {
+   if( !str ){
       *input = -1;
       return k_input_type_unknown;
    }
 
    u32 len = strlen(str);
 
-   if( len == 0 )
-   {
+   if( len == 0 ){
       *input = -1;
       return k_input_type_unknown;
    }
 
-   if( len == 1 )
-   {
+   if( len == 1 ){
       u8 uch = str[0];
 
-      if( (uch >= (u8)'a') && (uch <= (u8)'z') )
-      {
+      if( (uch >= (u8)'a') && (uch <= (u8)'z') ){
          *input = SDLK_a + (uch-(u8)'a');
          return k_input_type_keyboard_key;
       }
 
-      if( (uch >= (u8)'0') && (uch <= (u8)'9') )
-      {
+      if( (uch >= (u8)'0') && (uch <= (u8)'9') ){
          *input = SDLK_0 + (uch-(u8)'0');
          return k_input_type_keyboard_key;
       }
    }
 
-   for( int i=0; i<vg_list_size(vg_all_bindable_inputs); i++ )
-   {
+   for( int i=0; i<vg_list_size(vg_all_bindable_inputs); i++ ){
       struct input_en *desc = &vg_all_bindable_inputs[i];
 
-      if( !strcmp( desc->alias, str ) )
-      {
+      if( !strcmp( desc->alias, str ) ){
          *input = desc->id;
          return desc->type;
       }
@@ -288,8 +269,7 @@ VG_STATIC void vg_print_binding_info( struct input_binding *bind )
          vg_input_to_str(bind->axis.keyboard_negative, 
                          k_input_type_keyboard_key ));
    }
-   else
-   {
+   else{
       vg_info( "      gamepad_id: %s\n", 
          vg_input_to_str(bind->button.gamepad_id, k_input_type_gamepad_button));
       vg_info( "      keyboard_id: %s\n",
@@ -307,16 +287,14 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
 {
    int axis_mod = 0;
    char modch = ' ';
-   if( (mod[0] == '-') || (mod[0] == '+') )
-   {
+   if( (mod[0] == '-') || (mod[0] == '+') ){
       axis_mod = 1;
       modch = mod[0];
       mod ++;
    }
    
    int invert = 0;
-   if( (str[0] == '-' ) )
-   {
+   if( (str[0] == '-' ) ){
       invert = 1;
       str ++;
    }
@@ -324,16 +302,13 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
    u32 id;
    enum input_type type = vg_str_to_input( str, &id );
 
-   if( bind->type == k_input_type_button )
-   {
-      if( axis_mod )
-      {
+   if( bind->type == k_input_type_button ){
+      if( axis_mod ){
          vg_error( "Cannot use axis modifiers on button input!\n" );
          return;
       }
 
-      if( invert )
-      {
+      if( invert ){
          vg_error( "Cannot invert button input!\n" );
          return;
       }
@@ -344,8 +319,7 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
          bind->button.mouse_id = id;
       else if( type == k_input_type_gamepad_button )
          bind->button.gamepad_id = id;
-      else
-      {
+      else{
          vg_error( "Unknown button or key '%s'\n", str );
          return;
       }
@@ -353,12 +327,9 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
    else if( (bind->type == k_input_type_axis ) ||
             (bind->type == k_input_type_axis_norm))
    {
-      if( axis_mod )
-      {
-         if( type == k_input_type_keyboard_key )
-         {
-            if( invert )
-            {
+      if( axis_mod ){
+         if( type == k_input_type_keyboard_key ){
+            if( invert ){
                vg_error( "Cannot invert a keyboard key!\n" );
                return;
             }
@@ -368,21 +339,17 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
             else
                bind->axis.keyboard_negative = id;
          }
-         else
-         {
+         else{
             vg_error( "You can only bind keyboard keys to +- axises\n" );
             return;
          }
       }
-      else
-      {
-         if( type == k_input_type_gamepad_axis )
-         {
+      else{
+         if( type == k_input_type_gamepad_axis ){
             bind->axis.gamepad_inverted = invert;
             bind->axis.gamepad_axis = id;
          }
-         else
-         {
+         else{
             vg_error( "You can only bind gamepad axises to this\n" );
             return;
          }
@@ -399,8 +366,7 @@ VG_STATIC void vg_apply_bind_str( struct input_binding *bind,
 
 VG_STATIC int vg_rebind_input_cmd( int argc, const char *argv[] )
 {
-   if( argc == 0 )
-   {
+   if( argc == 0 ){
       vg_info( "Usage: bind jump x\n" );
       vg_info( "       bind -steerh j\n" );
       vg_info( "       bind steerh gp-ls-h\n" );
@@ -410,20 +376,17 @@ VG_STATIC int vg_rebind_input_cmd( int argc, const char *argv[] )
    const char *str_bind_name = argv[0];
    struct input_binding *bind = vg_get_named_input( str_bind_name );
 
-   if( !bind )
-   {
+   if( !bind ){
       vg_error( "There is no bind with that name '%s'\n", str_bind_name );
       return 0;
    }
 
-   if( argc == 1 )
-   {
+   if( argc == 1 ){
       vg_print_binding_info( bind );
       return 0;
    }
 
-   if( argc == 2 )
-   {
+   if( argc == 2 ){
       const char *str_input_id  = argv[1];
 
       vg_apply_bind_str( bind, str_bind_name, str_input_id );
@@ -440,19 +403,14 @@ VG_STATIC void vg_rebind_input_cmd_poll( int argc, const char *argv[] )
 
    const char *str_bind_name = argv[0];
 
-   if( argc == 1 )
-   {
-      for( u32 i=0; i<vg_input.named_input_count; i++ )
-      {
+   if( argc == 1 ){
+      for( u32 i=0; i<vg_input.named_input_count; i++ ){
          struct input_binding *bind = &vg_input.named_inputs[i];
          console_suggest_score_text( bind->name, argv[argc-1], 0 );
       }
    }
-
-   else if( argc == 2 )
-   {
-      for( int i=0; i<vg_list_size(vg_all_bindable_inputs); i++ )
-      {
+   else if( argc == 2 ){
+      for( int i=0; i<vg_list_size(vg_all_bindable_inputs); i++ ){
          struct input_en *desc = &vg_all_bindable_inputs[i];
          console_suggest_score_text( desc->alias, argv[argc-1], 0 );
       }
@@ -467,14 +425,11 @@ VG_STATIC u8 vg_getkey( SDL_Keycode kc )
 
 VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
 {
-   if( vg_console.enabled )
-   {
-      for( i32 i=0; i<num; i++ )
-      {
+   if( vg_console.enabled ){
+      for( i32 i=0; i<num; i++ ){
          struct input_binding *bind = &binds[i];
 
-         if( bind->type == k_input_type_button )
-         {
+         if( bind->type == k_input_type_button ){
             bind->button.prev = bind->button.value;
             bind->button.value = 0;
          }
@@ -483,12 +438,10 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
       return;
    }
 
-   for( i32 i=0; i<num; i++ )
-   {
+   for( i32 i=0; i<num; i++ ){
       struct input_binding *bind = &binds[i];
 
-      if( bind->type == k_input_type_button )
-      {
+      if( bind->type == k_input_type_button ){
          bind->button.prev = bind->button.value;
          bind->button.value = 0;
 
@@ -496,20 +449,17 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
             bind->button.value |= 
                vg_input.controller_buttons[ bind->button.gamepad_id ];
 
-         if( bind->button.keyboard_id != -1 )
-         {
+         if( bind->button.keyboard_id != -1 ){
             bind->button.value |= vg_getkey( bind->button.keyboard_id );
          }
 
-         if( bind->button.mouse_id != -1 )
-         {
+         if( bind->button.mouse_id != -1 ){
             if( SDL_GetMouseState(NULL, NULL) & 
                                     SDL_BUTTON( bind->button.mouse_id ) )
                bind->button.value |= 1;
          }
       }
-      else if( bind->type == k_input_type_axis )
-      {
+      else if( bind->type == k_input_type_axis ){
          float keyboard_value = 0.0f,
                gamepad_value = 0.0f;
 
@@ -521,8 +471,7 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
             if( vg_getkey( bind->axis.keyboard_negative ) )
                keyboard_value -= 1.0f;
 
-         if( bind->axis.gamepad_axis != -1 )
-         {
+         if( bind->axis.gamepad_axis != -1 ){
             gamepad_value = 
                vg_input.controller_axises[ bind->axis.gamepad_axis ];
 
@@ -530,7 +479,7 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
                gamepad_value *= -1.0f;
          }
 
-         float deadz   = vg_clampf( g_controller_deadzone, 0.0f, 0.999f ),
+         float deadz   = vg_clampf( controller_deadzone, 0.0f, 0.999f ),
                high    = vg_maxf( 0.0f, fabsf(gamepad_value) - deadz ),
                norm    = high / (1.0f-deadz);
 
@@ -541,8 +490,7 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
          else
             bind->axis.value = gamepad_value;
       }
-      else if( bind->type == k_input_type_axis_norm )
-      {
+      else if( bind->type == k_input_type_axis_norm ){
          float value = 0.0f;
          if( bind->axis.keyboard_positive != -1 )
             if( vg_getkey( bind->axis.keyboard_positive ))
@@ -559,21 +507,17 @@ VG_STATIC void vg_input_update( u32 num, struct input_binding *binds )
 
 VG_STATIC void vg_input_controller_event( SDL_Event *ev )
 {
-   if( ev->type == SDL_CONTROLLERAXISMOTION )
-   {
-      if( ev->caxis.which == vg_input.controller_joystick_id )
-      {
+   if( ev->type == SDL_CONTROLLERAXISMOTION ){
+      if( ev->caxis.which == vg_input.controller_joystick_id ){
          vg_input.controller_axises[ ev->caxis.axis ] = 
             (float)ev->caxis.value / 32767.0f;
       }
    }
-   else if( ev->type == SDL_CONTROLLERBUTTONDOWN )
-   {
+   else if( ev->type == SDL_CONTROLLERBUTTONDOWN ){
       if( ev->cbutton.which == vg_input.controller_joystick_id )
          vg_input.controller_buttons[ ev->cbutton.button ] = 1;
    }
-   else if( ev->type == SDL_CONTROLLERBUTTONUP )
-   {
+   else if( ev->type == SDL_CONTROLLERBUTTONUP ){
       if( ev->cbutton.which == vg_input.controller_joystick_id )
          vg_input.controller_buttons[ ev->cbutton.button ] = 0;
    }
@@ -582,10 +526,8 @@ VG_STATIC void vg_input_controller_event( SDL_Event *ev )
 VG_STATIC void vg_try_attach_controller(void)
 {
    int joy_count = SDL_NumJoysticks();
-   for( int i=0; i<joy_count; i++ ) 
-   {
-      if( SDL_IsGameController(i) ) 
-      {
+   for( int i=0; i<joy_count; i++ ) {
+      if( SDL_IsGameController(i) ) {
          vg_input.controller_handle = SDL_GameControllerOpen(i);
          vg_input.controller_joystick_id = i;
          vg_success( "Attached game controller with joystick ID %d\n", i );
@@ -594,22 +536,18 @@ VG_STATIC void vg_try_attach_controller(void)
    }
 }
 
-
 VG_STATIC void vg_update_inputs(void)
 {
    vg_input.sdl_keys = SDL_GetKeyboardState(NULL);
 
-   if( vg_input.controller_handle )
-   {
-      if( !SDL_GameControllerGetAttached( vg_input.controller_handle ) )
-      {
+   if( vg_input.controller_handle ){
+      if( !SDL_GameControllerGetAttached( vg_input.controller_handle ) ){
          SDL_GameControllerClose( vg_input.controller_handle );
          vg_input.controller_handle = NULL;
       }
    }
 
-   if( !vg_input.controller_handle )
-   {
+   if( !vg_input.controller_handle ){
       vg_input.controller_axises[ SDL_CONTROLLER_AXIS_TRIGGERLEFT ] = -1.0f;
       vg_input.controller_axises[ SDL_CONTROLLER_AXIS_TRIGGERRIGHT ] = -1.0f;
       vg_try_attach_controller();
@@ -631,21 +569,9 @@ VG_STATIC void vg_input_init(void)
 {
    vg_acquire_thread_sync();
 
-   vg_function_push( (struct vg_cmd)
-   {
-      .name = "bind",
-      .function = vg_rebind_input_cmd,
-      .poll_suggest = vg_rebind_input_cmd_poll
-   });
-
-   vg_var_push( (struct vg_var){
-      .name = "controller_deadzone",
-      .data = &g_controller_deadzone,
-      .data_type = k_var_dtype_f32,
-      .opt_f32 = { .clamp = 0 },
-      .persistent = 1
-   });
+   vg_console_reg_cmd( "bind", vg_rebind_input_cmd, vg_rebind_input_cmd_poll );
 
+   VG_VAR_F32( controller_deadzone, flags=VG_VAR_PERSISTENT );
    vg_info( "Checking for controller\n" );
    SDL_GameControllerAddMappingsFromFile( "gamecontrollerdb.txt" );
    
@@ -667,8 +593,7 @@ VG_STATIC void vg_input_init(void)
 
 VG_STATIC void vg_input_free(void)
 {
-   if( vg_input.controller_handle )
-   {
+   if( vg_input.controller_handle ){
       SDL_GameControllerClose( vg_input.controller_handle );
       vg_input.controller_handle = NULL;
    }
index 02d7a07d9c465f0bdb59946af9f7acc115d7afe4..a16f5020481a35a7ce3b76048859c144b89d3238 100644 (file)
@@ -19,12 +19,10 @@ typedef v3f line_co;
 #define VG__CYAN  0xffffff00
 #define VG__NONE  0x00000000
 
-static struct vg_shader _shader_lines = 
-{
+static struct vg_shader _shader_lines = {
    .name = "[vg] lines",
    .link = NULL,
-   .vs = 
-   {
+   .vs = {
       .orig_file = NULL,
       .static_src = 
 
@@ -41,8 +39,7 @@ static struct vg_shader _shader_lines =
        "       gl_Position = vert_pos;"
        "}"
    },
-   .fs = 
-   {
+   .fs = {
       .orig_file = NULL,
       .static_src = 
 
@@ -57,14 +54,11 @@ static struct vg_shader _shader_lines =
    }
 };
 
-
-struct
-{
+struct{
    u32 draw,
        allow_input;
        
-   struct vg_lines_vert
-   {
+   struct vg_lines_vert{
       v3f co;
       u32 colour;
    } 
@@ -78,14 +72,8 @@ VG_STATIC void vg_lines_init(void)
 {
    vg_info( "vg_lines_init\n" );
 
-   vg_var_push( (struct vg_var){
-      .name = "vg_lines",
-      .data = &vg_lines.draw,
-      .data_type = k_var_dtype_i32,
-      .opt_i32 = { .min=0, .max=1, .clamp=1 },
-      .persistent = 1
-   });
-   
+   vg_console_reg_var( "vg_lines", &vg_lines.draw, k_var_dtype_i32, 
+                       VG_VAR_CHEAT );
    vg_shader_register( &_shader_lines );
 
    vg_acquire_thread_sync();
@@ -181,14 +169,12 @@ VG_STATIC void vg_line( line_co from, line_co to, u32 colour )
 VG_STATIC void line_tangent_basis( v3f n, v3f tx, v3f ty )
 {
    /* Compute tangent basis (box2d) */
-   if( fabsf( n[0] ) >= 0.57735027f )
-   {
+   if( fabsf( n[0] ) >= 0.57735027f ){
       tx[0] =  n[1];
       tx[1] = -n[0];
       tx[2] =  0.0f;
    }
-   else
-   {
+   else{
       tx[0] =  0.0f;
       tx[1] =  n[2];
       tx[2] = -n[1];
index 5c0690c9186b6010a0ecee9515fc9528cc2397a0..ca79e18d0bf77df3da6421de40176b916ff83d5c 100644 (file)
@@ -49,13 +49,11 @@ VG_STATIC void vg_profile_end( struct vg_profile *profile )
    u64 time_end = SDL_GetPerformanceCounter(),
        delta    = time_end - profile->start;
 
-   if( profile->mode == k_profile_mode_frame )
-   {
+   if( profile->mode == k_profile_mode_frame ){
       profile->samples[ profile->buffer_current ] = delta;
       vg_profile_increment( profile );
    }
-   else
-   {
+   else{
       profile->samples[ profile->buffer_current ] += delta;
    }
 }
@@ -81,8 +79,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
    double avgs[8];
    int    ptrs[8];
 
-   for( int i=0; i<count; i++ )
-   {
+   for( int i=0; i<count; i++ ){
       ptrs[i] = profiles[i]->buffer_current;
       avgs[i] = 0.0f;
    }
@@ -92,12 +89,10 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
 
    double rate_mul = 1000.0 / (double)SDL_GetPerformanceFrequency();
 
-   for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ )
-   {
+   for( int i=0; i<VG_PROFILE_SAMPLE_COUNT-1; i++ ){
       double total = 0.0;
 
-      for( int j=0; j<count; j++ )
-      {
+      for( int j=0; j<count; j++ ){
          ptrs[j] --;
 
          if( ptrs[j] < 0 )
@@ -127,8 +122,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
                        1,
                        k_text_align_left );
 
-   for( int i=0; i<count; i++ )
-   {
+   for( int i=0; i<count; i++ ){
       snprintf( infbuf, 64, "%.4fms %s", 
                         avgs[i] * (1.0f/(VG_PROFILE_SAMPLE_COUNT-1)),
                         profiles[i]->name );
@@ -143,13 +137,7 @@ VG_STATIC void vg_profile_drawn( struct vg_profile **profiles, u32 count,
 
 VG_STATIC void vg_profiler_init(void)
 {
-   vg_var_push( (struct vg_var){
-      .name = "vg_profiler",
-      .data = &vg_profiler,
-      .data_type = k_var_dtype_i32,
-      .opt_i32 = { .min=0, .max=1, .clamp=1 },
-      .persistent = 1
-   });
+   VG_VAR_I32( vg_profiler, flags=VG_VAR_PERSISTENT );
 }
 
 #endif /* VG_PROFILER_H */