From 02de1c17961b9a67bd6b75d5991408067041ffe9 Mon Sep 17 00:00:00 2001 From: Elias Fleckenstein Date: Wed, 9 Jun 2021 18:26:52 +0200 Subject: [PATCH] Modularize input --- plugins/game/game.c | 43 +++++++++++++++++++----------------- plugins/game/game.h | 9 ++++++++ plugins/movement/Makefile | 4 ++++ plugins/movement/movement.c | 44 +++++++++++++++++++++++++++++++++++++ 4 files changed, 80 insertions(+), 20 deletions(-) create mode 100644 plugins/movement/Makefile create mode 100644 plugins/movement/movement.c diff --git a/plugins/game/game.c b/plugins/game/game.c index c6ca4f8..521f7eb 100644 --- a/plugins/game/game.c +++ b/plugins/game/game.c @@ -36,6 +36,8 @@ struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT] = {{NULL}}; struct list *air_functions = NULL; +struct input_handler *input_handlers[256] = {NULL}; + void quit() { running = false; @@ -157,6 +159,17 @@ void register_air_function(struct generator_function func) air_functions = add_element(air_functions, buf); } +void register_input_handler(unsigned char c, struct input_handler handler) +{ + if (input_handlers[c]) + return; + + struct input_handler *buf = malloc(sizeof(struct input_handler)); + *buf = handler; + + input_handlers[c] = buf; +} + /* Player */ static void player_death(struct entity *self) @@ -372,27 +385,12 @@ static void render(render_entity_list entity_list) /* Input */ -static void handle_input(char c) +static void handle_input(unsigned char c) { - bool dead = player_dead(); - - switch (c) { - case 'q': - quit(); - break; - case 'w': - dead || move(&player, 0, -1); - break; - case 'a': - dead || move(&player, -1, 0); - break; - case 's': - dead || move(&player, 0, 1); - break; - case 'd': - dead || move(&player, 1, 0); - break; - } + struct input_handler *handler = input_handlers[c]; + + if (handler && (handler->run_if_dead || ! player_dead())) + handler->callback(); } /* Multithreading */ @@ -459,6 +457,11 @@ __attribute__ ((constructor)) static void init() for (int x = 0; x < MAP_WIDTH; x++) for (int y = 0; y < MAP_HEIGHT; y++) map[x][y] = (struct node) {&wall}; + + register_input_handler('q', (struct input_handler) { + .run_if_dead = true, + .callback = &quit, + }); } void game() diff --git a/plugins/game/game.h b/plugins/game/game.h index d5457a9..212eeae 100644 --- a/plugins/game/game.h +++ b/plugins/game/game.h @@ -67,6 +67,12 @@ struct generator_function void (*callback)(int x, int y); }; +struct input_handler +{ + bool run_if_dead; + void (*callback)(); +}; + extern int score; extern struct color black; @@ -84,6 +90,8 @@ extern struct entity *entity_collision_map[MAP_WIDTH][MAP_HEIGHT]; extern struct list *air_functions; +extern struct input_handler *input_handlers[256]; + void quit(); struct color get_color(const char *str); bool is_outside(int x, int y); @@ -98,6 +106,7 @@ void set_color(struct color color, bool bg); void light_color(struct color *color, double light); void mix_color(struct color *color, struct color other, double ratio); void register_air_function(struct generator_function func); +void register_input_handler(unsigned char c, struct input_handler handler); struct list *add_element(struct list *list, void *element); #endif diff --git a/plugins/movement/Makefile b/plugins/movement/Makefile new file mode 100644 index 0000000..9d23279 --- /dev/null +++ b/plugins/movement/Makefile @@ -0,0 +1,4 @@ +plugins/movement/movement.so: plugins/movement/movement.c plugins/game/game.h + cc -g -shared -fpic -o plugins/movement/movement.so plugins/movement/movement.c + +PLUGINS := ${PLUGINS} plugins/movement/movement.so diff --git a/plugins/movement/movement.c b/plugins/movement/movement.c new file mode 100644 index 0000000..494c8b0 --- /dev/null +++ b/plugins/movement/movement.c @@ -0,0 +1,44 @@ +#include "../game/game.h" + +static void move_up() +{ + move(&player, 0, -1); +} + +static void move_left() +{ + move(&player, -1, 0); +} + +static void move_down() +{ + move(&player, 0, 1); +} + +static void move_right() +{ + move(&player, 1, 0); +} + +__attribute__((constructor)) static void init() +{ + register_input_handler('w', (struct input_handler) { + .run_if_dead = false, + .callback = &move_up, + }); + + register_input_handler('a', (struct input_handler) { + .run_if_dead = false, + .callback = &move_left, + }); + + register_input_handler('s', (struct input_handler) { + .run_if_dead = false, + .callback = &move_down, + }); + + register_input_handler('d', (struct input_handler) { + .run_if_dead = false, + .callback = &move_right, + }); +} -- 2.44.0