aboutsummaryrefslogtreecommitdiffstats
path: root/src/collision.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/collision.c')
-rw-r--r--src/collision.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/collision.c b/src/collision.c
new file mode 100644
index 0000000..5810c84
--- /dev/null
+++ b/src/collision.c
@@ -0,0 +1,42 @@
+/*
+ * Copyright (c) 2019-2020, yzrh <yzrh@noema.org>
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+#include <stdbool.h>
+
+#include <SDL2/SDL.h>
+
+#include "screen.h"
+#include "renderer.h"
+
+bool
+collision(int *a, int *b)
+{
+ if (a[0] < b[0] + b[2] &&
+ a[0] + a[2] > b[0] &&
+ a[1] < b[1] + b[3] &&
+ a[1] + a[3] > b[1])
+ return 1;
+ return 0;
+}
+
+bool
+collision_block(int *a, int *b)
+{
+ return collision(
+ (int[]) {a[0], a[1], SCREEN_UNIT, SCREEN_UNIT},
+ (int[]) {b[0], b[1], SCREEN_UNIT, SCREEN_UNIT});
+}
+
+bool
+collision_bound(float x, float y, Snake_Text *message)
+{
+ if (x > message->pos.x &&
+ x < message->pos.x + message->pos.w &&
+ y > message->pos.y &&
+ y < message->pos.y + message->pos.h)
+ return 1;
+ return 0;
+}