From 901f1dc8c2229d573907c8061f6ff58633ab5052 Mon Sep 17 00:00:00 2001 From: julesgrc0 Date: Sat, 23 Mar 2024 14:52:21 +0100 Subject: [PATCH] save keyboard/joystick state to config --- src/core/config.c | 122 ++++++++++++++++++++++++++++++++++--- src/core/config.h | 19 ++++++ src/core/controls.c | 41 ++++++------- src/core/controls.h | 8 ++- src/screens/game/bridge.c | 4 +- src/screens/game/bridge.h | 3 +- src/screens/game/game.c | 8 +-- src/screens/menu/menu.c | 69 ++++++--------------- src/terrain/block.h | 9 ++- src/terrain/chunk_group.c | 2 +- src/terrain/terrain_data.c | 2 +- 11 files changed, 193 insertions(+), 94 deletions(-) diff --git a/src/core/config.c b/src/core/config.c index d949f44..43c03df 100644 --- a/src/core/config.c +++ b/src/core/config.c @@ -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); @@ -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); @@ -57,7 +56,48 @@ 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 { @@ -65,6 +105,20 @@ w_config *load_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 @@ -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); @@ -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; +} \ No newline at end of file diff --git a/src/core/config.h b/src/core/config.h index a4222b7..c711ba7 100644 --- a/src/core/config.h +++ b/src/core/config.h @@ -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); \ No newline at end of file diff --git a/src/core/controls.c b/src/core/controls.c index 6375644..b3266fb 100644 --- a/src/core/controls.c +++ b/src/core/controls.c @@ -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; } @@ -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 } @@ -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) { diff --git a/src/core/controls.h b/src/core/controls.h index bd8881f..63cad55 100644 --- a/src/core/controls.h +++ b/src/core/controls.h @@ -1,6 +1,7 @@ #pragma once #include "../gui/gui.h" #include "../stdafx.h" +#include "config.h" #pragma pack(push, 1) typedef struct w_controls { @@ -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; }; @@ -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); diff --git a/src/screens/game/bridge.c b/src/screens/game/bridge.c index f2bb402..e3dd72f 100644 --- a/src/screens/game/bridge.c +++ b/src/screens/game/bridge.c @@ -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"); @@ -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; diff --git a/src/screens/game/bridge.h b/src/screens/game/bridge.h index e5f05b9..e415657 100644 --- a/src/screens/game/bridge.h +++ b/src/screens/game/bridge.h @@ -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; @@ -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); diff --git a/src/screens/game/game.c b/src/screens/game/game.c index c98329f..28b1265 100644 --- a/src/screens/game/game.c +++ b/src/screens/game/game.c @@ -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; @@ -38,7 +38,7 @@ 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); @@ -46,7 +46,7 @@ void game_screen(w_state *state) { 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); @@ -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); diff --git a/src/screens/menu/menu.c b/src/screens/menu/menu.c index ec92e21..4aa92d0 100644 --- a/src/screens/menu/menu.c +++ b/src/screens/menu/menu.c @@ -7,17 +7,9 @@ void menu_screen(w_state *state) { bool is_active = true; - Texture block_textures[6] = { - get_texture_by_id(state, "blocks\\grass.png"), - get_texture_by_id(state, "blocks\\dirt.png"), - get_texture_by_id(state, "blocks\\stone.png"), - get_texture_by_id(state, "blocks\\mineral.png"), - get_texture_by_id(state, "blocks\\mineral_or.png"), - get_texture_by_id(state, "blocks\\mineral_iron.png")}; - - w_chunkgroup *grp = create_chunkgroup(CHUNK_GROUP_MID_LEN); - w_chunkview *view = create_chunkview(grp->chunks[0]); - w_camera *camera = create_camera(0, CHUNK_MID_H * CUBE_H); + Texture block_textures[3] = {get_texture_by_id(state, "blocks\\grass.png"), + get_texture_by_id(state, "blocks\\dirt.png"), + get_texture_by_id(state, "blocks\\stone.png")}; w_guicontext *ctx = create_gui(); ctx->font_size = 25; @@ -48,49 +40,31 @@ void menu_screen(w_state *state) { ctx, Vector2Add(title_text->position, VEC(0, title_text->font_size + 10)), (char *)TextFormat("made by @julesgrc0 - %s", WISPY_VERSION), 20, WHITE); - float angle = 0.0; + // TODO: pre-render the cubes to a texture + unsigned int cubes[RENDER_CUBE_COUNT * RENDER_CUBE_COUNT] = {0}; + for (unsigned int y = 0; y < RENDER_CUBE_COUNT; y++) { + for (unsigned int x = 0; x < RENDER_CUBE_COUNT; x++) { + cubes[x + y * RENDER_CUBE_COUNT] = GetRandomValue(0, 2); + } + } while (!WindowShouldClose() && is_active) { - float speed = GetFrameTime() * 0.1f; - angle += speed; - angle = fmodf(angle, 360.f); - -#if defined(WISPY_ANDROID) - camera->position.x += sinf(angle) * 1000.f * speed; - camera->position.y += cosf(angle) * 1000.f * speed; -#else - add_camera_vec(camera, VEC(sinf(angle) * 1000.f * speed, - cosf(angle) * 1000.f * speed)); -#endif - - update_chunkview(view, grp, camera, update_renderblock); - update_chunkview_lighting(view, get_camera_center(camera), - DEFAULT_LIGHT_RADIUS * 0.75); - BeginTextureMode(state->render); ClearBackground(BLACK); DrawRectangleGradientV(0, 0, RENDER_W, RENDER_H, (Color){66, 135, 245, 255}, (Color){142, 184, 250, 255}); -#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX) - begin_camera(camera); -#endif - - for (unsigned int i = 0; i < view->len; i++) { - DrawTexturePro(block_textures[view->blocks[i].block.type - 1], - view->blocks[i].src, -#if defined(WISPY_ANDROID) - get_rect_to_camera(view->blocks[i].dst, camera), -#else - view->blocks[i].dst, -#endif - VEC_ZERO, 0, view->blocks[i].light); + for (unsigned int y = 0; y < RENDER_CUBE_COUNT; y++) { + for (unsigned int x = 0; x < RENDER_CUBE_COUNT; x++) { + DrawTexturePro( + block_textures[ + cubes[x + y * RENDER_CUBE_COUNT] + ], CUBE_SRC_RECT, + RECT((float)x * CUBE_W, (float)y * CUBE_H, CUBE_W, CUBE_H), + VEC_ZERO, 0, WHITE); + } } -#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX) - end_camera(); -#endif - if (update_button(play_button)) { is_active = false; } @@ -112,9 +86,6 @@ void menu_screen(w_state *state) { EndDrawing(); } - destroy_chunkgroup(grp); - destroy_chunkview(view); - destroy_text(credit_text); destroy_text(title_text); destroy_button(play_button); @@ -122,8 +93,6 @@ void menu_screen(w_state *state) { destroy_button(exit_button); destroy_gui(ctx); - destroy_camera(camera); - if (is_active) { state->state = FS_EXIT; } diff --git a/src/terrain/block.h b/src/terrain/block.h index 71ec3b8..92a074c 100644 --- a/src/terrain/block.h +++ b/src/terrain/block.h @@ -1,12 +1,11 @@ #pragma once #include "../stdafx.h" -#define BLOCK_BACKGROUND_MASK 0x80 +#define MAX_BLOCK_VALUE_TYPE 6 +#define BLOCK_IS_BACKGROUND(b) (b > MAX_BLOCK_VALUE_TYPE) +#define BLOCK_SET_BACKGROUND(b) (b + MAX_BLOCK_VALUE_TYPE) +#define BLOCK_TYPE(b) (b % MAX_BLOCK_VALUE_TYPE) -#define BLOCK_IS_BACKGROUND(block_type) (block_type & BLOCK_BACKGROUND_MASK) -#define BLOCK_SET_BACKGROUND(block_type) (block_type | BLOCK_BACKGROUND_MASK) -#define BLOCK_UNSET_BACKGROUND(block_type) (block_type & ~BLOCK_BACKGROUND_MASK) -#define BLOCK_TYPE(block_type) (block_type & ~BLOCK_BACKGROUND_MASK) #define CUBE_SRC_RECT \ (Rectangle) { 0, 0, 8, 8 } diff --git a/src/terrain/chunk_group.c b/src/terrain/chunk_group.c index 8ba8d8a..066b38b 100644 --- a/src/terrain/chunk_group.c +++ b/src/terrain/chunk_group.c @@ -93,7 +93,7 @@ w_block *get_chunkgroup_block(w_chunkgroup *grp, Vector2 position) { return NULL; } - w_chunk *chunk = get_chunkgroup_chunk(grp, position.x / CHUNK_W); + w_chunk *chunk = get_chunkgroup_chunk(grp, (unsigned int)(position.x / CHUNK_W)); if (chunk == NULL) { return NULL; } diff --git a/src/terrain/terrain_data.c b/src/terrain/terrain_data.c index 26835ee..de149cb 100644 --- a/src/terrain/terrain_data.c +++ b/src/terrain/terrain_data.c @@ -107,7 +107,7 @@ bool write_chunk_file(unsigned int position, w_block *blocks) { for (unsigned int i = 0; i < in_size; i++) { if (blocks[i].is_background) { - in_buffer[i] = BLOCK_SET_BACKGROUND(in_buffer[i]); + in_buffer[i] = BLOCK_SET_BACKGROUND(blocks[i].type); } else { in_buffer[i] = blocks[i].type; }