Compare commits

2 Commits

Author SHA1 Message Date
acfaf873ce Code Editor WIP 2021-12-14 20:09:23 +01:00
be083a23e2 [FEAT] New terminal console system 2021-12-14 15:59:14 +01:00
4 changed files with 371 additions and 97 deletions

View File

@@ -46,23 +46,21 @@ int pitch;
uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA,
0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF };
//[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<std::string> 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 };
const Uint8 *keys;
Uint8 key_just_pressed = 0;
int mouse_x, mouse_y, mouse_wheel;
Uint32 mouse_buttons;
const char* get_filename() {
return lua_filename;
}
void reinit() {
if (mini_bak != NULL) SDL_DestroyTexture(mini_bak);
switch (current_mode) {
@@ -102,6 +100,18 @@ void reinit() {
//SDL_RenderSetLogicalSize(mini_ren, 160, 120);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120);
break;
case 3: // SUPERSECRET MODE FOR THE EDITOR!!!
screen_width = 80;
screen_height = 30;
current_color = 0x1f;
current_border = 9;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[0x1200];
//SDL_RenderSetLogicalSize(mini_ren, 640, 480);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 240);
break;
}
}
@@ -198,11 +208,11 @@ int main(int argc,char*argv[]) {
//setmode(0);
} else {
//setmode(old_mode);
//lua_resume();
lua_quit();
reinit();
lua_init(lua_filename);
lua_call_init();
lua_resume();
//lua_quit();
//reinit();
//lua_init(lua_filename);
//lua_call_init();
}
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
lua_quit();
@@ -224,6 +234,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) {
@@ -258,6 +269,7 @@ int main(int argc,char*argv[]) {
break;
case 1:
case 2:
case 3:
for (int y=0; y<screen_height; ++y) {
for (int x=0; x<screen_width; ++x) {
const uint8_t chr_color = COLSCR(x,y);
@@ -299,10 +311,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) {
@@ -467,6 +478,8 @@ void debugchr(const uint8_t chr) {
char_screen[pos++] = 32;
char_screen[pos] = 32;
cursor_x--;
} else {
play("c");
}
} else {
char_screen[pos] = chr;
@@ -478,16 +491,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<len;++i) {
char_screen[cursor++] = str[i];
if (cursor >= 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 +529,39 @@ 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 if (cmd[0]=='c' && cmd[1]=='o' && cmd[2]=='n' && cmd[3]=='t' && cmd[4]=='\0') {
lua_resume();
} else {
lua_call_cmd(cmd);
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();
void get_cmd_by_index() {
if (cmd_list.size() == 0) return;
const char* cmd = cmd_list[cmd_list.size()-cmd_index-1].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()-1) cmd_index++; else play("c");
get_cmd_by_index();
}
void prev_cmd() {
if (cmd_index > 0) cmd_index--; else play("c");
get_cmd_by_index();
}
uint8_t ascii(const char *str, uint8_t index) {
@@ -598,6 +631,7 @@ void play(const char* str) {
void setmode(const uint8_t mode) {
current_mode = mode;
reinit();
cls();
}
void load(const char* str) {

20
ascii.h
View File

@@ -110,7 +110,25 @@
#define KEY_RALT 230
#define KEY_RGUI 231
#define COLOR_BLACK 0
#define COLOR_BLUE 1
#define COLOR_GREEN 2
#define COLOR_CYAN 3
#define COLOR_RED 4
#define COLOR_MAGENTA 5
#define COLOR_BROWN 6
#define COLOR_LIGHT_GRAY 7
#define COLOR_DARK_GRAY 8
#define COLOR_LIGHT_BLUE 9
#define COLOR_LIGHT_GREEN 10
#define COLOR_LIGHT_CYAN 11
#define COLOR_LIGHT_RED 12
#define COLOR_LIGHT_MAGENTA 13
#define COLOR_YELLOW 14
#define COLOR_WHITE 15
void loop();
const char* get_filename();
void cls(uint8_t value=32);
void ink(uint8_t value); // global::ink
@@ -158,7 +176,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();

73
lua.cpp
View File

@@ -350,7 +350,7 @@ extern "C" {
static int cpp_setmode(lua_State *L) {
int val = luaL_checkinteger(L, 1);
setmode(val);
setmode(mid(0,val,3));
return 0;
}
@@ -400,8 +400,8 @@ extern "C" {
lua_State *L;
uint8_t lua_state = STATE_STOPPED;
bool init_exists = false;
bool update_exists = false;
//bool init_exists = false;
//bool update_exists = false;
bool lua_is_playing() {
return lua_state == STATE_PLAYING;
@@ -412,12 +412,12 @@ const char boot[] = "function init()setmode(1)cls()play('o5l0v5cegv4cegv3cegv2ce
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;
//init_exists = update_exists = false;
bool file_loaded = true;
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,13 +615,14 @@ 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);
return;
}
/*
lua_getglobal(L, "init");
if (lua_isfunction(L,-1)) init_exists = true;
lua_pop(L,1);
@@ -629,41 +630,57 @@ void lua_init(const char* filename, const bool start_playing) {
lua_getglobal(L, "update");
if (lua_isfunction(L,-1)) update_exists = true;
lua_pop(L,1);
*/
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);
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(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
lua_state = STATE_STOPPED;
if (lua_isfunction(L,-1)) {
if (lua_pcall(L, 0, 0, 0)) {
debug("RUNTIME ERROR:",false);
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
lua_state = STATE_STOPPED;
}
}
}
void lua_call_update() {
if (!update_exists) return;
lua_getglobal(L, "update");
if (lua_pcall(L, 0, 0, 0)) {
debug("RUNTIME ERROR");
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
lua_state = STATE_STOPPED;
if (lua_isfunction(L,-1)) {
if (lua_pcall(L, 0, 0, 0)) {
debug("RUNTIME ERROR:",false);
debug(lua_tostring(L, -1));
lua_pop(L,1);
setmode(0);
lua_state = STATE_STOPPED;
}
} else {
lua_state = STATE_PAUSED;
}
}

287
main.cpp
View File

@@ -1,57 +1,262 @@
#include "ascii.h"
#include <stdio.h>
#include <string>
#include <list>
std::list<std::string> code;
int current_editor = 0;
void do_terminal() {
SDL_Keymod mods = SDL_GetModState();
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
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(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(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'); } }
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('-'); } }
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('+'); } }
}
//cls(0);
//pdebug();
}
void init_terminal();
void do_terminal();
void init_code_editor();
void do_code_editor();
void loop() {
do_terminal();
/*
if (btnp(KEY_TAB)) {
current_editor = (++current_editor)%5;
current_editor = (++current_editor)%2;
switch(current_editor) {
case 0:
init_terminal();
break;
case 1:
init_code_editor();
break;
}
}
switch(current_editor) {
case 0:
do_terminal();
break;
case 1:
do_sprite_editor();
do_code_editor();
break;
}
*/
}
uint8_t get_char(uint8_t key) {
SDL_Keymod mods = SDL_GetModState();
if (key != KEY_UNKNOWN) {
if (key < 30) {
if ((mods & KMOD_SHIFT) || (mods & KMOD_CAPS)) {
return key+61;
} else {
return key+93;
}
}
else if (key == KEY_0) { if (mods & KMOD_SHIFT) { return '='; } else { return '0'; } }
else if (key == KEY_1) { if (mods & KMOD_SHIFT) { return '!'; } else if (mods & KMOD_RALT) { return '|'; } else { return '1'; } }
else if (key == KEY_2) { if (mods & KMOD_SHIFT) { return '"'; } else if (mods & KMOD_RALT) { return '@'; } else { return '2'; } }
else if (key == KEY_3) { if (mods & KMOD_SHIFT) { return 144; } else if (mods & KMOD_RALT) { return '#'; } else { return '3'; } }
else if (key == KEY_4) { if (mods & KMOD_SHIFT) { return '$'; } else if (mods & KMOD_RALT) { return '~'; } else { return '4'; } }
else if (key == KEY_5) { if (mods & KMOD_SHIFT) { return '%'; } else if (mods & KMOD_RALT) { return 180; } else { return '5'; } }
else if (key == KEY_6) { if (mods & KMOD_SHIFT) { return '&'; } else if (mods & KMOD_RALT) { return 173; } else { return '6'; } }
else if (key == KEY_7) { if (mods & KMOD_SHIFT) { return '/'; } else { return '7'; } }
else if (key == KEY_8) { if (mods & KMOD_SHIFT) { return '('; } else { return '8'; } }
else if (key == KEY_9) { if (mods & KMOD_SHIFT) { return ')'; } else { return '9'; } }
else if (key == KEY_SPACE) return 32;
else if (key == KEY_MINUS) { if (mods & KMOD_SHIFT) { return '?'; } else { return '\''; } }
else if (key == KEY_EQUALS) { if (mods & KMOD_SHIFT) { return 174; } else { return 175; } }
else if (key == KEY_COMMA) { if (mods & KMOD_SHIFT) { return ';'; } else { return ','; } }
else if (key == KEY_PERIOD) { if (mods & KMOD_SHIFT) { return ':'; } else { return '.'; } }
else if (key == KEY_SLASH) { if (mods & KMOD_SHIFT) { return '_'; } else { return '-'; } }
else if (key == KEY_LEFTBRACKET) { if (mods & KMOD_SHIFT) { return 160; } else if (mods & KMOD_RALT) { return '['; } else { return 96; } }
else if (key == KEY_RIGHTBRACKET) { if (mods & KMOD_SHIFT) { return '*'; } else if (mods & KMOD_RALT) { return ']'; } else { return '+'; } }
else if (key == KEY_APOSTROPHE) { if (mods & KMOD_SHIFT) { return 162; } else if (mods & KMOD_RALT) { return '{'; } else { return 161; } }
else if (key == KEY_BACKSLASH) { if (mods & KMOD_SHIFT) { return 'C'; } else if (mods & KMOD_RALT) { return '}'; } else { return 'c'; } }
else if (key == KEY_GRAVE) { if (mods & KMOD_SHIFT) { return '>'; } else { return '<'; } }
else if (key == KEY_NONUSBACKSLASH) { if (mods & KMOD_SHIFT) { return 164; } else if (mods & KMOD_RALT) { return '\\'; } else { return 163; } }
else return 0;
} else {
return 0;
}
}
void init_terminal() {
setmode(0);
cls();
}
void do_terminal() {
//SDL_Keymod mods = SDL_GetModState();
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
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_BACKSPACE) debugchr(8);
else {
uint8_t chr = get_char(key);
if (chr != 0) debugchr(chr);
}
}
}
void load_code() {
FILE *f = fopen(get_filename(), "rb");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
fseek(f, 0, SEEK_SET); /* same as rewind(f); */
char *buffer = (char*)malloc(fsize + 1);
fread(buffer, 1, fsize, f);
fclose(f);
buffer[fsize] = 0;
int start = 0;
for (int pos=0;pos<fsize;++pos) {
if (buffer[pos] == '\r') {
buffer[pos]='\0';
code.push_back(&buffer[start]);
start=pos+2;
}
}
free(buffer);
}
std::list<std::string>::iterator ls[28];
static int col = 0;
static int line = 0;
bool cursor_inverted = false;
int blink_wait = 30;
void refresh_code_editor() {
color(COLOR_WHITE, COLOR_BLUE);
cls();
color(COLOR_BLACK, COLOR_LIGHT_GRAY);
print(" File Edit Run ",0,0);
print(" ",0,29);
color(COLOR_WHITE, COLOR_BLUE);
std::list<std::string>::iterator it = code.begin();
int i=0;
while (it != code.end() && i<28) {
ls[i] = it;
print((*it).c_str(), 0, i+1);
++i; ++it;
}
}
void init_code_editor() {
setmode(3);
load_code();
refresh_code_editor();
}
std::string get_current_line() {
return *ls[line];
}
void invert_cursor() {
cursor_inverted = !cursor_inverted;
uint16_t pos = 0x1200 + col + (line+1)*80;
uint8_t col = peek(pos);
poke(pos, ((col >> 4) & 0x0f) + ((col << 4) & 0xf0) );
}
void move_cursor_up() {
if (line > 0) {
if (cursor_inverted) invert_cursor();
line--;
if (get_current_line().size() < col) col = get_current_line().size();
blink_wait=1;
}
}
void move_cursor_down() {
if (line < code.size()-1) {
if (cursor_inverted) invert_cursor();
line++;
if (get_current_line().size() < col) col = get_current_line().size();
blink_wait=1;
}
}
void move_cursor_right() {
if ( col < get_current_line().size() ) {
if (cursor_inverted) invert_cursor();
col++;
blink_wait=1;
} else {
if (line < code.size()-1) {
col = 0;
move_cursor_down();
}
}
}
void move_cursor_left() {
if (col > 0) {
if (cursor_inverted) invert_cursor();
col--;
blink_wait=1;
} else {
if (line > 0) {
move_cursor_up();
col = get_current_line().size();
}
}
}
void split_line() {
std::string str = get_current_line();
*ls[line] = str.substr(col);
std::list<std::string>::iterator newline = ls[line]++;
code.insert(newline, str.substr(0, col));
if (cursor_inverted) invert_cursor();
line++; col=0;
blink_wait=1;
refresh_code_editor();
}
void join_lines() {
if (cursor_inverted) invert_cursor();
std::string str = get_current_line();
//std::list<std::string>::iterator prevline = ls[line]--;
code.erase(ls[line]);
line--;
const int line_size = (*ls[line]).size();
*ls[line] += str;
col = line_size;
blink_wait=1;
refresh_code_editor();
}
void delete_char() {
if (col == 0) {
if (line > 0) join_lines();
} else {
if (cursor_inverted) invert_cursor();
std::string str = get_current_line();
std::string a = str.substr(0,col-1);
std::string b = str.substr(col);
*ls[line] = a+b;
col--;
blink_wait=1;
refresh_code_editor();
}
}
void do_code_editor() {
const uint8_t key = whichbtn();
if (key != KEY_UNKNOWN) {
if (key == KEY_RETURN or key == KEY_KP_ENTER) {
split_line();
}
else if (key == KEY_UP) move_cursor_up();
else if (key == KEY_DOWN) move_cursor_down();
else if (key == KEY_LEFT) move_cursor_left();
else if (key == KEY_RIGHT) move_cursor_right();
else if (key == KEY_BACKSPACE) delete_char();
else {
uint8_t chr = get_char(key);
if (chr != 0) debugchr(chr);
}
}
blink_wait--;
if (blink_wait == 0) {
blink_wait = 30;
invert_cursor();
}
}