Skip to content

Commit

Permalink
WIP: configurable input mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
xtreme8000 committed Jul 5, 2023
1 parent 511e695 commit 8304df7
Show file tree
Hide file tree
Showing 21 changed files with 501 additions and 227 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ add_executable(cavex
source/stack.c
source/util.c
source/world.c
source/config.c

source/lodepng/lodepng.c

source/parson/parson.c
)

target_compile_definitions(cavex PRIVATE PLATFORM_PC CGLM_ALL_UNALIGNED)
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ include $(DEVKITPPC)/wii_rules
#---------------------------------------------------------------------------------
TARGET := $(notdir $(CURDIR))
BUILD := build
SOURCES := source source/block source/graphics source/network source/game source/game/gui source/platform source/item source/cNBT source/lodepng
SOURCES := source source/block source/graphics source/network source/game source/game/gui source/platform source/item source/cNBT source/lodepng source/parson
DATA :=
TEXTURES := textures
INCLUDES :=
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ You need to download these libraries yourself and place their source files to th
| [LodePNG](https://github.com/lvandeve/lodepng) | `lodepng.h` and `lodepng.c` | `source/lodepng/` |
| [cglm](https://github.com/recp/cglm) | `include/cglm/` | `source/cglm/` |
| [cNBT](https://github.com/chmod222/cNBT) | `buffer.c`, `buffer.h`, `list.h`, `nbt_loading.c`, `nbt_parsing.c`, `nbt_treeops.c`, `nbt_util.c` and `nbt.h` | `source/cNBT/` |
| [parson](https://github.com/kgabis/parson) | `parson.h` and `parson.c` | `source/parson/` |
| [M*LIB](https://github.com/P-p-H-d/mlib) | any root `*.h` | compiler include path |

### Wii
Expand Down Expand Up @@ -72,6 +73,7 @@ cavex
│ ├── world
│ └── ...
├── boot.dol
├── config.json
├── icon.png
└── meta.xml
```
Expand All @@ -89,4 +91,4 @@ cmake ..
make
```

Please also copy the fragment and vertex shaders from `resources/` next to your `./cavex` executable.
Please also copy the fragment and vertex shaders from `resources/` next to your `assets/` directory.
20 changes: 20 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"paths": {
"texturepack": "assets",
"worlds": "saves"
},
"input": {
"player_forward": [0, 200],
"player_backward": [1, 201],
"player_left": [2, 202],
"player_right": [3, 203],
"player_jump": [5, 205],
"player_sneak": [204],
"item_action_left": [6, 100, 210],
"item_action_right": [7, 101, 211],
"scroll_left": [9, 208],
"scroll_right": [8, 209],
"inventory": [4, 206],
"open_menu": [10, 214]
}
}
20 changes: 20 additions & 0 deletions config_pc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"paths": {
"texturepack": "assets",
"worlds": "saves"
},
"input": {
"player_forward": [87],
"player_backward": [83],
"player_left": [65],
"player_right": [68],
"player_jump": [32],
"player_sneak": [340],
"item_action_left": [1000],
"item_action_right": [1001],
"scroll_left": [],
"scroll_right": [],
"inventory": [69],
"open_menu": [257]
}
}
50 changes: 50 additions & 0 deletions source/config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#include <assert.h>

#include "config.h"

bool config_create(struct config* c, const char* filename) {
assert(c && filename);
c->root = json_parse_file(filename);

if(!c->root)
return false;

if(json_value_get_type(c->root) != JSONObject) {
config_destroy(c);
return false;
}

return true;
}

const char* config_read_string(struct config* c, const char* key,
const char* fallback) {
assert(c && key);
// TODO: only give out copy
const char* res = json_object_dotget_string(json_object(c->root), key);
return res ? res : fallback;
}

bool config_read_int_array(struct config* c, const char* key, int* dest,
size_t* length) {
assert(c && key && dest);

JSON_Array* entry = json_object_dotget_array(json_object(c->root), key);

if(!entry)
return false;

*length = *length < json_array_get_count(entry) ?
*length :
json_array_get_count(entry);

for(size_t k = 0; k < *length; k++)
dest[k] = json_array_get_number(entry, k);

return true;
}

void config_destroy(struct config* c) {
assert(c && c->root);
json_value_free(c->root);
}
19 changes: 19 additions & 0 deletions source/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef CONFIG_H
#define CONFIG_H

#include <stdbool.h>

#include "parson/parson.h"

struct config {
JSON_Value* root;
};

bool config_create(struct config* c, const char* filename);
const char* config_read_string(struct config* c, const char* key,
const char* fallback);
bool config_read_int_array(struct config* c, const char* key, int* dest,
size_t* length);
void config_destroy(struct config* c);

#endif
16 changes: 9 additions & 7 deletions source/game/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void camera_ray_pick(struct world* w, float gx0, float gy0, float gz0,
}
}

void camera_update(struct camera* c, float dt) {
void camera_physics(struct camera* c, float dt) {
assert(c);

float jdx, jdy;
Expand Down Expand Up @@ -136,13 +136,11 @@ void camera_update(struct camera* c, float dt) {
acc_z -= cos(c->rx) * sin(c->ry) * speed_c;
}

if(input_held(IB_JUMP)) {
if(input_held(IB_JUMP))
acc_y += speed_c;
}

if(input_held(IB_INVENTORY)) {
if(input_held(IB_SNEAK))
acc_y -= speed_c;
}

c->controller.vx += acc_x * dt;
c->controller.vy += acc_y * dt;
Expand Down Expand Up @@ -179,14 +177,18 @@ void camera_update(struct camera* c, float dt) {
}

c->ry = glm_clamp(c->ry, glm_rad(0.5F), GLM_PI - glm_rad(0.5F));
}

void camera_update(struct camera* c) {
assert(c);

glm_perspective(glm_rad(gstate.config.fov),
(float)gfx_width() / (float)gfx_height(), 0.1F,
gstate.config.render_distance, c->projection);

glm_lookat((vec3) {c->x, c->y, c->z},
(vec3) {c->x + sin(c->rx) * sin(c->ry), c->y + cos(c->ry),
c->z + cos(c->rx) * sin(c->ry)},
(vec3) {c->x + sinf(c->rx) * sinf(c->ry), c->y + cosf(c->ry),
c->z + cosf(c->rx) * sinf(c->ry)},
(vec3) {0, 1, 0}, c->view);

mat4 view_proj;
Expand Down
3 changes: 2 additions & 1 deletion source/game/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ struct camera_ray_result {
void camera_ray_pick(struct world* w, float gx0, float gy0, float gz0,
float gx1, float gy1, float gz1,
struct camera_ray_result* res);
void camera_update(struct camera* c, float dt);
void camera_physics(struct camera* c, float dt);
void camera_update(struct camera* c);

#endif
2 changes: 2 additions & 0 deletions source/game/game_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <stdbool.h>
#include <stddef.h>

#include "../config.h"
#include "../item/inventory.h"
#include "../platform/time.h"
#include "../world.h"
Expand All @@ -37,6 +38,7 @@

struct game_state {
sig_atomic_t quit;
struct config config_user;
struct {
float dt, fps;
float dt_gpu, dt_vsync;
Expand Down
20 changes: 9 additions & 11 deletions source/game/gui/screen_ingame.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@

#include <malloc.h>

static void screen_ingame_render3D(struct screen* s, mat4 view) {
static void screen_ingame_reset(struct screen* s, int width, int height) {
input_joystick_absolute(false);
}

void screen_ingame_render3D(struct screen* s, mat4 view) {
if(gstate.world_loaded && gstate.camera_hit.hit) {
struct block_data blk
= world_get_block(&gstate.world, gstate.camera_hit.x,
Expand Down Expand Up @@ -106,6 +110,8 @@ static void screen_ingame_render3D(struct screen* s, mat4 view) {
}

static void screen_ingame_update(struct screen* s, float dt) {
camera_physics(&gstate.camera, dt);

if(gstate.camera_hit.hit && input_pressed(IB_ACTION2)
&& !gstate.digging.active) {
struct item_data item;
Expand Down Expand Up @@ -295,16 +301,8 @@ static void screen_ingame_render2D(struct screen* s, int width, int height) {
}
icon_offset += gutil_control_icon(icon_offset, CONTROL_HOME, "Save & quit");

// draw inventory
/*gfx_bind_texture(TEXTURE_GUI);
gutil_texquad((width - 176 * 2) / 2, (height - 167 * 2) / 2, 176 / 2, 138,
176 / 2, 80, 176 * 2, 80 * 2);
gutil_texquad_rt((width - 176 * 2) / 2, (height - 167 * 2) / 2 + 80 * 2,
352 / 2, 0, 85 / 2, 176, 176 * 2, 85 * 2);*/

gfx_bind_texture(TEXTURE_GUI2);

// draw hotbar
gfx_bind_texture(TEXTURE_GUI2);
gutil_texquad((width - 182 * 2) / 2, height - 32 * 8 / 5 - 22 * 2, 0, 0,
182, 22, 182 * 2, 22 * 2);

Expand Down Expand Up @@ -347,7 +345,7 @@ static void screen_ingame_render2D(struct screen* s, int width, int height) {
}

struct screen screen_ingame = {
.reset = NULL,
.reset = screen_ingame_reset,
.update = screen_ingame_update,
.render2D = screen_ingame_render2D,
.render3D = screen_ingame_render3D,
Expand Down
7 changes: 6 additions & 1 deletion source/game/gui/screen_load_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
#include "../../graphics/gui_util.h"
#include "../../network/server_local.h"
#include "../../platform/gfx.h"
#include "../../platform/input.h"
#include "../game_state.h"

static void screen_lworld_reset(struct screen* s, int width, int height) {
input_joystick_absolute(false);
}

static void screen_lworld_update(struct screen* s, float dt) {
if(gstate.world_loaded)
screen_set(&screen_ingame);
Expand Down Expand Up @@ -53,7 +58,7 @@ static void screen_lworld_render2D(struct screen* s, int width, int height) {
}

struct screen screen_load_world = {
.reset = NULL,
.reset = screen_lworld_reset,
.update = screen_lworld_update,
.render2D = screen_lworld_render2D,
.render3D = NULL,
Expand Down
24 changes: 14 additions & 10 deletions source/game/gui/screen_select_world.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,21 @@ static int side_padding = 4;
struct world_option {
string_t name;
string_t directory;
string_t path;
int64_t last_access;
int64_t byte_size;
};

static void screen_sworld_reset(struct screen* s, int width, int height) {
input_joystick_absolute(true);

if(worlds) {
while(!stack_empty(worlds)) {
struct world_option opt;
stack_pop(worlds, &opt);
string_clear(opt.name);
string_clear(opt.directory);
string_clear(opt.path);
}

stack_destroy(worlds);
Expand All @@ -68,27 +72,27 @@ static void screen_sworld_reset(struct screen* s, int width, int height) {
worlds = malloc(sizeof(struct stack));
stack_create(worlds, 8, sizeof(struct world_option));

DIR* d = opendir("saves");
const char* saves_path
= config_read_string(&gstate.config_user, "paths.worlds", "saves");

DIR* d = opendir(saves_path);

if(d) {
struct dirent* dir;
while((dir = readdir(d))) {
if(dir->d_type & DT_DIR && *dir->d_name != '.') {
struct world_option opt;
string_init_set_str(opt.directory, dir->d_name);
string_init_printf(opt.path, "%s/%s", saves_path, dir->d_name);

struct level_archive la;
if(level_archive_create(&la, opt.directory)) {
struct world_option opt;
string_init(opt.name);
string_init_set_str(opt.directory, dir->d_name);

if(level_archive_create(&la, opt.path)) {
char name[64];

if(!level_archive_read(&la, LEVEL_NAME, name, sizeof(name)))
strcpy(name, "Missing name");

string_set_str(opt.name, name);
string_init_set_str(opt.name, name);
string_init_set_str(opt.directory, dir->d_name);

if(!level_archive_read(&la, LEVEL_DISK_SIZE, &opt.byte_size,
0))
Expand All @@ -103,7 +107,7 @@ static void screen_sworld_reset(struct screen* s, int width, int height) {
level_archive_destroy(&la);
stack_push(worlds, &opt);
} else {
string_clear(opt.directory);
string_clear(opt.path);
}
}
}
Expand Down Expand Up @@ -139,7 +143,7 @@ static void screen_sworld_update(struct screen* s, float dt) {

struct server_rpc rpc;
rpc.type = SRPC_LOAD_WORLD;
string_init_set(rpc.payload.load_world.name, opt.directory);
string_init_set(rpc.payload.load_world.name, opt.path);
svin_rpc_send(&rpc);

screen_set(&screen_load_world);
Expand Down
5 changes: 4 additions & 1 deletion source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ int main(void) {
fatInitDefault();
#endif

config_create(&gstate.config_user, "config.json");

time_reset();
input_init();
gfx_setup();
Expand Down Expand Up @@ -97,7 +99,7 @@ int main(void) {
bool render_world
= gstate.current_screen->render_world && gstate.world_loaded;

camera_update(&gstate.camera, gstate.stats.dt);
camera_update(&gstate.camera);

if(render_world) {
world_pre_render(&gstate.world, &gstate.camera, gstate.camera.view);
Expand Down Expand Up @@ -174,5 +176,6 @@ int main(void) {
input_poll();
gfx_finish(true);
}

return 0;
}

0 comments on commit 8304df7

Please sign in to comment.