A game engine for top-down 2D RPG games.
rpg
game-engine
raylib
c99
1#include <keraforge.h>
2#include <raylib.h>
3#include <stdio.h>
4#include <string.h>
5
6
7f32 kf_deadzone = 0.2f;
8
9struct _kf_inputbinds kf_inputbinds = {
10 .count = 1,
11};
12
13
14kf_inputbind_t kf_addinput(char *id, KeyboardKey key, KeyboardKey alt, MouseButton mouse, GamepadButton gamepad, GamepadAxis axis)
15{
16 kf_inputbind_t i = kf_inputbinds.count;
17 if (i >= KF_INPUTBIND_MAX)
18 {
19 fprintf(stderr, "error: max keybind count is 255.\n");
20 return -1;
21 }
22 kf_inputbinds.count++;
23
24 printf("add keybind: %d: %s (k=%d a=%d m=%d g=%d x=%d)\n", i, id, key, alt, mouse, gamepad, axis);
25 kf_inputbinds.id[i] = id;
26 kf_inputbinds.key[i] = key;
27 kf_inputbinds.alt[i] = alt;
28 kf_inputbinds.mouse[i] = mouse;
29 kf_inputbinds.gamepad[i] = gamepad;
30 kf_inputbinds.axis[i] = axis;
31
32 return i;
33}
34
35kf_inputbind_t kf_getinput(char *id)
36{
37 for (int i = 0 ; i < KF_INPUTBIND_MAX ; i++)
38 {
39 if (strcmp(id, kf_inputbinds.id[i]) == 0)
40 {
41 return i;
42 }
43 }
44
45 return KF_INPUTBIND_NONE;
46}
47
48
49int kf_checkkeypress(kf_inputbind_t id)
50{
51 return
52 (kf_inputbinds.key[id] != KEY_NULL && IsKeyPressed(kf_inputbinds.key[id])) ||
53 (kf_inputbinds.alt[id] != KEY_NULL && IsKeyPressed(kf_inputbinds.alt[id]));
54}
55
56int kf_checkkeyrelease(kf_inputbind_t id)
57{
58 return
59 (kf_inputbinds.key[id] != KEY_NULL && IsKeyReleased(kf_inputbinds.key[id])) ||
60 (kf_inputbinds.alt[id] != KEY_NULL && IsKeyReleased(kf_inputbinds.alt[id]));
61}
62
63int kf_checkkeydown(kf_inputbind_t id)
64{
65 return
66 (kf_inputbinds.key[id] != KEY_NULL && IsKeyDown(kf_inputbinds.key[id])) ||
67 (kf_inputbinds.alt[id] != KEY_NULL && IsKeyDown(kf_inputbinds.alt[id]));
68}
69
70int kf_checkkeyup(kf_inputbind_t id)
71{
72 return
73 (kf_inputbinds.key[id] != KEY_NULL && IsKeyUp(kf_inputbinds.key[id])) ||
74 (kf_inputbinds.alt[id] != KEY_NULL && IsKeyUp(kf_inputbinds.alt[id]));
75}
76
77int kf_checkmousepress(kf_inputbind_t id)
78{
79 return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonPressed(kf_inputbinds.mouse[id]);
80}
81
82int kf_checkmouserelease(kf_inputbind_t id)
83{
84 return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonReleased(kf_inputbinds.mouse[id]);
85}
86
87int kf_checkmousedown(kf_inputbind_t id)
88{
89 return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonDown(kf_inputbinds.mouse[id]);
90}
91
92int kf_checkmouseup(kf_inputbind_t id)
93{
94 return kf_inputbinds.mouse[id] != MOUSE_BUTTON_UNKNOWN && IsMouseButtonUp(kf_inputbinds.mouse[id]);
95}
96
97int kf_checkgamepadpress(kf_inputbind_t id)
98{
99 return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonPressed(0, kf_inputbinds.gamepad[id]);
100}
101
102int kf_checkgamepadrelease(kf_inputbind_t id)
103{
104 return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonReleased(0, kf_inputbinds.gamepad[id]);
105}
106
107int kf_checkgamepaddown(kf_inputbind_t id)
108{
109 return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonDown(0, kf_inputbinds.gamepad[id]);
110}
111
112int kf_checkgamepadup(kf_inputbind_t id)
113{
114 return kf_inputbinds.gamepad[id] != GAMEPAD_BUTTON_UNKNOWN && IsGamepadButtonUp(0, kf_inputbinds.gamepad[id]);
115}
116
117float kf_getgamepadaxis(kf_inputbind_t id)
118{
119 return kf_inputbinds.axis[id] != GAMEPAD_AXIS_UNKNOWN ? GetGamepadAxisMovement(0, kf_inputbinds.gamepad[id]) : 0;
120}
121
122
123int kf_checkinputpress(kf_inputbind_t id)
124{
125 return kf_checkkeypress(id) || kf_checkmousepress(id) || kf_checkgamepadpress(id);
126}
127
128int kf_checkinputrelease(kf_inputbind_t id)
129{
130 return kf_checkkeyrelease(id) || kf_checkmouserelease(id) || kf_checkgamepadrelease(id);
131}
132
133int kf_checkinputdown(kf_inputbind_t id)
134{
135 return kf_checkkeydown(id) || kf_checkmousedown(id) || kf_checkgamepaddown(id);
136}
137
138int kf_checkinputup(kf_inputbind_t id)
139{
140 return kf_checkkeyup(id) || kf_checkmouseup(id) || kf_checkgamepadup(id);
141}