Support large maps up to 8192x8192 tiles

Raise hard map size cap from 4096 to 8192 via MAX_MAP_SIZE constant.
Replace fixed 16 KB fgets buffer with dynamic line reader that handles
arbitrarily long tile rows. Check calloc returns for tile layer
allocation.

Add entity distance culling: skip updates for entities beyond 2x screen
width from camera, skip rendering beyond 64 px margin. Dead entities
bypass culling so cleanup callbacks always run. Player, spacecraft,
drone, and moving platforms set ENTITY_ALWAYS_UPDATE to opt out.

Replace naive 4-neighbor flood fill with scanline algorithm (O(height)
stack instead of O(area)) for safe use on large maps.

Raise MAX_ENTITY_SPAWNS from 128 to 512 and MAX_EXIT_ZONES from 8 to
16 to support populated large levels.
This commit is contained in:
Thomas
2026-03-01 15:17:58 +00:00
parent ad2d68a8b4
commit df3bc29fb7
11 changed files with 181 additions and 45 deletions

View File

@@ -1,7 +1,33 @@
#include "engine/entity.h"
#include "engine/camera.h"
#include <string.h>
#include <stdio.h>
/* ── Distance culling ──────────────────────────────── */
/* Margin around the viewport for update culling (in pixels).
* Entities within this margin still update even if off-screen,
* so they are active before scrolling into view. */
#define UPDATE_MARGIN (SCREEN_WIDTH * 2.0f)
/* Margin for render culling — tighter than update since only
* entities actually visible (plus a small buffer) need drawing. */
#define RENDER_MARGIN 64.0f
/* Check whether a body is within a rectangle expanded by margin. */
static bool in_camera_range(const Body *body, const Camera *cam, float margin) {
float left = cam->pos.x - margin;
float top = cam->pos.y - margin;
float right = cam->pos.x + cam->viewport.x + margin;
float bottom = cam->pos.y + cam->viewport.y + margin;
float ex = body->pos.x + body->size.x;
float ey = body->pos.y + body->size.y;
return body->pos.x < right && ex > left &&
body->pos.y < bottom && ey > top;
}
void entity_manager_init(EntityManager *em) {
memset(em, 0, sizeof(EntityManager));
}
@@ -43,7 +69,8 @@ void entity_destroy(EntityManager *em, Entity *e) {
}
}
void entity_update_all(EntityManager *em, float dt, const Tilemap *map) {
void entity_update_all(EntityManager *em, float dt, const Tilemap *map,
const Camera *cam) {
for (int i = 0; i < em->count; i++) {
Entity *e = &em->entities[i];
if (!e->active) continue;
@@ -53,6 +80,14 @@ void entity_update_all(EntityManager *em, float dt, const Tilemap *map) {
e->flags |= ENTITY_DEAD;
}
/* Distance culling: skip far-away entities unless they opt out.
* Dead entities always get their update callback so type-specific
* cleanup (destroy, particles, etc.) can run. */
if (cam && !(e->flags & (ENTITY_ALWAYS_UPDATE | ENTITY_DEAD))) {
if (!in_camera_range(&e->body, cam, UPDATE_MARGIN))
continue;
}
/* Call type-specific update */
if (em->update_fn[e->type]) {
em->update_fn[e->type](e, dt, map);
@@ -65,6 +100,10 @@ void entity_render_all(EntityManager *em, const Camera *cam) {
Entity *e = &em->entities[i];
if (!e->active) continue;
/* Render culling: skip entities outside the visible area. */
if (cam && !in_camera_range(&e->body, cam, RENDER_MARGIN))
continue;
if (em->render_fn[e->type]) {
em->render_fn[e->type](e, cam);
}