Skip to content

Commit

Permalink
json support for config
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 26, 2024
1 parent c2429cf commit 6dc87ca
Show file tree
Hide file tree
Showing 11 changed files with 87 additions and 32 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
/Debug
/wispy
/.vscode/settings.json
/.vscode/c_cpp_properties.json
/out
/android/keys/*.jks
/android/keys/*.b64
Expand Down
6 changes: 4 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ add_custom_target(pack_assets

add_subdirectory(lib/raylib)
add_subdirectory(lib/zlib)
add_subdirectory(lib/json-c)

include_directories(
${CMAKE_SOURCE_DIR}/lib/raylib/src
${CMAKE_SOURCE_DIR}/lib/zlib
${CMAKE_SOURCE_DIR}/lib/json-c
)

file(GLOB_RECURSE SOURCES "src/*.c")
Expand All @@ -66,10 +68,10 @@ if(ANDROID)
target_link_libraries(${PROJECT_NAME} raylib zlibstatic android log)
elseif(UNIX)
add_executable(${PROJECT_NAME} ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE raylib zlibstatic pthread)
target_link_libraries(${PROJECT_NAME} PRIVATE raylib zlibstatic json-c pthread)
elseif(MSVC)
add_executable(${PROJECT_NAME} WIN32 ${SOURCES})
target_link_libraries(${PROJECT_NAME} PRIVATE raylib zlibstatic)
target_link_libraries(${PROJECT_NAME} PRIVATE raylib zlibstatic json-c)
else()
message(FATAL_ERROR "Unsupported platform")
endif()
Expand Down
1 change: 0 additions & 1 deletion json-c
Submodule json-c deleted from 401249
56 changes: 49 additions & 7 deletions src/core/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,29 @@ w_config *load_config() {
strcat(config_path, CONFIG_NAME);

if (FileExists(config_path)) {
unsigned int size = 0;
char *data = LoadFileData(config_path, &size);
cfg = (w_config *)memmove(cfg, data, sizeof(w_config));
char *data = LoadFileText(config_path);
if (data == NULL) {
free(config_path);
free(cfg);
return NULL;
}
json_object *root = json_tokener_parse(data);
if (root == NULL) {
free(data);
free(config_path);
free(cfg);
return NULL;
}

sfree(data);
load_config_uint(root, cfg, fullscreen);
load_config_uint(root, cfg, msaa4x);
load_config_uint(root, cfg, vsync);
load_config_uint(root, cfg, width);
load_config_uint(root, cfg, height);
load_config_uint(root, cfg, max_fps);

json_object_put(root);
free(data);
} else {
#endif // !__ANDROID__

Expand All @@ -46,17 +64,41 @@ w_config *load_config() {
}

void save_config(w_config *config) {
#if !defined(_DEBUG) && !defined(__ANDROID__)
// #if !defined(_DEBUG) && !defined(__ANDROID__)
#if 1
char *config_path = malloc(MAX_PATH * 2);
if (config_path == NULL)
return;

config_path[0] = 0;

strcat(config_path, GetApplicationDirectory());
strcat(config_path, CONFIG_NAME);

SaveFileData(config_path, config, sizeof(w_config));
json_object *root = json_object_new_object();
if (root == NULL) {
free(config_path);
return;
}

json_object *js_fullscreen = json_object_new_uint64(config->fullscreen);
json_object *js_msaa4x = json_object_new_uint64(config->msaa4x);
json_object *js_vsync = json_object_new_uint64(config->vsync);
json_object *js_width = json_object_new_uint64(config->width);
json_object *js_height = json_object_new_uint64(config->height);
json_object *js_max_fps = json_object_new_uint64(config->max_fps);

json_object_object_add(root, "fullscreen", js_fullscreen);
json_object_object_add(root, "msaa4x", js_msaa4x);
json_object_object_add(root, "vsync", js_vsync);
json_object_object_add(root, "width", js_width);
json_object_object_add(root, "height", js_height);
json_object_object_add(root, "max_fps", js_max_fps);

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

free(config_path);
#endif // !_DEBUG
}
10 changes: 8 additions & 2 deletions src/core/config.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
#pragma once
#include "../stdafx.h"
#define CONFIG_NAME "config.dat"

#define CONFIG_NAME "config.json"

#define load_config_uint(jsobj, config, name) \
json_object *js_##name = json_object_object_get(jsobj, #name); \
config->##name = (unsigned int)json_object_get_uint64(js_##name); \
json_object_put(js_##name);

typedef struct w_config {
unsigned int fullscreen : 1;
Expand All @@ -14,4 +20,4 @@ typedef struct w_config {
} w_config;

w_config *load_config();
void save_config(w_config *config);
void save_config(w_config *config);
8 changes: 3 additions & 5 deletions src/entities/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,7 @@ w_player *create_player(unsigned int x) {
memset(player, 0, sizeof(w_player));
player->jump = PJ_FALL;
player->src = PLAYER_SRC_RECT;
player->dst = RECT((RENDER_W - CUBE_W) / 2.f, (RENDER_H - CUBE_H * 2) / 2.f,
CUBE_W * 0.7f, // TODO: adjust player size
CUBE_H * 2);
player->dst = RECT(0, 0, CUBE_W * 0.7f, CUBE_H * 1.8f);
player->position = VEC((float)((x + CHUNK_GROUP_MID_LEN) * FULL_CHUNK_W),
CHUNK_MID_H * CUBE_H);
return player;
Expand Down Expand Up @@ -48,15 +46,15 @@ void update_player_input(w_player *player, w_controls *ctrl) {
if (player->duration > 0) {
player->duration--;
player->jump = player->duration > 0 ? PJ_JUMP : PJ_FALL;
player->velocity.y -= (MAX_PLAYER_VELOCITY_Y / 2.F * player->duration);
player->velocity.y -= player->duration * MAX_PLAYER_VELOCITY_Y;
if (player->duration <= 0) {
player->delay = (1 / PHYSICS_TICK) * 0.15f;
}
}

if (ctrl->jump && player->jump == PJ_GROUD && player->duration <= 0 &&
player->delay <= 0) {
player->duration = (1 / PHYSICS_TICK) / 4;
player->duration = PLAYER_JUMP_DURATION;
player->jump = PJ_JUMP;
}

Expand Down
5 changes: 3 additions & 2 deletions src/entities/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
#define PLAYER_ANIMTATION_TIME(x) ((x * (1 / PHYSICS_TICK)) / 30)
#define PLAYER_ANIMATION_IDLE PLAYER_ANIMTATION_TIME(12)
#define PLAYER_ANIMATION_WALK PLAYER_ANIMTATION_TIME(4)
#define PLAYER_ANIMATION_FALL PLAYER_ANIMTATION_TIME(2)

#define PLAYER_SPEED 250.f
#define PLAYER_JUMP 3.f
#define PLAYER_JUMP 2.f
#define PLAYER_JUMP_DURATION PLAYER_ANIMTATION_TIME(8)
#define PLAYER_JUMP_DELAY PLAYER_ANIMTATION_TIME(4)
#define PLAYER_FRICTION 0.8f

#define MAX_PLAYER_VELOCITY_X 3.f
Expand Down
18 changes: 8 additions & 10 deletions src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ void game_screen(w_state *state) {

float dt = GetFrameTime();
float speed = dt * PLAYER_SPEED;
if (dt < MIN_FRAME_TIME) {
smooth_camera(td->camera, td->camera_target, speed);
smooth_vec(&target_player, td->player->position,
Vector2Distance(td->player->position, target_player) * speed);
} else {
set_camera_vec(td->camera, td->camera_target);
target_player = td->player->position;
}

#ifdef _WIN32
if (TryEnterCriticalSection(&td->chunk_view->csec))
Expand All @@ -91,16 +99,6 @@ void game_screen(w_state *state) {
(Color){66, 135, 245, 255},
(Color){142, 184, 250, 255});

if (dt < MIN_FRAME_TIME) {
smooth_camera(td->camera, td->camera_target, speed);
smooth_vec(&target_player, td->player->position,
Vector2Distance(td->player->position, target_player) *
speed);
} else {
set_camera_vec(td->camera, td->camera_target);
target_player = td->player->position;
}

begin_camera(td->camera);
for (unsigned int i = 0; i < td->chunk_view->len; i++) {
DrawTexturePro(block_textures[td->chunk_view->blocks[i].block.type - 1],
Expand Down
6 changes: 6 additions & 0 deletions src/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
#include <zlib.h>
///

/// JSON-C
#ifndef __ANDROID__
#include <json.h>
#endif
///

/// RAYLIB
#include <raylib.h>
#include <raymath.h>
Expand Down
6 changes: 4 additions & 2 deletions src/terrain/chunk_view.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ bool update_chunkview(w_chunkview *chunk_view, w_chunkgroup *grp,
}

unsigned int position = (unsigned int)Clamp(
floorf((view.x - (view.width / 2.f)) / (float)FULL_CHUNK_W), 0, UINT_MAX);
floorf((view.x - (view.width / 2.f)) / (float)FULL_CHUNK_W), 0,
(float)UINT_MAX);
if (position != chunk_view->target->position) {
int group_move = need_chunkgroup_update(grp, position);
if (group_move > 0) {
Expand All @@ -108,7 +109,8 @@ bool update_chunkview(w_chunkview *chunk_view, w_chunkgroup *grp,
}

unsigned int position_next = (unsigned int)Clamp(
roundf((view.x + (view.width / 2.f)) / (float)FULL_CHUNK_W), 0, UINT_MAX);
roundf((view.x + (view.width / 2.f)) / (float)FULL_CHUNK_W), 0,
(float)UINT_MAX);
if (position_next != position) {
chunk_view->next = get_chunkgroup_chunk(grp, position_next);
} else {
Expand Down

0 comments on commit 6dc87ca

Please sign in to comment.