Skip to content

Commit

Permalink
Speed-up debug grid
Browse files Browse the repository at this point in the history
`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.
  • Loading branch information
glebm committed Sep 26, 2024
1 parent b78ec23 commit 9eb5eb0
Showing 1 changed file with 35 additions and 29 deletions.
64 changes: 35 additions & 29 deletions Source/engine/render/scrollrt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "engine/render/scrollrt.h"

#include <cmath>
#include <cstddef>
#include <cstdint>

#include <ankerl/unordered_dense.h>
Expand Down Expand Up @@ -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<float>(dx) / static_cast<float>(steps);
auto iy = static_cast<float>(dy) / static_cast<float>(steps);
auto sx = static_cast<float>(from.x);
auto sy = static_cast<float>(from.y);

for (int i = 0; i <= steps; i++, sx += ix, sy += iy)
out.SetPixel({ static_cast<int>(sx), static_cast<int>(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<ptrdiff_t>(dy * pitch);
}
} else {
while (height-- > 0) {
out.SetPixel(from, col);
from.x += dx;
out.SetPixel(from, col);
from.x += dx;
from.y += dy;
}
}
}
}
}
}
Expand Down

0 comments on commit 9eb5eb0

Please sign in to comment.