@@ -32,13 +32,17 @@ void CreateWorld(World* world, SDL_GPUDevice* device)
3232 for (int x = 0 ; x < WORLD_WIDTH ; x ++ )
3333 for (int z = 0 ; z < WORLD_WIDTH ; z ++ )
3434 {
35- world -> SortedChunks [x ][z ][0 ] = x ;
36- world -> SortedChunks [x ][z ][1 ] = z ;
3735 world -> Chunks [x ][z ] = SDL_malloc (sizeof (Chunk ));
3836 CreateChunk (world -> Chunks [x ][z ], device );
3937 }
40- int w = WORLD_WIDTH ;
41- SortXY (w / 2 , w / 2 , (int * ) world -> SortedChunks , w * w );
38+ for (int x = 0 ; x < WORLD_WIDTH - 2 ; x ++ )
39+ for (int z = 0 ; z < WORLD_WIDTH - 2 ; z ++ )
40+ {
41+ world -> SortedChunks [x ][z ][0 ] = x + 1 ;
42+ world -> SortedChunks [x ][z ][1 ] = z + 1 ;
43+ }
44+ int w = WORLD_WIDTH - 2 ;
45+ SortXY (w / 2 + 1 , w / 2 + 1 , (int * ) world -> SortedChunks , w * w );
4246}
4347
4448void DestroyWorld (World * world )
@@ -76,6 +80,7 @@ static void Move(World* world, const Camera* camera)
7680 for (int x = 0 ; x < WORLD_WIDTH ; x ++ )
7781 for (int z = 0 ; z < WORLD_WIDTH ; z ++ )
7882 {
83+ SDL_assert (world -> Chunks [x ][z ]);
7984 const int a = x - offsetX ;
8085 const int b = z - offsetZ ;
8186 if (Contains (world , a , b ))
@@ -94,6 +99,7 @@ static void Move(World* world, const Camera* camera)
9499 {
95100 if (!world -> Chunks [x ][z ])
96101 {
102+ SDL_assert (size > 0 );
97103 Chunk * chunk = out [-- size ];
98104 chunk -> Flags |= ChunkFlagGenerate ;
99105 world -> Chunks [x ][z ] = chunk ;
@@ -139,25 +145,37 @@ void UpdateWorld(World* world, const Camera* camera, Save* save, Noise* noise)
139145 SDL_CancelGPUCommandBuffer (commandBuffer );
140146 return ;
141147 }
148+ bool generated = true;
142149 for (int x = 0 ; x < WORLD_WIDTH ; x ++ )
143150 for (int y = 0 ; y < WORLD_WIDTH ; y ++ )
144151 {
145- int a = world -> SortedChunks [x ][y ][0 ];
146- int b = world -> SortedChunks [x ][y ][1 ];
147- Chunk * chunk = world -> Chunks [a ][b ];
152+ Chunk * chunk = world -> Chunks [x ][y ];
148153 if (chunk -> Flags & ChunkFlagGenerate )
149154 {
150155 GenerateChunk (chunk , noise );
151- // save
156+ generated = false;
157+ // TODO: use save
158+ // return;
152159 }
153- if (chunk -> Flags & ChunkFlagMesh )
160+ }
161+ if (generated )
162+ {
163+ for (int x = 0 ; x < WORLD_WIDTH - 2 ; x ++ )
164+ for (int y = 0 ; y < WORLD_WIDTH - 2 ; y ++ )
154165 {
166+ int a = world -> SortedChunks [x ][y ][0 ];
167+ int b = world -> SortedChunks [x ][y ][1 ];
168+ Chunk * chunk = world -> Chunks [a ][b ];
169+ if (!(chunk -> Flags & ChunkFlagMesh ))
170+ {
171+ continue ;
172+ }
155173 Chunk * neighbors [3 ][3 ] = {0 };
156174 for (int i = -1 ; i <= 1 ; i ++ )
157175 for (int j = -1 ; j <= 1 ; j ++ )
158176 {
159177 int k = a + i ;
160- int l = y + j ;
178+ int l = b + j ;
161179 if (Contains (world , k , l ))
162180 {
163181 neighbors [i + 1 ][j + 1 ] = world -> Chunks [k ][l ];
@@ -168,6 +186,7 @@ void UpdateWorld(World* world, const Camera* camera, Save* save, Noise* noise)
168186 {
169187 CreateIndexBuffer (world , pass , chunk -> VoxelBuffers [i ].Size * 1.5 );
170188 }
189+ // return;
171190 }
172191 }
173192 SDL_EndGPUCopyPass (pass );
@@ -176,8 +195,8 @@ void UpdateWorld(World* world, const Camera* camera, Save* save, Noise* noise)
176195
177196void RenderWorld (World * world , const Camera * camera , SDL_GPUCommandBuffer * commandBuffer , SDL_GPURenderPass * pass , ChunkMeshType type )
178197{
179- for (int x = 0 ; x < WORLD_WIDTH ; x ++ )
180- for (int y = 0 ; y < WORLD_WIDTH ; y ++ )
198+ for (int x = 0 ; x < WORLD_WIDTH - 2 ; x ++ )
199+ for (int y = 0 ; y < WORLD_WIDTH - 2 ; y ++ )
181200 {
182201 int a = world -> SortedChunks [x ][y ][0 ];
183202 int b = world -> SortedChunks [x ][y ][1 ];
0 commit comments