Created
October 24, 2019 00:21
-
-
Save TheCherry/70678495378b3c8c222dd1e156a0e711 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stddef.h> | |
#include <stdio.h> | |
#include "raylib.h" | |
#include "../extern_libs/cJSON/cJSON.h" | |
#include "../extern_libs/cJSON/cJSON_Utils.h" | |
#include "headers/views.h" | |
#include "headers/errors.h" | |
struct ControllerInput input_mapping[16]; | |
void load_bool(cJSON *object, bool *dst, char *key) { | |
cJSON *input_data; | |
input_data = cJSON_GetObjectItemCaseSensitive(object, key); | |
if (!cJSON_IsBool(input_data) && !cJSON_IsNull(input_data)) throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
if (cJSON_IsNull(input_data)) | |
*dst = false; | |
else | |
*dst = input_data->valueint; | |
} | |
void load_int(cJSON *object, int *dst, char *key) { | |
cJSON *input_data; | |
input_data = cJSON_GetObjectItemCaseSensitive(object, key); | |
if (!cJSON_IsNumber(input_data) && !cJSON_IsNull(input_data)) throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
if (cJSON_IsNull(input_data)) | |
*dst = -1; | |
else | |
*dst = input_data->valueint; | |
} | |
void lj(char *json) { | |
const cJSON *mapping = NULL; | |
cJSON *input = NULL; | |
cJSON *input_data = NULL; | |
cJSON *monitor_json = cJSON_Parse(json); | |
if (monitor_json == NULL) | |
{ | |
// const char *error_ptr = cJSON_GetErrorPtr(); | |
// if (error_ptr != NULL) | |
// { | |
// fprintf(stderr, "Error before: %s\n", error_ptr); | |
// } | |
throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
} | |
mapping = cJSON_GetObjectItemCaseSensitive(monitor_json, "mapping"); | |
if (!cJSON_IsObject(mapping)) throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
for (size_t i = 0; i < 16; i++) | |
{ | |
input = cJSON_GetObjectItemCaseSensitive(mapping, input_mapping[i].id_name); | |
if (!cJSON_IsObject(input)) throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
load_bool(input, &input_mapping[i].is_axis, "is_axis"); | |
load_bool(input, &input_mapping[i].flipped, "flipped"); | |
load_int(input, &input_mapping[i].index, "index"); | |
} | |
} | |
void config_save() { | |
char *json_config_str = NULL; | |
cJSON *root = cJSON_CreateObject(); | |
cJSON *mapping = cJSON_AddObjectToObject(root, "mapping"); | |
for (size_t i = 0; i < 16; i++) { | |
cJSON *input = cJSON_AddObjectToObject(mapping, input_mapping[i].id_name); | |
cJSON_AddBoolToObject(input, "is_axis", input_mapping[i].is_axis); | |
cJSON_AddBoolToObject(input, "flipped", input_mapping[i].flipped); | |
cJSON_AddNumberToObject(input, "index", input_mapping[i].index); | |
} | |
json_config_str = cJSON_Print(root); | |
FILE* fp; | |
int err = fopen_s(&fp, "config.json", "w"); | |
if (err < 0) { | |
throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
view_state = VIEW_EXIT; | |
return; | |
} | |
fprintf(fp, json_config_str); | |
fclose(fp); | |
} | |
#define NewControllerInput(i, id, _axis, _flip, _index, path, _name, x, y) \ | |
do { \ | |
input_mapping[i].id_name = id; \ | |
input_mapping[i].is_axis = _axis; \ | |
input_mapping[i].flipped = _flip; \ | |
input_mapping[i].index = _index; \ | |
input_mapping[i].image = LoadTexture(path); \ | |
input_mapping[i].name = _name; \ | |
input_mapping[i].text_pos = (Vector2) { x, y }; \ | |
} while(0) | |
void init_mapping() { | |
NewControllerInput(0, "A", false, false, -1, "resources/images/controller/a.png", "A Button", 731, 328); | |
NewControllerInput(1, "B", false, false, -1, "resources/images/controller/b.png", "B Button", 731, 390); | |
NewControllerInput(2, "C-U", false, false, -1, "resources/images/controller/c_u.png", "C-UP Button", 731, 85); | |
NewControllerInput(3, "C-R", false, false, -1, "resources/images/controller/c_r.png", "C-RIGHT Button", 731, 145); | |
NewControllerInput(4, "C-D", false, false, -1, "resources/images/controller/c_d.png", "C-DOWN Button", 731, 205); | |
NewControllerInput(5, "C-L", false, false, -1, "resources/images/controller/c_l.png", "C-LEFT Button", 731, 266); | |
NewControllerInput(6, "AN-LR", false, false, -1, "resources/images/controller/analog_lr.png", "Analog Left-Right", 574, 529); | |
NewControllerInput(7, "AN-DU", false, false, -1, "resources/images/controller/analog_du.png", "Analog Down-Up", 574, 590); | |
NewControllerInput(8, "Z", false, false, -1, "resources/images/controller/z.png", "Z Trigger", 166, 590); | |
NewControllerInput(9, "D-U", false, false, -1, "resources/images/controller/digi_u.png", "Digital Up", 7, 158); | |
NewControllerInput(10, "D-R", false, false, -1, "resources/images/controller/digi_r.png", "Digital Right", 7, 345); | |
NewControllerInput(11, "D-D", false, false, -1, "resources/images/controller/digi_d.png", "Digital Down", 7, 283); | |
NewControllerInput(12, "D-L", false, false, -1, "resources/images/controller/digi_l.png", "Digital Left", 7, 220); | |
NewControllerInput(13, "L", false, false, -1, "resources/images/controller/l.png", "L Button", 7, 94); | |
NewControllerInput(14, "R", false, false, -1, "resources/images/controller/r.png", "R Button", 731, 22); | |
NewControllerInput(15, "S", false, false, -1, "resources/images/controller/start.png", "Start", 166, 529); | |
} | |
void load_config() { | |
char *json_config_str; | |
init_mapping(); | |
if (!FileExists("config.json")) { | |
if (!FileExists("config.example.json")) { | |
throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
view_state = VIEW_EXIT; | |
return; | |
} | |
FILE* fp; | |
json_config_str = LoadText("config.example.json"); | |
int err = fopen_s(&fp, "config.json", "w+"); | |
if (err < 0) { | |
throw_error(ERROR_CONFIG_EXAMPLE_NOT_FOUND); | |
view_state = VIEW_EXIT; | |
return; | |
} | |
fprintf(fp, json_config_str); | |
fclose(fp); | |
} else { | |
json_config_str = LoadText("config.json"); | |
} | |
lj(json_config_str); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment