From 9eb5eb0697942a247292c48fec02a62faf344ea0 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Thu, 26 Sep 2024 21:40:38 +0100 Subject: [PATCH] Speed-up debug grid `dev.display.grid()` previously tanked the FPS even on my beefy machine. It is now much faster. I've also noticed that the grid is glitchy when but this is also the case before this PR. --- Source/engine/render/scrollrt.cpp | 64 +++++++++++++++++-------------- 1 file changed, 35 insertions(+), 29 deletions(-) diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index 52f997ebfcd..32dc679376f 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -6,6 +6,7 @@ #include "engine/render/scrollrt.h" #include +#include #include #include @@ -1154,41 +1155,46 @@ void DrawView(const Surface &out, Point startPosition) { .flags = UiFlags::ColorRed | UiFlags::AlignCenter | UiFlags::VerticalCenter }); } if (DebugGrid) { - auto drawDebugSquare = [&out](Point center, Displacement hor, Displacement ver, uint8_t col) { - auto drawLine = [&out](Point from, Point to, uint8_t col) { - int dx = to.x - from.x; - int dy = to.y - from.y; - int steps = std::abs(dx) > std::abs(dy) ? std::abs(dx) : std::abs(dy); - auto ix = static_cast(dx) / static_cast(steps); - auto iy = static_cast(dy) / static_cast(steps); - auto sx = static_cast(from.x); - auto sy = static_cast(from.y); - - for (int i = 0; i <= steps; i++, sx += ix, sy += iy) - out.SetPixel({ static_cast(sx), static_cast(sy) }, col); - }; - drawLine(center - hor, center + ver, col); - drawLine(center + hor, center + ver, col); - drawLine(center - hor, center - ver, col); - drawLine(center + hor, center - ver, col); - }; - - Displacement hor = { TILE_WIDTH / 2, 0 }; - Displacement ver = { 0, TILE_HEIGHT / 2 }; + int halfTileWidth = TILE_WIDTH / 2; + int halfTileHeight = TILE_HEIGHT / 2; if (*sgOptions.Graphics.zoom) { - hor *= 2; - ver *= 2; + halfTileWidth *= 2; + halfTileHeight *= 2; } - Point center = pixelCoords + hor - ver; + const Point center { pixelCoords.x + halfTileWidth, pixelCoords.y - halfTileHeight }; if (megaTiles) { - hor *= 2; - ver *= 2; + halfTileWidth *= 2; + halfTileHeight *= 2; } - uint8_t col = PAL16_BEIGE; - - drawDebugSquare(center, hor, ver, col); + const uint8_t col = PAL16_BEIGE; + for (const auto &[originX, dx] : { std::pair(center.x - halfTileWidth, 1), std::pair(center.x + halfTileWidth, -1) }) { + // We only need to draw half of the grid cell boundaries (one triangle). + // The other triangle will be drawn when drawing the adjacent grid cells. + const int dy = 1; + Point from { originX, center.y }; + int height = halfTileHeight; + if (out.InBounds(from) && out.InBounds(from + Displacement { 2 * dx * height, dy * height })) { + uint8_t *dst = out.at(from.x, from.y); + const int pitch = out.pitch(); + while (height-- > 0) { + *dst = col; + dst += dx; + *dst = col; + dst += dx; + dst += static_cast(dy * pitch); + } + } else { + while (height-- > 0) { + out.SetPixel(from, col); + from.x += dx; + out.SetPixel(from, col); + from.x += dx; + from.y += dy; + } + } + } } } }