added cvar persistence
authorhgn <hgodden00@gmail.com>
Wed, 12 Jan 2022 02:57:15 +0000 (02:57 +0000)
committerhgn <hgodden00@gmail.com>
Wed, 12 Jan 2022 02:57:15 +0000 (02:57 +0000)
fishladder.c
vg/vg.h
vg/vg_console.h

index 5948f627efc2113f388fad462af4dd50f513ca7c..210797b5c349a80eed80d0e14e03f1bcf51a46a3 100644 (file)
@@ -3298,7 +3298,8 @@ void vg_start(void)
                .name = "colours",
                .data = &colour_set_id,
                .data_type = k_convar_dtype_i32,
-               .opt_i32 = { .min = 0, .max = 2, .clamp = 1 }
+               .opt_i32 = { .min = 0, .max = 2, .clamp = 1 },
+               .persistent = 1
        });
 
        // Combined quad, long quad / empty circle / filled circle mesh
diff --git a/vg/vg.h b/vg/vg.h
index 6e2d0ff48b0cb896598596ebbfdf579aaf6a4e83..575bf036eeabf667aa322983555a3a2dee818830 100644 (file)
--- a/vg/vg.h
+++ b/vg/vg.h
@@ -210,9 +210,6 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        const unsigned char* glver = glGetString( GL_VERSION );
        vg_success( "Load setup complete, OpenGL version: %s\n", glver );
        
-       vg_console_init();
-       vg_register_exit( &vg_console_free, "Console" );
-       
        vg_run_gfx_diagnostics();
        
        for( int id = 0; id <= GLFW_JOYSTICK_LAST; id ++ )
@@ -241,6 +238,9 @@ static void vg_init( int argc, char *argv[], const char *window_name )
        {
                vg_start();
        
+               vg_console_init();
+               vg_register_exit( &vg_console_free, "Console" );
+       
                vg_audio_init();
                vg_register_exit( &vg_audio_free, "vg_audio_free" );
                
index 558df185bae8a9086ee9531577f8c361a5ffa171..43677e31c5f7f2912b284216de2f32fdacf0443f 100644 (file)
@@ -1,3 +1,5 @@
+// Copyright (C) 2021 Harry Godden (hgn) - All Rights Reserved
+
 struct vg_console
 {
        struct vg_convar
@@ -20,6 +22,8 @@ struct vg_console
                                int min, max, clamp;
                        } opt_i32;
                };
+
+               int persistent; // Should be stored to cfg/auto.conf
        } 
        *convars;
        
@@ -44,7 +48,43 @@ struct vg_console
 }
 vg_console = { .scale = 2 };
 
-static int vg_console_enabled(void) { return vg_console.enabled; }
+// Declerations
+// ------------
+
+// Registration
+static void vg_convar_push( struct vg_convar cv );
+static void vg_function_push( struct vg_cmd cmd );
+
+static void vg_console_draw( void );
+void vg_console_println( const char *str );
+static int vg_console_list( int argc, char const *argv[] );
+static void vg_console_init(void);
+static void vg_console_write_persistent(void);
+static void vg_console_free(void);
+static void execute_console_input( const char *cmd );
+
+// Console interface
+// -----------------
+
+static void console_make_selection( int* start, int* end );
+static void console_move_cursor( int* cursor0, int* cursor1, int dir, int snap_together );
+static int console_makeroom( int datastart, int length );
+static int console_delete_char( int direction );
+static void console_to_clipboard(void);
+static void console_clipboard_paste(void);
+static void console_put_char( char c );
+static void console_history_get( char* buf, int entry_num );
+static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, int action, int mods );
+static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar );
+static int vg_console_enabled(void);
+
+// =========================================================================================================
+// Implementation
+
+static int vg_console_enabled(void) 
+{ 
+       return vg_console.enabled; 
+}
 
 static void vg_convar_push( struct vg_convar cv )
 {
@@ -152,10 +192,59 @@ static void vg_console_init(void)
                .name = "list",
                .function = vg_console_list
        });
+
+       // Read and exec persistent commands
+       FILE *fp = fopen( "cfg/auto.conf", "r" );
+       if( fp )
+       {
+               char line[256];
+
+               while( fgets( line, sizeof( line ), fp ) )
+               {
+                       line[ strcspn( line, "\r\n#" ) ] = 0x00;
+
+                       if( line[0] != 0x00 )
+                       {
+                               execute_console_input( line );
+                       }
+               }
+
+               fclose( fp );
+       }
+}
+
+static void vg_console_write_persistent(void)
+{
+       FILE *fp = fopen( "cfg/auto.conf", "w" );
+       
+       for( int i = 0; i < arrlen( vg_console.convars ); i ++ )
+       {
+               struct vg_convar *cv = &vg_console.convars[i];
+
+               if( cv->persistent )
+               {
+                       switch( cv->data_type )
+                       {
+                               case k_convar_dtype_i32:
+                                       fprintf( fp, "%s %d\n", cv->name, *(i32 *)(cv->data) );
+                               break;
+                               case k_convar_dtype_u32:
+                                       fprintf( fp, "%s %u\n", cv->name, *(u32 *)(cv->data) );
+                               break;
+                               case k_convar_dtype_f32:
+                                       fprintf( fp, "%s %.5f\n", cv->name, *(float *)(cv->data ) );
+                               break;
+                       }
+               }
+       }
+
+       fclose( fp );
 }
 
 static void vg_console_free(void)
 {
+       vg_console_write_persistent();
+
        arrfree( vg_console.convars );
        arrfree( vg_console.functions );
 }