From b6b632899d3e52b4771f50c1f012899ce5ae6c3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Tue, 27 Sep 2022 13:55:24 +0000 Subject: [PATCH] all storage systems are dynamic now --- code/foundation/CMakeLists.txt | 1 - code/foundation/src/core/game.c | 55 +++++++++------ code/foundation/src/ents/entity.c | 13 +++- code/foundation/src/ents/entity.h | 10 +++ code/foundation/src/ents/entity_spawnlist.c | 11 ++- code/foundation/src/ents/items.c | 23 +++++-- code/foundation/src/ents/items.h | 4 ++ code/foundation/src/ents/items_list.c | 32 +++++---- code/foundation/src/ents/items_list_helpers.h | 10 +-- code/foundation/src/gen/assets.c | 68 +++++++++++-------- code/foundation/src/gen/assets.h | 40 ++++++++--- code/foundation/src/gen/assets_list.c | 60 +++++++--------- code/foundation/src/renderers/renderer_3d.c | 8 +-- code/foundation/src/renderers/renderer_v0.c | 8 +-- code/foundation/src/world/blocks.c | 36 +++++----- code/foundation/src/world/blocks.h | 29 +++++++- code/foundation/src/world/blocks_list.c | 39 +++++------ code/foundation/src/world/entity_view.h | 2 +- code/games/sandbox/CMakeLists.txt | 1 + .../sandbox}/src/gui/build_mode.c | 0 .../sandbox}/src/gui/inventory.c | 0 .../sandbox/src/platform.c} | 2 +- 22 files changed, 272 insertions(+), 180 deletions(-) rename code/{foundation => games/sandbox}/src/gui/build_mode.c (100%) rename code/{foundation => games/sandbox}/src/gui/inventory.c (100%) rename code/{foundation/src/platform/platform_raylib.c => games/sandbox/src/platform.c} (99%) diff --git a/code/foundation/CMakeLists.txt b/code/foundation/CMakeLists.txt index dcb07a4..6d0f8e9 100644 --- a/code/foundation/CMakeLists.txt +++ b/code/foundation/CMakeLists.txt @@ -4,7 +4,6 @@ add_library(eco2d-foundation STATIC src/core/game.c src/core/camera.c - src/platform/platform_raylib.c src/platform/signal_handling.c src/platform/profiler.c diff --git a/code/foundation/src/core/game.c b/code/foundation/src/core/game.c index 7fa5c4b..d00b5e5 100644 --- a/code/foundation/src/core/game.c +++ b/code/foundation/src/core/game.c @@ -6,6 +6,7 @@ #include "platform/signal_handling.h" #include "net/network.h" #include "ents/entity.h" +#include "ents/items.h" #include "world/world_view.h" #include "world/entity_view.h" #include "core/camera.h" @@ -31,7 +32,7 @@ static WORLD_PKT_READER(pkt_reader) { pkt_header header = {0}; uint32_t ok = pkt_header_decode(&header, data, datalen); header.udata = udata; - + if (ok && header.ok) { return pkt_handlers[header.id].handler(&header) >= 0; } else { @@ -66,7 +67,7 @@ static WORLD_PKT_WRITER(mp_cli_pkt_writer) { void world_viewers_init(uint32_t num_viewers) { zpl_buffer_init(world_viewers, zpl_heap(), num_viewers); - + for (uint32_t i = 0; i < num_viewers; i++) { zpl_buffer_append(world_viewers, world_view_create(i)); } @@ -128,31 +129,39 @@ float game_time() { void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_viewers, int32_t seed, uint16_t chunk_size, uint16_t chunk_amount, int8_t is_dash_enabled) { game_mode = play_mode; game_should_close = false; - + #ifndef _DEBUG const char *host_ip = "lab.zakto.pw"; #else const char *host_ip = "127.0.0.1"; #endif - + uint16_t host_port = (port > 0) ? port : 27000; - + if (ip != NULL) { host_ip = ip; } - + + // NOTE: initialise subsystems + { + assets_setup(); + blocks_setup(); + item_setup(); + entity_spawndef_setup(); + } + if (game_mode != GAMEKIND_HEADLESS) { platform_init(); - + world_viewers_init(num_viewers); active_viewer = &world_viewers[0]; camera_reset(); } - + if (game_mode != GAMEKIND_SINGLE) { network_init(); } - + if (game_mode == GAMEKIND_CLIENT) { world_setup_pkt_handlers(pkt_reader, mp_cli_pkt_writer); network_client_connect(host_ip, host_port); @@ -161,13 +170,13 @@ void game_init(const char *ip, uint16_t port, game_kind play_mode, uint32_t num_ world_setup_pkt_handlers(pkt_reader, game_mode == GAMEKIND_SINGLE ? sp_pkt_writer : mp_pkt_writer); world_init(seed, chunk_size, chunk_amount); if (is_dash_enabled) flecs_dash_init(); - + if (game_mode == GAMEKIND_HEADLESS) { network_server_start(0, 27000); //ecs_set_target_fps(world_ecs(), 60); } } - + if (game_mode == GAMEKIND_SINGLE) { for (uint32_t i = 0; i < num_viewers; i++) { pkt_00_init_send(i); @@ -180,27 +189,35 @@ int8_t game_is_networked() { } void game_shutdown() { - + if (game_mode == GAMEKIND_CLIENT) { network_client_disconnect(); } else { world_destroy(); - + if (game_mode == GAMEKIND_HEADLESS) { network_server_stop(); } } - + if (game_mode != GAMEKIND_SINGLE) { network_destroy(); } - + if (game_mode != GAMEKIND_HEADLESS) { world_viewers_destroy(); - + // TODO(zaklaus): crashes on exit //platform_shutdown(); } + + // NOTE: shutdown subsystems + { + item_cleanup(); + entity_spawndef_cleanup(); + blocks_cleanup(); + assets_cleanup(); + } } uint8_t game_is_running() { @@ -228,10 +245,10 @@ void game_update() { } else { world_update(); - + if (game_mode == GAMEKIND_HEADLESS) { network_server_tick(); - + static float ms_report = 2.5f; if (ms_report < get_cached_time()) { ms_report = get_cached_time() + 5.f; @@ -239,7 +256,7 @@ void game_update() { } } } - + last_update = get_cached_time(); } diff --git a/code/foundation/src/ents/entity.c b/code/foundation/src/ents/entity.c index 81985db..2819255 100644 --- a/code/foundation/src/ents/entity.c +++ b/code/foundation/src/ents/entity.c @@ -11,6 +11,17 @@ // NOTE(zaklaus): bring in entity spawnlist #include "entity_spawnlist.c" +void entity_spawndef_cleanup() { + zpl_array_free(entity_spawnlist); entity_spawnlist = NULL; +} +void entity_spawndef_register(spawndef def) { + if (!entity_spawnlist) { + zpl_array_init(entity_spawnlist, zpl_heap()); + } + + zpl_array_append(entity_spawnlist, def); +} + uint64_t entity_spawn(uint16_t class_id) { ecs_entity_t e = ecs_new(world_ecs(), 0); @@ -38,7 +49,7 @@ uint64_t entity_spawn(uint16_t class_id) { } uint64_t entity_spawn_id(uint16_t id){ - for (size_t i = 0; i < MAX_ENTITY_SPAWNDEFS; ++i){ + for (zpl_isize i = 0; i < zpl_array_count(entity_spawnlist); ++i){ if (entity_spawnlist[i].id == id){ ZPL_ASSERT(entity_spawnlist[i].proc); return entity_spawnlist[i].proc(); diff --git a/code/foundation/src/ents/entity.h b/code/foundation/src/ents/entity.h index eaea315..edb2191 100644 --- a/code/foundation/src/ents/entity.h +++ b/code/foundation/src/ents/entity.h @@ -1,8 +1,18 @@ #pragma once #include "platform/system.h" +#include "gen/assets.h" #define ENTITY_ACTION_VELOCITY_THRESHOLD 0.05f +typedef struct { + asset_id id; + uint64_t (*proc)(); +} spawndef; + +void entity_spawndef_setup(); +void entity_spawndef_cleanup(); +void entity_spawndef_register(spawndef def); + uint64_t entity_spawn(uint16_t class_id /* 0 = no streaming */); uint64_t entity_spawn_id(uint16_t id); void entity_batch_despawn(uint64_t *ids, size_t num_ids); diff --git a/code/foundation/src/ents/entity_spawnlist.c b/code/foundation/src/ents/entity_spawnlist.c index 43f93c3..c2f45cc 100644 --- a/code/foundation/src/ents/entity_spawnlist.c +++ b/code/foundation/src/ents/entity_spawnlist.c @@ -1,11 +1,8 @@ // NOTE(zaklaus): access to spawners #include "ents/storage.h" -static struct { - asset_id id; - uint64_t (*proc)(); -} entity_spawnlist[] = { - { .id = ASSET_CHEST, .proc = storage_spawn } -}; +static spawndef *entity_spawnlist = 0; -#define MAX_ENTITY_SPAWNDEFS ((sizeof(entity_spawnlist))/(sizeof(entity_spawnlist[0]))) +void entity_spawndef_setup(void) { + entity_spawndef_register((spawndef){ .id = ASSET_CHEST, .proc = storage_spawn }); +} diff --git a/code/foundation/src/ents/items.c b/code/foundation/src/ents/items.c index 49101bd..5d1c201 100644 --- a/code/foundation/src/ents/items.c +++ b/code/foundation/src/ents/items.c @@ -9,10 +9,21 @@ #include "zpl.h" #include "items_list.c" -#define ITEMS_COUNT (sizeof(items)/sizeof(item_desc)) + +void item_cleanup() { + zpl_array_free(items); items = NULL; +} + +void item_register(item_desc desc) { + if (!items) { + zpl_array_init(items, zpl_heap()); + } + + zpl_array_append(items, desc); +} static inline item_id item_resolve_proxy(item_id id) { - ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); + ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); item_usage usage = items[id].usage; if (usage == UKIND_PROXY) { return item_find(items[id].proxy.id); @@ -38,7 +49,7 @@ uint64_t item_spawn(asset_id kind, uint32_t qty) { } item_id item_find(asset_id kind) { - for (item_id i=0; i= 0 && id < ITEMS_COUNT); + ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); return items[id].max_quantity; } item_usage item_get_usage(item_id id) { - ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); + ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); return items[id].usage; } bool item_get_place_directional(item_id id) { - ZPL_ASSERT(id >= 0 && id < ITEMS_COUNT); + ZPL_ASSERT(id >= 0 && id < zpl_array_count(items)); return items[id].place.directional; } diff --git a/code/foundation/src/ents/items.h b/code/foundation/src/ents/items.h index 9cb21ba..960bf94 100644 --- a/code/foundation/src/ents/items.h +++ b/code/foundation/src/ents/items.h @@ -41,6 +41,10 @@ typedef struct { typedef uint16_t item_id; +void item_setup(); +void item_cleanup(); +void item_register(item_desc desc); + // NOTE(zaklaus): item drops uint64_t item_spawn(asset_id kind, uint32_t qty); void item_despawn(uint64_t id); diff --git a/code/foundation/src/ents/items_list.c b/code/foundation/src/ents/items_list.c index 074ad25..b30c61c 100644 --- a/code/foundation/src/ents/items_list.c +++ b/code/foundation/src/ents/items_list.c @@ -2,18 +2,20 @@ #include "world/entity_view.h" #include "items_list_helpers.h" -static item_desc items[] = { - { .kind = 0, .max_quantity = 0, }, - ITEM_BLOCK(ASSET_DEMO_ICEMAKER, 64, ASSET_WATER), - ITEM_SELF(ASSET_FENCE, 64), - ITEM_SELF(ASSET_WOOD, 64), - ITEM_HOLD(ASSET_TREE, 64), - - ITEM_SELF_DIR(ASSET_BELT, 999), - ITEM_PROXY(ASSET_BELT_LEFT, ASSET_BELT), - ITEM_PROXY(ASSET_BELT_RIGHT, ASSET_BELT), - ITEM_PROXY(ASSET_BELT_UP, ASSET_BELT), - ITEM_PROXY(ASSET_BELT_DOWN, ASSET_BELT), - - ITEM_ENT(ASSET_CHEST, 32, ASSET_CHEST), -}; +static item_desc *items = 0; + +void item_setup() { + item_register((item_desc){ .kind = 0, .max_quantity = 0, }); + item_register(ITEM_BLOCK(ASSET_DEMO_ICEMAKER, 64, ASSET_WATER)); + item_register(ITEM_SELF(ASSET_FENCE, 64)); + item_register(ITEM_SELF(ASSET_WOOD, 64)); + item_register(ITEM_HOLD(ASSET_TREE, 64)); + + item_register(ITEM_SELF_DIR(ASSET_BELT, 999)); + item_register(ITEM_PROXY(ASSET_BELT_LEFT, ASSET_BELT)); + item_register(ITEM_PROXY(ASSET_BELT_RIGHT, ASSET_BELT)); + item_register(ITEM_PROXY(ASSET_BELT_UP, ASSET_BELT)); + item_register(ITEM_PROXY(ASSET_BELT_DOWN, ASSET_BELT)); + + item_register(ITEM_ENT(ASSET_CHEST, 32, ASSET_CHEST)); +} diff --git a/code/foundation/src/ents/items_list_helpers.h b/code/foundation/src/ents/items_list_helpers.h index 166d50f..11d4f71 100644 --- a/code/foundation/src/ents/items_list_helpers.h +++ b/code/foundation/src/ents/items_list_helpers.h @@ -1,14 +1,14 @@ #pragma once #define ITEM_HOLD(asset, qty)\ -{\ +(item_desc){\ .kind = asset,\ .usage = UKIND_HOLD,\ .max_quantity = qty,\ } #define ITEM_BLOCK(asset, qty, build_asset)\ -{\ +(item_desc){\ .kind = asset,\ .usage = UKIND_PLACE,\ .max_quantity = qty,\ @@ -18,7 +18,7 @@ } #define ITEM_BLOCK_DIR(asset, qty, build_asset)\ -{\ +(item_desc){\ .kind = asset,\ .usage = UKIND_PLACE,\ .max_quantity = qty,\ @@ -29,7 +29,7 @@ } #define ITEM_PROXY(asset, proxy_id)\ -{\ +(item_desc){\ .kind = asset,\ .usage = UKIND_PROXY,\ .proxy = {\ @@ -38,7 +38,7 @@ } #define ITEM_ENT(asset, qty, eid)\ -{\ +(item_desc){\ .kind = asset,\ .usage = UKIND_PLACE_ITEM,\ .max_quantity = qty,\ diff --git a/code/foundation/src/gen/assets.c b/code/foundation/src/gen/assets.c index a751f07..d3d664d 100644 --- a/code/foundation/src/gen/assets.c +++ b/code/foundation/src/gen/assets.c @@ -2,17 +2,15 @@ #include "raylib.h" #include "gen/texgen.h" -#define ASSETS_COUNT (sizeof(assets)/sizeof(asset)) - typedef struct { asset_id id; asset_kind kind; - + union { Texture2D tex; Sound snd; }; - + // NOTE(zaklaus): metadata } asset; @@ -20,80 +18,92 @@ typedef struct { #define ASSET_FRAME_RENDER_MS (1.0/5.0) #define ASSET_FRAME_SKIP 4 -static int64_t assets_frame_counter = 1; -static double assets_frame_next_draw = 0.0; +static int64_t assets_resources_frame_counter = 1; +static double assets_resources_frame_next_draw = 0.0; #include -int32_t assets_setup(void) { - for (uint32_t i=0; ikind) { case AKIND_TEXTURE: { b->tex = texgen_build_sprite(b->id); }break; - + case AKIND_ANIM: { b->tex = texgen_build_anim(b->id, 0); }break; - + case AKIND_SOUND: { // TODO(zaklaus): soundgen }break; - + default: break; } } - assets_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS; + assets_resources_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS; return 0; } -int32_t assets_frame(void) { - if (assets_frame_next_draw < get_cached_time()) { - for (uint32_t i=0; ikind) { case AKIND_ANIM: { UnloadTexture(b->tex); - b->tex = texgen_build_anim(b->id, assets_frame_counter); + b->tex = texgen_build_anim(b->id, assets_resources_frame_counter); }break; - + default: break; } } - - assets_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS; - assets_frame_counter += ASSET_FRAME_SKIP; + + assets_resources_frame_next_draw = get_cached_time() + ASSET_FRAME_RENDER_MS; + assets_resources_frame_counter += ASSET_FRAME_SKIP; } - + return 0; } -void assets_destroy(void) { - for (uint32_t i=0; i