better console handling // simple map serialzer
authorhgn <hgodden00@gmail.com>
Thu, 11 Nov 2021 18:38:48 +0000 (18:38 +0000)
committerhgn <hgodden00@gmail.com>
Thu, 11 Nov 2021 18:38:48 +0000 (18:38 +0000)
fishladder.c
vg/vg_console.h

index 0c07ddf56bc0e78f03b8180623f0b4d170602841..4817bdf9a2a20004f7795879f8f96c918fe4b910 100644 (file)
@@ -383,13 +383,72 @@ static struct cell *pcell( v2i pos )
        return &world.data[ pos[1]*world.w + pos[0] ];
 }
 
+static void map_serialize( FILE *stream )
+{
+       for( int y = 0; y < world.h; y ++ )
+       {
+               for( int x = 0; x < world.w; x ++ )
+               {
+                       struct cell *cell = pcell( (v2i){ x, y } );
+                       
+                       if( cell->state & FLAG_WALL ) fputc( '#', stream );
+                       else if( cell->state & FLAG_INPUT ) fputc( '+', stream );
+                       else if( cell->state & FLAG_OUTPUT ) fputc( '-', stream );
+                       else if( cell->state & FLAG_CANAL ) fputc( '*', stream );
+                       else fputc( ' ', stream );
+               }
+               
+               fputc( ';', stream );
+               
+               int terminal_write_count = 0;
+               
+               for( int x = 0; x < world.w; x ++ )
+               {
+                       for( int i = 0; i < arrlen( world.io ); i ++ )
+                       {
+                               struct cell_terminal *term = &world.io[i];
+                               if( term->id == y*world.w+x )
+                               {
+                                       if( terminal_write_count )
+                                               fputc( ',', stream );
+                                       terminal_write_count ++;
+                               
+                                       for( int j = 0; j < arrlen( term->conditions ); j ++ )
+                                               fputc( term->conditions[j], stream );
+                               }
+                       }
+               }
+               
+               fputc( '\n', stream );
+       }
+}
+
 int main( int argc, char *argv[] )
 {
        vg_init( argc, argv, "Fish (Marbles Computer) Ladder Simulator 2022 | N,M: change level | SPACE: Test | LeftClick: Toggle tile" );
 }
 
+static void console_save_map( int argc, char *argv[] )
+{
+       if( argc >= 1 )
+       {
+               FILE *test_writer = fopen( argv[0], "w" );
+               if( test_writer )
+               {
+                       map_serialize( test_writer );
+                       
+                       fclose( test_writer );
+               }
+       }
+}
+
 void vg_start(void)
 {
+       vg_function_push( (struct vg_cmd){
+               .name = "save_map",
+               .function = console_save_map
+       });
+
        // Quad mesh
        {
                float quad_mesh[] =
index 94de7d205db4c8d46b65eeac44daddf4de837219..23b4480c2b46a0fa1de59c8535ff83abf4d52662 100644 (file)
@@ -23,6 +23,13 @@ struct vg_console
        } 
        *convars;
        
+       struct vg_cmd
+       {
+               void (*function)( int argc, char *argv[] );
+               const char *name;
+       }
+       *functions;
+       
        char lines[16][512];
        u32 current, len;
        
@@ -44,6 +51,11 @@ static void vg_convar_push( struct vg_convar cv )
        arrpush( vg_console.convars, cv ); 
 }
 
+static void vg_function_push( struct vg_cmd cmd )
+{
+       arrpush( vg_console.functions, cmd );
+}
+
 static void vg_console_draw( void )
 {
        if( !vg_console.enabled )
@@ -121,61 +133,64 @@ static void vg_console_init(void)
 static void vg_console_free(void)
 {
        arrfree( vg_console.convars );
+       arrfree( vg_console.functions );
 }
 
-// Returns advance amount
-static int console_token_read( char *buf, int buf_len, const char *src )
+static void execute_console_input( const char *cmd )
 {
-       int wait_for_next = 0;
-
-       for( int i = 0; i < buf_len; i ++ )
+       char temp[512];
+       char *args[9];
+       int arg_count = 0;
+       
+       int in_token = 0;
+       
+       // Split string into tokens
+       for( int i = 0; i < vg_list_size( temp ); i ++ )
        {
-               if( src[i] )
+               if( cmd[i] )
                {
-                       if( isspace( src[i] ) )
+                       if( isspace( cmd[i] ) )
                        {
-                               buf[i] = '\0';
-                               wait_for_next = 1;
+                               temp[i] = '\0';
+                               in_token = 0;
                        }
-                       else 
+                       else
                        {
-                               if( wait_for_next )
-                                       return i;
+                               temp[i] = cmd[i];
                                
-                               buf[i] = src[i];
-                       }       
+                               if( !in_token )
+                               {
+                                       args[ arg_count ++ ] = temp + i;
+                                       in_token = 1;
+                               }
+                       }
                }
                else
                {
-                       buf[i] = '\0';
-                       return i;
+                       temp[i] = '\0';
+                       break;
                }
        }
        
-       return 0;
-}
-
-static void execute_console_input( const char *cmd )
-{
-       char temp[512];
-       int cmd_pos = console_token_read( temp, 512, cmd );
+       if( arg_count == 0 )
+               return;
        
        int data_int;
        
        for( int i = 0; i < arrlen( vg_console.convars ); i ++ )
        {
                struct vg_convar *cv = &vg_console.convars[ i ];
-               if( !strcmp( cv->name, temp ) )
+               if( !strcmp( cv->name, args[0] ) )
                {
-                       // Matched, try get value
-                       if( console_token_read( temp, 512, cmd+cmd_pos ) )
+                       // Cvar Matched, try get value
+                       if( arg_count >= 2 )
                        {
                                switch( cv->data_type )
                                {
                                        case k_convar_dtype_u32:
                                        case k_convar_dtype_i32: 
                                                
-                                               data_int = atoi( temp ); 
+                                               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;
                                        
                                        break;
@@ -196,7 +211,19 @@ static void execute_console_input( const char *cmd )
                }
        }
        
-       vg_error( "No command/variable named '%s'\n", temp );
+       // Try commands
+       for( int i = 0; i < arrlen( vg_console.functions ); i ++ )
+       {
+               struct vg_cmd *cmd = &vg_console.functions[ i ];
+               if( !strcmp( cmd->name, args[0] ) )
+               {
+                       // Call function
+                       cmd->function( arg_count-1, args+1 );
+                       return;
+               }
+       }
+       
+       vg_error( "No command/variable named '%s'\n", args[0] );
 }
 
 // =============================================================================================================================