diff options
author | yzrh <yzrh@noema.org> | 2020-10-10 17:16:14 +0000 |
---|---|---|
committer | yzrh <yzrh@noema.org> | 2020-10-10 17:37:53 +0000 |
commit | b9b74e88028f81e7b169a5e168102138e8c2c46c (patch) | |
tree | 2b8f9f8896db3a6871c56215acf71cb502fe7be4 /src/collision.c | |
download | snake-sdl-b9b74e88028f81e7b169a5e168102138e8c2c46c.tar.gz snake-sdl-b9b74e88028f81e7b169a5e168102138e8c2c46c.tar.zst |
Initial commit.
Diffstat (limited to 'src/collision.c')
-rw-r--r-- | src/collision.c | 42 |
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; +} |