A game engine for top-down 2D RPG games.
rpg game-engine raylib c99

input bindings

Changed files
+154 -14
include
src
+1
compile_flags.txt
··· 3 3 -Werror 4 4 -std=c99 5 5 -Iinclude/ 6 + -xc-header
+2 -1
include/keraforge.h
··· 4 4 #include <keraforge/_header.h> 5 5 #include <keraforge/actor.h> 6 6 #include <keraforge/fs.h> 7 + #include <keraforge/input.h> 8 + #include <keraforge/math.h> 7 9 #include <keraforge/world.h> 8 - #include <keraforge/math.h> 9 10 10 11 #endif
+1
include/keraforge/actor.h
··· 19 19 20 20 /* Check if the actor can move in the given direction. */ 21 21 int kf_actor_canmovetowards(struct kf_world *world, struct kf_actor *actor, struct kf_vec2(f32) dir); 22 + /* Add the given force to the actor's velocity. */ 22 23 void kf_actor_addforce(struct kf_actor *self, struct kf_vec2(f32) force); 23 24 /* Move the actor according to their velocity. */ 24 25 void kf_actor_move(struct kf_world *world, struct kf_actor *actor, f32 deltatime);
-1
include/keraforge/fs.h
··· 8 8 9 9 /* Read binary file contents. */ 10 10 u8 *kf_readbin(char *filename, size_t *plen); 11 - 12 11 /* Write binary file contents. */ 13 12 int kf_writebin(char *filename, u8 *data, size_t len); 14 13
+51
include/keraforge/input.h
··· 1 + #ifndef __kf_input__ 2 + #define __kf_input__ 3 + 4 + #include <keraforge/_header.h> 5 + #include <raylib.h> 6 + 7 + #define MOUSE_BUTTON_UNKNOWN ((MouseButton)-1) 8 + #define GAMEPAD_AXIS_UNKNOWN ((GamepadAxis)-1) 9 + 10 + #define KF_INPUTBIND_MAX UINT8_MAX 11 + typedef u8 kf_inputbind_t; 12 + extern const kf_inputbind_t kf_inputbind_none; 13 + 14 + struct _kf_inputbinds 15 + { 16 + kf_inputbind_t count; 17 + char *id[KF_INPUTBIND_MAX]; 18 + KeyboardKey key[KF_INPUTBIND_MAX]; 19 + MouseButton mouse[KF_INPUTBIND_MAX]; 20 + GamepadButton gamepad[KF_INPUTBIND_MAX]; 21 + GamepadAxis axis[KF_INPUTBIND_MAX]; 22 + }; 23 + 24 + extern struct _kf_inputbinds kf_inputbinds; 25 + 26 + /* Add a new input binding. */ 27 + kf_inputbind_t kf_addinput(char *id, KeyboardKey key, MouseButton mouse, GamepadButton gamepad, GamepadAxis axis); 28 + /* Get an input's index by it's translation key. */ 29 + kf_inputbind_t kf_getinput(char *id); 30 + 31 + int kf_checkkeypress(kf_inputbind_t id); 32 + int kf_checkkeyrelease(kf_inputbind_t id); 33 + int kf_checkkeydown(kf_inputbind_t id); 34 + int kf_checkkeyup(kf_inputbind_t id); 35 + int kf_checkmousepress(kf_inputbind_t id); 36 + int kf_checkmouserelease(kf_inputbind_t id); 37 + int kf_checkmousedown(kf_inputbind_t id); 38 + int kf_checkmouseup(kf_inputbind_t id); 39 + int kf_checkgamepadpress(kf_inputbind_t id); 40 + int kf_checkgamepadrelease(kf_inputbind_t id); 41 + int kf_checkgamepaddown(kf_inputbind_t id); 42 + int kf_checkgamepadup(kf_inputbind_t id); 43 + float kf_getgamepadaxis(kf_inputbind_t id); 44 + 45 + int kf_checkinputpress(kf_inputbind_t id); 46 + int kf_checkinputrelease(kf_inputbind_t id); 47 + int kf_checkinputdown(kf_inputbind_t id); 48 + int kf_checkinputup(kf_inputbind_t id); 49 + 50 + 51 + #endif
+1 -3
src/actor.c
··· 1 - #include "keraforge/math.h" 2 - #include "keraforge/world.h" 3 1 #include <keraforge.h> 4 2 #include <stdlib.h> 5 3 #include <raymath.h> ··· 86 84 if (self->speedmod > -(1+speed_deadzone) && self->speedmod < 1+speed_deadzone) 87 85 self->speedmod = 1; 88 86 else if (self->speedmod) 89 - self->speedmod /= self->friction; 87 + self->speedmod -= self->friction * self->friction * dt; 90 88 }
+1 -1
src/fs.c
··· 1 - #include <keraforge/fs.h> 1 + #include <keraforge.h> 2 2 #include <stdio.h> 3 3 #include <stdlib.h> 4 4
+75
src/input.c
··· 1 + #include "keraforge/input.h" 2 + #include <keraforge.h> 3 + #include <raylib.h> 4 + #include <stdio.h> 5 + #include <string.h> 6 + 7 + const kf_inputbind_t kf_inputbind_none = (kf_inputbind_t)-1; 8 + struct _kf_inputbinds kf_inputbinds = {0}; 9 + 10 + kf_inputbind_t kf_addinput(char *id, KeyboardKey key, MouseButton mouse, GamepadButton gamepad, GamepadAxis axis) 11 + { 12 + kf_inputbind_t i = kf_inputbinds.count; 13 + if (i >= KF_INPUTBIND_MAX || i == kf_inputbind_none) 14 + { 15 + fprintf(stderr, "error: max keybind count is 255.\n"); 16 + return -1; 17 + } 18 + kf_inputbinds.count++; 19 + 20 + printf("add keybind: %d: %s (k=%d m=%d g=%d x=%d)\n", i, id, key, mouse, gamepad, axis); 21 + kf_inputbinds.id[i] = id; 22 + kf_inputbinds.key[i] = key; 23 + kf_inputbinds.mouse[i] = mouse; 24 + kf_inputbinds.gamepad[i] = gamepad; 25 + kf_inputbinds.axis[i] = axis; 26 + 27 + return i; 28 + } 29 + 30 + kf_inputbind_t kf_getinput(char *id) 31 + { 32 + for (int i = 0 ; i < KF_INPUTBIND_MAX ; i++) 33 + { 34 + if (strcmp(id, kf_inputbinds.id[i]) == 0) 35 + { 36 + return i; 37 + } 38 + } 39 + 40 + return kf_inputbind_none; 41 + } 42 + 43 + int kf_checkkeypress(kf_inputbind_t id) { return kf_inputbinds.key[id] != KEY_NULL && IsKeyPressed(kf_inputbinds.key[id]); } 44 + int kf_checkkeyrelease(kf_inputbind_t id) { return kf_inputbinds.key[id] != KEY_NULL && IsKeyReleased(kf_inputbinds.key[id]); } 45 + int kf_checkkeydown(kf_inputbind_t id) { return kf_inputbinds.key[id] != KEY_NULL && IsKeyDown(kf_inputbinds.key[id]); } 46 + int kf_checkkeyup(kf_inputbind_t id) { return kf_inputbinds.key[id] != KEY_NULL && IsKeyUp(kf_inputbinds.key[id]); } 47 + int kf_checkmousepress(kf_inputbind_t id) { return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonPressed(kf_inputbinds.mouse[id]); } 48 + int kf_checkmouserelease(kf_inputbind_t id) { return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonReleased(kf_inputbinds.mouse[id]); } 49 + int kf_checkmousedown(kf_inputbind_t id) { return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonDown(kf_inputbinds.mouse[id]); } 50 + int kf_checkmouseup(kf_inputbind_t id) { return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonUp(kf_inputbinds.mouse[id]); } 51 + int kf_checkgamepadpress(kf_inputbind_t id) { return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonPressed(0, kf_inputbinds.gamepad[id]); } 52 + int kf_checkgamepadrelease(kf_inputbind_t id) { return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonReleased(0, kf_inputbinds.gamepad[id]); } 53 + int kf_checkgamepaddown(kf_inputbind_t id) { return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonDown(0, kf_inputbinds.gamepad[id]); } 54 + int kf_checkgamepadup(kf_inputbind_t id) { return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonUp(0, kf_inputbinds.gamepad[id]); } 55 + float kf_getgamepadaxis(kf_inputbind_t id) { return kf_inputbinds.axis[id] != GAMEPAD_AXIS_UNKNOWN ? GetGamepadAxisMovement(0, kf_inputbinds.gamepad[id]) : 0; } 56 + 57 + int kf_checkinputpress(kf_inputbind_t id) 58 + { 59 + return kf_checkkeypress(id) || kf_checkmousepress(id) || kf_checkgamepadpress(id); 60 + } 61 + 62 + int kf_checkinputrelease(kf_inputbind_t id) 63 + { 64 + return kf_checkkeyrelease(id) || kf_checkmouserelease(id) || kf_checkgamepadrelease(id); 65 + } 66 + 67 + int kf_checkinputdown(kf_inputbind_t id) 68 + { 69 + return kf_checkkeydown(id) || kf_checkmousedown(id) || kf_checkgamepaddown(id); 70 + } 71 + 72 + int kf_checkinputup(kf_inputbind_t id) 73 + { 74 + return kf_checkkeyup(id) || kf_checkmouseup(id) || kf_checkgamepadup(id); 75 + }
+20 -5
src/main.c
··· 1 - #include "keraforge/actor.h" 2 - #include "keraforge/math.h" 3 - #include "keraforge/world.h" 1 + #include "keraforge/input.h" 4 2 #include <raylib.h> 5 3 #include <raymath.h> 6 4 #include <keraforge.h> ··· 11 9 static f32 dt = 0; 12 10 static struct kf_vec2(u32) select = { 0, 0 }; 13 11 12 + static kf_inputbind_t 13 + inputbind_move_up, 14 + inputbind_move_down, 15 + inputbind_move_left, 16 + inputbind_move_right 17 + ; 18 + 19 + static void loadbinds() 20 + { 21 + inputbind_move_up = kf_addinput("move_up", KEY_W, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN); 22 + inputbind_move_down = kf_addinput("move_down", KEY_S, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN); 23 + inputbind_move_left = kf_addinput("move_left", KEY_A, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN); 24 + inputbind_move_right = kf_addinput("move_right", KEY_D, MOUSE_BUTTON_UNKNOWN, GAMEPAD_BUTTON_UNKNOWN, GAMEPAD_AXIS_UNKNOWN); 25 + } 26 + 14 27 static void _player_tick(struct kf_world *world, struct kf_actor *self) 15 28 { 16 - bool w = IsKeyDown(KEY_W), s = IsKeyDown(KEY_S); 17 - bool a = IsKeyDown(KEY_A), d = IsKeyDown(KEY_D); 29 + bool w = kf_checkinputdown(inputbind_move_up), s = kf_checkinputdown(inputbind_move_down); 30 + bool a = kf_checkinputdown(inputbind_move_left), d = kf_checkinputdown(inputbind_move_right); 18 31 struct kf_vec2(f32) v = {0, 0}; 19 32 20 33 if (w && s) ··· 53 66 { 54 67 (void)argc; 55 68 (void)argv; 69 + 70 + loadbinds(); 56 71 57 72 SetTraceLogLevel(LOG_WARNING); 58 73 InitWindow(800, 600, "Keraforge");
+2 -3
src/world.c
··· 1 - #include <keraforge/world.h> 2 - #include <keraforge/actor.h> 1 + #include <keraforge.h> 3 2 #include <raylib.h> 4 3 #include <stdlib.h> 5 4 #include <string.h> 6 5 #include <math.h> 7 6 8 - struct _kf_tiles kf_tiles; 7 + struct _kf_tiles kf_tiles = {0}; 9 8 10 9 struct kf_world *kf_world_new(u32 width, u32 height, kf_tileid_t fill) 11 10 {