aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKikooDX <kikoodx@paranoici.org>2022-08-13 19:41:55 +0200
committerKikooDX <kikoodx@paranoici.org>2022-08-13 19:41:55 +0200
commit0024dac8674e64695ca29fd8a7730c57ed847d26 (patch)
treeaaf1ca92a4c7e6c59e9b6145954406e9ee3f15e9
parent605d6d09708a99c1c57cf4a0427d997614d288f8 (diff)
downloadlzy-0024dac8674e64695ca29fd8a7730c57ed847d26.tar.gz
consistant rect behavior
-rw-r--r--inc/lzy.h43
-rw-r--r--src/main.c31
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);
}