Only render objects on screen

This commit is contained in:
2024-01-23 23:09:08 +01:00
parent 0423a962df
commit 0a4c1fd154
4 changed files with 49 additions and 6 deletions

View File

@@ -41,6 +41,7 @@ add_executable(PixelDefense
game/sounds.h
game/ui_widgets.c
game/ui_widgets.h
game/utils.h
)

View File

@@ -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);

View File

@@ -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,6 +203,13 @@ void terrainRender(BzTileMap *map, BzTileLayer *layer) {
tile = terrainGetAnimationFrame(tile, frameIdx).frame;
}
Rectangle rec = bzTilesetGetTileRegion(tileset, tile);
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;

21
game/utils.h Normal file
View File

@@ -0,0 +1,21 @@
#ifndef PIXELDEFENSE_UTILS_H
#define PIXELDEFENSE_UTILS_H
#include <raylib.h>
#include <breeze.h>
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