From 755213684516ea4511fb15a093490c11e53658fe Mon Sep 17 00:00:00 2001 From: JailDoctor Date: Sat, 4 Dec 2021 10:52:08 +0100 Subject: [PATCH] [FEAT] All screen modes implemented --- ascii.cpp | 130 +++++++++++++++++++++++++++++++++++++++++------------- ascii.h | 1 + game.lua | 5 ++- lua.cpp | 13 +++++- 4 files changed, 116 insertions(+), 33 deletions(-) diff --git a/ascii.cpp b/ascii.cpp index e8ab10d..6aa7faf 100644 --- a/ascii.cpp +++ b/ascii.cpp @@ -7,11 +7,13 @@ #define swap(a, b) {auto tmp=a;a=b;b=tmp;} char window_title[256]; -uint8_t char_screen[1200]; -uint8_t color_screen[1200]; +uint8_t mem[2400]; +uint8_t *char_screen = NULL; +uint8_t *color_screen = NULL; uint8_t screen_width = 40; uint8_t screen_height = 30; uint8_t current_color = 0x1e; +uint8_t current_mode = 1; uint8_t cursor_x = 0; uint8_t cursor_y = 0; bool sounding = false; @@ -23,7 +25,7 @@ uint32_t audio_len = 0; SDL_Window *mini_win; SDL_Renderer *mini_ren; -SDL_Texture *mini_bak; +SDL_Texture *mini_bak = NULL; SDL_AudioDeviceID mini_audio_device; Uint32 *pixels; int pitch; @@ -31,11 +33,12 @@ 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 40 +#define debug_line_size 80 #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]; 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 }; const Uint8 *keys; @@ -45,11 +48,42 @@ int mouse_x, mouse_y, mouse_wheel; Uint32 mouse_buttons; void reinit() { - screen_width = 40; - screen_height = 30; - current_color = 0x1e; - cursor_x = 0; - cursor_y = 0; + if (mini_bak != NULL) SDL_DestroyTexture(mini_bak); + switch (current_mode) { + case 0: + screen_width = 80; + 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) { @@ -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_ren = SDL_CreateRenderer(mini_win, -1, SDL_RENDERER_PRESENTVSYNC); //SDL_CreateWindowAndRenderer(512,512,0,&mini_win,&mini_ren); - SDL_RenderSetLogicalSize(mini_ren, 320, 240); - mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240); + //SDL_RenderSetLogicalSize(mini_ren, 320, 240); + //mini_bak = SDL_CreateTexture(mini_ren, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, 320, 240); bool exit = false; SDL_Event mini_eve; @@ -96,8 +130,9 @@ int main(int argc,char*argv[]) { if (mini_eve.key.keysym.scancode == SDL_SCANCODE_ESCAPE) { if (lua_is_playing()) { lua_quit(); - reinit(); + setmode(0); } else { + setmode(1); lua_init(); lua_call_init(); } @@ -121,27 +156,57 @@ int main(int argc,char*argv[]) { if (lua_is_playing()) { lua_call_update(); } 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(); } SDL_LockTexture(mini_bak, NULL, (void**)&pixels, &pitch); - for (int y=0; y> 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; + switch (current_mode) { + case 0: + for (int y=0; y> 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; + case 1: + case 2: + for (int y=0; y> 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;isize;++i) pixels[i] = palette[screen_surface->p[i]]; @@ -157,7 +222,7 @@ int main(int argc,char*argv[]) { void cls(uint8_t value) { 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) { @@ -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); for (int i=0; i < len; ++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