diff options
author | KikooDX <kikoodx@paranoici.org> | 2022-02-28 16:26:59 +0100 |
---|---|---|
committer | KikooDX <kikoodx@paranoici.org> | 2022-02-28 16:26:59 +0100 |
commit | d6a1983d077b5ca0334e9bda72a508526a444831 (patch) | |
tree | 092ec69c3e7a87220d2f818b4197de6803ca7083 | |
parent | 51c3eff62227bdbf91d91a7f3cca79bc64300e4b (diff) | |
download | lzy-d6a1983d077b5ca0334e9bda72a508526a444831.tar.gz |
minimal sdl implementation
-rw-r--r-- | inc/lzy.h | 132 | ||||
-rw-r--r-- | src/main.c | 14 |
2 files changed, 126 insertions, 20 deletions
@@ -5,8 +5,10 @@ extern "C" { #endif #include <stdint.h> -int LZY_Init(void); -int LZY_Deinit(void); +#define LZY_UNUSED(x) (void)(x) + +int LZY_Init(const char *title); +void LZY_Quit(void); int LZY_DrawBegin(void); int LZY_DrawEnd(void); void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b); @@ -14,17 +16,27 @@ void LZY_DrawSetColorNone(void); int LZY_DrawClear(void); void LZY_CycleEvents(void); int LZY_KeyDown(unsigned int scancode); +int LZY_ShouldQuit(void); +void LZY_Log(const char *msg); const char *LZY_GetError(void); #ifdef FXCG50 #include <gint/keycodes.h> enum LZY_ScanCode { - LZY_SC_ESCAPE = KEY_EXIT, + LZY_SC_LEFT = KEY_LEFT, + LZY_SC_RIGHT = KEY_RIGHT, + LZY_SC_UP = KEY_UP, + LZY_SC_DOWN = KEY_DOWN, + LZY_SC_QUIT = KEY_EXIT, }; #else /* end FXCG50, begin SDL2 */ #include <SDL.h> enum LZY_ScanCode { - LZY_SC_ESCAPE = SDLK_ESCAPE, + LZY_SC_LEFT = SDL_SCANCODE_LEFT, + LZY_SC_RIGHT = SDL_SCANCODE_RIGHT, + LZY_SC_UP = SDL_SCANCODE_UP, + LZY_SC_DOWN = SDL_SCANCODE_DOWN, + LZY_SC_QUIT = SDL_SCANCODE_ESCAPE, }; #endif /* SDL2 */ #ifdef __cplusplus @@ -37,20 +49,27 @@ enum LZY_ScanCode { #ifndef LZY_TILE_SIZE #define LZY_TILE_SIZE 16 #endif +#ifndef LZY_DISPLAY_WIDTH +#define LZY_DISPLAY_WIDTH 400 +#endif +#ifndef LZY_DISPLAY_HEIGHT +#define LZY_DISPLAY_HEIGHT 224 +#endif + #ifdef FXCG50 #include <gint/display.h> #include <gint/keyboard.h> #include <stdint.h> -static color_t draw_color; +static color_t draw_color = C_BLACK; +static int should_quit = 0; -int LZY_Init(void) { +int LZY_Init(const char *title) { + LZY_UNUSED(title); return 0; } -int LZY_Deinit(void) { - return 0; -} +void LZY_Quit(void) {} int LZY_DrawBegin(void) { return 0; @@ -76,12 +95,21 @@ int LZY_DrawClear(void) { void LZY_CycleEvents(void) { clearevents(); + should_quit = should_quit || keydown(LZY_SC_QUIT); } int LZY_KeyDown(unsigned int scancode) { return keydown(scancode); } +int LZY_ShouldQuit(void) { + return should_quit; +} + +void LZY_Log(const char *msg) { + LZY_UNUSED(msg); +} + const char *LZY_GetError(void) { return NULL; } @@ -90,12 +118,50 @@ const char *LZY_GetError(void) { #include <SDL.h> #include <stdint.h> -int LZY_Init(void) { +static const char *error = NULL; +static SDL_Window *window = NULL; +static SDL_Renderer *renderer = NULL; +static int should_quit = 0; + +int LZY_Init(const char *title) { + if (SDL_Init(SDL_INIT_VIDEO) < 0) { + error = SDL_GetError(); + return 1; + } + + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 0); + + window = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, LZY_DISPLAY_WIDTH, + LZY_DISPLAY_HEIGHT, SDL_WINDOW_SHOWN); + if (window == NULL) { + error = SDL_GetError(); + return 2; + } + + SDL_SetWindowMinimumSize(window, LZY_DISPLAY_WIDTH, LZY_DISPLAY_HEIGHT); + + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + if (renderer == NULL) { + error = SDL_GetError(); + return 3; + } + return 0; } -int LZY_Deinit(void) { - return 0; +void LZY_Quit(void) { + if (renderer != NULL) { + SDL_DestroyRenderer(renderer); + renderer = NULL; + } + + if (window != NULL) { + SDL_DestroyWindow(window); + window = NULL; + } + + SDL_Quit(); } int LZY_DrawBegin(void) { @@ -103,25 +169,59 @@ int LZY_DrawBegin(void) { } int LZY_DrawEnd(void) { + SDL_RenderPresent(renderer); return 0; } -void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) {} +void LZY_DrawSetColor(uint8_t r, uint8_t g, uint8_t b) { + SDL_SetRenderDrawColor(renderer, r, g, b, 0xff); +} -void LZY_DrawSetColorNone(void) {} +void LZY_DrawSetColorNone(void) { + SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00); +} int LZY_DrawClear(void) { + if (SDL_RenderClear(renderer) < 0) { + error = SDL_GetError(); + return 1; + } return 0; } -void LZY_CycleEvents(void) {} +void LZY_CycleEvents(void) { + SDL_Event e; + + while (SDL_PollEvent(&e) != 0) { + switch (e.type) { + case SDL_QUIT: + should_quit = 1; + break; + case SDL_KEYDOWN: + break; + case SDL_KEYUP: + break; + default: + break; + } + } +} int LZY_KeyDown(unsigned int scancode) { + LZY_Log("LZY_KeyDown not implemented"); return 0; } +int LZY_ShouldQuit(void) { + return should_quit; +} + +void LZY_Log(const char *msg) { + SDL_Log(msg); +} + const char *LZY_GetError(void) { - return NULL; + return error; } #endif /* SDL2 */ #endif /* LZY_IMPLEMENTATION */ @@ -2,10 +2,14 @@ #include "lzy.h" int main(void) { - if (LZY_Init()) + if (LZY_Init("lzy example")) { + LZY_Log(LZY_GetError()); + LZY_Quit(); return 1; + } + LZY_Log("init was great success!"); - LZY_DrawSetColor(255, 0, 0); + LZY_DrawSetColor(0xff, 0xff, 0x00); LZY_DrawBegin(); LZY_DrawClear(); @@ -13,7 +17,9 @@ int main(void) { do { LZY_CycleEvents(); - } while (!LZY_KeyDown(LZY_SC_ESCAPE)); + } while (!LZY_ShouldQuit()); - return 1; + LZY_Log("cya"); + LZY_Quit(); + return 0; } |