aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkdx <kikoodx@paranoici.org>2022-09-09 19:49:18 +0200
committerkdx <kikoodx@paranoici.org>2022-09-09 19:49:18 +0200
commitd6f17a30073a3a1f3f18f4dd00e0094fb2890118 (patch)
tree9f0c34bd2fe47febb76f5cd6382572ab55c37c6b
parentcbf3419346dfbea109471925e6bbc3fce4db9a55 (diff)
downloadlzr-d6f17a30073a3a1f3f18f4dd00e0094fb2890118.tar.gz
bindings pop-up menu
-rw-r--r--demo.c3
-rw-r--r--lzr.c83
-rw-r--r--lzr.h1
3 files changed, 84 insertions, 3 deletions
diff --git a/demo.c b/demo.c
index 2fa041c..d8238c9 100644
--- a/demo.c
+++ b/demo.c
@@ -1,10 +1,11 @@
#include "lzr.h"
+#include <SDL2/SDL_scancode.h>
#include <dx/log.h>
#include <stdbool.h>
int main(void)
{
- LZR_Config cfg = {400, 224, 60, 16, "LZR demo"};
+ LZR_Config cfg = {400, 224, 60, 16, "LZR demo", false};
if (LZR_Init(cfg))
return 1;
LZR_ImageLoad("coucou.bmp");
diff --git a/lzr.c b/lzr.c
index edf9d17..bd48b55 100644
--- a/lzr.c
+++ b/lzr.c
@@ -7,8 +7,10 @@
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
+#include <unistd.h>
-#define UNPACKED_COLOR color[0], color[1], color[2]
+#define UNPACKED_COLOR color[0], color[1], color[2]
+#define SCODE_BIND_MENU SDL_SCANCODE_F1
static LZR_Config config = {0};
static char *basepath = NULL;
@@ -56,6 +58,80 @@ static int _scode_to_button(unsigned int scode)
return -1;
}
+static void _draw_btn(SDL_Renderer *ren, int btn, int x, int y,
+ unsigned int size)
+{
+ const unsigned int size_2thirds = size * 2 / 3;
+ switch (btn) {
+ case LZR_BUTTON_LEFT:
+ filledTrigonRGBA(ren, x - size / 2, y, x + size / 2,
+ y + size_2thirds, x + size / 2,
+ y - size_2thirds, 0, 0, 0, 255);
+ break;
+ case LZR_BUTTON_RIGHT:
+ filledTrigonRGBA(ren, x + size / 2, y, x - size / 2,
+ y + size_2thirds, x - size / 2,
+ y - size_2thirds, 0, 0, 0, 255);
+ break;
+ case LZR_BUTTON_UP:
+ filledTrigonRGBA(ren, x, y - size / 2, x - size_2thirds,
+ y + size / 2, x + size_2thirds, y + size / 2,
+ 0, 0, 0, 255);
+ break;
+ case LZR_BUTTON_DOWN:
+ filledTrigonRGBA(ren, x, y + size / 2, x - size_2thirds,
+ y - size / 2, x + size_2thirds, y - size / 2,
+ 0, 0, 0, 255);
+ break;
+ case LZR_BUTTON_O:
+ filledCircleRGBA(ren, x, y, size * 2 / 3, 0, 0, 0, 255);
+ break;
+ case LZR_BUTTON_X:
+ thickLineRGBA(ren, x - size / 2, y - size / 2, x + size / 2,
+ y + size / 2, size / 16 + 1, 0, 0, 0, 255);
+ thickLineRGBA(ren, x + size / 2, y - size / 2, x - size / 2,
+ y + size / 2, size / 16 + 1, 0, 0, 0, 255);
+ break;
+ default:
+ break;
+ }
+}
+
+static void _bind_menu(void)
+{
+ dx_log_trace("entering bind menu");
+ SDL_Window *win = NULL;
+ SDL_Renderer *ren = NULL;
+ if (SDL_CreateWindowAndRenderer(256, 256, 0, &win, &ren) < 0) {
+ dx_log_error("%s", SDL_GetError());
+ return;
+ }
+ int btn = 0;
+ SDL_Event e;
+ while (btn < LZR_BUTTON_COUNT) {
+ while (SDL_PollEvent(&e)) {
+ if (e.type != SDL_KEYDOWN || e.key.repeat ||
+ e.key.keysym.scancode == SCODE_BIND_MENU)
+ continue;
+ if (e.key.keysym.scancode == SDL_SCANCODE_ESCAPE ||
+ e.type == SDL_QUIT)
+ goto exit_bind_menu;
+ LZR_ButtonBind(btn, e.key.keysym.scancode);
+ btn++;
+ }
+ SDL_SetRenderDrawColor(ren, 220, 220, 200, 255);
+ SDL_RenderClear(ren);
+ SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
+ _draw_btn(ren, btn, 128, 128, 104);
+ SDL_RenderPresent(ren);
+ sleep(0);
+ }
+exit_bind_menu:
+ SDL_DestroyRenderer(ren);
+ SDL_DestroyWindow(win);
+ dx_log_trace("leaving bind menu");
+}
+
int LZR_Init(LZR_Config cfg)
{
memcpy(&config, &cfg, sizeof(config));
@@ -193,6 +269,9 @@ bool LZR_PollEvent(LZR_Event *e)
return true;
case SDL_KEYDOWN: {
const int b = _scode_to_button(se.key.keysym.scancode);
+ if (!config.disable_bind_menu &&
+ se.key.keysym.scancode == SCODE_BIND_MENU)
+ _bind_menu();
if (se.key.repeat || b < 0)
break;
e->type = LZR_EVENT_BUTTON_DOWN;
@@ -232,7 +311,7 @@ bool LZR_ButtonDown(LZR_Button btn)
void LZR_ButtonBind(LZR_Button btn, unsigned int code)
{
- if (btn < LZR_BUTTON_COUNT) {
+ if (btn < LZR_BUTTON_COUNT && code != SCODE_BIND_MENU) {
map[btn] = code;
dx_log_trace("bound key %s to button %u",
SDL_GetScancodeName(map[btn]), btn);
diff --git a/lzr.h b/lzr.h
index 1d44dbe..b62ca41 100644
--- a/lzr.h
+++ b/lzr.h
@@ -10,6 +10,7 @@ typedef struct LZR_Config {
unsigned int target_fps;
unsigned int tile_size;
const char *title;
+ bool disable_bind_menu;
} LZR_Config;
typedef enum LZR_EventType {