From ccc93530fe9f1578e2bf98071631db3459ba6bb0 Mon Sep 17 00:00:00 2001 From: 0undefined Date: Thu, 27 Mar 2025 16:11:20 +0100 Subject: Add some worldgen --- include/worldgen.h | 24 +++++++++++++ state_mainstate/include/states/mainstate.h | 6 ++++ state_mainstate/src/mainstate.c | 58 ++++++++++++------------------ state_mainstate/src/worldgen.c | 43 ++++++++++++++++++++++ 4 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 include/worldgen.h create mode 100644 state_mainstate/src/worldgen.c diff --git a/include/worldgen.h b/include/worldgen.h new file mode 100644 index 0000000..5e3601e --- /dev/null +++ b/include/worldgen.h @@ -0,0 +1,24 @@ +#ifndef STATES_WORLDGEN_H +#define STATES_WORLDGEN_H +#ifdef __cplusplus +extern "C" { +#endif + +#include + +// first 4 bits for material type +#define BLOCK_none 0 +#define BLOCK_grass 1 +#define BLOCK_rock 2 + +// next 4 bits for shape type +#define SHAPE_filled (1 << 4) +#define SHAPE_slope (2 << 4) + +void gen_terrain(u8 *world, usize height, usize length, usize width); + + +#ifdef __cplusplus +} +#endif +#endif diff --git a/state_mainstate/include/states/mainstate.h b/state_mainstate/include/states/mainstate.h index 4421c4b..f29c7b9 100644 --- a/state_mainstate/include/states/mainstate.h +++ b/state_mainstate/include/states/mainstate.h @@ -13,11 +13,17 @@ #include +#define WORLD_HEIGHT 32 +#define WORLD_WIDTH 32 +#define WORLD_LENGTH 32 +#define WORLD_SIZE (WORLD_HEIGHT * WORLD_LENGTH * WORLD_WIDTH) + typedef struct mainstate_state { /* Resources */ Shader shaders[10]; RenderBatch terrain; RenderObject objects[10]; + u8 world[WORLD_HEIGHT * WORLD_WIDTH * WORLD_LENGTH]; i_ctx input_ctx; binding_t input_bindings[10]; vec3 cam_dir; diff --git a/state_mainstate/src/mainstate.c b/state_mainstate/src/mainstate.c index 00e3e6a..8593e21 100644 --- a/state_mainstate/src/mainstate.c +++ b/state_mainstate/src/mainstate.c @@ -4,6 +4,7 @@ #include #include #include +#include #define FOV_ORTHO 1 @@ -176,7 +177,7 @@ void mainstate_init(mainstate_state *state, void* arg) { /// Setup the camera // Set the position (it is zero initialized) //glm_vec3_copy((vec3){4,3,4}, state->c.pos); - glm_vec3_copy((vec3){WORLD_SZ_X / 2.f, WORLD_SZ_Y / 2.f + 4.f, WORLD_SZ_Z / 2.f}, state->c.pos); + glm_vec3_copy((vec3){WORLD_WIDTH / 2.f, WORLD_HEIGHT / 2.f + 4.f, WORLD_LENGTH / 2.f}, state->c.pos); // Copy to the desired position glm_vec3_copy(state->c.pos, state->cam_pos); @@ -246,29 +247,16 @@ void mainstate_init(mainstate_state *state, void* arg) { ERROR("Failed to create render batch!"); exit(EXIT_FAILURE); } - for (int y = -16; y < 16; y++) { - for (int x = -16; x < 16; x++) { - Transform t = { - .position = {2 * x, 0, 2 * y}, - }; - if (-1 == renderbatch_add(&(state->terrain), &(state->objects[1]), &t)) { - ERROR("Failed to add model2 to render batch!"); - exit(EXIT_FAILURE); - } - } - } - for (int x = -4; x < 4; x++) { - Transform t = { - .position = {2 * x, 2, 8}, - }; - if (-1 == renderbatch_add(&(state->terrain), &(state->objects[0]), &t)) { - ERROR("Failed to add model2 to render batch!"); - exit(EXIT_FAILURE); - } - } - for (int x = -4; x < 4; x++) { + + gen_terrain(state->world, WORLD_HEIGHT, WORLD_LENGTH, WORLD_WIDTH); + for (usize i = 0; i < WORLD_SIZE; i++) { + if (state->world[i] == BLOCK_none) continue; + + const isize y = i / (WORLD_LENGTH * WORLD_WIDTH); // height + const isize z = (i - (WORLD_LENGTH * WORLD_WIDTH * y)) / WORLD_WIDTH; // length + const isize x = i % WORLD_WIDTH; // width Transform t = { - .position = {2 * x, 2, -8}, + .position = {x * 2, y * 2, z * 2}, }; if (-1 == renderbatch_add(&(state->terrain), &(state->objects[0]), &t)) { ERROR("Failed to add model2 to render batch!"); @@ -291,18 +279,18 @@ void mainstate_init(mainstate_state *state, void* arg) { // Setup controls - state->input_bindings[ 0] = BindState(/*'A'*/ 30, 0, move_cam_left, move_cam_left_stop); - state->input_bindings[ 1] = BindState(/*'D'*/ 32, 0, move_cam_right, move_cam_right_stop); - state->input_bindings[ 2] = BindState(/*'W'*/ 17, 0, move_cam_fwd, move_cam_fwd_stop); - state->input_bindings[ 3] = BindState(/*'S'*/ 31, 0, move_cam_bck, move_cam_bck_stop); - state->input_bindings[ 4] = BindState(/*'W'*/ 57, 0, move_cam_up, move_cam_up_stop); - state->input_bindings[ 5] = BindState(/*'S'*/ 29, 0, move_cam_dwn, move_cam_dwn_stop); - - state->input_bindings[ 6] = BindAction(/*'-'*/ 13, 0, fov_increment); - state->input_bindings[ 7] = BindAction(/*'='*/ 12, 0, fov_decrement); - - state->input_bindings[ 8] = BindAction(/*'Q'*/ 16, 0, cam_rotate_l); - state->input_bindings[ 9] = BindAction(/*'E'*/ 18, 0, cam_rotate_r); + state->input_bindings[ 0] = BindState(KEY_A, 0, move_cam_left, move_cam_left_stop); + state->input_bindings[ 1] = BindState(KEY_D, 0, move_cam_right, move_cam_right_stop); + state->input_bindings[ 2] = BindState(KEY_W, 0, move_cam_fwd, move_cam_fwd_stop); + state->input_bindings[ 3] = BindState(KEY_S, 0, move_cam_bck, move_cam_bck_stop); + state->input_bindings[ 4] = BindState(KEY_SPACE, 0, move_cam_up, move_cam_up_stop); + state->input_bindings[ 5] = BindState(MOD_CTRL, 0, move_cam_dwn, move_cam_dwn_stop); + + state->input_bindings[ 6] = BindAction(KEY_MINUS, 0, fov_increment); + state->input_bindings[ 7] = BindAction(KEY_EQUAL, 0, fov_decrement); + + state->input_bindings[ 8] = BindAction(KEY_Q, 0, cam_rotate_l); + state->input_bindings[ 9] = BindAction(KEY_E, 0, cam_rotate_r); state->input_ctx = (i_ctx){ .bindings = (binding_t*)&state->input_bindings, diff --git a/state_mainstate/src/worldgen.c b/state_mainstate/src/worldgen.c new file mode 100644 index 0000000..0155fc6 --- /dev/null +++ b/state_mainstate/src/worldgen.c @@ -0,0 +1,43 @@ +#include + +#include + +void gen_terrain(u8 *world, usize height, usize length, usize width) { + if (world == NULL) { + world = calloc(width * length * height, sizeof(u8)); + } + + // y: height + // z: depth/length + // x: width + + // This is pretty cache unfriendly + for (usize y = 0; y < height / 2; y++) { + for (usize z = 0; z < length; z++) { + for (usize x = 0; x < width; x++) { + world[y * width * length + + z * width + + x] = BLOCK_grass; + } + } + } + + for (usize y = 0; y < 4; y++) { + for (usize z = 0; z < 8; z++) { + for (usize x = 0; x < 8; x++) { + if (z > 0 && z < 7 && x > 0 && x < 7) continue; + world[((height / 2 + 1) + y) * width * length + + (z + (length / 2 - 4)) * width + + (x + (width / 2 - 4))] = BLOCK_rock; + } + } + } + + // Doorway + world[(height / 2 + 1) * width * length + + (length / 2) * width + + (width / 2 - 4)] = BLOCK_none; + world[((height / 2 + 1) + 1) * width * length + + (length / 2) * width + + (width / 2 - 4)] = BLOCK_none; +} -- cgit v1.3