Skip to content

Commit

Permalink
save keyboard/joystick state to config
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Mar 23, 2024
1 parent d417097 commit 901f1dc
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 94 deletions.
122 changes: 115 additions & 7 deletions src/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ w_config *load_config() {
free(cfg);
return NULL;
}

json_object *root = json_tokener_parse(config_data);
if (root == NULL) {
free(config_path);
Expand All @@ -36,16 +36,15 @@ w_config *load_config() {
json_object *js_fullscreen = json_object_object_get(root, "fullscreen");
cfg->fullscreen = (unsigned int)json_object_get_uint64(js_fullscreen);
json_object_put(js_fullscreen);


json_object *js_msaa4x = json_object_object_get(root, "msaa4x");
cfg->msaa4x = (unsigned int)json_object_get_uint64(js_msaa4x);
json_object_put(js_msaa4x);

json_object *js_vsync = json_object_object_get(root, "vsync");
cfg->vsync = (unsigned int)json_object_get_uint64(js_vsync);
json_object_put(js_vsync);

json_object *js_width = json_object_object_get(root, "width");
cfg->width = (unsigned int)json_object_get_uint64(js_width);
json_object_put(js_width);
Expand All @@ -57,14 +56,69 @@ w_config *load_config() {
json_object *js_max_fps = json_object_object_get(root, "max_fps");
cfg->max_fps = (unsigned int)json_object_get_uint64(js_max_fps);
json_object_put(js_max_fps);


#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX)
json_object *js_jump_key = json_object_object_get(root, "jump_key");
cfg->jump_key = (int)json_object_get_int(js_jump_key);
json_object_put(js_jump_key);

json_object *js_left_key = json_object_object_get(root, "left_key");
cfg->left_key = (int)json_object_get_int(js_left_key);
json_object_put(js_left_key);

json_object *js_right_key = json_object_object_get(root, "right_key");
cfg->right_key = (int)json_object_get_int(js_right_key);
json_object_put(js_right_key);

json_object *js_inventory_key =
json_object_object_get(root, "inventory_key");
cfg->inventory_key = (int)json_object_get_int(js_inventory_key);
json_object_put(js_inventory_key);

#elif defined(WISPY_ANDROID)
json_object *js_joystick_object = json_object_object_get(root, "joystick");
cfg->joystick_position = json_object_get_vec2(js_joystick_object);
json_object_put(js_joystick_object);

json_object *js_break_object = json_object_object_get(root, "break");
cfg->break_position = json_object_get_vec2(js_break_object);
json_object_put(js_break_object);

json_object *js_place_object = json_object_object_get(root, "place");
cfg->place_position = json_object_get_vec2(js_place_object);
json_object_put(js_place_object);

json_object *js_jump_object = json_object_object_get(root, "jump");
cfg->jump_position = json_object_get_vec2(js_jump_object);
json_object_put(js_jump_object);

json_object *js_inventory_object =
json_object_object_get(root, "inventory");
cfg->inventory_position = json_object_get_vec2(js_inventory_object);
json_object_put(js_inventory_object);
#endif

json_object_put(root);
UnloadFileText(config_data);
} else {
memset(cfg, 0, sizeof(w_config));
cfg->fullscreen = 1;
cfg->msaa4x = 1;

#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX)
cfg->jump_key = KEY_SPACE;
cfg->left_key = KEY_LEFT;
cfg->right_key = KEY_RIGHT;
cfg->inventory_key = KEY_LEFT_ALT;
#elif defined(WISPY_ANDROID)
cfg->joystick_position = VEC(PERCENT_W(0.25), PERCENT_H(0.95));
cfg->break_position = VEC(RENDER_W - PERCENT_W(0.05), PERCENT_H(0.75));
cfg->jump_position = VEC(PERCENT_W(0.85), RENDER_H - PERCENT_H(0.05));

cfg->place_position = {0};
cfg->inventory_position = {0};
#endif

#if defined(WISPY_ANDROID)
cfg->vsync = 1;
#endif
Expand All @@ -90,7 +144,7 @@ void save_config(w_config *config) {
strcat(config_path, GetAndroidApp()->activity->internalDataPath);
strcat(config_path, "/");
#else
strcat(config_path, GetApplicationDirectory());
strcat(config_path, GetApplicationDirectory());
#endif
strcat(config_path, CONFIG_NAME);

Expand Down Expand Up @@ -118,10 +172,64 @@ void save_config(w_config *config) {
json_object *js_max_fps = json_object_new_uint64(config->max_fps);
json_object_object_add(root, "max_fps", js_max_fps);

#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX)
json_object *js_jump_key = json_object_new_int(config->jump_key);
json_object_object_add(root, "jump_key", js_jump_key);

json_object *js_left_key = json_object_new_int(config->left_key);
json_object_object_add(root, "left_key", js_left_key);

json_object *js_right_key = json_object_new_int(config->right_key);
json_object_object_add(root, "right_key", js_right_key);

json_object *js_inventory_key = json_object_new_int(config->inventory_key);
json_object_object_add(root, "inventory_key", js_inventory_key);

#elif defined(WISPY_ANDROID)
json_object *js_joystick_object =
json_object_new_vec2(config->joystick_position);
json_object_object_add(root, "joystick", js_joystick_object);

json_object *js_break_object = json_object_new_vec2(config->break_position);
json_object_object_add(root, "break", js_break_object);

json_object *js_place_object = json_object_new_vec2(config->place_position);
json_object_object_add(root, "place", js_place_object);

json_object *js_jump_object = json_object_new_vec2(config->jump_position);
json_object_object_add(root, "jump", js_jump_object);

json_object *js_inventory_object =
json_object_new_vec2(config->inventory_position);
json_object_object_add(root, "inventory", js_inventory_object);
#endif

const char *data =
json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY_TAB);
json_object_to_json_string_ext(root, JSON_C_TO_STRING_PRETTY);
SaveFileText(config_path, (char *)data);

json_object_put(root);
free(config_path);
}

Vector2 json_object_get_vec2(json_object *obj) {
Vector2 vec = {0};
json_object *obj_x = json_object_object_get(obj, "x");
vec.x = (float)json_object_get_double(obj_x);
json_object_put(obj_x);

json_object *obj_y = json_object_object_get(obj, "y");
vec.y = (float)json_object_get_double(obj_y);
json_object_put(obj_y);

return vec;
}

json_object *json_object_new_vec2(Vector2 vec) {
json_object *obj = json_object_new_object();
json_object *obj_x = json_object_new_double(vec.x);
json_object_object_add(obj, "x", obj_x);
json_object *obj_y = json_object_new_double(vec.y);
json_object_object_add(obj, "y", obj_y);
return obj;
}
19 changes: 19 additions & 0 deletions src/core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,26 @@ typedef struct w_config {

unsigned int max_fps : 10;

#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX)
int jump_key;
int left_key;
int right_key;
int inventory_key;
#elif defined(WISPY_ANDROID)
Vector2 joystick_position;

Vector2 break_position;
Vector2 place_position;

Vector2 jump_position;

Vector2 inventory_position;
#endif

} w_config;

w_config *load_config();
void save_config(w_config *config);

Vector2 json_object_get_vec2(json_object * obj);
json_object *json_object_new_vec2(Vector2 vec);
41 changes: 19 additions & 22 deletions src/core/controls.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "controls.h"

w_controls *create_controls() {
w_controls *create_controls(w_config *cfg) {
w_controls *kb = (w_controls *)malloc(sizeof(w_controls));
if (kb == NULL)
return NULL;
memset(kb, 0, sizeof(w_controls));
kb->cfg = cfg;
return kb;
}

Expand All @@ -21,13 +22,10 @@ void update_controls(w_controls *kb) {
kb->right = kb->joystick.x > 0.5;
}
#else
kb->left = IsKeyDown(KEY_LEFT);
kb->right = IsKeyDown(KEY_RIGHT);
kb->up = IsKeyDown(KEY_UP);
kb->down = IsKeyDown(KEY_DOWN);
kb->jump = IsKeyDown(KEY_SPACE);
kb->inventory = IsKeyDown(KEY_RIGHT_ALT) || IsKeyDown(KEY_LEFT_SHIFT);
kb->shift = IsKeyDown(KEY_RIGHT_SHIFT) || IsKeyDown(KEY_LEFT_SHIFT);
kb->left = IsKeyDown(kb->cfg->left_key);
kb->right = IsKeyDown(kb->cfg->right_key);
kb->jump = IsKeyDown(kb->cfg->jump_key);
kb->inventory = IsKeyDown(kb->cfg->inventory_key);
#endif
}

Expand All @@ -46,21 +44,20 @@ bool check_collision_touch(Vector2 position, float size) {
return !Vector2Equals(get_collision_touch(position, size), VEC_ZERO);
}

Vector2 get_nearest_touch(Vector2 position)
{
const int touch_count = GetTouchPointCount();
Vector2 nearest = VEC_ZERO;
float distance = 0;
for (int i = 0; i < touch_count; i++) {
Vector2 point =
VEC(FORMAT_W(GetTouchPosition(i).x), FORMAT_H(GetTouchPosition(i).y));
float d = Vector2Distance(point, position);
if (d < distance || distance == 0) {
distance = d;
nearest = point;
}
Vector2 get_nearest_touch(Vector2 position) {
const int touch_count = GetTouchPointCount();
Vector2 nearest = VEC_ZERO;
float distance = 0;
for (int i = 0; i < touch_count; i++) {
Vector2 point =
VEC(FORMAT_W(GetTouchPosition(i).x), FORMAT_H(GetTouchPosition(i).y));
float d = Vector2Distance(point, position);
if (d < distance || distance == 0) {
distance = d;
nearest = point;
}
return nearest;
}
return nearest;
}

Vector2 get_collision_touch(Vector2 position, float size) {
Expand Down
8 changes: 7 additions & 1 deletion src/core/controls.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once
#include "../gui/gui.h"
#include "../stdafx.h"
#include "config.h"

#pragma pack(push, 1)
typedef struct w_controls {
Expand All @@ -10,9 +11,12 @@ typedef struct w_controls {
unsigned int jump : 1;
unsigned int left : 1;
unsigned int right : 1;

// -- NOT USED --
unsigned int up : 1;
unsigned int down : 1;
unsigned int shift : 1;
// --------------
};
uint8_t key;
};
Expand All @@ -22,10 +26,12 @@ typedef struct w_controls {
bool is_jumping;
bool is_breaking;
#endif

w_config *cfg;
} w_controls;
#pragma pack(pop)

w_controls *create_controls();
w_controls *create_controls(w_config *cfg);
void destroy_controls(w_controls *kb);

void update_controls(w_controls *kb);
Expand Down
4 changes: 2 additions & 2 deletions src/screens/game/bridge.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "bridge.h"

w_bridge *create_bridge() {
w_bridge *create_bridge(w_config* cfg) {
w_bridge *td = malloc(sizeof(w_bridge));
if (td == NULL) {
LOG("failed to allocate memory for bridge");
Expand Down Expand Up @@ -32,7 +32,7 @@ w_bridge *create_bridge() {
td->camera->target_position = get_camera_vec(td->camera);
#endif

td->ctrl = create_controls();
td->ctrl = create_controls(cfg);
if (td->ctrl == NULL) {
destroy_bridge(td);
return NULL;
Expand Down
3 changes: 2 additions & 1 deletion src/screens/game/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../../core/utils/camera.h"
#include "../../core/controls.h"
#include "../../core/state.h"
#include "../../core/config.h"

typedef struct w_bridge {
bool is_active;
Expand All @@ -29,7 +30,7 @@ typedef struct w_bridge {

} w_bridge;

w_bridge *create_bridge();
w_bridge *create_bridge(w_config* cfg);
void destroy_bridge(w_bridge *td);

void physics_update_bridge(w_bridge *td);
Expand Down
8 changes: 4 additions & 4 deletions src/screens/game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ void game_screen(w_state *state) {
get_texture_by_id(state, "player\\player_walk2.png"),
};

w_bridge *td = create_bridge();
w_bridge *td = create_bridge(state->config);
if (td == NULL)
return;

Expand All @@ -38,15 +38,15 @@ void game_screen(w_state *state) {
}

w_guijoystick *js = create_joystick(
ctx, VEC(PERCENT_W(0.25), PERCENT_H(0.95)), PERCENT_W(0.1));
ctx, state->config->joysitck_position, PERCENT_W(0.1));
if (js == NULL) {
destroy_bridge(td);
destroy_blockbreaker(bb);
destroy_gui(ctx);
return;
}

w_guiiconbutton *break_button = create_iconbutton(ctx, VEC(RENDER_W - PERCENT_W(0.05), PERCENT_H(0.75)),
w_guiiconbutton *break_button = create_iconbutton(ctx, cfg->break_position,
PERCENT_W(0.05), break_icon);
if (break_button == NULL) {
destroy_bridge(td);
Expand All @@ -57,7 +57,7 @@ void game_screen(w_state *state) {
return;
}

w_guiiconbutton *jump_button = create_iconbutton(ctx, VEC(PERCENT_W(0.85), RENDER_H - PERCENT_H(0.05)),
w_guiiconbutton *jump_button = create_iconbutton(ctx, cfg->jump_position,
PERCENT_W(0.05), player_textures[3]);
if (jump_button == NULL) {
destroy_bridge(td);
Expand Down
Loading

0 comments on commit 901f1dc

Please sign in to comment.