summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2021-11-14 01:27:15 +0100
committerJohnny Richard <johnny@johnnyrichard.com>2021-11-14 01:27:15 +0100
commite415b62a2e85f44e6ff285e08b16525605d2fc0f (patch)
treef31f2ad1c8309b4fc5c5bfde2172efdb10076db3
parentf584871064596b52efe12c2f8258f0839f6eb0fa (diff)
controller_t: Reduce memory usage using bitwise
-rw-r--r--Makefile2
-rw-r--r--src/controller.c72
-rw-r--r--src/controller.h22
-rw-r--r--src/main.c12
-rw-r--r--test/controller-test.c16
5 files changed, 73 insertions, 51 deletions
diff --git a/Makefile b/Makefile
index 2433900..2a46725 100644
--- a/Makefile
+++ b/Makefile
@@ -44,7 +44,7 @@ $(TEST_DIR)/%: $(TEST_DIR)/%-test
test: lib/libmunit.so $(BUILD_DIR) $(TESTS)
lib/libmunit.so: $(LIB_DIR) third_party/munit/munit.c
- $(CC) -Wall -Werror -fpic -c third_party/munit/munit.c -shared -o lib/libmunit.so
+ $(CC) -Wall -fpic -c third_party/munit/munit.c -shared -o lib/libmunit.so
clean:
@rm -rf $(BUILD_DIR)
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,
diff --git a/test/controller-test.c b/test/controller-test.c
index d6c1455..c271756 100644
--- a/test/controller-test.c
+++ b/test/controller-test.c
@@ -47,11 +47,11 @@ press_and_release_up(const MunitParameter params[], void* fixture)
SDL_Event event = an_event(SDL_KEYDOWN, SDLK_UP);
controller_update(&ctrl, &event);
- assert_true(ctrl.up);
+ assert_true(controller_is_up_pressed(&ctrl));
event = an_event(SDL_KEYUP, SDLK_UP);
controller_update(&ctrl, &event);
- assert_false(ctrl.up);
+ assert_false(controller_is_up_pressed(&ctrl));
return MUNIT_OK;
}
@@ -62,11 +62,11 @@ press_and_release_down(const MunitParameter params[], void* fixture)
SDL_Event event = an_event(SDL_KEYDOWN, SDLK_DOWN);
controller_update(&ctrl, &event);
- assert_true(ctrl.down);
+ assert_true(controller_is_down_pressed(&ctrl));
event = an_event(SDL_KEYUP, SDLK_DOWN);
controller_update(&ctrl, &event);
- assert_false(ctrl.down);
+ assert_false(controller_is_down_pressed(&ctrl));
return MUNIT_OK;
}
@@ -77,11 +77,11 @@ press_and_release_right(const MunitParameter params[], void* fixture)
SDL_Event event = an_event(SDL_KEYDOWN, SDLK_RIGHT);
controller_update(&ctrl, &event);
- assert_true(ctrl.right);
+ assert_true(controller_is_right_pressed(&ctrl));
event = an_event(SDL_KEYUP, SDLK_RIGHT);
controller_update(&ctrl, &event);
- assert_false(ctrl.right);
+ assert_false(controller_is_right_pressed(&ctrl));
return MUNIT_OK;
}
@@ -92,11 +92,11 @@ press_and_release_left(const MunitParameter params[], void* fixture)
SDL_Event event = an_event(SDL_KEYDOWN, SDLK_LEFT);
controller_update(&ctrl, &event);
- assert_true(ctrl.left);
+ assert_true(controller_is_left_pressed(&ctrl));
event = an_event(SDL_KEYUP, SDLK_LEFT);
controller_update(&ctrl, &event);
- assert_false(ctrl.left);
+ assert_false(controller_is_left_pressed(&ctrl));
return MUNIT_OK;
}