Working on the terminal...

This commit is contained in:
2021-12-05 10:16:37 +01:00
parent 9e547e7292
commit fe9220bb39
6 changed files with 99 additions and 28 deletions

View File

@@ -38,6 +38,7 @@ uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0
#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;
int debug_cursor_blink = 30;
//Uint8 keymapping[6] = { SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP, SDL_SCANCODE_DOWN, SDL_SCANCODE_Z, SDL_SCANCODE_X };
@@ -53,12 +54,12 @@ void reinit() {
case 0:
screen_width = 80;
screen_height = 30;
current_color = 0x1e;
current_color = 0x19;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[1200];
SDL_RenderSetLogicalSize(mini_ren, 640, 480);
//SDL_RenderSetLogicalSize(mini_ren, 640, 480);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 240);
break;
case 1:
@@ -69,7 +70,7 @@ void reinit() {
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[1200];
SDL_RenderSetLogicalSize(mini_ren, 320, 240);
//SDL_RenderSetLogicalSize(mini_ren, 320, 240);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
break;
case 2:
@@ -80,7 +81,7 @@ void reinit() {
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[300];
SDL_RenderSetLogicalSize(mini_ren, 160, 120);
//SDL_RenderSetLogicalSize(mini_ren, 160, 120);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120);
break;
}
@@ -104,7 +105,7 @@ int main(int argc,char*argv[]) {
for (int i=0; i<debug_total_size;++i) debug_text[i] = 32;
SDL_Init(49);
mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640+80, 480+80, SDL_WINDOW_SHOWN);
mini_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC);
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
//SDL_RenderSetLogicalSize(mini_ren, 320, 240);
@@ -129,12 +130,14 @@ int main(int argc,char*argv[]) {
if (mini_eve.type == SDL_KEYDOWN) {
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
if (lua_is_playing()) {
lua_quit();
lua_pause();
//lua_quit();
setmode(0);
} else {
setmode(1);
lua_init();
lua_call_init();
lua_resume();
//lua_init();
//lua_call_init();
}
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
lua_quit();
@@ -212,7 +215,12 @@ int main(int argc,char*argv[]) {
//for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]];
SDL_UnlockTexture(mini_bak);
SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
SDL_SetRenderDrawColor(mini_ren, (palette[9] >> 16)&0xff, (palette[9] >> 8)&0xff, palette[9]&0xff, 0);
//SDL_SetRenderDrawColor(mini_ren, 255, 0, 0, 0);
SDL_RenderClear(mini_ren);
SDL_Rect rect = {40, 40, 640, 480};
SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect);
//SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
SDL_RenderPresent(mini_ren);
}
lua_quit();
@@ -348,16 +356,24 @@ const char* tostr(int val) {
return SDL_itoa(val, tostr_tmp, 10);
}
void debug_set_prompt() {
debug_prompt = debug_cursor++;
debug_text[debug_prompt] = '>';
}
void debug_one_line_up() {
for (int i=0; i<debug_total_size-debug_line_size;++i) debug_text[i] = debug_text[i+debug_line_size];
for (int i=debug_total_size-debug_line_size; i<debug_total_size;++i) debug_text[i] = 32;
debug_cursor = debug_total_size-debug_line_size;
debug_set_prompt();
}
void debugchr(const uint8_t chr) {
if (chr == 8) {
if (debug_cursor>(debug_prompt+1)) {
debug_text[debug_cursor--] = 32;
debug_text[debug_cursor] = 32;
}
} else {
debug_text[debug_cursor++] = chr;
if (debug_cursor >= debug_total_size) debug_one_line_up();
@@ -372,6 +388,7 @@ void debug(const char *str) {
}
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();
}
void pdebug() {
@@ -384,6 +401,11 @@ void pdebug() {
}
}
void debug_get_cmd() {
debug_text[debug_cursor] = 0;
lua_call_cmd(&debug_text[debug_prompt+1]);
}
uint8_t ascii(const char *str, uint8_t index) {
return str[index];
}

View File

@@ -158,6 +158,7 @@ const char* tostr(int val);
void debugchr(const uint8_t chr);
void debug(const char *str);
void pdebug();
void debug_get_cmd();
uint8_t ascii(const char *str, uint8_t index);

View File

@@ -1,3 +1,4 @@
cls()
function init()
x, y, dx, dy = 0, 0, 1, 1
paper(COLOR_BLACK)

47
lua.cpp
View File

@@ -214,20 +214,25 @@ extern "C" {
}
}
#define STATE_STOPPED 0
#define STATE_PAUSED 1
#define STATE_PLAYING 2
lua_State *L;
bool is_playing = false;
uint8_t lua_state = STATE_STOPPED;
bool init_exists = false;
bool update_exists = false;
bool lua_is_playing() {
return is_playing;
return lua_state == STATE_PLAYING;
}
void lua_init() {
void lua_init(const bool start_playing) {
if (lua_state != STATE_STOPPED) lua_quit();
L = luaL_newstate();
if (luaL_loadfile(L, "game.lua")) {
debug("error loading game");
debug("ERROR LOADING GAME");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
@@ -400,7 +405,7 @@ void lua_init() {
lua_pushinteger(L, 15); lua_setglobal(L, "COLOR_WHITE");
if (lua_pcall(L,0, LUA_MULTRET, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
@@ -415,18 +420,28 @@ void lua_init() {
if (lua_isfunction(L,-1)) update_exists = true;
lua_pop(L,1);
is_playing = true;
lua_state = start_playing ? STATE_PLAYING : STATE_PAUSED;
}
void lua_call_cmd(const char* str) {
if (luaL_dostring(L, str)) {
debug(" ");
debug("ERROR");
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");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
is_playing = false;
lua_state = STATE_STOPPED;
}
}
@@ -434,16 +449,24 @@ void lua_call_update() {
if (!update_exists) return;
lua_getglobal(L, "update");
if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error");
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
is_playing = false;
lua_state = STATE_STOPPED;
}
}
void lua_quit() {
if (!is_playing) return;
is_playing = false;
if (lua_state == STATE_STOPPED) return;
lua_state = STATE_STOPPED;
lua_close(L);
}
void lua_pause() {
lua_state = STATE_PAUSED;
}
void lua_resume() {
lua_state = STATE_PLAYING;
}

5
lua.h
View File

@@ -1,7 +1,10 @@
#pragma once
bool lua_is_playing();
void lua_init();
void lua_init(const bool start_playing=true);
void lua_call_init();
void lua_call_update();
void lua_call_cmd(const char* str);
void lua_quit();
void lua_pause();
void lua_resume();

View File

@@ -3,14 +3,35 @@
int current_editor = 0;
void do_terminal() {
SDL_Keymod mods = SDL_GetModState();
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
if (key < 30) debugchr(key+61);
else if (key < 39) debugchr(key+19);
else if (key == KEY_0) debugchr(key+9);
else if (key == KEY_RETURN) debug(" ");
if (key < 30) {
if ((mods & KMOD_SHIFT) || (mods & KMOD_CAPS)) {
debugchr(key+61);
} else {
debugchr(key+93);
}
}
//else if (key < 39) debugchr(key+19);
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_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_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_SPACE) debugchr(32);
else if (key == KEY_BACKSPACE) debugchr(8);
else if (key == KEY_MINUS) { if (mods & KMOD_SHIFT) { debugchr('?'); } else { debugchr('\''); } }
else if (key == KEY_EQUALS) { if (mods & KMOD_SHIFT) { debugchr(174); } else { debugchr(175); } }
else if (key == KEY_COMMA) { if (mods & KMOD_SHIFT) { debugchr(';'); } else { debugchr(','); } }
else if (key == KEY_PERIOD) { if (mods & KMOD_SHIFT) { debugchr(':'); } else { debugchr('.'); } }
else if (key == KEY_SLASH) { if (mods & KMOD_SHIFT) { debugchr('_'); } else { debugchr('-'); } }
}
cls(0);
pdebug();