New console system WIP

This commit is contained in:
2021-12-14 12:42:09 +01:00
parent 3979588f85
commit 63ca77e3e6
4 changed files with 92 additions and 45 deletions

127
ascii.cpp
View File

@@ -5,6 +5,9 @@
#include "rom.c"
#include "play.h"
#include <vector>
#include <string>
#define swap(a, b) {auto tmp=a;a=b;b=tmp;}
#define AUDIO_NONE 0
@@ -43,13 +46,14 @@ int pitch;
uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA,
0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF };
#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 = 0;
//[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 };
@@ -145,11 +149,13 @@ void romcpy() {
SDL_memcpy(&mem[2560], rom, rom_size);
}
void debug_set_prompt();
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;
//[TODEL]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+80, 480+80, SDL_WINDOW_SHOWN);
@@ -187,13 +193,16 @@ int main(int argc,char*argv[]) {
if (lua_is_playing()) {
lua_pause();
//lua_quit();
old_mode = current_mode;
setmode(0);
debug_set_prompt();
//old_mode = current_mode;
//setmode(0);
} else {
setmode(old_mode);
lua_resume();
//lua_init();
//lua_call_init();
//setmode(old_mode);
//lua_resume();
lua_quit();
reinit();
lua_init(lua_filename);
lua_call_init();
}
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
lua_quit();
@@ -219,7 +228,8 @@ int main(int argc,char*argv[]) {
debug_cursor_blink--;
if (debug_cursor_blink == 0) {
debug_cursor_blink = 30;
debug_text[debug_cursor] = debug_text[debug_cursor]==32 ? 95 : 32;
const int pos = cursor_x+cursor_y*screen_width;
char_screen[pos] = char_screen[pos]==32 ? 95 : 32;
}
loop();
}
@@ -288,13 +298,11 @@ int main(int argc,char*argv[]) {
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);
if (!lua_is_playing()) {
SDL_memset(debug_text, 32, debug_total_size);
debug_cursor = 1;
debug_prompt = 0;
debug_text[debug_prompt] = '>';
}
cursor_x = cursor_y = 0;
if (!lua_is_playing()) {
char_screen[0] = '>';
cursor_x=1;
}
}
void ink(uint8_t value) {
@@ -436,41 +444,54 @@ const char* tostr(float val) {
return SDL_itoa(val, str_tmp, 10);
}
void debug_set_prompt() {
debug_prompt = debug_cursor++;
debug_text[debug_prompt] = '>';
void debug_one_line_up() {
int total = screen_width*screen_height-screen_width;
for (int i=0; i<total;++i) char_screen[i] = char_screen[i+screen_width];
for (int i=total; i<screen_width*screen_height;++i) char_screen[i] = 32;
cursor_y = screen_height-1;
//debug_set_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 debug_set_prompt() {
if (debug_prompt == cursor_x+cursor_y*screen_width) return;
if (cursor_x>0) { cursor_x=0;cursor_y++;if(cursor_y>=screen_height) debug_one_line_up(); }
char_screen[cursor_x+cursor_y*screen_width] = '>';
cursor_x++;
debug_prompt = cursor_x+cursor_y*screen_width;
}
void debugchr(const uint8_t chr) {
int pos = cursor_x+cursor_y*screen_width;
if (chr == 8) {
if (debug_cursor>(debug_prompt+1)) {
debug_text[debug_cursor--] = 32;
debug_text[debug_cursor] = 32;
if (cursor_x>1) {
char_screen[pos++] = 32;
char_screen[pos] = 32;
cursor_x--;
}
} else {
debug_text[debug_cursor++] = chr;
if (debug_cursor >= debug_total_size) debug_one_line_up();
char_screen[pos] = chr;
cursor_x++;
if (cursor_x >= screen_width) {
cursor_x=0; cursor_y++;
if (cursor_y >= screen_height) debug_one_line_up();
}
}
}
void debug(const char *str) {
const int len = SDL_strlen(str);
int cursor = cursor_x+cursor_y*screen_width;
for (int i=0; i<len;++i) {
debug_text[debug_cursor++] = str[i];
if (debug_cursor >= debug_total_size) debug_one_line_up();
char_screen[cursor++] = str[i];
if (cursor >= screen_width*screen_height) debug_one_line_up();
}
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();
cursor_x = 0;
cursor_y = (cursor/screen_width)+1;
if (cursor_y >= screen_height) debug_one_line_up();
//debug_set_prompt();
}
/*[TODEL]
void pdebug() {
int i=0;
for (int y=0; y<debug_num_lines;++y) {
@@ -480,10 +501,34 @@ void pdebug() {
}
}
}
*/
int cmd_index = 0;
void debug_get_cmd() {
debug_text[debug_cursor] = 0;
lua_call_cmd(&debug_text[debug_prompt+1]);
char_screen[cursor_x+cursor_y*screen_width] = 0;
//char *tmp = (char*)&char_screen[1+cursor_y*screen_width];
char *tmp = (char*)&char_screen[debug_prompt];
cmd_list.push_back(tmp);
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();
}
void next_cmd() {
if (cmd_index < cmd_list.size()) cmd_index++;
const char* cmd = cmd_list[cmd_list.size()-cmd_index].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;
cursor_x = pos%screen_width;
cursor_y = pos/screen_width;
}
void prev_cmd() {
}
uint8_t ascii(const char *str, uint8_t index) {