From 3e75a8e3fe937f6d003c80f2d495f3c09a7cd9cb Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Mon, 3 Nov 2014 23:27:47 -0800 Subject: [PATCH] GBA Input: Add input map loading --- src/gba/gba-input.c | 38 ++++++++++++++++++++++++++++++++++++++ src/gba/gba-input.h | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/src/gba/gba-input.c b/src/gba/gba-input.c index efa1faf2d..31d0feb26 100644 --- a/src/gba/gba-input.c +++ b/src/gba/gba-input.c @@ -1,10 +1,35 @@ #include "gba-input.h" +#include "util/configuration.h" + +#define SECTION_NAME_MAX 128 +#define KEY_NAME_MAX 32 + struct GBAInputMapImpl { int* map; uint32_t type; }; +static void _loadKey(struct GBAInputMap* map, uint32_t type, const struct Configuration* config, enum GBAKey key, const char* keyName) { + char sectionName[SECTION_NAME_MAX]; + snprintf(sectionName, SECTION_NAME_MAX, "input.%c%c%c%c", type >> 24, type >> 16, type >> 8, type); + sectionName[SECTION_NAME_MAX - 1] = '\0'; + + char keyKey[KEY_NAME_MAX]; + snprintf(keyKey, KEY_NAME_MAX, "key%s", keyName); + + const char* value = ConfigurationGetValue(config, sectionName, keyKey); + if (!value) { + return; + } + char* end; + long intValue = strtol(value, &end, 10); + if (*end) { + return; + } + GBAInputBindKey(map, type, intValue, key); +} + void GBAInputMapInit(struct GBAInputMap* map) { map->maps = 0; map->numMaps = 0; @@ -83,3 +108,16 @@ void GBAInputBindKey(struct GBAInputMap* map, uint32_t type, int key, enum GBAKe } impl->map[input] = key; } + +void GBAInputMapLoad(struct GBAInputMap* map, uint32_t type, const struct Configuration* config) { + _loadKey(map, type, config, GBA_KEY_A, "A"); + _loadKey(map, type, config, GBA_KEY_B, "B"); + _loadKey(map, type, config, GBA_KEY_L, "L"); + _loadKey(map, type, config, GBA_KEY_R, "R"); + _loadKey(map, type, config, GBA_KEY_START, "Start"); + _loadKey(map, type, config, GBA_KEY_SELECT, "Select"); + _loadKey(map, type, config, GBA_KEY_UP, "Up"); + _loadKey(map, type, config, GBA_KEY_DOWN, "Down"); + _loadKey(map, type, config, GBA_KEY_LEFT, "Left"); + _loadKey(map, type, config, GBA_KEY_RIGHT, "Right"); +} diff --git a/src/gba/gba-input.h b/src/gba/gba-input.h index 684598db2..3fa3b9bd9 100644 --- a/src/gba/gba-input.h +++ b/src/gba/gba-input.h @@ -3,6 +3,8 @@ #include "gba.h" +struct Configuration; + struct GBAInputMap { struct GBAInputMapImpl* maps; size_t numMaps; @@ -14,4 +16,6 @@ void GBAInputMapDeinit(struct GBAInputMap*); enum GBAKey GBAInputMapKey(struct GBAInputMap*, uint32_t type, int key); void GBAInputBindKey(struct GBAInputMap*, uint32_t type, int key, enum GBAKey input); +void GBAInputMapLoad(struct GBAInputMap*, uint32_t type, const struct Configuration*); + #endif