diff --git a/const.h b/const.h index e21c01d..8dfa114 100644 --- a/const.h +++ b/const.h @@ -15,8 +15,8 @@ typedef struct Vertex2 { } Vertex2; #define CHUNK_LENGTH 16 -#define CHUNK_RADIUS_H 8 // 8 -#define CHUNK_RADIUS_V 8 // 4 +#define CHUNK_RADIUS_H 5 // 8 +#define CHUNK_RADIUS_V 5 // 4 #define TEXT_GAP_H 1 / 3.0 #define TEXT_GAP_V 1 / 1.0 diff --git a/gen.c b/gen.c index b00d13f..45826d2 100644 --- a/gen.c +++ b/gen.c @@ -86,9 +86,6 @@ Chunk fetch_chunk(int x, int y, int z, int ci) { fclose(fp); return chunk; } - if (fp != 0) { - fclose(fp); - } if (!fp) { fp = fopen(fname, "wb"); } @@ -124,6 +121,10 @@ Chunk fetch_chunk(int x, int y, int z, int ci) { chunks2[i] = chunk; + if (!fp) { + return chunk; + } + fwrite(&chunk, sizeof(Chunk), 1, fp); fclose(fp); diff --git a/main.c b/main.c index 1787676..f85df7c 100644 --- a/main.c +++ b/main.c @@ -116,7 +116,7 @@ int main(void) { Vertex *text = malloc(CTRI_ALL); int cube_count = gen_cubes(cube, text, 1024 / CHUNK_LENGTH, 64 / CHUNK_LENGTH, - 1024 / CHUNK_LENGTH, 0,1); + 1024 / CHUNK_LENGTH, 0, 1); GLuint vertex_buffer[2]; GLuint vertex_array; @@ -169,13 +169,14 @@ int main(void) { glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); double frames = 0.0; - pthread_t thread_id; + pthread_t thread_id = 0; int is_render = 1; - glfwSetTime(7.5); + + int last_render = 0; + vec3 oldPos = {pos[0], pos[1], pos[2]}; while (!glfwWindowShouldClose(window)) { - vec3 oldPos = {pos[0], pos[1], pos[2]}; glfwGetFramebufferSize(window, &width, &height); const float ratio = width / (float)height; @@ -217,12 +218,12 @@ int main(void) { mat4x4_mul(mvp, mvp, m); glUniformMatrix4fv(mvp_location, 1, GL_FALSE, (const GLfloat *)&mvp); - + int ax = (int)(oldPos[0] / CHUNK_LENGTH) - (int)(pos[0] / CHUNK_LENGTH); int ay = (int)(oldPos[1] / CHUNK_LENGTH) - (int)(pos[1] / CHUNK_LENGTH); int az = (int)(oldPos[2] / CHUNK_LENGTH) - (int)(pos[2] / CHUNK_LENGTH); - if ((ax != 0 || ay != 0 || az != 0) && frames > 4.0) { + if ((ax != 0 || ay != 0 || az != 0) && last_render < 0) { struct args args; args.cube_count = &cube_count; @@ -234,28 +235,32 @@ int main(void) { args.vertex_buffer = vertex_buffer; args.is_render = &is_render; - if (thread_id != 0) - pthread_join(thread_id, NULL); + last_render = 120; - pthread_create(&(thread_id), NULL, render_chunks, (void *)&(args)); - - frames = 0; - glfwSetTime(0); + if (thread_id == 0) { + oldPos[0] = pos[0]; + oldPos[1] = pos[1]; + oldPos[2] = pos[2]; + pthread_create(&(thread_id), NULL, render_chunks, (void *)&(args)); + } } - frames = glfwGetTime(); - //Sprintf("%f\n", frames); + last_render--; glfwSwapBuffers(window); glfwPollEvents(); if (is_render == 2) { glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); - glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count*sizeof(Vertex), text); + glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex), + text); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); - glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count*sizeof(Vertex), cube); - is_render = 1; + glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex), + cube); + if (thread_id != 0) + pthread_join(thread_id,NULL); + thread_id = 0; } glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);