Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct s_chunk_thread_data
- {
- int state;
- s_lin_arena arena;
- s_transform* blocks[c_chunk_size][c_chunk_size];
- s_shared_circular_buffer<s_v2i, c_chunk_count * c_chunk_count> chunks_completed;
- };
- if(!game->chunks_generated) {
- game->chunks_generated = true;
- void* memory = malloc(MB(100));
- game->chunk_thread_data.arena = make_arena_from_memory(memory, "thread", MB(100));
- reset_chunks();
- generate_chunks(&game->chunk_thread_data);
- }
- func void reset_chunks()
- {
- for(int chunk_x = 0; chunk_x < c_chunk_count; chunk_x++) {
- for(int chunk_z = 0; chunk_z < c_chunk_count; chunk_z++) {
- s_chunk* chunk = &game->chunks[chunk_x][chunk_z];
- chunk->generated = false;
- memset(chunk->blocks, 0, sizeof(b8) * c_chunk_size * c_chunk_size * c_chunk_height);
- }
- }
- }
- func DWORD WINAPI generate_chunks(void* param)
- {
- s_chunk_thread_data* chunk_thread_data = (s_chunk_thread_data*)param;
- for(int x = 0; x < c_chunk_count * c_chunk_size; x++) {
- for(int z = 0; z < c_chunk_count * c_chunk_size; z++) {
- s_v2i chunk_index = v2i(x / 16, z / 16);
- s_chunk* chunk = &game->chunks[chunk_index.x][chunk_index.y];
- float f = fnlGetNoise2D(&game->noise, (float)x, (float)z);
- int y = (int)range_lerp(f, -1, 1, 0, c_chunk_height - 1);
- for(int i = 0; i <= y; i++) {
- chunk->blocks[x % c_chunk_size][i][z % c_chunk_size] = true;
- }
- }
- }
- for(int chunk_x = 0; chunk_x < c_chunk_count; chunk_x++) {
- for(int chunk_z = 0; chunk_z < c_chunk_count; chunk_z++) {
- int count = 0;
- s_chunk* chunk = &game->chunks[chunk_x][chunk_z];
- chunk->num_visible_blocks = count_visible_blocks_in_chunk(v2i(chunk_x, chunk_z));
- printf("processing chunk %i, %i (%i blocks)\n", chunk_x, chunk_z, chunk->num_visible_blocks);
- chunk_thread_data->blocks[chunk_x][chunk_z] = (s_transform*)la_get_zero(&chunk_thread_data->arena, sizeof(s_transform) * chunk->num_visible_blocks);
- for(int x = 0; x < c_chunk_size; x++) {
- for(int y = 0; y < c_chunk_height; y++) {
- for(int z = 0; z < c_chunk_size; z++) {
- int xx = x + chunk_x * c_chunk_size;
- int zz = z + chunk_z * c_chunk_size;
- if(!is_block_visible(v2i(chunk_x, chunk_z), v3i(x, y, z))) { continue; }
- s_m4 model = m4_identity();
- s_v3 pos = v3(xx, y, zz) * c_block_size;
- model = m4_multiply(model, m4_translate(pos));
- model = m4_multiply(model, m4_scale(v3(c_block_size)));
- chunk_thread_data->blocks[chunk_x][chunk_z][count].do_light = 0;
- chunk_thread_data->blocks[chunk_x][chunk_z][count].model = model;
- chunk_thread_data->blocks[chunk_x][chunk_z][count].color = v4(
- range_lerp((float)xx, 0, (float)c_chunk_size * c_chunk_count - 1, 0, 1),
- range_lerp((float)y, 0, (float)c_chunk_height - 1, 0, 1),
- range_lerp((float)zz, 0, (float)c_chunk_size * c_chunk_count - 1, 0, 1),
- 1.0f
- );
- count += 1;
- }
- }
- }
- assert(count == chunk->num_visible_blocks);
- chunk_thread_data->chunks_completed.add(v2i(chunk_x, chunk_z));
- }
- }
- chunk_thread_data->state = 1;
- return 0;
- }
- template <typename t, int n>
- void s_shared_circular_buffer<t, n>::add(t new_element)
- {
- int index = InterlockedIncrement((LONG*)&write_index) - 1;
- elements[index % n] = new_element;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement