Register
Handmade Hero»Forums»Code»Day 035 - Sparse Tilemap Storage
19 posts
Day 035 - Sparse Tilemap Storage
2 months ago Edited by echu on Dec. 25, 2019, 11:05 a.m.
I just finished watching day 035 where Casey implements the sparse tilemap storage. He seems to end the code with a boxed in room with a single "staircase" and no doors, and I get that result when running from his original source. I got right to the end, and everything seemed fine until he implemented the "staircase".

However, I still get doors generated, with strange results. I get a weird "break" in the stored buffer that's hard to describe, so I've attached some screenshots and my code. I've reduced the tile size and increased the number of drawn columns and rows, so you can see what I mean. My player is drawn in green and I've chosen to draw the invalided area in pink, but all the same concepts still apply.

After I implement the "staircase" (%2 and %3 in the lookup table).
Before I implement the "staircase" (%1 and %2 in the lookup table).
How the original day 035 source runs.

I know it's a problem somewhere within the random number generator and how the choices are being picked, since I can go back to the original behavior via %1 and %2 instead of %2 and %3. It can't be a random bug, because I'm using the exact same lookup table.

My code is heavily commented, so I'd appreciate a fresh set of eyes. I'm sure it's some small detail that I've just missed in the video.

This is my heavily commented code!
This is my build.bat, may or may not be useful.
19 posts
Day 035 - Sparse Tilemap Storage
2 months ago Edited by echu on Dec. 25, 2019, 11:45 a.m.
It turned out I made a very silly mistake in GetTileChunk() in handmade_tile.cpp. The TileChunk I thought I was getting had bogus indexing, so it was no surprise that I got stranded on "islands". Listening to the Q&A at 01:31:37 where Casey mentions the indexing scheme for tiles, I got the feeling to check it out. Low and behold...

Bugged
1
TileChunk = &TileMap->TileChunks[tileChunkZ * (tileChunkY * TileMap->tileChunkCountX) + (tileChunkY * TileMap->tileChunkCountX) + tileChunkX];


Corrected
1
TileChunk = &TileMap->TileChunks[tileChunkZ * TileMap->tileChunkCountY * TileMap->tileChunkCountX + tileChunkY * TileMap->tileChunkCountX + tileChunkX];


I've removed the code files, but the images should still work. Here is what I have for the final result.