From 0797e540d67e185968e007baac0527704e19271a Mon Sep 17 00:00:00 2001 From: Zeno Rogue Date: Sun, 27 Apr 2025 01:23:00 +0200 Subject: [PATCH] render entities only if visible --- rogueviz/ru/classes.cpp | 2 ++ rogueviz/ru/entity.cpp | 6 ++++++ rogueviz/ru/geometry.cpp | 15 ++++++++++++++- rogueviz/ru/render.cpp | 4 +++- 4 files changed, 25 insertions(+), 2 deletions(-) diff --git a/rogueviz/ru/classes.cpp b/rogueviz/ru/classes.cpp index 04aae650..f425f0d7 100644 --- a/rogueviz/ru/classes.cpp +++ b/rogueviz/ru/classes.cpp @@ -116,6 +116,8 @@ struct entity { double gwhere_x, gwhere_y; double gvel_x, gvel_y; + bool visible(room *r); + void clearg() { gwhere_x = where_x; gwhere_y = where_y; diff --git a/rogueviz/ru/entity.cpp b/rogueviz/ru/entity.cpp index f327e85a..264bea5a 100644 --- a/rogueviz/ru/entity.cpp +++ b/rogueviz/ru/entity.cpp @@ -16,6 +16,12 @@ bbox entity::get_pixel_bbox_at(double x, double y) { return b; } +bool entity::visible(room *r) { + auto bb = get_intersect(pixel_to_block(get_pixel_bbox()), room_bb); + for(int y = bb.miny; y < bb.maxy; y++) for(int x = bb.minx; x < bb.maxx; x++) if(r->fov[y][x]) return true; + return false; + } + void entity::apply_grav() { if(non_hyperbolic) return apply_portal_grav(); diff --git a/rogueviz/ru/geometry.cpp b/rogueviz/ru/geometry.cpp index 9f8ead50..39061d06 100644 --- a/rogueviz/ru/geometry.cpp +++ b/rogueviz/ru/geometry.cpp @@ -94,8 +94,21 @@ bbox join(bbox a, bbox b) { return r; } +bbox room_bb{0, 0, room_x, room_y}; +bbox screen_bb{0, 0, screen_x, screen_y}; + +bbox get_intersect(bbox a, bbox b) { + bbox r; + r.minx = max(a.minx, b.minx); + r.miny = max(a.miny, b.miny); + r.maxx = min(a.maxx, b.maxx); + r.maxy = min(a.maxy, b.maxy); + return r; + } + bool intersect(bbox a, bbox b) { - return max(a.minx, b.minx) < min(a.maxx, b.maxx) && max(a.miny, b.miny) < min(a.maxy, b.maxy); + auto g = get_intersect(a, b); + return g.minx < g.maxx && g.miny < g.maxy; } void print(hstream& hs, const bbox& b) { diff --git a/rogueviz/ru/render.cpp b/rogueviz/ru/render.cpp index 84dc3431..0dd82b7e 100644 --- a/rogueviz/ru/render.cpp +++ b/rogueviz/ru/render.cpp @@ -245,7 +245,9 @@ void man::draw() { void render_room_objects(room *r) { initquickqueue(); if(r == current_room) m.draw(); - for(auto& e: r->entities) e->draw(); + for(auto& e: r->entities) + if(e->visible(r)) + e->draw(); quickqueue(); }