implement game configuration file

this integrates https://github.com/cktan/tomlc99 into the repo as a dependency
This commit is contained in:
2024-09-30 21:13:58 -03:00
committed by veclavtalica
parent ec15d8ec4b
commit 57fe5e8946
165 changed files with 4797 additions and 92 deletions

View File

@ -1,5 +1,6 @@
#include "twn_input_c.h"
#include "twn_util.h"
#include "twn_engine_context_c.h"
#include <SDL2/SDL.h>
#include <stb_ds.h>
@ -9,7 +10,7 @@
static void update_action_pressed_state(InputState *input, Action *action) {
for (size_t i = 0; i < SDL_arraysize(action->bindings); ++i) {
for (size_t i = 0; i < (uint64_t)ctx.keybind_slots; ++i) {
switch (action->bindings[i].source) {
case BUTTON_SOURCE_NOT_SET:
break;
@ -74,7 +75,7 @@ static void input_bind_code_to_action(InputState *input,
Action *action = &action_item->value;
/* check every binding to make sure this code isn't already bound */
for (size_t i = 0; i < SDL_arraysize(action->bindings); ++i) {
for (size_t i = 0; i < (uint64_t)ctx.keybind_slots; ++i) {
Button *binding = &action->bindings[i];
if (binding->source != source)
@ -105,7 +106,7 @@ static void input_bind_code_to_action(InputState *input,
}
/* if we're at max bindings, forget the first element and shift the rest */
if (action->num_bindings == SDL_arraysize(action->bindings)) {
if (action->num_bindings == (uint64_t)ctx.keybind_slots) {
--action->num_bindings;
size_t shifted_size = (sizeof action->bindings) - (sizeof action->bindings[0]);
SDL_memmove(action->bindings, action->bindings + 1, shifted_size);
@ -133,7 +134,7 @@ static void input_unbind_code_from_action(InputState *input,
/* check every binding to make sure this code is bound */
size_t index = 0;
bool is_bound = false;
for (index = 0; index < SDL_arraysize(action->bindings); ++index) {
for (index = 0; index < (uint64_t)ctx.keybind_slots; ++index) {
Button *binding = &action->bindings[index];
if (binding->source != source)
@ -249,13 +250,21 @@ void input_add_action(InputState *input, char *action_name) {
return;
}
shput(input->action_hash, action_name, (Action) { 0 });
Action new_action = { 0 };
new_action.bindings = ccalloc(ctx.keybind_slots, sizeof *new_action.bindings);
shput(input->action_hash, action_name, new_action);
}
void input_delete_action(InputState *input, char *action_name) {
if (shdel(input->action_hash, action_name) == 0)
ActionHashItem *action = shgetp_null(input->action_hash, action_name);
if (action == NULL) {
log_warn("(%s) Action \"%s\" is not registered.", __func__, action_name);
return;
}
SDL_free(action->value.bindings);
shdel(input->action_hash, action_name);
}
@ -314,5 +323,10 @@ bool input_is_mouse_captured(InputState *input) {
void input_reset_state(InputState *input) {
for (size_t i = 0; i < shlenu(input->action_hash); ++i) {
Action *action = &input->action_hash[i].value;
SDL_free(action->bindings);
}
stbds_shfree(input->action_hash);
}