From 0a4c1fd15440e9a29daad14263758bd5a65a438c Mon Sep 17 00:00:00 2001 From: Klemen Plestenjak Date: Tue, 23 Jan 2024 23:09:08 +0100 Subject: [PATCH] Only render objects on screen --- CMakeLists.txt | 1 + game/main.c | 19 ++++++++++++++----- game/map_init.c | 14 +++++++++++++- game/utils.h | 21 +++++++++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) create mode 100644 game/utils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 6236525..5461553 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -41,6 +41,7 @@ add_executable(PixelDefense game/sounds.h game/ui_widgets.c game/ui_widgets.h + game/utils.h ) diff --git a/game/main.c b/game/main.c index 6dcf625..268077d 100644 --- a/game/main.c +++ b/game/main.c @@ -12,6 +12,7 @@ #include "map_layers.h" #include "buildings.h" #include "ui_widgets.h" +#include "utils.h" #include "pathfinding.h" #include "sounds.h" @@ -376,8 +377,9 @@ static void renderGame(Game *game, float dt) { i32 numDraws = ecs_query_entity_count(game->drawQuery); DrawData *drawData = bzStackAlloc(&game->stackAlloc, numDraws * sizeof(*drawData)); ecs_iter_t it = ecs_query_iter(ECS, game->drawQuery); - ecs_entity_t worker = 0; i32 drawIdx = 0; + Camera2D camera = game->camera; + Rectangle camBounds = getCameraBounds(camera); while (ecs_iter_next(&it)) { Position *p = ecs_field(&it, Position, 1); Size *s = ecs_field(&it, Size, 2); @@ -385,12 +387,17 @@ static void renderGame(Game *game, float dt) { 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}; - if (dst.width == 10 && dst.height == 10) { - worker = it.entities[i]; - } Vector2 origin = {dst.width * 0.5f, dst.height}; dst.x += origin.x - dst.width * 0.5f; dst.y += origin.y - dst.height * 0.5f; + Rectangle collider = { + p[i].x - s[i].x * 0.5f, + p[i].y - s[i].y * 0.5f, + .width = s[i].x, + .height = s[i].y + }; + if (!CheckCollisionRecs(camBounds, collider)) + continue; Rectangle src = t[i].rec; // Fixes texture bleeding issue src.x += 0.01f; @@ -417,7 +424,7 @@ static void renderGame(Game *game, float dt) { drawData[drawIdx++] = draw; } } - BZ_ASSERT(drawIdx == numDraws); + numDraws = drawIdx; qsort(drawData, numDraws, sizeof(*drawData), cmpDrawData); Texture2D tex = game->tileset.tiles; @@ -450,6 +457,7 @@ static void renderGame(Game *game, float dt) { elapsed += dt * 2; elapsed = Clamp(elapsed, 0, 1.0f); attack = false; +#if 0 if (worker && false) { Position *pos = ecs_get_mut(ECS, worker, Position); DrawCircle(pos->x, pos->y, 2.0f, BLUE); @@ -463,6 +471,7 @@ static void renderGame(Game *game, float dt) { *rot = targetRot * bzEase(BZ_EASE_IN_BACK, elapsed); bzLogInfo("%.2f", Vector2Angle(*pos, lockedTarget) * RAD2DEG); } +#endif ecs_progress(ECS, dt); ecs_enable(ECS, renderDebugPathSystem, game->debug.drawPath); diff --git a/game/map_init.c b/game/map_init.c index 9bbca07..ac55d71 100644 --- a/game/map_init.c +++ b/game/map_init.c @@ -7,6 +7,7 @@ #include "entity_factory.h" #include "game_state.h" #include "map_layers.h" +#include "utils.h" bool initGameObjectsLayer(BzTileMap *map, BzTileObjectGroup *objectGroup) { Game *game = ecs_singleton_get_mut(ECS, Game); @@ -186,6 +187,10 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) { static f32 elapsed = 0.0f; elapsed += GetFrameTime(); + const Game *game = ecs_singleton_get(ECS, Game); + Camera2D camera = game->camera; + Rectangle camBounds = getCameraBounds(camera); + for (i32 y = 0; y < layer->height; y++) { for (i32 x = 0; x < layer->width; x++) { BzTile tile = bzTileLayerGetTile(layer, x, y); @@ -198,7 +203,14 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) { tile = terrainGetAnimationFrame(tile, frameIdx).frame; } Rectangle rec = bzTilesetGetTileRegion(tileset, tile); - DrawTextureRec(tileset->tiles, rec, drawPos, WHITE); + Rectangle bounds = { + drawPos.x, + drawPos.y, + tileset->tileWidth, + tileset->tileHeight + }; + if (CheckCollisionRecs(camBounds, bounds)) + DrawTextureRec(tileset->tiles, rec, drawPos, WHITE); } drawPos.x += (float) tileset->tileWidth; } diff --git a/game/utils.h b/game/utils.h new file mode 100644 index 0000000..3421d5f --- /dev/null +++ b/game/utils.h @@ -0,0 +1,21 @@ +#ifndef PIXELDEFENSE_UTILS_H +#define PIXELDEFENSE_UTILS_H + +#include +#include + +static Rectangle getCameraBounds(Camera2D camera) { + Rectangle bounds = { + .x = camera.target.x, + .y = camera.target.y, + .width = GetScreenWidth() / camera.zoom, + .height = GetScreenHeight() / camera.zoom, + }; + //bounds.width *= 0.8f; + //bounds.height *= 0.8f; + bounds.x -= bounds.width * 0.5f; + bounds.y -= bounds.height * 0.5f; + return bounds; +} + +#endif //PIXELDEFENSE_UTILS_H