aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2022-09-09 18:21:06 +0200
committerkdx <kikoodx@paranoici.org>2022-09-09 18:22:43 +0200
commitcbf3419346dfbea109471925e6bbc3fce4db9a55 (patch)
tree4a80b8d195af69de81a82e204ce9d4e4f4164be1
parent4ec501274d38259075ca68a3dd0e50f7f1634bc8 (diff)
downloadlzr-cbf3419346dfbea109471925e6bbc3fce4db9a55.tar.gz
button binding function
-rw-r--r--lzr.c17
-rw-r--r--lzr.h3
2 files changed, 16 insertions, 4 deletions
diff --git a/lzr.c b/lzr.c
index 4e0ec7b..edf9d17 100644
--- a/lzr.c
+++ b/lzr.c
@@ -23,6 +23,9 @@ static struct {
int width, height;
} images[LZR_MAX_IMAGES] = {0};
static unsigned int color[3] = {0};
+static unsigned int map[LZR_BUTTON_COUNT] = {
+ SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP,
+ SDL_SCANCODE_DOWN, SDL_SCANCODE_X, SDL_SCANCODE_C};
static bool input[LZR_BUTTON_COUNT] = {false};
static char *_path_prefix(const char *path)
@@ -47,9 +50,6 @@ static char *_path_prefix(const char *path)
static int _scode_to_button(unsigned int scode)
{
- static const unsigned int map[LZR_BUTTON_COUNT] = {
- SDL_SCANCODE_LEFT, SDL_SCANCODE_RIGHT, SDL_SCANCODE_UP,
- SDL_SCANCODE_DOWN, SDL_SCANCODE_X, SDL_SCANCODE_C};
for (int i = 0; i < LZR_BUTTON_COUNT; i++)
if (map[i] == scode)
return i;
@@ -230,6 +230,17 @@ bool LZR_ButtonDown(LZR_Button btn)
return false;
}
+void LZR_ButtonBind(LZR_Button btn, unsigned int code)
+{
+ if (btn < LZR_BUTTON_COUNT) {
+ map[btn] = code;
+ dx_log_trace("bound key %s to button %u",
+ SDL_GetScancodeName(map[btn]), btn);
+ } else
+ dx_log_warn("button %u can't be remapped to key %s", btn,
+ SDL_GetScancodeName(code));
+}
+
int LZR_DrawBegin(void)
{
if (config.target_fps > 0)
diff --git a/lzr.h b/lzr.h
index 451848d..1d44dbe 100644
--- a/lzr.h
+++ b/lzr.h
@@ -44,7 +44,8 @@ void LZR_Quit(void);
bool LZR_ShouldQuit(void);
bool LZR_PollEvent(LZR_Event *e);
void LZR_CycleEvents(void);
-bool LZR_ButtonDown(LZR_Button);
+bool LZR_ButtonDown(LZR_Button btn);
+void LZR_ButtonBind(LZR_Button btn, unsigned int code);
int LZR_ImageLoad(const char *path);
int LZR_DrawBegin(void);
int LZR_DrawEnd(void);