From 91f005b4bb8022c7b0f897f365f90be0661dfe29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Thu, 2 Feb 2023 15:40:48 +0100 Subject: [PATCH] Add support for sprite rendering --- code/foundation/src/models/components.h | 6 ++++++ code/foundation/src/systems/modules/system_health.c | 3 +-- code/foundation/src/world/entity_view.c | 7 ++++++- code/foundation/src/world/entity_view.h | 6 ++++++ code/foundation/src/world/world.c | 6 ++++++ code/games/minimal/src/renderer.c | 6 ++++++ code/games/sandbox/src/renderer.c | 6 ++++++ code/games/survival/src/renderer.c | 6 ++++++ 8 files changed, 43 insertions(+), 3 deletions(-) diff --git a/code/foundation/src/models/components.h b/code/foundation/src/models/components.h index 4e1cfeb..7629496 100644 --- a/code/foundation/src/models/components.h +++ b/code/foundation/src/models/components.h @@ -142,6 +142,11 @@ typedef struct { float durability; // 1.0 - 0.0 (0.0 = broken), we can only ever merge items of the same durability } Item; +typedef struct { + int spritesheet; + int frame; +} Sprite; + typedef struct { char _unused; } BlockHarvest; @@ -266,6 +271,7 @@ typedef struct { X(Creature)\ X(SeeksFood)\ X(SeeksCompanion)\ + X(Sprite)\ X(StreamInfo) #define X(comp) extern ECS_COMPONENT_DECLARE(comp); diff --git a/code/foundation/src/systems/modules/system_health.c b/code/foundation/src/systems/modules/system_health.c index 2d3d5bc..609da21 100644 --- a/code/foundation/src/systems/modules/system_health.c +++ b/code/foundation/src/systems/modules/system_health.c @@ -66,11 +66,10 @@ void TickDownHealDelay(ecs_iter_t *it) { HealDelay *h = ecs_field(it, HealDelay, 1); for (int i = 0; i < it->count; i++) { - --h[i].delay; + TICK_VAR(h[i].delay); if (h[i].delay == 0) { ecs_remove(it->world, it->entities[i], HealDelay); } } } - diff --git a/code/foundation/src/world/entity_view.c b/code/foundation/src/world/entity_view.c index 31b9404..b7fe255 100644 --- a/code/foundation/src/world/entity_view.c +++ b/code/foundation/src/world/entity_view.c @@ -44,8 +44,13 @@ pkt_desc pkt_entity_view_desc[] = { { PKT_UINT(entity_view, asset) }, { PKT_UINT(entity_view, progress_active) }, { PKT_UINT(entity_view, is_producer) }, + { PKT_HALF(entity_view, progress_value) }, - + + { PKT_KEEP_IF(entity_view, kind, EKIND_SPRITE, 2) }, + { PKT_UINT(entity_view, spritesheet) }, + { PKT_UINT(entity_view, frame) }, + { PKT_KEEP_IF(entity_view, has_items, true, 3) }, { PKT_UINT(entity_view, has_items) }, { PKT_UINT(entity_view, selected_item) }, diff --git a/code/foundation/src/world/entity_view.h b/code/foundation/src/world/entity_view.h index 1d2d54a..1d3cbcc 100644 --- a/code/foundation/src/world/entity_view.h +++ b/code/foundation/src/world/entity_view.h @@ -13,6 +13,8 @@ X(EKIND_PLAYER)\ X(EKIND_ITEM)\ X(EKIND_DEVICE)\ + X(EKIND_SPRITE)\ + X(EKIND_WEAPON)\ X(EKIND_VEHICLE)\ X(EKIND_DEMO_NPC)\ X(EKIND_MONSTER)\ @@ -78,6 +80,10 @@ typedef struct entity_view { bool is_producer; uint32_t progress_active; float progress_value; + + // sprite index + int spritesheet; + int frame; // NOTE(zaklaus): inventory uint8_t has_items; diff --git a/code/foundation/src/world/world.c b/code/foundation/src/world/world.c index 589b219..5c16c3f 100644 --- a/code/foundation/src/world/world.c +++ b/code/foundation/src/world/world.c @@ -76,6 +76,12 @@ entity_view* world_build_entity_view(int64_t e) { view.is_producer = ecs_get(world_ecs(), e, Producer) != 0; } + if (ecs_get(world_ecs(), e, Sprite)) { + Sprite const* spr = ecs_get(world_ecs(), e, Sprite); + view.spritesheet = spr->spritesheet; + view.frame = spr->frame; + } + view.inside_vehicle = ecs_get(world_ecs(), e, IsInVehicle) != 0 ? true : false; Inventory* inv = 0; diff --git a/code/games/minimal/src/renderer.c b/code/games/minimal/src/renderer.c index f7b732a..8ad3c89 100644 --- a/code/games/minimal/src/renderer.c +++ b/code/games/minimal/src/renderer.c @@ -69,6 +69,12 @@ void renderer_draw_entry(uint64_t key, entity_view *data, game_world_render_entr Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE; DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time)); }break; + case EKIND_SPRITE: + case EKIND_WEAPON: { + float x = data->x - 32.f; + float y = data->y - 32.f; + DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE)); + } break; case EKIND_DEVICE:{ float x = data->x - 32.f; float y = data->y - 32.f; diff --git a/code/games/sandbox/src/renderer.c b/code/games/sandbox/src/renderer.c index 5a92fff..cda568b 100644 --- a/code/games/sandbox/src/renderer.c +++ b/code/games/sandbox/src/renderer.c @@ -72,6 +72,12 @@ void renderer_draw_entry(uint64_t key, entity_view *data, game_world_render_entr Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE; DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time)); }break; + case EKIND_SPRITE: + case EKIND_WEAPON: { + float x = data->x - 32.f; + float y = data->y - 32.f; + DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE)); + } break; case EKIND_DEVICE:{ Texture2D tex = GetSpriteTexture2D(assets_find(data->asset)); float x = data->x - tex.width/2; diff --git a/code/games/survival/src/renderer.c b/code/games/survival/src/renderer.c index 3182403..7601e62 100644 --- a/code/games/survival/src/renderer.c +++ b/code/games/survival/src/renderer.c @@ -57,6 +57,12 @@ void renderer_draw_entry(uint64_t key, entity_view *data, game_world_render_entr Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE; DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time)); }break; + case EKIND_SPRITE: + case EKIND_WEAPON: { + float x = data->x; + float y = data->y; + DrawSpriteEco(&main_sprite_sheet, data->frame, x, y, 0.0f, 2.0f, WHITE); + } break; case EKIND_DEVICE:{ float x = data->x - 32.f; float y = data->y - 32.f;