- new debug system

This commit is contained in:
2022-10-28 13:49:08 +02:00
parent a029771b07
commit 1a732d7c71
4 changed files with 96 additions and 168 deletions

View File

@@ -914,14 +914,14 @@ void lua_init(char* filenames) {
int size; int size;
char* buffer = file_getfilebuffer(file_start, size); char* buffer = file_getfilebuffer(file_start, size);
if (luaL_loadbuffer(L, buffer, size, file_start)) { if (luaL_loadbuffer(L, buffer, size, file_start)) {
debug("error loading game"); debug("LOADING ERROR:");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
return; return;
} }
free(buffer); free(buffer);
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));
lua_pop(L,1); lua_pop(L,1);
return; return;
@@ -945,7 +945,7 @@ void lua_call_init() {
if (!init_exists) return; if (!init_exists) return;
lua_getglobal(L, "_init"); lua_getglobal(L, "_init");
if (lua_pcall(L, 0, 0, 0)) { if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error"); debug("RUNTIME ERROR:");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
is_playing = false; is_playing = false;
@@ -956,7 +956,7 @@ void lua_call_update() {
if (!update_exists) return; if (!update_exists) return;
lua_getglobal(L, "_update"); lua_getglobal(L, "_update");
if (lua_pcall(L, 0, 0, 0)) { if (lua_pcall(L, 0, 0, 0)) {
debug("runtime error"); debug("RUNTIME ERROR:");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
is_playing = false; is_playing = false;

View File

@@ -1,36 +1,9 @@
#include "mini.h" #include "mini.h"
int current_editor = 0;
void do_terminal() {
cls(0);
pdebug();
}
void do_sprite_editor() {
cls(14);
//palt(0, false);
rectfill(0, 0, 160, 7, 2);
rectfill(0, 115, 160, 119, 2);
sspr(0, 0, 128, 32, 0, 83);
sspr(0, 0, 8, 8, 8, 16, 64, 64);
rect(8, 16, 72, 80, 0);
spr(17, mousex(), mousey());
if (mbtn(1) && mousex()>=8 && mousey()>=8 && mousex()<72 && mousey()<72) {
sset((mousex()-8)/8, (mousey()-16)/8);
}
}
void loop() { void loop() {
if (btnp(KEY_TAB)) { settrans(255);
current_editor = (++current_editor)%5; setcolor(0,0xff0000);
} const int w=scrw();
switch(current_editor) { const int h=scrh();
case 0: rect(0,0,w-1,h-1,0);
do_terminal();
break;
case 1:
do_sprite_editor();
break;
}
} }

215
mini.cpp
View File

@@ -43,14 +43,13 @@ namespace ds {
int cam[2] = {0, 0}; int cam[2] = {0, 0};
int clip[4] = {0, 0, screen_width, screen_height}; int clip[4] = {0, 0, screen_width, screen_height};
int clp[4] = {0, 0, screen_width-1, screen_height-1}; int clp[4] = {0, 0, screen_width-1, screen_height-1};
//uint8_t draw_palette[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
//uint8_t screen_palette[16] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
uint8_t trans = 0; uint8_t trans = 0;
uint16_t fill_pattern = 0b1111111111111111; uint16_t fill_pattern = 0b1111111111111111;
bool fill_trans = false; bool fill_trans = false;
} }
bool should_exit = false; bool should_exit = false;
bool should_quit = false;
SDL_Window *mini_win; SDL_Window *mini_win;
SDL_Renderer *mini_ren; SDL_Renderer *mini_ren;
SDL_Texture *mini_bak; SDL_Texture *mini_bak;
@@ -70,12 +69,6 @@ uint16_t font[96];
char base64glyphs[193] = "/h/AqV/hhhh/GMYYMGz/t/eS33H477wsjjswY4IOPHEFFVVVAVAVAVIc+dJHMMM/h/qBgBgVqq+//eIUi/dJzhAAhzAAA/eM" char base64glyphs[193] = "/h/AqV/hhhh/GMYYMGz/t/eS33H477wsjjswY4IOPHEFFVVVAVAVAVIc+dJHMMM/h/qBgBgVqq+//eIUi/dJzhAAhzAAA/eM"
"AMShAAAQsjAAAwsjAAAeSzAAAcU3AAAEqRAAABVaiAAMezhAAAAAMAADH4wAASb2SAAMAttAQYcefACGOe+AAAVVAAAAbbAA"; "AMShAAAQsjAAAwsjAAAeSzAAAcU3AAAEqRAAABVaiAAMezhAAAAAMAADH4wAASb2SAAMAttAQYcefACGOe+AAAVVAAAAbbAA";
int debug_line_size = (screen_width/4);
int debug_num_lines = (screen_height/6);
int debug_total_size = debug_line_size*debug_num_lines;
char *debug_text = NULL;
int debug_cursor = 0;
//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;
Uint8 key_just_pressed = 0; Uint8 key_just_pressed = 0;
@@ -118,8 +111,6 @@ void reinit() {
ds::cam[0] = ds::cam[1] = 0; ds::cam[0] = ds::cam[1] = 0;
ds::clip[0] = ds::clip[1] = 0; ds::clip[2] = screen_width; ds::clip[3] = screen_height; ds::clip[0] = ds::clip[1] = 0; ds::clip[2] = screen_width; ds::clip[3] = screen_height;
ds::clp[0] = ds::clp[1] = 0; ds::clp[2] = screen_width-1; ds::clp[3] = screen_height-1; ds::clp[0] = ds::clp[1] = 0; ds::clp[2] = screen_width-1; ds::clp[3] = screen_height-1;
//for(int i=0;i<16;++i)ds::draw_palette[i]=i;
//for(int i=0;i<16;++i)ds::screen_palette[i]=i;
ds::trans=0; ds::trans=0;
ds::fill_pattern = 0b1111111111111111; ds::fill_pattern = 0b1111111111111111;
ds::fill_trans = false; ds::fill_trans = false;
@@ -142,6 +133,14 @@ void quitaudio() {
JA_Quit(); JA_Quit();
} }
int scrw() {
return screen_width;
}
int scrh() {
return screen_height;
}
uint8_t newsurf(int w, int h) { uint8_t newsurf(int w, int h) {
int i = 0; int i = 0;
while (i<10 && surfaces[i].p != NULL) ++i; while (i<10 && surfaces[i].p != NULL) ++i;
@@ -163,21 +162,7 @@ uint8_t loadsurf(const char* filename) {
surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h); surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h);
surfaces[i].size = surfaces[i].w*surfaces[i].h; surfaces[i].size = surfaces[i].w*surfaces[i].h;
free(buffer); free(buffer);
/*
FILE *f = fopen(filename, "rb");
if (f) {
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
uint8_t *buffer = (uint8_t*)malloc(size);
fread(buffer, size, 1, f);
fclose(f);
surfaces[i].p = LoadGif(buffer, &surfaces[i].w, &surfaces[i].h);
surfaces[i].size = surfaces[i].w*surfaces[i].h;
free(buffer);
}
*/
return i; return i;
} }
@@ -199,100 +184,93 @@ void setmap(uint8_t surface) {
} }
int main(int argc,char*argv[]){ int main(int argc,char*argv[]){
int bi = 0;
for (int ci=0; ci<96; ci+=2) { while (!should_quit) {
font[ci] = base64font[bi]-48+((base64font[bi+1]-48)<<6)+((base64font[bi+2]-48&7)<<12); should_exit=false;
font[ci+1] = ((base64font[bi+2]-48)>>3)+((base64font[bi+3]-48)<<3)+((base64font[bi+4]-48)<<9);
bi += 5;
}
/*FILE *f = fopen("sprites.bmp", "rb"); // initfont()
if (f) { int bi = 0;
uint8_t buffer[SPRITES_SIZE_BYTES/2]; for (int ci=0; ci<96; ci+=2) {
fseek(f, 118, SEEK_SET); font[ci] = base64font[bi]-48+((base64font[bi+1]-48)<<6)+((base64font[bi+2]-48&7)<<12);
fread(buffer, SPRITES_SIZE_BYTES/2, 1, f); font[ci+1] = ((base64font[bi+2]-48)>>3)+((base64font[bi+3]-48)<<3)+((base64font[bi+4]-48)<<9);
fclose(f); bi += 5;
for (int y=0; y<SPRITES_HEIGHT; ++y) {
for (int x=0; x<(SPRITES_WIDTH/2); ++x) {
SPRITES(x*2, SPRITES_WIDTH-1-y) = buffer[x+y*(SPRITES_WIDTH/2)] >> 4;
SPRITES(x*2+1, SPRITES_WIDTH-1-y) = buffer[x+y*(SPRITES_WIDTH/2)] & 0xf;
}
} }
}*/
read_ini(); read_ini();
setdest(newsurf(screen_width, screen_height)); setdest(newsurf(screen_width, screen_height));
debug_line_size = (screen_width/4); SDL_Init(SDL_INIT_EVERYTHING);
debug_num_lines = (screen_height/6); mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width*screen_zoom, screen_height*screen_zoom, SDL_WINDOW_SHOWN);
debug_total_size = debug_line_size*debug_num_lines; mini_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC);
if (debug_text != NULL) free(debug_text); //SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
debug_text = (char*)malloc(debug_total_size); SDL_RenderSetLogicalSize(mini_ren, screen_width, screen_height);
for (int i=0; i<debug_total_size;++i) debug_text[i] = 32; SDL_ShowCursor(show_cursor);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height);
SDL_Init(49); SDL_Event mini_eve;
mini_win = SDL_CreateWindow(window_title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width*screen_zoom, screen_height*screen_zoom, 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, screen_width, screen_height);
SDL_ShowCursor(show_cursor);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, screen_width, screen_height);
SDL_Event mini_eve; reinit();
initaudio();
debug("MINI SYSTEM BOOTING...");
lua_init(lua_files);
lua_call_init();
reinit(); while(!should_exit) {
initaudio(); key_just_pressed = 0;
debug("MINI SYSTEM BOOTING..."); mouse_wheel = 0;
lua_init(lua_files); while(SDL_PollEvent(&mini_eve)) {
lua_call_init(); if (mini_eve.type == SDL_QUIT) { should_exit=true; should_quit=true; break; }
if (mini_eve.type == SDL_KEYDOWN) {
while(!should_exit) { if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
key_just_pressed = 0; if (lua_is_playing()) {
mouse_wheel = 0; lua_quit();
while(SDL_PollEvent(&mini_eve)) { quitaudio();
if (mini_eve.type == SDL_QUIT) { should_exit=true; break; } reinit();
if (mini_eve.type == SDL_KEYDOWN) { } else {
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { should_exit=true;
if (lua_is_playing()) { //initaudio();
lua_quit(); //lua_init(lua_files);
quitaudio(); //lua_call_init();
reinit(); }
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) {
should_exit=true;
//lua_quit();
//reinit();
//initaudio();
//lua_init(lua_files);
//lua_call_init();
} else { } else {
initaudio(); key_just_pressed = mini_eve.key.keysym.scancode;
lua_init(lua_files);
lua_call_init();
} }
} else if (mini_eve.key.keysym.scancode == SDL_SCANCODE_F5) { }
lua_quit(); if (mini_eve.type == SDL_MOUSEWHEEL) {
reinit(); mouse_wheel = mini_eve.wheel.y;
initaudio();
lua_init(lua_files);
lua_call_init();
} else {
key_just_pressed = mini_eve.key.keysym.scancode;
} }
} }
if (mini_eve.type == SDL_MOUSEWHEEL) { keys = SDL_GetKeyboardState(NULL);
mouse_wheel = mini_eve.wheel.y; mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y);
mouse_x /= screen_zoom; mouse_y /= screen_zoom;
if (lua_is_playing()) {
lua_call_update();
} else {
loop();
} }
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch);
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_RenderPresent(mini_ren);
} }
keys = SDL_GetKeyboardState(NULL); lua_quit();
mouse_buttons = SDL_GetMouseState(&mouse_x, &mouse_y); quitaudio();
mouse_x /= screen_zoom; mouse_y /= screen_zoom; for (int i=0;i<10;++i) freesurf(i);
SDL_DestroyTexture(mini_bak);
if (lua_is_playing()) { SDL_DestroyRenderer(mini_ren);
lua_call_update(); SDL_DestroyWindow(mini_win);
} else { SDL_Quit();
loop();
}
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch);
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_RenderPresent(mini_ren);
} }
lua_quit();
SDL_Quit();
return 0; return 0;
} }
@@ -926,39 +904,14 @@ const char* tostr(int val) {
return SDL_itoa(val, tostr_tmp, 10); return SDL_itoa(val, tostr_tmp, 10);
} }
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;
}
void debug(const char *str) { void debug(const char *str) {
const int len = SDL_strlen(str); printf("%s\n",str);
for (int i=0; i<len;++i) {
debug_text[debug_cursor++] = str[i];
if (debug_cursor >= debug_total_size) 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();
}
void pdebug() {
palette[0]=0x00000000;
palette[1]=0x0000ff00;
color(1);
int i=0;
for (int y=0; y<debug_num_lines;++y) {
for (int x=0; x<debug_line_size;++x) {
print_char(debug_text[i++], x*4, y*6);
}
}
} }
uint8_t ascii(const char *str, uint8_t index) { uint8_t ascii(const char *str, uint8_t index) {
return str[index]; return str[index];
} }
char fstr[255]; char fstr[255];
void fopen(const char *filename, uint8_t mode) { void fopen(const char *filename, uint8_t mode) {

4
mini.h
View File

@@ -112,6 +112,9 @@
void loop(); void loop();
int scrw();
int scrh();
uint8_t newsurf(int w, int h); uint8_t newsurf(int w, int h);
uint8_t loadsurf(const char* filename); uint8_t loadsurf(const char* filename);
void freesurf(uint8_t surface); void freesurf(uint8_t surface);
@@ -230,7 +233,6 @@ int rnd(int x);
const char* tostr(int val); const char* tostr(int val);
void debug(const char *str); void debug(const char *str);
void pdebug();
uint8_t ascii(const char *str, uint8_t index); uint8_t ascii(const char *str, uint8_t index);