]> git.lizzy.rs Git - dungeon_game.git/commitdiff
Modularize input
authorElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 16:26:52 +0000 (18:26 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Wed, 9 Jun 2021 16:26:52 +0000 (18:26 +0200)
plugins/game/game.c
plugins/game/game.h
plugins/movement/Makefile [new file with mode: 0644]
plugins/movement/movement.c [new file with mode: 0644]

index c6ca4f81f3874dccdd1e946864e6d34d3ee0e874..521f7eb94a25b7aa814e635b47ef73c52f228e89 100644 (file)
@@ -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()
index d5457a9eb9e7973e50cfcd6a13267817c5ebed53..212eeae05321d7e9aab42e687f27add0df045824 100644 (file)
@@ -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 (file)
index 0000000..9d23279
--- /dev/null
@@ -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 (file)
index 0000000..494c8b0
--- /dev/null
@@ -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,
+       });
+}