summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2021-04-13 02:53:13 +0200
committerJohnny Richard <johnny@johnnyrichard.com>2021-10-28 02:22:57 +0200
commitd8336885a4c053544fe3491335da0f76afffdfb9 (patch)
treeb3cd866a257406281144075c0af2407b9de2978c
parent9a285cc3bdb8d5870fea5f07235a3e44786a0317 (diff)
Move controller update to a new update function
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
-rw-r--r--Makefile3
-rw-r--r--src/controller.c46
-rw-r--r--src/controller.h4
-rw-r--r--src/main.c38
4 files changed, 53 insertions, 38 deletions
diff --git a/Makefile b/Makefile
index 68d2425..6c615f3 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,8 @@ SOURCE_DIR ?= ./src
BUILD_DIR ?= ./build
CFLAGS := -Wall
-OBJS := $(BUILD_DIR)/main.o
+OBJS := $(BUILD_DIR)/main.o \
+ $(BUILD_DIR)/controller.o
ifeq ($(OS),Windows_NT)
CC := gcc
diff --git a/src/controller.c b/src/controller.c
new file mode 100644
index 0000000..5fb1e0b
--- /dev/null
+++ b/src/controller.c
@@ -0,0 +1,46 @@
+#include <SDL.h>
+#include "controller.h"
+
+
+void
+controller_update(controller_t *self,
+ SDL_Event *event)
+{
+ 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;
+ }
+ }
+
+ if (event->type == SDL_KEYUP) {
+ switch (event->key.keysym.sym) {
+ case SDLK_UP:
+ self->up = false;
+ break;
+
+ case SDLK_DOWN:
+ self->down = false;
+ break;
+
+ case SDLK_LEFT:
+ self->left = false;
+ break;
+
+ case SDLK_RIGHT:
+ self->right = false;
+ break;
+ }
+ }
+}
+
diff --git a/src/controller.h b/src/controller.h
index bf22335..a97fc7a 100644
--- a/src/controller.h
+++ b/src/controller.h
@@ -1,6 +1,7 @@
#ifndef CONTROLLER_H
#define CONTROLLER_H
+#include <SDL.h>
#include <stdbool.h>
#ifdef __cplusplus
@@ -14,6 +15,9 @@ typedef struct controller_t {
bool right;
} controller_t;
+void controller_update(controller_t *self,
+ SDL_Event *event);
+
#ifdef __cplusplus
}
#endif
diff --git a/src/main.c b/src/main.c
index 4f685b2..19c4acf 100644
--- a/src/main.c
+++ b/src/main.c
@@ -47,43 +47,7 @@ main (int argc,
if (event.type == SDL_QUIT) {
quit = true;
}
-
- if (event.type == SDL_KEYDOWN) {
- switch (event.key.keysym.sym) {
- case SDLK_UP:
- controller.up = true;
- break;
- case SDLK_DOWN:
- controller.down = true;
- break;
- case SDLK_LEFT:
- controller.left = true;
- break;
- case SDLK_RIGHT:
- controller.right = true;
- break;
- }
- }
-
- if (event.type == SDL_KEYUP) {
- switch (event.key.keysym.sym) {
- case SDLK_UP:
- controller.up = false;
- break;
-
- case SDLK_DOWN:
- controller.down = false;
- break;
-
- case SDLK_LEFT:
- controller.left = false;
- break;
-
- case SDLK_RIGHT:
- controller.right = false;
- break;
- }
- }
+ controller_update(&controller, &event);
}
if (controller.up) rect.y -= 1;