From 645111b53c87c0d7c18480d71be9bb918ae79e1f Mon Sep 17 00:00:00 2001 From: Vladyslav Hrytsenko Date: Wed, 28 Sep 2022 23:01:47 +0300 Subject: [PATCH] code: changed zoom behavior --- code/games/minimal/src/main.c | 2 +- code/games/minimal/src/platform.c | 4 +- code/games/minimal/src/worldgen.c | 161 +++++++-------------------- code/games/minimal_3d/src/platform.c | 3 +- code/games/sandbox/src/platform.c | 3 +- 5 files changed, 51 insertions(+), 122 deletions(-) diff --git a/code/games/minimal/src/main.c b/code/games/minimal/src/main.c index 02628dc..9d9456a 100644 --- a/code/games/minimal/src/main.c +++ b/code/games/minimal/src/main.c @@ -21,7 +21,7 @@ #define DEFAULT_WORLD_SEED 302097 #define DEFAULT_CHUNK_SIZE 16 /* amount of blocks within a chunk (single axis) */ -#define DEFAULT_WORLD_SIZE 32 /* amount of chunks within a world (single axis) */ +#define DEFAULT_WORLD_SIZE 5 /* amount of chunks within a world (single axis) */ int main(int argc, char** argv) { zpl_opts opts={0}; diff --git a/code/games/minimal/src/platform.c b/code/games/minimal/src/platform.c index 9661446..514bb76 100644 --- a/code/games/minimal/src/platform.c +++ b/code/games/minimal/src/platform.c @@ -79,11 +79,13 @@ void platform_input_update_input_frame(game_keystate_data data) { void platform_input() { float mouse_z = (GetMouseWheelMove()*0.5f); + float mouse_modified = target_zoom < 4 ? mouse_z / (zpl_exp(4 - (target_zoom))) : mouse_z; if (mouse_z != 0.0f) { - target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 11.0f); + target_zoom = zpl_clamp(target_zoom + mouse_modified, 0.1f, 11.0f); } + // NOTE(zaklaus): keystate handling { float x=0.0f, y=0.0f; diff --git a/code/games/minimal/src/worldgen.c b/code/games/minimal/src/worldgen.c index f26cbd8..9165a43 100644 --- a/code/games/minimal/src/worldgen.c +++ b/code/games/minimal/src/worldgen.c @@ -21,6 +21,10 @@ typedef WORLD_BLOCK_OBSERVER(world_block_observer_proc); #define BLOCK_INVALID 0xF +int eco_incircle(int x, int y, int radius) { + return (zpl_pow(x, 2) + zpl_pow(y, 2)) < zpl_pow(radius, 2); +} + block_id worldgen_biome_find(uint32_t biome, uint32_t kind) { asset_id asset = ASSET_INVALID; switch (biome) { @@ -36,7 +40,7 @@ block_id worldgen_biome_find(uint32_t biome, uint32_t kind) { } } } - + return blocks_find(asset); } @@ -48,7 +52,7 @@ static void world_fill_rect(block_id *data, block_id id, uint32_t x, uint32_t y, if (cx < 0 || cx >= world->dim) continue; if (cy < 0 || cy >= world->dim) continue; uint32_t i = (cy*world->dim) + cx; - + if (proc) { block_id new_id = (*proc)(data, id, i); if (new_id != BLOCK_INVALID) { @@ -56,28 +60,31 @@ static void world_fill_rect(block_id *data, block_id id, uint32_t x, uint32_t y, } else continue; } - + data[i] = id; } } } -static void world_fill_circle(block_id *data, block_id id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) { - for (uint32_t cy=y; cy= world->dim) continue; - if (cy < 0 || cy >= world->dim) continue; - uint32_t i = (cy*world->dim) + cx; - - if (proc) { - block_id new_id = (*proc)(data, id, i); - if (new_id != BLOCK_INVALID) { - id = new_id; +static void world_fill_circle(block_id *data, block_id id, uint32_t cx, uint32_t cy, uint32_t radius, world_block_observer_proc *proc) { + for (int x = -(int32_t)(radius); x < (int32_t)radius; ++x) { + for (int y = -(int32_t)(radius); y < (int32_t)radius; ++y) { + if (eco_incircle(x, y, radius)) { + int fx = x + cx; + int fy = y + cy; + + uint32_t i = (fy*world->dim) + fx; + + if (proc) { + block_id new_id = (*proc)(data, id, i); + if (new_id != BLOCK_INVALID) { + id = new_id; + } + else continue; } - else continue; + + data[i] = id; } - - data[i] = id; } } } @@ -91,21 +98,21 @@ static void world_fill_rect_anchor(block_id *data, block_id id, uint32_t x, uint static WORLD_BLOCK_OBSERVER(shaper) { uint32_t kind = id; uint32_t old_kind = data[block_idx]; - + if (kind == BLOCK_KIND_WALL && kind == old_kind) { return worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_HILL); } if (kind == BLOCK_KIND_HILL && kind == old_kind) { return worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_HILL_SNOW); } - + return id; } static block_id world_perlin_cond_offset(uint32_t block_idx, double chance, uint32_t ofx, uint32_t ofy) { uint32_t x = block_idx % world->dim + ofx; uint32_t y = block_idx / world->dim + ofy; - + return perlin_fbm(world->seed, x, y, WORLD_PERLIN_FREQ, WORLD_PERLIN_OCTAVES) < chance; } @@ -153,7 +160,7 @@ static WORLD_BLOCK_OBSERVER(shaper_noise33) { #if 0 static void world_fill_mountain(uint32_t x, uint32_t y) { - + } #endif @@ -163,7 +170,7 @@ static void world_fill_mountain(uint32_t x, uint32_t y) { int32_t worldgen_build(world_data *wld) { // TODO(zaklaus): pass world as an arg instead world = wld; - + // TODO: perform world gen // atm, we will fill the world with ground and surround it by walls block_id wall_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_WALL); @@ -172,105 +179,23 @@ int32_t worldgen_build(world_data *wld) { block_id watr_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER); block_id lava_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_LAVA); block_id tree_id = blocks_find(ASSET_TREE); - + srand(world->seed); - + // walls world_fill_rect(world->data, wall_id, 0, 0, world->dim, world->dim, NULL); - + // ground - world_fill_rect(world->data, grnd_id, 1, 1, world->dim-2, world->dim-2, NULL); - world_fill_rect(world->data, dirt_id, 1, 1, world->dim-2, world->dim-2, shaper_noise05); - world_fill_rect(world->outer_data, tree_id, 1, 1, world->dim-2, world->dim-2, shaper_noise01b); - - // water -#if 1 - for (int i=0; idata, watr_id, RAND_RANGE(0, world->dim), RAND_RANGE(0, world->dim), 4+RAND_RANGE(0,3), 4+RAND_RANGE(0,3), 0.5f, 0.5f, shaper_noise80); - } -#endif - - // ice rink -#if 0 - world_fill_rect_anchor(world->data, watr_id, 450, 125, 10, 10, 0.0f, 0.0f, NULL); -#endif - - // lava -#if 1 - for (int i=0; idata, lava_id, RAND_RANGE(0, world->dim), RAND_RANGE(0, world->dim), 4+RAND_RANGE(0,3), 4+RAND_RANGE(0,3), 0.5f, 0.5f, shaper_noise80); - } -#endif - - - // hills -#if 1 - const uint32_t HILLS_SIZE = 21; - for (int i=0; idata, wall_id, RAND_RANGE(0, world->dim), RAND_RANGE(0, world->dim), RAND_RANGE(0,HILLS_SIZE), RAND_RANGE(0,HILLS_SIZE), 0.5f, 0.5f, shaper_noise50); - } -#endif - - // vehicles -#if 1 - for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - entity_set_position(e, dest->x, dest->y); - } -#endif - - // items -#if 1 - for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - entity_set_position(e, dest->x, dest->y); - } - - for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - entity_set_position(e, dest->x, dest->y); - } - - for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - entity_set_position(e, dest->x, dest->y); - } - - for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - entity_set_position(e, dest->x, dest->y); - } - - for (int i=0; ix = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE); - entity_set_position(e, dest->x, dest->y); - } - -#endif - + world_fill_rect(world->data, watr_id, 1, 1, world->dim-2, world->dim-2, NULL); + + int radius = 25; + + // wide boy circle + world_fill_circle(world->data, dirt_id, world->dim / 2, world->dim / 2, radius, NULL); + + // narrow boy cirlce + world_fill_circle(world->data, grnd_id, world->dim / 2, world->dim / 2, radius * 0.7f, NULL); + + return WORLD_ERROR_NONE; } diff --git a/code/games/minimal_3d/src/platform.c b/code/games/minimal_3d/src/platform.c index 9661446..c1396a8 100644 --- a/code/games/minimal_3d/src/platform.c +++ b/code/games/minimal_3d/src/platform.c @@ -79,9 +79,10 @@ void platform_input_update_input_frame(game_keystate_data data) { void platform_input() { float mouse_z = (GetMouseWheelMove()*0.5f); + float mouse_modified = target_zoom < 4 ? mouse_z / (zpl_exp(4 - (target_zoom))) : mouse_z; if (mouse_z != 0.0f) { - target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 11.0f); + target_zoom = zpl_clamp(target_zoom + mouse_modified, 0.1f, 11.0f); } // NOTE(zaklaus): keystate handling diff --git a/code/games/sandbox/src/platform.c b/code/games/sandbox/src/platform.c index c804817..f5795f2 100644 --- a/code/games/sandbox/src/platform.c +++ b/code/games/sandbox/src/platform.c @@ -136,9 +136,10 @@ void platform_input_update_input_frame(game_keystate_data data) { void platform_input() { float mouse_z = (GetMouseWheelMove()*0.5f); + float mouse_modified = target_zoom < 4 ? mouse_z / (zpl_exp(4 - (target_zoom))) : mouse_z; if (mouse_z != 0.0f) { - target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 11.0f); + target_zoom = zpl_clamp(target_zoom + mouse_modified, 0.1f, 11.0f); } // NOTE(zaklaus): keystate handling