[FEAT] All screen modes implemented

This commit is contained in:
2021-12-04 10:52:08 +01:00
parent 0482467037
commit 7552136845
4 changed files with 116 additions and 33 deletions

130
ascii.cpp
View File

@@ -7,11 +7,13 @@
#define swap(a, b) {auto tmp=a;a=b;b=tmp;} #define swap(a, b) {auto tmp=a;a=b;b=tmp;}
char window_title[256]; char window_title[256];
uint8_t char_screen[1200]; uint8_t mem[2400];
uint8_t color_screen[1200]; uint8_t *char_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;
uint8_t current_color = 0x1e; uint8_t current_color = 0x1e;
uint8_t current_mode = 1;
uint8_t cursor_x = 0; uint8_t cursor_x = 0;
uint8_t cursor_y = 0; uint8_t cursor_y = 0;
bool sounding = false; bool sounding = false;
@@ -23,7 +25,7 @@ uint32_t audio_len = 0;
SDL_Window *mini_win; SDL_Window *mini_win;
SDL_Renderer *mini_ren; SDL_Renderer *mini_ren;
SDL_Texture *mini_bak; SDL_Texture *mini_bak = NULL;
SDL_AudioDeviceID mini_audio_device; SDL_AudioDeviceID mini_audio_device;
Uint32 *pixels; Uint32 *pixels;
int pitch; int pitch;
@@ -31,11 +33,12 @@ int pitch;
uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA, uint32_t palette[16] = { 0x00000000, 0x000000AA, 0x0000AA00, 0x0000AAAA, 0x00AA0000, 0x00AA00AA, 0x00AA5500, 0x00AAAAAA,
0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF }; 0x00555555, 0x005555FF, 0x0055FF55, 0x0055FFFF, 0x00FF5555, 0x00FF55FF, 0x00FFFF55, 0x00FFFFFF };
#define debug_line_size 40 #define debug_line_size 80
#define debug_num_lines 30 #define debug_num_lines 30
#define debug_total_size 1200 //debug_line_size*debug_num_lines #define debug_total_size 2400 //debug_line_size*debug_num_lines
char debug_text[debug_total_size]; char debug_text[debug_total_size];
int debug_cursor = 0; int debug_cursor = 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 }; //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;
@@ -45,11 +48,42 @@ int mouse_x, mouse_y, mouse_wheel;
Uint32 mouse_buttons; Uint32 mouse_buttons;
void reinit() { void reinit() {
screen_width = 40; if (mini_bak != NULL) SDL_DestroyTexture(mini_bak);
screen_height = 30; switch (current_mode) {
current_color = 0x1e; case 0:
cursor_x = 0; screen_width = 80;
cursor_y = 0; screen_height = 30;
current_color = 0x1e;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[1200];
SDL_RenderSetLogicalSize(mini_ren, 640, 480);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 640, 240);
break;
case 1:
screen_width = 40;
screen_height = 30;
current_color = 0x1e;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[1200];
SDL_RenderSetLogicalSize(mini_ren, 320, 240);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
break;
case 2:
screen_width = 20;
screen_height = 15;
current_color = 0x1e;
cursor_x = 0;
cursor_y = 0;
char_screen = &mem[0];
color_screen = &mem[300];
SDL_RenderSetLogicalSize(mini_ren, 160, 120);
mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 160, 120);
break;
}
} }
void audioCallback(void * userdata, uint8_t * stream, int len) { void audioCallback(void * userdata, uint8_t * stream, int len) {
@@ -73,8 +107,8 @@ int main(int argc,char*argv[]) {
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, 480, SDL_WINDOW_SHOWN);
mini_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC); mini_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC);
//SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren); //SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren);
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); //mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240);
bool exit = false; bool exit = false;
SDL_Event mini_eve; SDL_Event mini_eve;
@@ -96,8 +130,9 @@ int main(int argc,char*argv[]) {
if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) {
if (lua_is_playing()) { if (lua_is_playing()) {
lua_quit(); lua_quit();
reinit(); setmode(0);
} else { } else {
setmode(1);
lua_init(); lua_init();
lua_call_init(); lua_call_init();
} }
@@ -121,27 +156,57 @@ int main(int argc,char*argv[]) {
if (lua_is_playing()) { if (lua_is_playing()) {
lua_call_update(); lua_call_update();
} else { } else {
debug_cursor_blink--;
if (debug_cursor_blink == 0) {
debug_cursor_blink = 30;
debug_text[debug_cursor] = debug_text[debug_cursor]==32 ? 95 : 32;
}
loop(); loop();
} }
SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch); SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch);
for (int y=0; y<screen_height; ++y) { switch (current_mode) {
for (int x=0; x<screen_width; ++x) { case 0:
const uint8_t chr_color = COLSCR(x,y); for (int y=0; y<screen_height; ++y) {
const uint32_t ink_color = palette[chr_color & 0x0f]; for (int x=0; x<screen_width; ++x) {
const uint32_t paper_color = palette[chr_color >> 4]; const uint8_t chr_color = current_color; //COLSCR(x,y);
const uint8_t chr = CHRSCR(x,y); const uint32_t ink_color = palette[chr_color & 0x0f];
for (int l=0; l<8; ++l) { const uint32_t paper_color = palette[chr_color >> 4];
const uint8_t line = font[chr*8+l]; const uint8_t chr = CHRSCR(x,y);
for (int b=0; b<8; ++b) { for (int l=0; l<8; ++l) {
if ((line >> (7-b)) & 0x01) { const uint8_t line = font[chr*8+l];
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color; for (int b=0; b<8; ++b) {
} else { if ((line >> (7-b)) & 0x01) {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color; pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color;
} else {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color;
}
}
} }
} }
} }
} break;
case 1:
case 2:
for (int y=0; y<screen_height; ++y) {
for (int x=0; x<screen_width; ++x) {
const uint8_t chr_color = COLSCR(x,y);
const uint32_t ink_color = palette[chr_color & 0x0f];
const uint32_t paper_color = palette[chr_color >> 4];
const uint8_t chr = CHRSCR(x,y);
for (int l=0; l<8; ++l) {
const uint8_t line = font[chr*8+l];
for (int b=0; b<8; ++b) {
if ((line >> (7-b)) & 0x01) {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = ink_color;
} else {
pixels[b+(x*8)+((y*8)+l)*(screen_width*8)] = paper_color;
}
}
}
}
}
break;
} }
//for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]]; //for (int i=0;i<screen_surface->size;++i) pixels[i] = palette[screen_surface->p[i]];
@@ -157,7 +222,7 @@ int main(int argc,char*argv[]) {
void cls(uint8_t value) { void cls(uint8_t value) {
SDL_memset(char_screen, value, screen_width*screen_height); SDL_memset(char_screen, value, screen_width*screen_height);
SDL_memset(color_screen, current_color, screen_width*screen_height); if (current_mode != 0) SDL_memset(color_screen, current_color, screen_width*screen_height);
} }
void ink(uint8_t value) { void ink(uint8_t value) {
@@ -184,7 +249,7 @@ void print(const char *str, int x, int y) {
if ((cursor_x+len) > screen_width) len -= ((cursor_x+len) - screen_width); if ((cursor_x+len) > screen_width) len -= ((cursor_x+len) - screen_width);
for (int i=0; i < len; ++i) { for (int i=0; i < len; ++i) {
CHRSCR(cursor_x+i, cursor_y) = str[i]; CHRSCR(cursor_x+i, cursor_y) = str[i];
COLSCR(cursor_x+i, cursor_y) = current_color; if (current_mode != 0) COLSCR(cursor_x+i, cursor_y) = current_color;
} }
} }
@@ -298,7 +363,7 @@ void pdebug() {
for (int y=0; y<debug_num_lines;++y) { for (int y=0; y<debug_num_lines;++y) {
for (int x=0; x<debug_line_size;++x) { for (int x=0; x<debug_line_size;++x) {
CHRSCR(x, y) = debug_text[i++]; CHRSCR(x, y) = debug_text[i++];
COLSCR(x, y) = 0x1e; //COLSCR(x, y) = 0x1e;
} }
} }
} }
@@ -339,3 +404,8 @@ void nosound() {
audio_len = 0; audio_len = 0;
sounding = false; sounding = false;
} }
void setmode(const uint8_t mode) {
current_mode = mode;
reinit();
}

View File

@@ -163,3 +163,4 @@ void poke(uint16_t addr, uint8_t val);
void sound(float freq, uint32_t len); void sound(float freq, uint32_t len);
void nosound(); void nosound();
void setmode(const uint8_t mode);

View File

@@ -6,15 +6,16 @@ function init()
end end
function update() function update()
setmode(2)
paper(COLOR_BLACK) paper(COLOR_BLACK)
cls() cls()
x = x + dx x = x + dx
y = y + dy y = y + dy
if x == 39 or x == 0 then if x == 19 or x == 0 then
sound(440, 50) sound(440, 50)
dx = -dx dx = -dx
end end
if y == 29 or y == 0 then if y == 14 or y == 0 then
sound(880, 50) sound(880, 50)
dy = -dy dy = -dy
end end

13
lua.cpp
View File

@@ -167,7 +167,7 @@ extern "C" {
lua_pushinteger(L, strlen(str)); lua_pushinteger(L, strlen(str));
return 1; return 1;
} }
static int cpp_setchar(lua_State *L) { static int cpp_setchar(lua_State *L) {
int index = luaL_checkinteger(L, 1); int index = luaL_checkinteger(L, 1);
int b0 = luaL_checkinteger(L, 2); int b0 = luaL_checkinteger(L, 2);
@@ -206,6 +206,12 @@ extern "C" {
nosound(); nosound();
return 0; return 0;
} }
static int cpp_setmode(lua_State *L) {
int val = luaL_checkinteger(L, 1);
setmode(val);
return 0;
}
} }
lua_State *L; lua_State *L;
@@ -224,6 +230,7 @@ void lua_init() {
debug("error loading game"); debug("error loading game");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
return; return;
} }
@@ -265,6 +272,7 @@ void lua_init() {
lua_pushcfunction(L,cpp_poke); lua_setglobal(L, "poke"); lua_pushcfunction(L,cpp_poke); lua_setglobal(L, "poke");
lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound"); lua_pushcfunction(L,cpp_sound); lua_setglobal(L, "sound");
lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound"); lua_pushcfunction(L,cpp_nosound); lua_setglobal(L, "nosound");
lua_pushcfunction(L,cpp_setmode); lua_setglobal(L, "setmode");
lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN"); lua_pushinteger(L, 0); lua_setglobal(L, "KEY_UNKNOWN");
lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A"); lua_pushinteger(L, 4); lua_setglobal(L, "KEY_A");
@@ -395,6 +403,7 @@ void lua_init() {
debug("runtime error"); debug("runtime error");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
return; return;
} }
@@ -416,6 +425,7 @@ void lua_call_init() {
debug("runtime error"); debug("runtime error");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
is_playing = false; is_playing = false;
} }
} }
@@ -427,6 +437,7 @@ void lua_call_update() {
debug("runtime error"); debug("runtime error");
debug(lua_tostring(L, -1)); debug(lua_tostring(L, -1));
lua_pop(L,1); lua_pop(L,1);
setmode(0);
is_playing = false; is_playing = false;
} }
} }