diff --git a/ascii.cpp b/ascii.cpp index 67bddd5..53ec42c 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -6,6 +6,7 @@ #define swap(a, b) {auto tmp=a;a=b;b=tmp;} +char lua_filename[1024]; char window_title[256]; uint8_t mem[2400]; uint8_t *char_screen = NULL; @@ -40,6 +41,7 @@ char debug_text[debug_total_size]; int debug_cursor = 0; int debug_prompt = 0; int debug_cursor_blink = 30; +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 }; const Uint8 *keys; @@ -54,7 +56,7 @@ void reinit() { case 0: screen_width = 80; screen_height = 30; - current_color = 0x19; + current_color = 0x07; cursor_x = 0; cursor_y = 0; char_screen = &mem[0]; @@ -101,6 +103,8 @@ void audioCallback(void * userdata, uint8_t * stream, int len) { } 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; isize;++i) pixels[i] = palette[screen_surface->p[i]]; SDL_UnlockTexture(mini_bak); - SDL_SetRenderDrawColor(mini_ren, (palette[9] >> 16)&0xff, (palette[9] >> 8)&0xff, palette[9]&0xff, 0); + SDL_SetRenderDrawColor(mini_ren, (palette[0] >> 16)&0xff, (palette[0] >> 8)&0xff, palette[0]&0xff, 0); //SDL_SetRenderDrawColor(mini_ren, 255, 0, 0, 0); SDL_RenderClear(mini_ren); SDL_Rect rect = {40, 40, 640, 480}; @@ -410,6 +421,13 @@ uint8_t ascii(const char *str, uint8_t index) { return str[index]; } +char chr_trans[2] = "X"; +const char* chr(uint8_t ascii) { + uint8_t* peiv = (uint8_t*)chr_trans; + *peiv = ascii; + return chr_trans; +} + void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7) { font[index*8] = b0; font[index*8+1] = b1; @@ -447,3 +465,8 @@ void setmode(const uint8_t mode) { current_mode = mode; reinit(); } + +void load(const char* str) { + SDL_strlcpy(lua_filename, str, SDL_strlen(str)+1); + should_reset = true; +} \ No newline at end of file diff --git a/ascii.h b/ascii.h index 54901b4..0173705 100644 --- a/ascii.h +++ b/ascii.h @@ -161,6 +161,7 @@ void pdebug(); void debug_get_cmd(); uint8_t ascii(const char *str, uint8_t index); +const char* chr(uint8_t ascii); void setchar(uint8_t index, uint8_t b0, uint8_t b1, uint8_t b2, uint8_t b3, uint8_t b4, uint8_t b5, uint8_t b6, uint8_t b7); uint8_t peek(uint16_t addr); @@ -168,4 +169,6 @@ void poke(uint16_t addr, uint8_t val); void sound(float freq, uint32_t len); void nosound(); -void setmode(const uint8_t mode); \ No newline at end of file +void setmode(const uint8_t mode); + +void load(const char* str); diff --git a/lua.cpp b/lua.cpp index 899967d..3c0aac9 100644 --- a/lua.cpp +++ b/lua.cpp @@ -162,6 +162,11 @@ extern "C" { lua_pushinteger(L, ascii(str, index)); return 1; } + static int cpp_chr(lua_State *L) { + int val = luaL_checkinteger(L, 1); + lua_pushstring(L, chr(val)); + return 1; + } static int cpp_strlen(lua_State *L) { const char* str = luaL_checkstring(L, 1); lua_pushinteger(L, strlen(str)); @@ -212,6 +217,12 @@ extern "C" { setmode(val); return 0; } + + static int cpp_load(lua_State *L) { + const char* str = luaL_checkstring(L, 1); + load(str); + return 0; + } } #define STATE_STOPPED 0 @@ -227,16 +238,18 @@ bool lua_is_playing() { return lua_state == STATE_PLAYING; } -void lua_init(const bool start_playing) { +void lua_init(const char* filename, const bool start_playing) { if (lua_state != STATE_STOPPED) lua_quit(); L = luaL_newstate(); + init_exists = update_exists = false; + bool file_loaded = true; - if (luaL_loadfile(L, "game.lua")) { + if (luaL_loadfile(L, filename)) { debug("ERROR LOADING GAME"); debug(lua_tostring(L, -1)); lua_pop(L,1); setmode(0); - return; + file_loaded = false; } lua_pushcfunction(L,cpp_cls); lua_setglobal(L, "cls"); @@ -270,6 +283,7 @@ void lua_init(const bool start_playing) { lua_pushcfunction(L,cpp_srand); lua_setglobal(L, "srand"); lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr"); lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii"); + lua_pushcfunction(L,cpp_chr); lua_setglobal(L, "chr"); lua_pushcfunction(L,cpp_strlen); lua_setglobal(L, "strlen"); lua_pushcfunction(L,cpp_setchar); lua_setglobal(L, "setchar"); @@ -278,6 +292,7 @@ void lua_init(const bool start_playing) { lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound"); lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound"); lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode"); + lua_pushcfunction(L,cpp_load); lua_setglobal(L, "load"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); @@ -404,6 +419,8 @@ void lua_init(const bool start_playing) { lua_pushinteger(L, 14); lua_setglobal(L, "COLOR_YELLOW"); lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE"); + if (!file_loaded) return; + if (lua_pcall(L,0, LUA_MULTRET, 0)) { debug("RUNTIME ERROR"); debug(lua_tostring(L, -1)); diff --git a/lua.h b/lua.h index cf92ba7..2405b77 100644 --- a/lua.h +++ b/lua.h @@ -1,7 +1,7 @@ #pragma once bool lua_is_playing(); -void lua_init(const bool start_playing=true); +void lua_init(const char* filename, const bool start_playing=true); void lua_call_init(); void lua_call_update(); void lua_call_cmd(const char* str); diff --git a/main.cpp b/main.cpp index 8ed9852..9e76b4d 100644 --- a/main.cpp +++ b/main.cpp @@ -17,10 +17,10 @@ void do_terminal() { else if (key == KEY_0) { if (mods & KMOD_SHIFT) { debugchr('='); } else { debugchr('0'); } } else if (key == KEY_1) { if (mods & KMOD_SHIFT) { debugchr('!'); } else if (mods & KMOD_RALT) { debugchr('|'); } else { debugchr('1'); } } else if (key == KEY_2) { if (mods & KMOD_SHIFT) { debugchr('"'); } else if (mods & KMOD_RALT) { debugchr('@'); } else { debugchr('2'); } } - else if (key == KEY_3) { if (mods & KMOD_SHIFT) { debugchr('·'); } else if (mods & KMOD_RALT) { debugchr('#'); } else { debugchr('3'); } } + else if (key == KEY_3) { if (mods & KMOD_SHIFT) { debugchr(144); } else if (mods & KMOD_RALT) { debugchr('#'); } else { debugchr('3'); } } else if (key == KEY_4) { if (mods & KMOD_SHIFT) { debugchr('$'); } else if (mods & KMOD_RALT) { debugchr('~'); } else { debugchr('4'); } } - else if (key == KEY_5) { if (mods & KMOD_SHIFT) { debugchr('%'); } else if (mods & KMOD_RALT) { debugchr('€'); } else { debugchr('5'); } } - else if (key == KEY_6) { if (mods & KMOD_SHIFT) { debugchr('&'); } else if (mods & KMOD_RALT) { debugchr('¬'); } else { debugchr('6'); } } + else if (key == KEY_5) { if (mods & KMOD_SHIFT) { debugchr('%'); } else if (mods & KMOD_RALT) { debugchr(180); } else { debugchr('5'); } } + else if (key == KEY_6) { if (mods & KMOD_SHIFT) { debugchr('&'); } else if (mods & KMOD_RALT) { debugchr(173); } else { debugchr('6'); } } 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'); } }