diff --git a/tests/caveview/cave_mesher.c b/tests/caveview/cave_mesher.c index 6145212..fac488f 100644 --- a/tests/caveview/cave_mesher.c +++ b/tests/caveview/cave_mesher.c @@ -1,3 +1,5 @@ +// This file takes minecraft chunks (decoded by cave_parse) and +// uses stb_voxel_render to turn them into vertex buffers. #define STB_GLEXT_DECLARE "glext_list.h" #include "stb_gl.h" @@ -452,6 +454,7 @@ void reset_cache_size(int size) cache_size = size; } +// this must be called inside mutex void deref_fastchunk(fast_chunk *fc) { if (fc) { @@ -672,6 +675,9 @@ void mesh_init(void) { int i; + chunk_cache_mutex = SDL_CreateMutex(); + chunk_get_mutex = SDL_CreateMutex(); + for (i=0; i < 256; ++i) { memcpy(minecraft_tex1_for_blocktype[i], minecraft_info[i]+1, 6); minecraft_trans_for_blocktype[i] = (minecraft_info[i][0] != C_solid && minecraft_info[i][0] != C_water); diff --git a/tests/caveview/cave_render.c b/tests/caveview/cave_render.c index 9a3f6da..4d05cd1 100644 --- a/tests/caveview/cave_render.c +++ b/tests/caveview/cave_render.c @@ -1,3 +1,6 @@ +// This file takes renders vertex buffers and manages +// threads that invoke mesh building (found in cave_mesher.c) + #include "stb_voxel_render.h" #define STB_GLEXT_DECLARE "glext_list.h" @@ -57,10 +60,6 @@ typedef struct } chunk_mesh; -extern SDL_mutex * chunk_cache_mutex; -extern SDL_mutex * chunk_get_mutex; - - void scale_texture(unsigned char *src, int x, int y, int w, int h) { int i,j,k; @@ -511,6 +510,8 @@ void world_init(void) reset_cache_size(32); } +extern SDL_mutex * chunk_cache_mutex; + int mesh_worker_handler(void *data) { mesh_worker *mw = data; @@ -549,6 +550,8 @@ int mesh_worker_handler(void *data) // when done, free the chunks + // for efficiency we just take the mutex once around the whole thing, + // though this spreads the mutex logic over two files SDL_LockMutex(chunk_cache_mutex); for (j=0; j < 4; ++j) for (i=0; i < 4; ++i) { @@ -594,9 +597,6 @@ void prepare_threads(void) if (num_mesh_workers > MAX_MESH_WORKERS) num_mesh_workers = MAX_MESH_WORKERS; - chunk_cache_mutex = SDL_CreateMutex(); - chunk_get_mutex = SDL_CreateMutex(); - for (i=0; i < num_mesh_workers; ++i) { mesh_worker *data = &mesh_data[i]; data->request_received = SDL_CreateSemaphore(0);