From be083a23e24a5b289bd7d8e96cd823a25c336310 Mon Sep 17 00:00:00 2001 From: Raimon Zamora Date: Tue, 14 Dec 2021 15:59:14 +0100 Subject: [PATCH] [FEAT] New terminal console system --- ascii.cpp | 48 +++++++++++++++++++++++++++++++++--------------- ascii.h | 2 +- lua.cpp | 36 ++++++++++++++++++++++++------------ main.cpp | 8 +++++++- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/ascii.cpp b/ascii.cpp index a68fb2a..032d4c5 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -224,6 +224,7 @@ int main(int argc,char*argv[]) { if (lua_is_playing()) { lua_call_update(); + if (!lua_is_playing()) debug_set_prompt(); } else { debug_cursor_blink--; if (debug_cursor_blink == 0) { @@ -299,10 +300,9 @@ void cls(uint8_t value) { SDL_memset(char_screen, value, screen_width*screen_height); if (current_mode != 0) SDL_memset(color_screen, current_color, screen_width*screen_height); cursor_x = cursor_y = 0; - if (!lua_is_playing()) { - char_screen[0] = '>'; - cursor_x=1; - } + //if (!lua_is_playing()) { + // debug_set_prompt() + //} } void ink(uint8_t value) { @@ -478,16 +478,21 @@ void debugchr(const uint8_t chr) { } } -void debug(const char *str) { +void debug(const char *str, const bool newline) { const int len = SDL_strlen(str); int cursor = cursor_x+cursor_y*screen_width; for (int i=0; i= screen_width*screen_height) debug_one_line_up(); } - cursor_x = 0; - cursor_y = (cursor/screen_width)+1; - if (cursor_y >= screen_height) debug_one_line_up(); + if (newline) { + cursor_x = 0; + cursor_y = (cursor/screen_width)+1; + if (cursor_y >= screen_height) debug_one_line_up(); + } else { + cursor_x = cursor%screen_width; + cursor_y = cursor/screen_width; + } //debug_set_prompt(); } @@ -511,24 +516,36 @@ void debug_get_cmd() { char_screen[cursor_x+cursor_y*screen_width] = 32; cursor_x=0;cursor_y++;if(cursor_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(); + const char* cmd = cmd_list[cmd_list.size()-1].c_str(); + + if (cmd[0]=='r' && cmd[1]=='u' && cmd[2]=='n' && cmd[3]=='\0') { + lua_quit(); + reinit(); + lua_init(lua_filename); + lua_call_init(); + } else { + lua_call_cmd(cmd); + debug_set_prompt(); + } } -void next_cmd() { - if (cmd_index < cmd_list.size()) cmd_index++; +void get_cmd_by_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; + pos = debug_prompt+strlen(cmd); cursor_x = pos%screen_width; cursor_y = pos/screen_width; } +void next_cmd() { + if (cmd_index < cmd_list.size()) cmd_index++; + get_cmd_by_index(); +} void prev_cmd() { - + if (cmd_index > 0) cmd_index--; + get_cmd_by_index(); } uint8_t ascii(const char *str, uint8_t index) { @@ -598,6 +615,7 @@ void play(const char* str) { void setmode(const uint8_t mode) { current_mode = mode; reinit(); + cls(); } void load(const char* str) { diff --git a/ascii.h b/ascii.h index 8572b0d..bddae6d 100644 --- a/ascii.h +++ b/ascii.h @@ -158,7 +158,7 @@ int rnd(int x); const char* tostr(float val); void debugchr(const uint8_t chr); -void debug(const char *str); +void debug(const char *str, const bool newline=true); //void pdebug(); void debug_get_cmd(); void next_cmd(); diff --git a/lua.cpp b/lua.cpp index ed2e944..8663641 100644 --- a/lua.cpp +++ b/lua.cpp @@ -417,7 +417,7 @@ void lua_init(const char* filename, const bool start_playing) { if (filename == NULL) { if (luaL_loadstring(L, boot)) { - debug("BOOT ERROR"); + debug("BOOT ERROR:", false); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); @@ -425,7 +425,7 @@ void lua_init(const char* filename, const bool start_playing) { } } else { if (luaL_loadfile(L, filename)) { - debug("ERROR LOADING GAME"); + debug("ERROR LOADING GAME:",false); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); @@ -615,7 +615,7 @@ void lua_init(const char* filename, const bool start_playing) { if (!file_loaded) return; if (lua_pcall(L,0, LUA_MULTRET, 0)) { - debug("RUNTIME ERROR"); + debug("RUNTIME ERROR:", false); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); @@ -634,20 +634,32 @@ void lua_init(const char* filename, const bool start_playing) { } void lua_call_cmd(const char* str) { - if (luaL_dostring(L, str)) { - //debug(" "); - debug("ERROR"); - debug(lua_tostring(L, -1)); - lua_pop(L,1); + if (str[0]=='?') { + const int size = strlen(str)+14; + char* cmd = (char*)malloc(size); + memcpy(cmd, "print(tostr(", 12); + memcpy(&cmd[12], &str[1], strlen(str)-1); + cmd[size-1]='\0'; + cmd[size-3]=cmd[size-2]=')'; + if (luaL_dostring(L, cmd)) { + debug("ERROR:",false); + debug(lua_tostring(L, -1)); + lua_pop(L,1); + } + } else { + if (luaL_dostring(L, str)) { + debug("ERROR:",false); + debug(lua_tostring(L, -1)); + lua_pop(L,1); + } } - //debug(" "); } void lua_call_init() { if (!init_exists) return; lua_getglobal(L, "init"); if (lua_pcall(L, 0, 0, 0)) { - debug("RUNTIME ERROR"); + debug("RUNTIME ERROR:",false); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); @@ -656,10 +668,10 @@ void lua_call_init() { } void lua_call_update() { - if (!update_exists) return; + if (!update_exists) { lua_state = STATE_PAUSED; return; } lua_getglobal(L, "update"); if (lua_pcall(L, 0, 0, 0)) { - debug("RUNTIME ERROR"); + debug("RUNTIME ERROR:",false); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); diff --git a/main.cpp b/main.cpp index ac98b30..193586a 100644 --- a/main.cpp +++ b/main.cpp @@ -24,7 +24,9 @@ void do_terminal() { else if (key == KEY_7) { if (mods & KMOD_SHIFT) { debugchr('/'); } else { debugchr('7'); } } else if (key == KEY_8) { if (mods & KMOD_SHIFT) { debugchr('('); } else { debugchr('8'); } } else if (key == KEY_9) { if (mods & KMOD_SHIFT) { debugchr(')'); } else { debugchr('9'); } } - else if (key == KEY_RETURN) debug_get_cmd(); + else if (key == KEY_RETURN or key == KEY_KP_ENTER) debug_get_cmd(); + else if (key == KEY_UP) next_cmd(); + else if (key == KEY_DOWN) prev_cmd(); else if (key == KEY_SPACE) debugchr(32); else if (key == KEY_BACKSPACE) debugchr(8); else if (key == KEY_MINUS) { if (mods & KMOD_SHIFT) { debugchr('?'); } else { debugchr('\''); } } @@ -34,6 +36,10 @@ void do_terminal() { else if (key == KEY_SLASH) { if (mods & KMOD_SHIFT) { debugchr('_'); } else { debugchr('-'); } } else if (key == KEY_LEFTBRACKET) { if (mods & KMOD_SHIFT) { debugchr(160); } else if (mods & KMOD_RALT) { debugchr('['); } else { debugchr(96); } } else if (key == KEY_RIGHTBRACKET) { if (mods & KMOD_SHIFT) { debugchr('*'); } else if (mods & KMOD_RALT) { debugchr(']'); } else { debugchr('+'); } } + else if (key == KEY_APOSTROPHE) { if (mods & KMOD_SHIFT) { debugchr(162); } else if (mods & KMOD_RALT) { debugchr('{'); } else { debugchr(161); } } + else if (key == KEY_BACKSLASH) { if (mods & KMOD_SHIFT) { debugchr('C'); } else if (mods & KMOD_RALT) { debugchr('}'); } else { debugchr('c'); } } + else if (key == KEY_GRAVE) { if (mods & KMOD_SHIFT) { debugchr('>'); } else { debugchr('<'); } } + else if (key == KEY_NONUSBACKSLASH) { if (mods & KMOD_SHIFT) { debugchr(164); } else if (mods & KMOD_RALT) { debugchr('\\'); } else { debugchr(163); } } } //cls(0); //pdebug();