I looked at the loop and I can't see the problem. After creating the new chunk and changing the Chunk pointer to point to the new chunk we would just enter the following if-statement where we initialize the chunk data and break out of the loop.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40 | do
{
if((TileChunkX == Chunk->TileChunkX) &&
(TileChunkY == Chunk->TileChunkY) &&
(TileChunkZ == Chunk->TileChunkZ))
{
break;
}
if(Arena && (Chunk->TileChunkX != TILE_CHUNK_UNINITIALIZED) && (!Chunk->NextInHash))
{
Chunk->NextInHash = PushStruct(Arena, tile_chunk);
Chunk = Chunk->NextInHash; //// <-- point to new chunk
Chunk->TileChunkX = TILE_CHUNK_UNINITIALIZED; //// <-- flag new chunk as uninitialized
}
if(Arena && (Chunk->TileChunkX == TILE_CHUNK_UNINITIALIZED)) //// <-- this is now true
{
uint32 TileCount = TileMap->ChunkDim*TileMap->ChunkDim;
Chunk->TileChunkX = TileChunkX;
Chunk->TileChunkY = TileChunkY;
Chunk->TileChunkZ = TileChunkZ;
Chunk->Tiles = PushArray(Arena, TileCount, uint32);
// TODO(casey): Do we want to always initialize?
for(uint32 TileIndex = 0;
TileIndex < TileCount;
++TileIndex)
{
Chunk->Tiles[TileIndex] = 1;
}
Chunk->NextInHash = 0;
break; //// <-- break here
}
Chunk = Chunk->NextInHash;
} while(Chunk);
|
Your suggested fix would cause us an extra run through the loop since the chunk pointer won't be changed to point to the newly created chunk until the very last line. Other than that I can't see any difference (please tell me if I'm wrong, it is 1.20 AM here and I should probably sleep instead of posting things on the internet :P).