This commit is contained in:
2022-10-18 20:09:57 +02:00
4 changed files with 24 additions and 368 deletions

1
.gitignore vendored
View File

@@ -6,3 +6,4 @@ wiki/*
scr_min.c scr_min.c
tests.lua tests.lua
fake_editor.lua fake_editor.lua
*.bin

View File

@@ -21,6 +21,10 @@ uint8_t *char_screen = NULL;
uint8_t *color_screen = NULL; uint8_t *color_screen = NULL;
uint8_t screen_width = 40; uint8_t screen_width = 40;
uint8_t screen_height = 30; uint8_t screen_height = 30;
int v_screen_w = 640;
int v_screen_h = 480;
uint8_t hborder = 40;
uint8_t vborder = 40;
uint8_t current_color = 0x1e; uint8_t current_color = 0x1e;
uint8_t current_border = 0; uint8_t current_border = 0;
uint8_t current_mode = 1; uint8_t current_mode = 1;
@@ -65,6 +69,8 @@ const char* get_filename() {
void reinit() { void reinit() {
if (mini_bak != NULL) SDL_DestroyTexture(mini_bak); if (mini_bak != NULL) SDL_DestroyTexture(mini_bak);
counter=0; counter=0;
hborder = vborder = 40;
v_screen_w = 640; v_screen_h = 480;
switch (current_mode) { switch (current_mode) {
case 0: case 0:
screen_width = 80; screen_width = 80;
@@ -102,17 +108,21 @@ void reinit() {
//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); mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120);
break; break;
case 3: // SUPERSECRET MODE FOR THE EDITOR!!! case 3:
screen_width = 80; hborder = 104;
screen_height = 30; vborder = 88;
current_color = 0x1f; v_screen_w = 512;
current_border = 9; v_screen_h = 384;
screen_width = 32;
screen_height = 24;
current_color = 0x07;
current_border = 0;
cursor_x = 0; cursor_x = 0;
cursor_y = 0; cursor_y = 0;
char_screen = &mem[0]; char_screen = &mem[0];
color_screen = &mem[0x1200]; color_screen = &mem[768];
//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); mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 256, 192);
break; break;
} }
} }
@@ -233,9 +243,9 @@ int main(int argc,char*argv[]) {
keys = SDL_GetKeyboardState(NULL); keys = SDL_GetKeyboardState(NULL);
int mx, my; int mx, my;
mouse_buttons = SDL_GetMouseState(&mx, &my); mouse_buttons = SDL_GetMouseState(&mx, &my);
if (mx>=40 && my>=40 && mx<680 && my<520) { if (mx>=hborder && my>=vborder && mx<(v_screen_w+hborder) && my<(v_screen_h+vborder)) {
mouse_x = (mx-40) / (640/screen_width); mouse_x = (mx-hborder) / (v_screen_w/screen_width);
mouse_y = (my-40) / (480/screen_height); mouse_y = (my-vborder) / (v_screen_h/screen_height);
} }
if (lua_is_playing()) { if (lua_is_playing()) {
@@ -303,7 +313,7 @@ int main(int argc,char*argv[]) {
SDL_SetRenderDrawColor(mini_ren, (palette[current_border] >> 16)&0xff, (palette[current_border] >> 8)&0xff, palette[current_border]&0xff, 0); SDL_SetRenderDrawColor(mini_ren, (palette[current_border] >> 16)&0xff, (palette[current_border] >> 8)&0xff, palette[current_border]&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 = {hborder, vborder, v_screen_w, v_screen_h};
SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect); SDL_RenderCopy(mini_ren, mini_bak, NULL, &rect);
//SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL); //SDL_RenderCopy(mini_ren, mini_bak, NULL, NULL);
SDL_RenderPresent(mini_ren); SDL_RenderPresent(mini_ren);

BIN
chuleta_font.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 35 KiB

357
main.cpp
View File

@@ -3,41 +3,15 @@
#include <string> #include <string>
#include <list> #include <list>
std::list<std::string> code;
int current_editor = 0;
void init_terminal(); void init_terminal();
void do_terminal(); void do_terminal();
void init_code_editor();
void do_code_editor();
void save_code();
void loop() { void loop() {
if (btnp(KEY_TAB)) {
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(); do_terminal();
break;
case 1:
do_code_editor();
break;
}
} }
void execute_run() { void execute_run() {
if (current_editor == 1) save_code(); //if (current_editor == 1) save_code();
} }
uint8_t get_char(uint8_t key) { uint8_t get_char(uint8_t key) {
@@ -97,332 +71,3 @@ void do_terminal() {
} }
} }
} }
void save_code() {
const char* file = get_filename();
FILE *f = fopen(file, "w");
for (std::list<std::string>::iterator it = code.begin(); it != code.end(); it++) {
fprintf(f, "%s\n", (*it).c_str());
}
fclose(f);
}
void load_code() {
const char* file = get_filename();
FILE *f = fopen(file, "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] == '\n') {
buffer[pos]='\0';
code.push_back(&buffer[start]);
start=pos+1;
} else if (buffer[pos] == '\r') {
buffer[pos]='\0';
code.push_back(&buffer[start]);
start=pos+1;
if (buffer[start] == '\n') {pos++; start++;}
}
}
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;
#define PARSER_NOTHING 0
#define PARSER_NUMBER 1
#define PARSER_IDENTIFIER 2
#define PARSER_STRING 3
#define PARSER_COMMENT 4
#define PARSER_FUNCTION 5
uint16_t parser_offset = 0;
uint8_t parser_pos = 0;
uint8_t parser_start=0;
void parser_setup(uint16_t address) {
parser_offset = address;
parser_pos = 0;
parser_start = 0;
}
const uint8_t parser_get_char(uint8_t offset=0) {
if ((parser_pos+offset) >= 80) return 0;
return peek(parser_offset+parser_pos+offset);
}
char alpha[80] = "";
uint8_t alpha_pos = 0;
void parser_next(const bool keep=false) {
if (keep) { alpha[alpha_pos++] = parser_get_char(); alpha[alpha_pos]=0; } else { alpha[0]=alpha_pos=0; }
if (parser_pos < 80) parser_pos++;
}
#define ISZERO (parser_get_char()=='0')
#define ISX (parser_get_char()=='z' || parser_get_char()=='Z')
#define ISDIGIT (parser_get_char()>='0' && parser_get_char() <='9')
#define WILLBEDIGIT (parser_get_char(1)>='0' && parser_get_char(1) <='9')
#define ISPOINT (parser_get_char()=='.')
#define ISALPHA (parser_get_char()>='_' || (parser_get_char()>='a' && parser_get_char()<='z') || (parser_get_char()>='A' && parser_get_char()<='Z'))
#define ISQUOTES (parser_get_char()=='"' || parser_get_char()=='\'')
#define ISMINUS (parser_get_char()=='-')
#define WILLBEMINUS (parser_get_char(1)=='-')
#define ISPAREN (parser_get_char()=='(')
#define ENDED (parser_get_char()==0)
const char* keywords[] = { "and", "break", "do", "else", "elseif", "end", "false", "for", "function", "goto", "if", "in", "local", "nil", "not", "or", "repeat", "return", "then", "true", "until", "while" };
const bool is_keyword() {
for (int i=0;i<22;++i) if (strcmp(alpha, keywords[i]) == 0) return true;
return false;
}
void parser_do_color(uint8_t type) {
uint8_t color = COLOR_WHITE;
switch (type) {
case PARSER_NUMBER: color = COLOR_LIGHT_GREEN; break;
case PARSER_COMMENT: color = COLOR_LIGHT_GRAY; break;
case PARSER_STRING: color = COLOR_RED; break;
case PARSER_IDENTIFIER: if (is_keyword()) color = COLOR_MAGENTA; break;
case PARSER_FUNCTION: color = COLOR_YELLOW; break;
};
color = (color&0x0f)+(0x10);
for (int i=parser_start;i<parser_pos;++i) poke(0x1200+parser_offset+i, color);
parser_start = parser_pos;
}
void refresh_line_color(uint8_t line) {
parser_setup(80*(line+1));
while (!ENDED) {
uint8_t result = PARSER_NOTHING;
if (ISZERO) {
result=PARSER_NUMBER; parser_next();
if (ISX) {
if (WILLBEDIGIT) {
parser_next();
while (ISDIGIT) parser_next();
}
} else if (ISDIGIT) {
while (ISDIGIT) parser_next();
if (ISPOINT) {
parser_next();
while (ISDIGIT) parser_next();
}
} else if (ISPOINT) {
parser_next();
while (ISDIGIT) parser_next();
}
} else if (ISDIGIT) {
result = PARSER_NUMBER;
while (ISDIGIT) parser_next();
if (ISPOINT) {
parser_next();
while (ISDIGIT) parser_next();
}
} else if (ISALPHA) {
result = PARSER_IDENTIFIER;
while (ISALPHA || ISDIGIT) parser_next(true);
if (ISPAREN) result = PARSER_FUNCTION;
} else if (ISQUOTES) {
result = PARSER_STRING; parser_next();
while (!ISQUOTES && !ENDED) parser_next();
parser_next();
} else if (ISMINUS) {
if (WILLBEMINUS) {
result = PARSER_COMMENT;
while (!ENDED) parser_next();
}
}
if (result == PARSER_NOTHING) parser_next();
parser_do_color(result);
}
}
void refresh_color_highlight() {
cursor_inverted = false;
for (int i=0;i<28;++i) refresh_line_color(i);
}
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;
}
refresh_color_highlight();
}
void init_code_editor() {
mode(3);
code.clear();
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 move_line_end() {
if (cursor_inverted) invert_cursor();
col = get_current_line().size();
blink_wait=1;
}
void move_line_home() {
if (cursor_inverted) invert_cursor();
col = 0;
blink_wait=1;
}
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 supr_char() {
if ((col == (*ls[line]).size()) && (line == code.size()-1)) return;
move_cursor_right();
delete_char();
}
void add_char(uint8_t chr) {
std::string str = get_current_line();
std::string a = str.substr(0,col);
std::string b = str.substr(col);
std::string c = " "; c[0] = chr;
*ls[line] = a+c+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 if (key == KEY_DELETE) supr_char();
else if (key == KEY_END) move_line_end();
else if (key == KEY_HOME) move_line_home();
else {
uint8_t chr = get_char(key);
if (chr != 0) add_char(chr);
}
}
blink_wait--;
if (blink_wait == 0) {
blink_wait = 30;
invert_cursor();
}
}