Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed-up debug grid #7436

Merged
merged 1 commit into from
Sep 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading