From 5484039e3e9433936c2d400bb1ecde21adb3d9d5 Mon Sep 17 00:00:00 2001 From: Sara Gerretsen Date: Tue, 23 Sep 2025 14:25:26 +0200 Subject: [PATCH] feat: added basic culling to cell renderer --- src/simulation.cpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/simulation.cpp b/src/simulation.cpp index e50de58..c068106 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -271,8 +271,8 @@ void Draw(SDL_Renderer *renderer, double cellSizePercent) { viewOffset.x += input::scrollMotion.x; viewOffset.y += input::scrollMotion.y; } - int w; - SDL_GetCurrentRenderOutputSize(renderer, &w, nullptr); + int w, h; + SDL_GetCurrentRenderOutputSize(renderer, &w, &h); float const cellWidth = static_cast(w) * cellSizePercent; SDL_FRect cellRect{ 0, 0, cellWidth, cellWidth @@ -281,6 +281,8 @@ void Draw(SDL_Renderer *renderer, double cellSizePercent) { for (Cell const &cell : living) { cellRect.x = (viewOffset.x + cell.x) * cellRect.w; cellRect.y = (viewOffset.y + cell.y) * cellRect.h; + if (cellRect.x < -cellRect.w || cellRect.y < -cellRect.h || cellRect.x > w || cellRect.y > h) + continue; SDL_RenderFillRect(renderer, &cellRect); } if (!drawDebugInfo) { @@ -293,6 +295,8 @@ void Draw(SDL_Renderer *renderer, double cellSizePercent) { for (Cell const &cell : wl->changes) { cellRect.x = (viewOffset.x + cell.x) * cellRect.w; cellRect.y = (viewOffset.y + cell.y) * cellRect.h; + if (cellRect.x < -cellRect.w || cellRect.y < -cellRect.h || cellRect.x > w || cellRect.y > h) + continue; SDL_RenderRect(renderer, &cellRect); } } @@ -302,6 +306,8 @@ void Draw(SDL_Renderer *renderer, double cellSizePercent) { for (Cell const &cell : wl->changes) { cellRect.x = (viewOffset.x + cell.x) * cellRect.w; cellRect.y = (viewOffset.y + cell.y) * cellRect.h; + if (cellRect.x < -cellRect.w || cellRect.y < -cellRect.h || cellRect.x > w || cellRect.y > h) + continue; SDL_RenderRect(renderer, &cellRect); } } @@ -311,6 +317,8 @@ void Draw(SDL_Renderer *renderer, double cellSizePercent) { for (Cell const &cell : wl->changes) { cellRect.x = (viewOffset.x + cell.x) * cellRect.w; cellRect.y = (viewOffset.y + cell.y) * cellRect.h; + if (cellRect.x < -cellRect.w || cellRect.y < -cellRect.h || cellRect.x > w || cellRect.y > h) + continue; SDL_RenderRect(renderer, &cellRect); } }