From efce4d6d29c0c70d598d6a92b3b34b2efc5a8568 Mon Sep 17 00:00:00 2001 From: Franck STAUFFER Date: Mon, 3 Aug 2020 17:34:41 +0200 Subject: [PATCH] Use SDL2 --- src/main.c | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main.c b/src/main.c index 5cee222..57c5f64 100644 --- a/src/main.c +++ b/src/main.c @@ -1,4 +1,4 @@ -#include "SDL/SDL.h" +#include "SDL2/SDL.h" #include #include #include @@ -37,11 +37,12 @@ byte st = 0; word stack[16]; byte v[16]; -SDL_Surface* s; SDL_Event e; +SDL_Surface* s; +SDL_Window* w; -Uint8 fg; -Uint8 bg; +Uint32 fg; +Uint32 bg; void init_chip8(void) @@ -81,6 +82,7 @@ void cleanup(void) { SDL_FreeSurface(s); + SDL_DestroyWindow(w); SDL_Quit(); } @@ -92,7 +94,15 @@ init_SDL(const char* path) return 0; } - s = SDL_SetVideoMode(64 * SCALE, 32 * SCALE, 8, SDL_HWSURFACE); + char caption[7 + strlen(path)]; + sprintf(caption, "CHIP-8 - %s", path); + w = SDL_CreateWindow(caption, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 64 * SCALE, 32 * SCALE, SDL_WINDOW_SHOWN); + if (!w) { + fputs("ERROR: Failed to create a window\n", stderr); + return 0; + } + + s = SDL_GetWindowSurface(w); if (!s) { fputs("ERROR: Failed to create SDL surface\n", stderr); return 0; @@ -105,10 +115,6 @@ init_SDL(const char* path) return 0; } - char caption[7 + strlen(path)]; - sprintf(caption, "CHIP-8 - %s", path); - SDL_WM_SetCaption(caption, NULL); - return 1; } @@ -315,7 +321,7 @@ render(void) SDL_FillRect(s, &r, (gfx[x][y]) ? fg : bg); } - SDL_Flip(s); + SDL_UpdateWindowSurface(w); } static inline uint_fast8_t @@ -323,7 +329,7 @@ op_handler(void) { switch (op) { case 0x00E0: - memset(gfx, 0, 32 * 63 * sizeof(byte)); + memset(gfx, 0, 32 * 64 * sizeof(byte)); SDL_Rect r; r.h = 32 * SCALE; @@ -332,6 +338,8 @@ op_handler(void) r.y = 0; SDL_FillRect(s, &r, bg); + SDL_UpdateWindowSurface(w); + pc += 2; return 1; case 0x00EE: