summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/controller.c72
-rw-r--r--src/controller.h22
-rw-r--r--src/main.c12
3 files changed, 64 insertions, 42 deletions
diff --git a/src/controller.c b/src/controller.c
index 09757c4..4e488e9 100644
--- a/src/controller.c
+++ b/src/controller.c
@@ -38,39 +38,59 @@ controller_update(controller_t *self,
{
if (event->type == SDL_KEYDOWN) {
switch (event->key.keysym.sym) {
- case SDLK_UP:
- self->up = true;
- break;
- case SDLK_DOWN:
- self->down = true;
- break;
- case SDLK_LEFT:
- self->left = true;
- break;
- case SDLK_RIGHT:
- self->right = true;
- break;
+ case SDLK_UP:
+ self->pressed_buttons |= BTN_UP;
+ break;
+ case SDLK_DOWN:
+ self->pressed_buttons |= BTN_DOWN;
+ break;
+ case SDLK_LEFT:
+ self->pressed_buttons |= BTN_LEFT;
+ break;
+ case SDLK_RIGHT:
+ self->pressed_buttons |= BTN_RIGHT;
+ break;
}
}
if (event->type == SDL_KEYUP) {
switch (event->key.keysym.sym) {
- case SDLK_UP:
- self->up = false;
- break;
+ case SDLK_UP:
+ self->pressed_buttons ^= BTN_UP;
+ break;
+ case SDLK_DOWN:
+ self->pressed_buttons ^= BTN_DOWN;
+ break;
+ case SDLK_LEFT:
+ self->pressed_buttons ^= BTN_LEFT;
+ break;
+ case SDLK_RIGHT:
+ self->pressed_buttons ^= BTN_RIGHT;
+ break;
+ }
+ }
+}
- case SDLK_DOWN:
- self->down = false;
- break;
+bool
+controller_is_up_pressed(controller_t *self)
+{
+ return self->pressed_buttons & BTN_UP;
+}
- case SDLK_LEFT:
- self->left = false;
- break;
+bool
+controller_is_down_pressed(controller_t *self)
+{
+ return self->pressed_buttons & BTN_DOWN;
+}
- case SDLK_RIGHT:
- self->right = false;
- break;
- }
- }
+bool
+controller_is_right_pressed(controller_t *self)
+{
+ return self->pressed_buttons & BTN_RIGHT;
}
+bool
+controller_is_left_pressed(controller_t *self)
+{
+ return self->pressed_buttons & BTN_LEFT;
+}
diff --git a/src/controller.h b/src/controller.h
index 736b9e6..ecc0389 100644
--- a/src/controller.h
+++ b/src/controller.h
@@ -33,23 +33,25 @@
#include <SDL.h>
#include <stdbool.h>
+#include <stdint.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
+enum btn_t {
+ BTN_UP = 1 << 0,
+ BTN_DOWN = 1 << 1,
+ BTN_RIGHT = 1 << 2,
+ BTN_LEFT = 1 << 3
+};
typedef struct controller_t {
- bool up;
- bool down;
- bool left;
- bool right;
+ uint8_t pressed_buttons;
} controller_t;
void controller_update(controller_t *self,
SDL_Event *event);
-#ifdef __cplusplus
-}
-#endif
+bool controller_is_up_pressed(controller_t *self);
+bool controller_is_down_pressed(controller_t *self);
+bool controller_is_right_pressed(controller_t *self);
+bool controller_is_left_pressed(controller_t *self);
#endif /* CONTROLLER_H */
diff --git a/src/main.c b/src/main.c
index 2c71864..b8e723b 100644
--- a/src/main.c
+++ b/src/main.c
@@ -74,7 +74,7 @@ main (int argc,
SDL_Rect rect = { .x = 0, .y = 0, .w = 20, .h = 20 };
bool quit = false;
- controller_t controller = {0};
+ controller_t ctrl = {0};
while (!quit) {
screenSurface = SDL_GetWindowSurface(window);
@@ -89,13 +89,13 @@ main (int argc,
if (event.type == SDL_QUIT) {
quit = true;
}
- controller_update(&controller, &event);
+ controller_update(&ctrl, &event);
}
- if (controller.up) rect.y -= 1;
- if (controller.down) rect.y += 1;
- if (controller.left) rect.x -= 1;
- if (controller.right) rect.x += 1;
+ if (controller_is_up_pressed(&ctrl)) rect.y -= 1;
+ if (controller_is_down_pressed(&ctrl)) rect.y += 1;
+ if (controller_is_left_pressed(&ctrl)) rect.x -= 1;
+ if (controller_is_right_pressed(&ctrl)) rect.x += 1;
SDL_FillRect(
screenSurface,