Skip to content

Commit 8c41812

Browse files
committed
Optimize case 15 of marching squares
1 parent f7909ca commit 8c41812

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

Source/engine/render/light_render.cpp

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,20 @@ namespace {
1818

1919
std::vector<uint8_t> LightmapBuffer;
2020

21+
void RenderFullTile(Point position, uint8_t lightLevel, uint8_t *lightmap, uint16_t pitch)
22+
{
23+
uint8_t *top = lightmap + (position.y + 1) * pitch + position.x - TILE_WIDTH / 2;
24+
uint8_t *bottom = top + (TILE_HEIGHT - 2) * pitch;
25+
for (int y = 0, w = 4; y < TILE_HEIGHT / 2 - 1; y++, w += 4) {
26+
int x = (TILE_WIDTH - w) / 2;
27+
memset(top + x, lightLevel, w);
28+
memset(bottom + x, lightLevel, w);
29+
top += pitch;
30+
bottom -= pitch;
31+
}
32+
memset(top, lightLevel, TILE_WIDTH);
33+
}
34+
2135
// Half-space method for drawing triangles
2236
// Points must be provided using counter-clockwise rotation
2337
// https://web.archive.org/web/20050408192410/http://sw-shader.sourceforge.net/rasterizer.html
@@ -367,8 +381,13 @@ void RenderCell(uint8_t quad[4], Point position, uint8_t lightLevel, uint8_t *li
367381
// Fill in the whole cell
368382
// All four tiles in the quad are lit
369383
case 15: {
370-
RenderTriangle(fpCenter0, fpCenter2, fpCenter1, lightLevel, lightmap, pitch, scanLines);
371-
RenderTriangle(fpCenter0, fpCenter3, fpCenter2, lightLevel, lightmap, pitch, scanLines);
384+
if (center3.x < 0 || center1.x >= pitch || center0.y < 0 || center2.y >= scanLines) {
385+
RenderTriangle(fpCenter0, fpCenter2, fpCenter1, lightLevel, lightmap, pitch, scanLines);
386+
RenderTriangle(fpCenter0, fpCenter3, fpCenter2, lightLevel, lightmap, pitch, scanLines);
387+
} else {
388+
// Optimized rendering path if full tile is visible
389+
RenderFullTile(center0, lightLevel, lightmap, pitch);
390+
}
372391
} break;
373392
}
374393
}

0 commit comments

Comments
 (0)