summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2022-04-20 02:56:09 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2022-04-20 02:56:09 +0200
commit2f537114acc74a9e09443f69ab2508b292fdfa91 (patch)
tree7e51f1280815c863b7b80ace74071c3b0302c67a
parente1df1f684e3accc6b54b16c23c4f977e7fc11ab6 (diff)
Add screen limit collision
-rw-r--r--src/main.c23
1 files changed, 19 insertions, 4 deletions
diff --git a/src/main.c b/src/main.c
index 8b14bfa..70b283d 100644
--- a/src/main.c
+++ b/src/main.c
@@ -99,10 +99,25 @@ main (int argc,
controller_update(&ctrl, &event);
}
- if (controller_is_up_pressed(&ctrl)) rect.y -= 8;
- if (controller_is_down_pressed(&ctrl)) rect.y += 8;
- if (controller_is_left_pressed(&ctrl)) rect.x -= 8;
- if (controller_is_right_pressed(&ctrl)) rect.x += 8;
+ if (controller_is_up_pressed(&ctrl) && rect.y > 0) {
+ rect.y -= 8;
+ rect.y = rect.y < 0 ? 0 : rect.y;
+ }
+
+ if (controller_is_down_pressed(&ctrl) && rect.y + 8 < SCREEN_HEIGHT) {
+ rect.y += 8;
+ rect.y = rect.y + rect.h > SCREEN_HEIGHT ? SCREEN_HEIGHT - rect.h : rect.y;
+ }
+
+ if (controller_is_left_pressed(&ctrl) && rect.x > 0) {
+ rect.x -= 8;
+ rect.x = rect.x < 0 ? 0 : rect.x;
+ }
+
+ if (controller_is_right_pressed(&ctrl) && rect.x + 8 < SCREEN_WIDTH) {
+ rect.x += 8;
+ rect.x = rect.x + rect.h > SCREEN_WIDTH ? SCREEN_WIDTH - rect.h : rect.x;
+ }
SDL_SetRenderDrawColor(renderer, 0xFF, 0, 0, 1);
SDL_RenderFillRect(renderer, &rect);