now thats a lot of damage!
[vg.git] / src / vg / vg_console.h
index fc2775ca49115efeb343b8207f5e70ba2e1ed592..62e311895e9e2bf6513bf950f8a6e11956cd3843 100644 (file)
@@ -6,6 +6,9 @@
 #include "vg/vg_ui.h"
 #include "vg/vg_log.h"
 
+typedef struct vg_convar vg_convar;
+typedef struct vg_cmd vg_cmd;
+
 struct vg_console
 {
        struct vg_convar
@@ -40,17 +43,16 @@ struct vg_console
 
                int persistent; /* Should this var be stored to cfg/auto.conf? */
        } 
-       *convars;
+       convars[ 32 ];
        
        struct vg_cmd
        {
                int (*function)( int argc, char const *argv[] );
                const char *name;
        }
-       *functions;
+       functions[ 32 ];
 
-   u32 convar_count, convar_cap,
-       function_count, function_cap;
+   u32 convar_count, function_count;
        
        char input[96];
        int cursor_user, cursor_pos, string_length;
@@ -59,69 +61,63 @@ struct vg_console
        int history_last, history_pos, history_count;
        
        int enabled;
-       int scale;
 }
-vg_console = { .scale = 1 };
+vg_console;
 
-static void vg_convar_push( struct vg_convar cv );
-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 );
 
-static void vg_console_draw( void );
+VG_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 );
+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 );
 
 /*
  * 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);
+VG_STATIC void console_make_selection( int* start, int* end );
+VG_STATIC void console_move_cursor( int* cursor0, int* cursor1, 
+                                    int dir, int snap_together );
+VG_STATIC int console_makeroom( int datastart, int length );
+VG_STATIC int console_delete_char( int direction );
+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 void console_proc_key( GLFWwindow* ptrW, int key, 
+                                 int scancode, int action, int mods );
+VG_STATIC void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar );
+VG_STATIC int vg_console_enabled(void);
 
 /*
  * Implementation
  */
-static int vg_console_enabled(void) 
+VG_STATIC int vg_console_enabled(void) 
 { 
        return vg_console.enabled; 
 }
 
-static void vg_convar_push( struct vg_convar cv )
+VG_STATIC void vg_convar_push( vg_convar cv )
 {
-   vg_info( "Console variable '%s' registered\n", cv.name );
-   vg_console.convars = buffer_reserve( vg_console.convars, 
-                                        vg_console.convar_count,
-                                        &vg_console.convar_cap, 1,
-                                        sizeof( struct vg_convar ) );
+   if( vg_console.convar_count > vg_list_size(vg_console.convars) )
+      vg_fatal_exit_loop( "Too many convars registered" );
 
+   vg_info( "Console variable '%s' registered\n", cv.name );
    vg_console.convars[ vg_console.convar_count ++ ] = cv;
 }
 
-static void vg_function_push( struct vg_cmd cmd )
+VG_STATIC void vg_function_push( struct vg_cmd cmd )
 {
-   vg_info( "Console command '%s' registered\n", cmd.name );
-   vg_console.functions = buffer_reserve( vg_console.functions,
-                                          vg_console.function_count,
-                                          &vg_console.function_cap, 1,
-                                          sizeof( struct vg_cmd ) );
+   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;
 }
 
-static void vg_console_draw( void )
+VG_STATIC void vg_console_draw( void )
 {
        if( !vg_console.enabled )
                return;
@@ -132,17 +128,17 @@ static void vg_console_draw( void )
    int const fh = 14;
    int console_lines = VG_MIN( 16, vg_log.buffer_line_count );
        
-       ui_global_ctx.cursor[0] = 0;
-       ui_global_ctx.cursor[1] = 0;
-       ui_global_ctx.cursor[3] = 16*fh*vg_console.scale;
-       ui_fill_x( &ui_global_ctx );
+       vg_uictx.cursor[0] = 0;
+       vg_uictx.cursor[1] = 0;
+       vg_uictx.cursor[3] = 16*fh;
+       ui_fill_x();
        
-       ui_new_node( &ui_global_ctx );
+       ui_new_node();
        {
-               ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
+               ui_fill_rect( vg_uictx.cursor, 0x77333333 );
        
-               ui_global_ctx.cursor[3] = fh*vg_console.scale;
-               ui_align_bottom( &ui_global_ctx ); 
+               vg_uictx.cursor[3] = fh;
+               ui_align_bottom();
 
                for( int i=0; i<console_lines; i ++ )
                {
@@ -151,38 +147,35 @@ static void vg_console_draw( void )
                        if( ptr < 0 )
                                ptr = vg_list_size( vg_log.buffer )-1;
          
-                       ui_text( &ui_global_ctx, ui_global_ctx.cursor, 
-               vg_log.buffer[ptr], vg_console.scale, 0 );
-                       ui_global_ctx.cursor[1] -= fh*vg_console.scale;
+                       ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
+                       vg_uictx.cursor[1] -= fh;
                }
 
        }
-       ui_end_down( &ui_global_ctx );
+       ui_end_down();
        
-       ui_global_ctx.cursor[1] += 2;
-       ui_global_ctx.cursor[3] = fh*vg_console.scale;
+       vg_uictx.cursor[1] += 2;
+       vg_uictx.cursor[3] = fh;
        
-       ui_new_node( &ui_global_ctx );
+       ui_new_node();
        {
-               ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x77333333 );
-               
-               ui_text( &ui_global_ctx, ui_global_ctx.cursor, 
-            vg_console.input, vg_console.scale, 0 );
+               ui_fill_rect( vg_uictx.cursor, 0x77333333 );
+               ui_text( vg_uictx.cursor, vg_console.input, 1, 0 );
                
                int start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user ),
                         end   = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user );
                
-               ui_global_ctx.cursor[0] = (start * UI_GLYPH_SPACING_X * vg_console.scale);
-               ui_global_ctx.cursor[2] = (start == end? 0.5f: (float)(end-start)) 
-         * (float)UI_GLYPH_SPACING_X * (float)vg_console.scale;
+               vg_uictx.cursor[0] = start * UI_GLYPH_SPACING_X;
+               vg_uictx.cursor[2] = (start == end? 0.5f: (float)(end-start)) 
+                                             * (float)UI_GLYPH_SPACING_X;
                
-               ui_fill_rect( &ui_global_ctx, ui_global_ctx.cursor, 0x66ffffff );
+               ui_fill_rect( vg_uictx.cursor, 0x66ffffff );
        }
-       ui_end_down( &ui_global_ctx );
+       ui_end_down();
    vg_mutex_unlock( &log_print_mutex );
 }
 
-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 ++ )
        {
@@ -190,8 +183,6 @@ static int vg_console_list( int argc, char const *argv[] )
                vg_info( "* %s\n", cmd->name );
        }
        
-       vg_info( "* snowsound\n" );
-       
        for( int i=0; i<vg_console.convar_count; i ++ )
        {
                struct vg_convar *cv = &vg_console.convars[ i ];
@@ -201,50 +192,20 @@ static int vg_console_list( int argc, char const *argv[] )
        return 0;
 }
 
-static int vg_console_chartest( int argc, char const *argv[] )
-{
-vg_info(" Copyright  .        . .       -----, ,----- ,---.   .---.  " );
-vg_info(" 2021-2022  |\\      /| |           /  |      |    | |    /| " );
-vg_info("            | \\    / | +--        /   +----- +---'  |   / | " );
-vg_info("            |  \\  /  | |         /    |      |   \\  |  /  | " );
-vg_info("            |   \\/   | |        /     |      |    \\ | /   | " );
-vg_info("            '        ' '--' [] '----- '----- '     ' '---'  " 
-        "SOFTWARE" );
-
-   vg_info( "\"THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG\"\n" );
-   vg_info( "'the quick brown fox jumps over the lazy dog'\n" );
-   vg_info( ":;!@#$%^& 0123456789 +-*/=~ ()[]{}<>\n" );
-   return 0;
-}
-
-static int _test_break( int argc, const char *argv[] )
+int _test_break( int argc, const char *argv[] )
 {
    vg_fatal_exit_loop( "Test crash from main, after loading (console)" );
    return 0;
 }
 
-static void vg_console_init(void)
+VG_STATIC void vg_console_init(void)
 {
-       vg_convar_push( (struct vg_convar)
-       { 
-      .name = "console_scale", .data = &vg_console.scale, 
-      .data_type = k_convar_dtype_i32, 
-               .opt_i32 = { .clamp = 1, .min = 1, .max = 7 },
-      .update = NULL
-   });
-       
        vg_function_push( (struct vg_cmd)
    {
                .name = "list",
                .function = vg_console_list
        });
 
-   vg_function_push( (struct vg_cmd)
-   {
-      .name = "chartest",
-      .function = vg_console_chartest
-   });
-
    vg_function_push( (struct vg_cmd)
    {
       .name = "crash",
@@ -252,7 +213,7 @@ static void vg_console_init(void)
    });
 }
 
-static void vg_console_load_autos(void)
+VG_STATIC void vg_console_load_autos(void)
 {
        /* Read and exec persistent commands */
        FILE *fp = fopen( "cfg/auto.conf", "r" );
@@ -274,7 +235,7 @@ static void vg_console_load_autos(void)
        }
 }
 
-static void vg_console_write_persistent(void)
+VG_STATIC void vg_console_write_persistent(void)
 {
        FILE *fp = fopen( "cfg/auto.conf", "w" );
        
@@ -302,15 +263,12 @@ static void vg_console_write_persistent(void)
        fclose( fp );
 }
 
-static void vg_console_free(void)
+VG_STATIC void vg_console_free(void)
 {
        vg_console_write_persistent();
-
-   vg_free( vg_console.convars );
-   vg_free( vg_console.functions );
 }
 
-static void execute_console_input( const char *cmd )
+VG_STATIC void execute_console_input( const char *cmd )
 {
        char temp[512];
        char const *args[9];
@@ -378,7 +336,8 @@ static void execute_console_input( const char *cmd )
                                        case k_convar_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 ):
+                     vg_minf( vg_maxf( data_float, cv->opt_f32.min), 
+                              cv->opt_f32.max ):
                      data_float;
                break;
                                }
@@ -425,13 +384,13 @@ static void execute_console_input( const char *cmd )
 /*
  * Console Interface
  */
-static void console_make_selection( int* start, int* end )
+VG_STATIC void console_make_selection( int* start, int* end )
 {
        *start = VG_MIN( vg_console.cursor_pos, vg_console.cursor_user );
        *end   = VG_MAX( vg_console.cursor_pos, vg_console.cursor_user );
 }
 
-static void console_move_cursor( int* cursor0, int* cursor1, 
+VG_STATIC void console_move_cursor( int* cursor0, int* cursor1, 
       int dir, int snap_together )
 {
        *cursor0 = VG_MAX( 0, vg_console.cursor_user + dir );
@@ -444,7 +403,7 @@ static void console_move_cursor( int* cursor0, int* cursor1,
                *cursor1 = *cursor0;
 }
 
-static int console_makeroom( int datastart, int length )
+VG_STATIC int console_makeroom( int datastart, int length )
 {
        int move_to = VG_MIN( datastart+length, vg_list_size( vg_console.input ) );
        int move_amount = strlen( vg_console.input )-datastart;
@@ -462,7 +421,7 @@ static int console_makeroom( int datastart, int length )
        return VG_MIN( length, vg_list_size( vg_console.input )-datastart );
 }
 
-static int console_delete_char( int direction )
+VG_STATIC int console_delete_char( int direction )
 {
        int start, end;
        console_make_selection( &start, &end );
@@ -486,7 +445,7 @@ static int console_delete_char( int direction )
        return start;
 }
 
-static void console_to_clipboard(void)
+VG_STATIC void console_to_clipboard(void)
 {
        int start, end;
        console_make_selection( &start, &end );
@@ -500,7 +459,7 @@ static void console_to_clipboard(void)
        }
 }
 
-static void console_clipboard_paste(void)
+VG_STATIC void console_clipboard_paste(void)
 {
        int datastart = console_delete_char(0);
        const char* clipboard = glfwGetClipboardString(NULL);
@@ -513,7 +472,7 @@ static void console_clipboard_paste(void)
                         &vg_console.cursor_pos, cpylength, 1 );
 }
 
-static void console_put_char( char c )
+VG_STATIC void console_put_char( char c )
 {
        if( !vg_console.enabled ) 
                return;
@@ -526,7 +485,7 @@ static void console_put_char( char c )
        console_move_cursor( &vg_console.cursor_user, &vg_console.cursor_pos, 1, 1 );
 }
 
-static void console_history_get( char* buf, int entry_num )
+VG_STATIC void console_history_get( char* buf, int entry_num )
 {
        if( !vg_console.history_count )
                return;
@@ -538,13 +497,13 @@ static void console_history_get( char* buf, int entry_num )
 }
 
 /* Receed secondary cursor */
-static void _console_left_select(void)
+VG_STATIC void _console_left_select(void)
 {
    console_move_cursor( &vg_console.cursor_user, NULL, -1, 0 );
 }
 
 /* Match and receed both cursors */
-static void _console_left(void)
+VG_STATIC void _console_left(void)
 {
    int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1;
 
@@ -552,12 +511,12 @@ static void _console_left(void)
                         &vg_console.cursor_pos, -cursor_diff, 1 );
 }
 
-static void _console_right_select(void)
+VG_STATIC void _console_right_select(void)
 {
    console_move_cursor( &vg_console.cursor_user, NULL, 1, 0 );
 }
 
-static void _console_right(void)
+VG_STATIC void _console_right(void)
 {
    int cursor_diff = vg_console.cursor_pos - vg_console.cursor_user? 0: 1;
 
@@ -565,7 +524,7 @@ static void _console_right(void)
                         &vg_console.cursor_pos, +cursor_diff, 1 );
 }
 
-static void _console_down(void)
+VG_STATIC void _console_down(void)
 {
    vg_console.history_pos = VG_MAX( 0, vg_console.history_pos-1 );
    console_history_get( vg_console.input, vg_console.history_pos );
@@ -575,7 +534,7 @@ static void _console_down(void)
                         vg_list_size( vg_console.input ), 1 );
 }
 
-static void _console_up(void)
+VG_STATIC void _console_up(void)
 {
    vg_console.history_pos = VG_MAX
    ( 
@@ -597,55 +556,55 @@ static void _console_up(void)
                         vg_list_size( vg_console.input ), 1);
 }
 
-static void _console_backspace(void)
+VG_STATIC void _console_backspace(void)
 {
    vg_console.cursor_user = console_delete_char( -1 );
    vg_console.cursor_pos = vg_console.cursor_user;
 }
 
-static void _console_delete(void)
+VG_STATIC void _console_delete(void)
 {
    vg_console.cursor_user = console_delete_char( 1 );
    vg_console.cursor_pos = vg_console.cursor_user;
 }
 
-static void _console_home_select(void)
+VG_STATIC void _console_home_select(void)
 {
    console_move_cursor( &vg_console.cursor_user, NULL, -10000, 0 );
 }
 
-static void _console_home(void)
+VG_STATIC void _console_home(void)
 {
    console_move_cursor( &vg_console.cursor_user, 
                         &vg_console.cursor_pos, -10000, 1 );
 }
 
-static void _console_end_select(void)
+VG_STATIC void _console_end_select(void)
 {
    console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0 );
 }
 
-static void _console_end(void)
+VG_STATIC void _console_end(void)
 {
    console_move_cursor( &vg_console.cursor_user, 
                         &vg_console.cursor_pos, 
                         vg_list_size( vg_console.input ), 1 );
 }
 
-static void _console_select_all(void)
+VG_STATIC void _console_select_all(void)
 {
    console_move_cursor( &vg_console.cursor_user, NULL, 10000, 0);
    console_move_cursor( &vg_console.cursor_pos, NULL, -10000, 0);
 }
 
-static void _console_cut(void)
+VG_STATIC void _console_cut(void)
 {
    console_to_clipboard();
    vg_console.cursor_user = console_delete_char(0);
    vg_console.cursor_pos = vg_console.cursor_user;
 }
 
-static void _console_enter(void)
+VG_STATIC void _console_enter(void)
 {
    if( !strlen( vg_console.input ) ) 
       return;
@@ -671,7 +630,7 @@ static void _console_enter(void)
    vg_console.input[0] = '\0';
 }
 
-static void console_proc_key( GLFWwindow* ptrW, int key, int scancode, 
+VG_STATIC void console_proc_key( GLFWwindow* ptrW, int key, int scancode, 
                               int action, int mods )
 {
        if( !action )
@@ -733,7 +692,7 @@ static void console_proc_key( GLFWwindow* ptrW, int key, int scancode,
 }
 
 /* Handle an OS based input of UTF32 character from the keyboard or such */
-static void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar )
+VG_STATIC void console_proc_wchar( GLFWwindow* ptrW, u32 uWchar )
 {
        if( uWchar <= 0x7F && (char)uWchar != 0x60)
        {