@@ -66,7 +66,7 @@ void World_LoadSingleplayer(void) {
66
66
Screen_Switch (SCREEN_GAME );
67
67
68
68
world .loadChunks = true;
69
- World_LoadChunks (false );
69
+ World_LoadChunks ();
70
70
}
71
71
72
72
clock_t updateClock ;
@@ -82,17 +82,24 @@ void World_Update(void) {
82
82
}
83
83
84
84
pthread_mutex_t chunk_mutex ;
85
+ pthread_mutex_t genChunk_mutex ;
85
86
void * World_ReadChunksQueues (void * state ) {
86
87
while (true) {
87
88
88
89
pthread_mutex_lock (& chunk_mutex );
89
90
90
- QueuedChunk * queuedChunk = world . generateChunksQueue ;
91
-
92
- if ( world . loadChunks == true && queuedChunk != NULL ) {
93
- Chunk_Generate ( queuedChunk -> chunk );
91
+ if ( world . loadChunks == true) {
92
+
93
+ Vector3 pos = ( Vector3 ) {( int ) floor ( player . position . x / CHUNK_SIZE_X ), ( int ) floor ( player . position . y / CHUNK_SIZE_Y ), ( int ) floor ( player . position . z / CHUNK_SIZE_Z )};
94
+ int index = World_GetClosestChunkIndex ( world . generateChunksQueue , pos );
94
95
95
- if (arrlen (world .generateChunksQueue ) > 0 ) arrdel (world .generateChunksQueue , 0 );
96
+ if (index != -1 ) {
97
+ Chunk_Generate (world .generateChunksQueue [index ]);
98
+ pthread_mutex_lock (& genChunk_mutex );
99
+ arrdel (world .generateChunksQueue , index );
100
+ pthread_mutex_unlock (& genChunk_mutex );
101
+ }
102
+
96
103
}
97
104
98
105
pthread_mutex_unlock (& chunk_mutex );
@@ -101,22 +108,35 @@ void *World_ReadChunksQueues(void *state) {
101
108
return NULL ;
102
109
}
103
110
111
+ int World_GetClosestChunkIndex (Chunk * * array , Vector3 pos ) {
112
+ int arrLength = arrlen (array );
113
+ if (arrLength > 0 ) {
114
+ Chunk * queuedChunk = array [0 ];
115
+ int index = 0 ;
116
+ for (int i = 0 ; i < arrLength ; i ++ ) {
117
+ if (Vector3Distance (array [i ]-> position , pos ) < Vector3Distance (queuedChunk -> position , pos )) {
118
+ queuedChunk = array [i ];
119
+ index = i ;
120
+ }
121
+ }
122
+ return index ;
123
+ }
124
+
125
+ return -1 ;
126
+ }
127
+
104
128
void World_QueueChunk (Chunk * chunk ) {
105
129
106
130
if (chunk -> hasStartedGenerating == false) {
107
- QueuedChunk queued ;
108
- queued .chunk = chunk ;
109
- queued .state = 0 ;
110
- arrput (world .generateChunksQueue , queued );
131
+ pthread_mutex_lock (& genChunk_mutex );
132
+ arrput (world .generateChunksQueue , chunk );
133
+ pthread_mutex_unlock (& genChunk_mutex );
111
134
112
135
}
113
136
chunk -> hasStartedGenerating = true;
114
137
115
138
if (chunk -> isBuilding == false) {
116
- QueuedChunk queued ;
117
- queued .chunk = chunk ;
118
- queued .state = 0 ;
119
- arrput (world .buildChunksQueue , queued );
139
+ arrput (world .buildChunksQueue , chunk );
120
140
chunk -> isBuilding = true;
121
141
}
122
142
}
@@ -164,68 +184,40 @@ void World_UpdateChunks(void) {
164
184
165
185
int meshUpdatesCount = 4 ;
166
186
167
- for (int i = arrlen (world .buildChunksQueue ) - 1 ; i >= 0 ; i -- ) {
168
- Chunk * chunk = world .buildChunksQueue [i ].chunk ;
187
+ for (int i = 0 ; i < meshUpdatesCount ; i ++ ) {
188
+ Vector3 pos = (Vector3 ) {(int )floor (player .position .x / CHUNK_SIZE_X ), (int )floor (player .position .y / CHUNK_SIZE_Y ), (int )floor (player .position .z / CHUNK_SIZE_Z )};
189
+ int index = World_GetClosestChunkIndex (world .buildChunksQueue , pos );
190
+ if (index == -1 ) continue ;
191
+ Chunk * chunk = world .buildChunksQueue [index ];
169
192
if (chunk -> isLightGenerated == true) {
170
193
if (Chunk_AreNeighbourGenerated (chunk ) == true) {
171
194
Chunk_BuildMesh (chunk );
172
195
chunk -> isBuilding = false;
173
- arrdel (world .buildChunksQueue , i );
174
- if (-- meshUpdatesCount == 0 ) return ;
196
+ arrdel (world .buildChunksQueue , index );
175
197
176
198
continue ;
177
199
}
178
200
}
179
201
}
202
+
180
203
}
181
204
182
- void World_LoadChunks (bool loadEdges ) {
205
+ void World_LoadChunks (void ) {
183
206
184
207
if (!world .loadChunks ) return ;
185
208
186
209
Vector3 pos = (Vector3 ) {(int )floor (player .position .x / CHUNK_SIZE_X ), (int )floor (player .position .y / CHUNK_SIZE_Y ), (int )floor (player .position .z / CHUNK_SIZE_Z )};
187
-
210
+
188
211
//Create chunks or prepare array of chunks to be sorted
189
212
int loadingHeight = fmin (world .drawDistance , 4 );
190
- int sortedLength = 0 ;
191
- struct { Vector3 chunkPos ; float dist ; } sortedChunks [(world .drawDistance + 1 ) * 2 * (world .drawDistance + 1 ) * 2 * (loadingHeight + 1 ) * 2 ];
192
- for (int x = - world .drawDistance ; x <= world .drawDistance ; x ++ ) {
193
- for (int z = - world .drawDistance ; z <= world .drawDistance ; z ++ ) {
194
- for (int y = - loadingHeight ; y <= loadingHeight ; y ++ ) {
213
+ for (int y = loadingHeight ; y >= - loadingHeight ; y -- ) {
214
+ for (int x = - world .drawDistance ; x <= world .drawDistance ; x ++ ) {
215
+ for (int z = - world .drawDistance ; z <= world .drawDistance ; z ++ ) {
195
216
Vector3 chunkPos = (Vector3 ) {pos .x + x , pos .y + y , pos .z + z };
196
- if (!loadEdges ) {
197
- sortedChunks [sortedLength ].chunkPos = chunkPos ;
198
- sortedChunks [sortedLength ].dist = Vector3Distance (chunkPos , pos );
199
- sortedLength ++ ;
200
- } else if (Vector3Distance (chunkPos , pos ) >= world .drawDistance - 1 ) {
201
- World_AddChunk (chunkPos );
202
- }
217
+ World_AddChunk (chunkPos );
203
218
}
204
219
}
205
220
}
206
-
207
- if (!loadEdges ) {
208
- //Sort array of chunks front to back
209
- for (int i = 1 ; i < sortedLength ; i ++ ) {
210
- int j = i ;
211
- while (j > 0 && sortedChunks [j - 1 ].dist > sortedChunks [j ].dist ) {
212
-
213
- struct { Vector3 chunkPos ; float dist ; } tempC ;
214
- tempC .chunkPos = sortedChunks [j ].chunkPos ;
215
- tempC .dist = sortedChunks [j ].dist ;
216
-
217
- sortedChunks [j ] = sortedChunks [j - 1 ];
218
- sortedChunks [j - 1 ].chunkPos = tempC .chunkPos ;
219
- sortedChunks [j - 1 ].dist = tempC .dist ;
220
- j = j - 1 ;
221
- }
222
- }
223
-
224
- //Create the chunks
225
- for (int i = 0 ; i < sortedLength ; i ++ ) {
226
- World_AddChunk (sortedChunks [i ].chunkPos );
227
- }
228
- }
229
221
230
222
//destroy far chunks
231
223
for (int i = hmlen (world .chunks ) - 1 ; i >= 0 ; i -- ) {
@@ -247,7 +239,7 @@ void World_LoadChunks(bool loadEdges) {
247
239
void World_Reload (void ) {
248
240
World_Unload ();
249
241
world .loadChunks = true;
250
- World_LoadChunks (false );
242
+ World_LoadChunks ();
251
243
}
252
244
253
245
void World_Unload (void ) {
0 commit comments