- u32 convar_count, function_count;
-
- char input[96];
- int cursor_user, cursor_pos, string_length;
-
- char history[32][96];
- int history_last, history_pos, history_count;
-
- int enabled;
-}
-vg_console;
-
-VG_STATIC void vg_convar_push( struct vg_convar 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 );
-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 vg_execute_console_input( const char *cmd );
-
-/*
- * Console interface
- */
-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 int _vg_console_enabled(void);
-VG_STATIC void console_proc_key( SDL_Keysym ev );
-
-/*
- * Implementation
- */
-VG_STATIC int _vg_console_enabled(void)
-{
- return vg_console.enabled;
-}
-
-VG_STATIC void vg_convar_push( vg_convar cv )
-{
- 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;
-}
-
-VG_STATIC void vg_function_push( struct vg_cmd 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;
-}
-
-VG_STATIC void _vg_console_draw( void )
-{
- if( !vg_console.enabled )
- return;
-
- SDL_AtomicLock( &log_print_sl );
-
- int ptr = vg_log.buffer_line_current;
- int const fh = 14,
- log_lines = 32;
- int console_lines = VG_MIN( log_lines, vg_log.buffer_line_count );
-
- vg_uictx.cursor[0] = 0;
- vg_uictx.cursor[1] = 0;
- vg_uictx.cursor[3] = log_lines*fh;
- ui_fill_x();
-
- ui_new_node();
- {
- ui_fill_rect( vg_uictx.cursor, 0x77333333 );
-
- vg_uictx.cursor[3] = fh;
- ui_align_bottom();
-
- for( int i=0; i<console_lines; i ++ )
- {
- ptr --;
-
- if( ptr < 0 )
- ptr = vg_list_size( vg_log.buffer )-1;
-
- ui_text( vg_uictx.cursor, vg_log.buffer[ptr], 1, 0 );
- vg_uictx.cursor[1] -= fh;
- }
-
- }
- ui_end_down();
-
- vg_uictx.cursor[1] += 2;
- vg_uictx.cursor[3] = fh;
-
- ui_new_node();
- {
- 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 );
-
- 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( vg_uictx.cursor, 0x66ffffff );
- }
- ui_end_down();
- SDL_AtomicUnlock( &log_print_sl );
-}
-
-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 );
- }
-
- for( int i=0; i<vg_console.convar_count; i ++ )
- {
- struct vg_convar *cv = &vg_console.convars[ i ];
- vg_info( "%s\n", cv->name );
- }
-
- return 0;
-}
-
-int _test_break( int argc, const char *argv[] )
-{
- vg_fatal_exit_loop( "Test crash from main, after loading (console)" );
- return 0;
-}
-
-VG_STATIC void _vg_console_init(void)
-{
- vg_function_push( (struct vg_cmd)
- {
- .name = "list",
- .function = _vg_console_list
- });
-
- vg_function_push( (struct vg_cmd)
- {
- .name = "crash",
- .function = _test_break
- });
-}
-
-VG_STATIC void vg_console_load_autos(void)
-{
- /* 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 )
- {
- vg_execute_console_input( line );
- }
- }
-
- fclose( fp );
- }
-}
-
-VG_STATIC void _vg_console_write_persistent(void)
-{
- FILE *fp = fopen( "cfg/auto.conf", "w" );
-
- for( int i=0; i<vg_console.convar_count; 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 );
-}
-
-VG_STATIC void _vg_console_free(void)
-{
- _vg_console_write_persistent();
-}
-
-VG_STATIC void vg_execute_console_input( const char *cmd )
-{
- char temp[512];
- char const *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( cmd[i] )
- {
- if( cmd[i] == ' ' || cmd[i] == '\t' )
- {
- temp[i] = '\0';
- in_token = 0;
-
- if( arg_count == vg_list_size( args ) )
- break;
- }
- else
- {
- temp[i] = cmd[i];
-
- if( !in_token )
- {
- args[ arg_count ++ ] = temp + i;
- in_token = 1;
- }
- }
- }
- else
- {
- temp[i] = '\0';
- break;
- }
- }
-
- if( arg_count == 0 )
- return;
-
- int data_int;
- float data_float;
-
- for( int i=0; i<vg_console.convar_count; i ++ )
- {
- struct vg_convar *cv = &vg_console.convars[ i ];
- if( !strcmp( cv->name, args[0] ) )
- {
- /* 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( 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;
- 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 ):
- data_float;
- break;
- }
-
- if( cv->update )
- cv->update();
- }
- else
- {
- switch( cv->data_type )
- {
- case k_convar_dtype_i32:
- vg_info( "= %d\n", *((int *)cv->data) );
- break;
- case k_convar_dtype_u32:
- vg_info( "= %u\n", *((u32 *)cv->data) );
- break;
- case k_convar_dtype_f32:
- vg_info( "= %.4f\n", *((float *)cv->data) );
- break;
- }
- }
-
- return;
- }
- }
-
- /*
- * Find and excecute command
- */
- for( int i=0; i<vg_console.function_count; i ++ )
- {
- struct vg_cmd *cmd = &vg_console.functions[ i ];
- if( !strcmp( cmd->name, args[0] ) )
- {
- cmd->function( arg_count-1, args+1 );
- return;
- }
- }
-
- vg_error( "No command/var named '%s'. Use 'list' to view all\n", args[0] );
-}
-
-/*
- * Console Interface
- */
-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 );
-}
-
-VG_STATIC void console_move_cursor( int* cursor0, int* cursor1,
- int dir, int snap_together )
-{
- *cursor0 = VG_MAX( 0, vg_console.cursor_user + dir );
- *cursor0 =
- VG_MIN(
- VG_MIN( vg_list_size( vg_console.input ), strlen( vg_console.input )),
- *cursor0 );
-
- if( snap_together )
- *cursor1 = *cursor0;
-}
-
-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;
- int move_end =
- VG_MIN( move_to+move_amount, vg_list_size( vg_console.input ) );
- move_amount = move_end-move_to;
-
- if( move_amount )
- memmove( &vg_console.input[ move_to ],
- &vg_console.input[ datastart ],
- move_end-move_to );
-
- vg_console.input[ move_end ] = '\0';
-
- return VG_MIN( length, vg_list_size( vg_console.input )-datastart );
-}
-
-VG_STATIC int console_delete_char( int direction )
-{
- int start, end;
- console_make_selection( &start, &end );
-
- /* There is no selection */
- 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 );
- }
-
- /* Still no selction, no need to do anything */
- if( !(end-start) )
- return start;
-
- /* Copy the end->terminator to start */
- int remaining_length = strlen( vg_console.input )+1-end;
- memmove( &vg_console.input[ start ],
- &vg_console.input[ end ],
- remaining_length );
- return start;
-}