From 0024dac8674e64695ca29fd8a7730c57ed847d26 Mon Sep 17 00:00:00 2001 From: KikooDX Date: Sat, 13 Aug 2022 19:41:55 +0200 Subject: [PATCH] consistant rect behavior --- inc/lzy.h | 43 +++++++++++++++++++------------------------ src/main.c | 31 +++++++++++++++++++++++-------- 2 files changed, 42 insertions(+), 32 deletions(-) diff --git a/inc/lzy.h b/inc/lzy.h index 12403e0..6c78c77 100644 --- a/inc/lzy.h +++ b/inc/lzy.h @@ -90,8 +90,8 @@ void LZY_DrawSetColorNone(void); int LZY_DrawClear(void); int LZY_DrawPoint(int x, int y); int LZY_DrawLine(int x0, int y0, int x1, int y1); -int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h); -int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h); +int LZY_DrawRect(int x, int y, int w, int h); +int LZY_DrawFillRect(int x, int y, int w, int h); int LZY_DrawTile(unsigned int id, int x, int y); int LZY_DrawTileEx(unsigned int id, int x, int y, unsigned int w, unsigned int h); @@ -210,6 +210,10 @@ static unsigned int tset_width, tset_height; static unsigned int font_width, font_height; static int timer_callback(volatile int *); +static int _LZY_Sign(int n) { + return (n > 0) - (n < 0); +} + int LZY_Init(const char *title, int target_fps, const char *tset_path, const char *font_path) { extern bopti_image_t LZY_GINT_TILESET; @@ -276,23 +280,24 @@ int LZY_DrawPoint(int x, int y) { return 0; } -int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) { - if (w < 1 || h < 1) - return -1; +int LZY_DrawRect(int x, int y, int w, int h) { + w += w == 0; + h += h == 0; x += draw_off_x; y += draw_off_y; - drect_border(x, y, x + w - 1, y + h - 1, C_NONE, 1, draw_color); + drect_border(x, y, x + w - _LZY_Sign(w), y + h - _LZY_Sign(h), C_NONE, + 1, draw_color); return 0; } -int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) { - if (w < 1 || h < 1) - return -1; +int LZY_DrawFillRect(int x, int y, int w, int h) { + w += w == 0; + h += h == 0; x += draw_off_x; y += draw_off_y; - drect(x, y, x + w - 1, y + h - 1, draw_color); + drect(x, y, x + w - _LZY_Sign(w), y + h - _LZY_Sign(h), draw_color); return 0; } @@ -716,13 +721,8 @@ int LZY_DrawPoint(int x, int y) { return 0; } -int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) { - const SDL_Rect rect = {x, y, w, h}; - - if (w == 0 || h == 0) { - error = "rectangle dimensions cannot be 0"; - return -1; - } +int LZY_DrawRect(int x, int y, int w, int h) { + const SDL_Rect rect = {x, y, w + (w == 0), h + (h == 0)}; if (SDL_RenderDrawRect(renderer, &rect) < 0) { error = SDL_GetError(); @@ -731,13 +731,8 @@ int LZY_DrawRect(int x, int y, unsigned int w, unsigned int h) { return 0; } -int LZY_DrawFillRect(int x, int y, unsigned int w, unsigned int h) { - const SDL_Rect rect = {x, y, w, h}; - - if (w == 0 || h == 0) { - error = "rectangle dimensions cannot be 0"; - return -1; - } +int LZY_DrawFillRect(int x, int y, int w, int h) { + const SDL_Rect rect = {x, y, w + (w == 0), h + (h == 0)}; if (SDL_RenderFillRect(renderer, &rect) < 0) { error = SDL_GetError(); diff --git a/src/main.c b/src/main.c index 458f362..b061254 100644 --- a/src/main.c +++ b/src/main.c @@ -11,6 +11,8 @@ static const int speed = 4; int main(int argc, char **argv) { int x = 0; int y = 0; + int w = 4; + int h = 4; (void)argc; (void)argv; @@ -27,14 +29,25 @@ int main(int argc, char **argv) { LZY_CycleEvents(); /* move player */ - if (LZY_KeyDown(LZYK_LEFT)) - x -= speed; - if (LZY_KeyDown(LZYK_RIGHT)) - x += speed; - if (LZY_KeyDown(LZYK_UP)) - y -= speed; - if (LZY_KeyDown(LZYK_DOWN)) - y += speed; + if (LZY_KeyDown(LZYK_X)) { + if (LZY_KeyDown(LZYK_LEFT)) + w -= speed; + if (LZY_KeyDown(LZYK_RIGHT)) + w += speed; + if (LZY_KeyDown(LZYK_UP)) + h -= speed; + if (LZY_KeyDown(LZYK_DOWN)) + h += speed; + } else { + if (LZY_KeyDown(LZYK_LEFT)) + x -= speed; + if (LZY_KeyDown(LZYK_RIGHT)) + x += speed; + if (LZY_KeyDown(LZYK_UP)) + y -= speed; + if (LZY_KeyDown(LZYK_DOWN)) + y += speed; + } /* draw */ LZY_DrawBegin(); @@ -52,6 +65,8 @@ int main(int argc, char **argv) { LZY_Log(LZY_GetError()); if (LZY_DrawTile(1, x, y)) LZY_Log(LZY_GetError()); + if (LZY_DrawRect(x, y, w, h)) + LZY_Log(LZY_GetError()); LZY_DrawTextF(0, 0, "%d ; %d", x, y); }