From 63ca77e3e66726d9e14b474f37b7de5647603d42 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Tue, 14 Dec 2021 12:42:09 +0100 Subject: [PATCH] New console system WIP --- ascii.cpp | 127 ++++++++++++++++++++++++++++++++++++------------------ ascii.h | 4 +- lua.cpp | 4 +- main.cpp | 2 +- 4 files changed, 92 insertions(+), 45 deletions(-) diff --git a/ascii.cpp b/ascii.cpp index 0093f48..a68fb2a 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -5,6 +5,9 @@ #include "rom.c" #include "play.h" +#include +#include + #define swap(a, b) {auto tmp=a;a=b;b=tmp;} #define AUDIO_NONE 0 @@ -43,13 +46,14 @@ int pitch; uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA, 0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF }; -#define debug_line_size 80 -#define debug_num_lines 30 -#define debug_total_size 2400 //debug_line_size*debug_num_lines -char debug_text[debug_total_size]; -int debug_cursor = 0; -int debug_prompt = 0; +//[TODEL]#define debug_line_size 80 +//#define debug_num_lines 30 +//#define debug_total_size 2400 //debug_line_size*debug_num_lines +//char debug_text[debug_total_size]; +//int debug_cursor = 0; +int debug_prompt = -1; int debug_cursor_blink = 30; +std::vector cmd_list; bool should_reset = false; //Uint8 keymapping[6] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X }; @@ -145,11 +149,13 @@ void romcpy() { SDL_memcpy(&mem[2560], rom, rom_size); } +void debug_set_prompt(); + int main(int argc,char*argv[]) { SDL_strlcpy(lua_filename, "game.lua", 9); if (argc > 1) SDL_strlcpy(lua_filename, argv[1], 1023); - for (int i=0; i0) { cursor_x=0;cursor_y++;if(cursor_y>=screen_height) debug_one_line_up(); } + char_screen[cursor_x+cursor_y*screen_width] = '>'; + cursor_x++; + debug_prompt = cursor_x+cursor_y*screen_width; } void debugchr(const uint8_t chr) { + int pos = cursor_x+cursor_y*screen_width; if (chr == 8) { - if (debug_cursor>(debug_prompt+1)) { - debug_text[debug_cursor--] = 32; - debug_text[debug_cursor] = 32; + if (cursor_x>1) { + char_screen[pos++] = 32; + char_screen[pos] = 32; + cursor_x--; } } else { - debug_text[debug_cursor++] = chr; - if (debug_cursor >= debug_total_size) debug_one_line_up(); + char_screen[pos] = chr; + cursor_x++; + if (cursor_x >= screen_width) { + cursor_x=0; cursor_y++; + if (cursor_y >= screen_height) debug_one_line_up(); + } } } void debug(const char *str) { const int len = SDL_strlen(str); + int cursor = cursor_x+cursor_y*screen_width; for (int i=0; i= debug_total_size) debug_one_line_up(); + char_screen[cursor++] = str[i]; + if (cursor >= screen_width*screen_height) debug_one_line_up(); } - debug_cursor = (int(debug_cursor/debug_line_size)+1)*debug_line_size; - if (debug_cursor >= debug_total_size) debug_one_line_up(); - debug_set_prompt(); + cursor_x = 0; + cursor_y = (cursor/screen_width)+1; + if (cursor_y >= screen_height) debug_one_line_up(); + //debug_set_prompt(); } +/*[TODEL] void pdebug() { int i=0; for (int y=0; y=screen_height)debug_one_line_up(); + cmd_index=0; + lua_call_cmd(cmd_list[cmd_list.size()-1].c_str()); + //if ((cursor_y>0) || (cursor_x>0)) { cursor_x=0;cursor_y++;if(cursor_y>=screen_height)debug_one_line_up(); } + debug_set_prompt(); +} + +void next_cmd() { + if (cmd_index < cmd_list.size()) cmd_index++; + const char* cmd = cmd_list[cmd_list.size()-cmd_index].c_str(); + int pos = cursor_x+cursor_y*screen_width; + for (int i=debug_prompt;i<=pos;++i) char_screen[i] = 32; + SDL_memcpy(&char_screen[debug_prompt], cmd, strlen(cmd)); + pos += debug_prompt; + cursor_x = pos%screen_width; + cursor_y = pos/screen_width; +} + +void prev_cmd() { + } uint8_t ascii(const char *str, uint8_t index) { diff --git a/ascii.h b/ascii.h index 6650f2e..8572b0d 100644 --- a/ascii.h +++ b/ascii.h @@ -159,8 +159,10 @@ const char* tostr(float val); void debugchr(const uint8_t chr); void debug(const char *str); -void pdebug(); +//void pdebug(); void debug_get_cmd(); +void next_cmd(); +void prev_cmd(); uint8_t ascii(const char *str, uint8_t index); const char* chr(uint8_t ascii); diff --git a/lua.cpp b/lua.cpp index 97a9988..ed2e944 100644 --- a/lua.cpp +++ b/lua.cpp @@ -635,12 +635,12 @@ void lua_init(const char* filename, const bool start_playing) { void lua_call_cmd(const char* str) { if (luaL_dostring(L, str)) { - debug(" "); + //debug(" "); debug("ERROR"); debug(lua_tostring(L, -1)); lua_pop(L,1); } - debug(" "); + //debug(" "); } void lua_call_init() { diff --git a/main.cpp b/main.cpp index 059db4d..ac98b30 100644 --- a/main.cpp +++ b/main.cpp @@ -36,7 +36,7 @@ void do_terminal() { else if (key == KEY_RIGHTBRACKET) { if (mods & KMOD_SHIFT) { debugchr('*'); } else if (mods & KMOD_RALT) { debugchr(']'); } else { debugchr('+'); } } } //cls(0); - pdebug(); + //pdebug(); } void loop() {