minor stuff
[vg.git] / vg_console.h
index 208b5b392b9245887583b6f7b34ebc32cb35f2b3..6f3c2bad7128e2650073138a6b8ed97137298101 100644 (file)
@@ -18,8 +18,9 @@
    { 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
+#define VG_VAR_PERSISTENT  0x1
+#define VG_VAR_CHEAT       0x2
+#define VG_VAR_CHEATTOGGLE 0x4
 
 typedef struct vg_var vg_var;
 typedef struct vg_cmd vg_cmd;
@@ -35,8 +36,9 @@ struct vg_console{
                        k_var_dtype_f32
                } 
                data_type;
-
       u32 flags;
+
+      union{ u32 _u32; f32 _f32; i32 _i32; } defaults;
        } 
        vars[ 128 ];
        
@@ -67,10 +69,9 @@ struct vg_console{
        char history[32][96];
        int history_last, history_pos, history_count;
        
-       int enabled;
+       i32 enabled, cheats;
 }
-vg_console;
-
+static vg_console;
 
 VG_STATIC void _vg_console_draw( void );
 void           _vg_console_println( const char *str );
@@ -90,8 +91,7 @@ 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; 
 }
 
@@ -108,6 +108,10 @@ void vg_console_reg_var( const char *alias, void *ptr, enum vg_var_dtype type,
    var->data_type = type;
    var->flags = flags;
 
+   if     ( type == k_var_dtype_f32 ) var->defaults._f32 = *((f32 *)ptr);
+   else if( type == k_var_dtype_i32 ) var->defaults._i32 = *((i32 *)ptr);
+   else if( type == k_var_dtype_u32 ) var->defaults._u32 = *((u32 *)ptr);
+
    vg_info( "Console variable '%s' registered\n", alias );
 }
 
@@ -128,8 +132,7 @@ void vg_console_reg_cmd( const char *alias,
    vg_info( "Console function '%s' registered\n", alias );
 }
 
-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 ++ ){
                struct vg_cmd *cmd = &vg_console.functions[ i ];
                vg_info( "* %s\n", cmd->name );
@@ -143,14 +146,12 @@ VG_STATIC int _vg_console_list( int argc, char const *argv[] )
        return 0;
 }
 
-int _test_break( int argc, const char *argv[] )
-{
+int _test_break( int argc, const char *argv[] ){
    vg_fatal_error( "Test crash from main, after loading (console)" );
    return 0;
 }
 
-int _vg_console_exec( int argc, const char *argv[] )
-{
+int _vg_console_exec( int argc, const char *argv[] ){
    if( argc < 1 ) return 0;
 
    char path[256];
@@ -178,20 +179,34 @@ int _vg_console_exec( int argc, const char *argv[] )
    return 0;
 }
 
-VG_STATIC void _vg_console_init(void)
-{
+int _ccmd_vg_console_defaults( int argc, const char *argv[] ){
+       for( int i=0; i<vg_console.var_count; i ++ ){
+               struct vg_var *cv = &vg_console.vars[i];
+
+      enum vg_var_dtype type = cv->data_type;
+      void *ptr = cv->data;
+
+      if     ( type == k_var_dtype_f32 ) *((f32 *)ptr) = cv->defaults._f32;
+      else if( type == k_var_dtype_i32 ) *((i32 *)ptr) = cv->defaults._i32;
+      else if( type == k_var_dtype_u32 ) *((u32 *)ptr) = cv->defaults._u32;
+       }
+
+   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_console_reg_cmd( "defaults", _ccmd_vg_console_defaults, NULL );
+   vg_console_reg_var( "cheats", &vg_console.cheats, k_var_dtype_i32, 0 );
 }
 
-VG_STATIC void vg_console_load_autos(void)
-{
+VG_STATIC void vg_console_load_autos(void){
    _vg_console_exec( 1, (const char *[]){ "auto.conf" } );
 }
 
-VG_STATIC void _vg_console_write_persistent(void)
-{
+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 ++ ){
@@ -301,11 +316,24 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
    if( cv ){
       /* Cvar Matched, try get value */
       if( arg_count >= 2 ){
+         if( cv->flags & VG_VAR_CHEAT ){
+            if( !vg_console.cheats ){
+               vg_error( "variable is cheat protected\n" );
+               return;
+            }
+         }
+
          if( (cv->data_type == k_var_dtype_u32) ||
              (cv->data_type == k_var_dtype_i32) )
          {
             int *ptr = cv->data;
             *ptr = atoi( args[1] ); 
+
+            if( cv->flags & VG_VAR_CHEATTOGGLE ){
+               if( *ptr ){
+                  _ccmd_vg_console_defaults( 0, NULL );
+               }
+            }
          }
          else if( cv->data_type == k_var_dtype_f32 ){
             float *ptr = cv->data;
@@ -331,8 +359,7 @@ VG_STATIC void vg_execute_console_input( const char *cmd )
        vg_error( "No command/var named '%s'. Use 'list' to view all\n", args[0] );
 }
 
-u32 str_lev_distance( const char *s1, const char *s2 )
-{
+u32 str_lev_distance( const char *s1, const char *s2 ){
    u32 m = strlen( s1 ),
        n = strlen( s2 );
 
@@ -369,8 +396,7 @@ u32 str_lev_distance( const char *s1, const char *s2 )
    return costs[n];
 }
 
-u32 str_lcs( const char *s1, const char *s2 )
-{
+u32 str_lcs( const char *s1, const char *s2 ){
    u32 m = VG_MIN( 31, strlen( s1 ) ),
        n = VG_MIN( 31, strlen( s2 ) );