Still working on the console...

This commit is contained in:
2021-12-05 16:34:05 +01:00
parent fe9220bb39
commit cf608cea78
5 changed files with 55 additions and 12 deletions

View File

@@ -6,6 +6,7 @@
#define swap(a, b) {auto tmp=a;a=b;b=tmp;} #define swap(a, b) {auto tmp=a;a=b;b=tmp;}
char lua_filename[1024];
char window_title[256]; char window_title[256];
uint8_t mem[2400]; uint8_t mem[2400];
uint8_t *char_screen = NULL; uint8_t *char_screen = NULL;
@@ -40,6 +41,7 @@ char debug_text[debug_total_size];
int debug_cursor = 0; int debug_cursor = 0;
int debug_prompt = 0; int debug_prompt = 0;
int debug_cursor_blink = 30; 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 }; //Uint8 keymapping[6] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X };
const Uint8 *keys; const Uint8 *keys;
@@ -54,7 +56,7 @@ void reinit() {
case 0: case 0:
screen_width = 80; screen_width = 80;
screen_height = 30; screen_height = 30;
current_color = 0x19; current_color = 0x07;
cursor_x = 0; cursor_x = 0;
cursor_y = 0; cursor_y = 0;
char_screen = &mem[0]; char_screen = &mem[0];
@@ -101,6 +103,8 @@ void audioCallback(void * userdata, uint8_t * stream, int len) {
} }
int main(int argc,char*argv[]) { 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; i<debug_total_size;++i) debug_text[i] = 32; for (int i=0; i<debug_total_size;++i) debug_text[i] = 32;
@@ -119,10 +123,17 @@ int main(int argc,char*argv[]) {
reinit(); reinit();
debug("ASCII SYSTEM BOOTING..."); debug("ASCII SYSTEM BOOTING...");
lua_init(); lua_init(lua_filename);
lua_call_init(); lua_call_init();
while(!exit) { while(!exit) {
if (should_reset) {
should_reset = false;
setmode(1);
reinit();
lua_init(lua_filename);
lua_call_init();
}
key_just_pressed = 0; key_just_pressed = 0;
mouse_wheel = 0; mouse_wheel = 0;
while(SDL_PollEvent(&mini_eve)) { while(SDL_PollEvent(&mini_eve)) {
@@ -142,7 +153,7 @@ int main(int argc,char*argv[]) {
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) { } else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
lua_quit(); lua_quit();
reinit(); reinit();
lua_init(); lua_init(lua_filename);
lua_call_init(); lua_call_init();
} else { } else {
key_just_pressed = mini_eve.key.keysym.scancode; key_just_pressed = mini_eve.key.keysym.scancode;
@@ -215,7 +226,7 @@ int main(int argc,char*argv[]) {
//for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]]; //for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]];
SDL_UnlockTexture(mini_bak); 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_SetRenderDrawColor(mini_ren, 255, 0, 0, 0);
SDL_RenderClear(mini_ren); SDL_RenderClear(mini_ren);
SDL_Rect rect = {40, 40, 640, 480}; SDL_Rect rect = {40, 40, 640, 480};
@@ -410,6 +421,13 @@ uint8_t ascii(const char *str, uint8_t index) {
return str[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) { 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] = b0;
font[index*8+1] = b1; font[index*8+1] = b1;
@@ -447,3 +465,8 @@ void setmode(const uint8_t mode) {
current_mode = mode; current_mode = mode;
reinit(); reinit();
} }
void load(const char* str) {
SDL_strlcpy(lua_filename, str, SDL_strlen(str)+1);
should_reset = true;
}

View File

@@ -161,6 +161,7 @@ void pdebug();
void debug_get_cmd(); void debug_get_cmd();
uint8_t ascii(const char *str, uint8_t index); 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); 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); 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 sound(float freq, uint32_t len);
void nosound(); void nosound();
void setmode(const uint8_t mode); void setmode(const uint8_t mode);
void load(const char* str);

23
lua.cpp
View File

@@ -162,6 +162,11 @@ extern "C" {
lua_pushinteger(L, ascii(str, index)); lua_pushinteger(L, ascii(str, index));
return 1; 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) { static int cpp_strlen(lua_State *L) {
const char* str = luaL_checkstring(L, 1); const char* str = luaL_checkstring(L, 1);
lua_pushinteger(L, strlen(str)); lua_pushinteger(L, strlen(str));
@@ -212,6 +217,12 @@ extern "C" {
setmode(val); setmode(val);
return 0; return 0;
} }
static int cpp_load(lua_State *L) {
const char* str = luaL_checkstring(L, 1);
load(str);
return 0;
}
} }
#define STATE_STOPPED 0 #define STATE_STOPPED 0
@@ -227,16 +238,18 @@ bool lua_is_playing() {
return lua_state == STATE_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(); if (lua_state != STATE_STOPPED) lua_quit();
L = luaL_newstate(); 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("ERROR LOADING GAME");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0); setmode(0);
return; file_loaded = false;
} }
lua_pushcfunction(L,cpp_cls); lua_setglobal(L, "cls"); 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_srand); lua_setglobal(L, "srand");
lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr"); lua_pushcfunction(L,cpp_tostr); lua_setglobal(L, "tostr");
lua_pushcfunction(L,cpp_ascii); lua_setglobal(L, "ascii"); 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_strlen); lua_setglobal(L, "strlen");
lua_pushcfunction(L,cpp_setchar); lua_setglobal(L, "setchar"); 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_sound); lua_setglobal(L, "sound");
lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound"); lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound");
lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode"); 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, 0); lua_setglobal(L, "KEY_UNKNOWN");
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); 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, 14); lua_setglobal(L, "COLOR_YELLOW");
lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE"); lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE");
if (!file_loaded) return;
if (lua_pcall(L,0, LUA_MULTRET, 0)) { if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("RUNTIME ERROR"); debug("RUNTIME ERROR");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));

2
lua.h
View File

@@ -1,7 +1,7 @@
#pragma once #pragma once
bool lua_is_playing(); 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_init();
void lua_call_update(); void lua_call_update();
void lua_call_cmd(const char* str); void lua_call_cmd(const char* str);

View File

@@ -17,10 +17,10 @@ void do_terminal() {
else if (key == KEY_0) { if (mods & KMOD_SHIFT) { debugchr('='); } else { debugchr('0'); } } 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_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_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_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_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('¬'); } else { debugchr('6'); } } 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_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_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_9) { if (mods & KMOD_SHIFT) { debugchr(')'); } else { debugchr('9'); } }