diff options
author | kdx <kikoodx@paranoici.org> | 2023-06-06 00:41:35 +0200 |
---|---|---|
committer | kdx <kikoodx@paranoici.org> | 2023-06-06 00:41:35 +0200 |
commit | 60ddda47ed683ff458e580c7ea85f3b447896b12 (patch) | |
tree | b8185a264ee9ac506f10d082eb4b1f86b1e4a619 | |
download | input-60ddda47ed683ff458e580c7ea85f3b447896b12.tar.gz |
initial commit
-rw-r--r-- | LICENSE | 19 | ||||
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | input.c | 138 | ||||
-rw-r--r-- | input.h | 17 |
4 files changed, 177 insertions, 0 deletions
@@ -0,0 +1,19 @@ +Copyright (c) 2023 kdx + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +of the Software, and to permit persons to whom the Software is furnished to do +so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..d2c7b13 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# input + +flexible high level input module written in C99 for TZR @@ -0,0 +1,138 @@ +#include "input.h" +#include "TZR.h" +#include <assert.h> + +enum { + BIND_SCANCODE, +}; + +struct { + const char *name; + struct { + unsigned int type; + union { + SDL_Scancode sc; + }; + } binds[INPUT_MAX_BINDS]; + int nb_binds; +} input[INPUT_MAX_ACTIONS] = {0}; +static int nb_actions = 0; + +void +input_new(const char *bind) +{ + assert(nb_actions < INPUT_MAX_ACTIONS); + input[nb_actions].name = bind; + nb_actions += 1; +} + +void +input_clear(const char *bind) +{ + for (int i = 0; i < nb_actions; i++) + if (strcmp(bind, input[i].name) == 0) { + input[i].nb_binds = 0; + return; + } + fprintf(stderr, "input_clear: bind '%s' not found\n", bind); + assert(0); +} + +void +input_bind_scancode(const char *bind, SDL_Scancode sc) +{ + for (int i = 0; i < nb_actions; i++) + if (strcmp(bind, input[i].name) == 0) { + assert(input[i].nb_binds < INPUT_MAX_BINDS); + input[i].binds[input[i].nb_binds].type = BIND_SCANCODE; + input[i].binds[input[i].nb_binds].sc = sc; + input[i].nb_binds += 1; + return; + } + fprintf(stderr, "input_bind_scancode: bind '%s' not found\n", bind); + assert(0); +} + +bool +input_down(const char *bind) +{ + for (int i = 0; i < nb_actions; i++) { + if (strcmp(bind, input[i].name) == 0) { + for (int k = 0; k < input[i].nb_binds; k++) + switch (input[i].binds[k].type) { + case BIND_SCANCODE: + if (TZR_IsKeyDown(input[i].binds[k].sc)) + return true; + break; + default: + __builtin_unreachable(); + } + return false; + } + } + fprintf(stderr, "input_down: bind '%s' not found\n", bind); + assert(0); +} + +bool +input_up(const char *bind) +{ + for (int i = 0; i < nb_actions; i++) { + if (strcmp(bind, input[i].name) == 0) { + for (int k = 0; k < input[i].nb_binds; k++) + switch (input[i].binds[k].type) { + case BIND_SCANCODE: + if (TZR_IsKeyUp(input[i].binds[k].sc)) + return true; + break; + default: + __builtin_unreachable(); + } + return false; + } + } + fprintf(stderr, "input_up: bind '%s' not found\n", bind); + assert(0); +} + +bool +input_pressed(const char *bind) +{ + for (int i = 0; i < nb_actions; i++) { + if (strcmp(bind, input[i].name) == 0) { + for (int k = 0; k < input[i].nb_binds; k++) + switch (input[i].binds[k].type) { + case BIND_SCANCODE: + if (TZR_IsKeyPressed(input[i].binds[k].sc)) + return true; + break; + default: + __builtin_unreachable(); + } + return false; + } + } + fprintf(stderr, "input_pressed: bind '%s' not found\n", bind); + assert(0); +} + +bool +input_released(const char *bind) +{ + for (int i = 0; i < nb_actions; i++) { + if (strcmp(bind, input[i].name) == 0) { + for (int k = 0; k < input[i].nb_binds; k++) + switch (input[i].binds[k].type) { + case BIND_SCANCODE: + if (TZR_IsKeyReleased(input[i].binds[k].sc)) + return true; + break; + default: + __builtin_unreachable(); + } + return false; + } + } + fprintf(stderr, "input_released: bind '%s' not found\n", bind); + assert(0); +} @@ -0,0 +1,17 @@ +#pragma once +#include <SDL2/SDL_scancode.h> +#include <stdbool.h> + +enum { + INPUT_MAX_ACTIONS = 16, + INPUT_MAX_BINDS = 4 +}; + +void input_deinit(void); +void input_new(const char *action); +void input_clear(const char *action); +void input_bind_scancode(const char *action, SDL_Scancode sc); +bool input_down(const char *action); +bool input_up(const char *action); +bool input_pressed(const char *action); +bool input_released(const char *action); |