From e7a0f91c36fd0eed0ff61ffb9d9b8b6a162b63fd Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Fri, 29 Dec 2023 09:42:15 +0100 Subject: [PATCH] Add draw sorting based on Y position --- game/components.c | 8 ------- game/components.h | 5 ----- game/game_state.h | 2 ++ game/main.c | 50 ++++++++++++++++++++++++++++++++++++++----- game/map_init.c | 2 -- game/systems.h | 7 ------ game/systems_entity.c | 28 ------------------------ 7 files changed, 47 insertions(+), 55 deletions(-) diff --git a/game/components.c b/game/components.c index afddd75..06db7ad 100644 --- a/game/components.c +++ b/game/components.c @@ -3,10 +3,6 @@ #include "unit_ai.h" #include "unit_actions.h" -ECS_TAG_DECLARE(TextureTerrain); -ECS_TAG_DECLARE(TextureBuildings); -ECS_TAG_DECLARE(TextureEntities); - ECS_COMPONENT_DECLARE(Resource); ECS_COMPONENT_DECLARE(TilePosition); @@ -42,10 +38,6 @@ ECS_TAG_DECLARE(Attackable); ECS_COMPONENT_DECLARE(Storage); void initComponentIDs(ecs_world_t *ecs) { - ECS_TAG_DEFINE(ecs, TextureTerrain); - ECS_TAG_DEFINE(ecs, TextureBuildings); - ECS_TAG_DEFINE(ecs, TextureEntities); - ECS_COMPONENT_DEFINE(ecs, Resource); ECS_COMPONENT_DEFINE(ecs, TilePosition); diff --git a/game/components.h b/game/components.h index e31aa05..6edf69d 100644 --- a/game/components.h +++ b/game/components.h @@ -6,11 +6,6 @@ #include "game_tileset.h" -extern ECS_TAG_DECLARE(TextureTerrain); -extern ECS_TAG_DECLARE(TextureBuildings); -extern ECS_TAG_DECLARE(TextureEntities); - - typedef enum ResourceType { RES_IRON, RES_WOOD, diff --git a/game/game_state.h b/game/game_state.h index c16364d..2da072f 100644 --- a/game/game_state.h +++ b/game/game_state.h @@ -39,6 +39,8 @@ typedef struct Game { bool spatialGrid; } debugDraw; f32 elapsed; + + ecs_query_t *drawQuery; } Game; extern ecs_world_t *ECS; diff --git a/game/main.c b/game/main.c index 3ca8484..873322d 100644 --- a/game/main.c +++ b/game/main.c @@ -148,6 +148,18 @@ ECS_DTOR(Path, path, { } }) +int cmpPos(ecs_entity_t e1, const void *v1, ecs_entity_t e2, const void *v2) { + const Position *pos1 = v1; + const Position *pos2 = v2; + + // NOTE: entities with lower Y are in front + f32 dif = pos2->y - pos1->y; + int cmpVal = 0; + if (dif < 0) cmpVal = 1; + else if (dif > 0) cmpVal = -1; + return cmpVal; +} + bool init(void *userData) { BZ_UNUSED(userData); SetExitKey(0); @@ -167,6 +179,16 @@ bool init(void *userData) { Game *game = ecs_singleton_get_mut(ECS, Game); game->screen = SCREEN_MAIN_MENU; game->font = LoadFontEx("assets/fonts/CompassPro.ttf", 92, NULL, 0); + game->drawQuery = ecs_query(ECS, { + .filter.terms = { + { ecs_id(Position) }, + { ecs_id(Size) }, + { ecs_id(Rotation) }, + { ecs_id(TextureRegion) } + }, + .order_by_component = ecs_id(Position), + .order_by = cmpPos + }); ECS_COMPONENT_DEFINE(ECS, InputState); ecs_singleton_set(ECS, InputState, {}); @@ -228,12 +250,12 @@ bool init(void *userData) { ECS_SYSTEM(ECS, renderDebugPath, EcsOnUpdate, Path); - ECS_SYSTEM(ECS, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain); - ECS_SYSTEM(ECS, renderBuildings, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureBuildings); - ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities); + //ECS_SYSTEM(ECS, renderTerrain, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureTerrain); + //ECS_SYSTEM(ECS, renderBuildings, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureBuildings); + //ECS_SYSTEM(ECS, renderEntities, EcsOnUpdate, Position, Size, Rotation, TextureRegion, TextureEntities); ECS_SYSTEM(ECS, renderColliders, EcsOnUpdate, Position, Size); - ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation, TextureEntities); + ECS_SYSTEM(ECS, renderRotationDirection, EcsOnUpdate, Position, Rotation); loadMap(game, "assets/maps/main_menu_01.tmj"); @@ -253,6 +275,7 @@ void deinit(void *userData) { // Destroy queries ecs_query_fini(input->queries.selected); + ecs_query_fini(game->drawQuery); Game gameCopy = *game; InputState inputCopy = *input; @@ -314,9 +337,26 @@ static void renderGame(Game *game, float dt) { ClearBackground(RAYWHITE); BeginMode2D(game->camera); + // Map bzTileMapDraw(&game->map); - + // Ground UI drawPlayerInputUIGround(); + // Entities + ecs_iter_t it = ecs_query_iter(ECS, game->drawQuery); + while (ecs_iter_next(&it)) { + Position *p = ecs_field(&it, Position, 1); + Size *s = ecs_field(&it, Size, 2); + Rotation *r = ecs_field(&it, Rotation, 3); + TextureRegion *t = ecs_field(&it, TextureRegion, 4); + for (i32 i = 0; i < it.count; i++) { + Rectangle dst = {p[i].x, p[i].y, s[i].x, s[i].y}; + Vector2 origin = {dst.width * 0.5f, dst.height * 0.5f}; + Rectangle src = t[i].rec; + if (t[i].flipX) src.width *= -1.0f; + if (t[i].flipY) src.height *= -1.0f; + DrawTexturePro(t[i].texture, src, dst, origin, t[i].rotation, WHITE); + } + } ecs_progress(ECS, dt); ecs_enable(ECS, renderDebugPathSystem, game->debugDraw.path); diff --git a/game/map_init.c b/game/map_init.c index 18f65d9..9d3b844 100644 --- a/game/map_init.c +++ b/game/map_init.c @@ -122,7 +122,6 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) { ecs_set(ECS, e, SpatialGridID, {gridID}); posX += sizeX * 0.5f; posY += sizeY * 0.5f; - ecs_add(ECS, e, TextureTerrain); ecs_set(ECS, e, Position, {posX, posY}); ecs_set(ECS, e, Size, {sizeX, sizeY}); ecs_set(ECS, e, Rotation, {0}); @@ -139,7 +138,6 @@ bool initTreesLayer(BzTileMap *map, BzTileLayer *layer) { ecs_entity_t createWorker(Position position, Size size, BzSpatialGrid *grid, BzTileset *tileset, BzTile gid) { ecs_entity_t e = ecs_new_id(ECS); - ecs_add(ECS, e, TextureEntities); ecs_set_ptr(ECS, e, Position, &position); ecs_set_ptr(ECS, e, Size, &size); BzSpatialGridID spatialID = bzSpatialGridInsert(grid, &e, diff --git a/game/systems.h b/game/systems.h index 6c0bb5d..3bf207a 100644 --- a/game/systems.h +++ b/game/systems.h @@ -94,13 +94,6 @@ void entityUpdateAnimationState(ecs_iter_t *it); void entityUpdateAnimation(ecs_iter_t *it); -/* - * 0: - */ -void renderTerrain(ecs_iter_t *it); -void renderBuildings(ecs_iter_t *it); -void renderEntities(ecs_iter_t *it); - /* * 1: Position * 2: Size diff --git a/game/systems_entity.c b/game/systems_entity.c index 11c1303..03d6c4c 100644 --- a/game/systems_entity.c +++ b/game/systems_entity.c @@ -208,34 +208,6 @@ void entityUpdateAnimation(ecs_iter_t *it) { } } -static void render(ecs_iter_t *it) { - Position *p = ecs_field(it, Position, 1); - Size *s = ecs_field(it, Size, 2); - Rotation *r = ecs_field(it, Rotation, 3); - TextureRegion *t = ecs_field(it, TextureRegion, 4); - - for (i32 i = 0; i < it->count; i++) { - Rectangle dst = {p[i].x, p[i].y, s[i].x, s[i].y}; - Vector2 origin = {dst.width * 0.5f, dst.height * 0.5f}; - Rectangle src = t[i].rec; - if (t[i].flipX) src.width *= -1.0f; - if (t[i].flipY) src.height *= -1.0f; - DrawTexturePro(t[i].texture, src, dst, origin, t[i].rotation, WHITE); - } -} - -void renderTerrain(ecs_iter_t *it) { - render(it); -} - -void renderBuildings(ecs_iter_t *it) { - render(it); -} - -void renderEntities(ecs_iter_t *it) { - render(it); -} - void renderColliders(ecs_iter_t *it) { Position *pos = ecs_field(it, Position, 1); Size *size = ecs_field(it, Size, 2);