fix crashes related to chunk loading

This commit is contained in:
onezplpl 2024-07-14 12:28:56 -04:00
parent 5c914908d5
commit 2b53defbb7
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
3 changed files with 28 additions and 22 deletions

View file

@ -15,8 +15,8 @@ typedef struct Vertex2 {
} Vertex2; } Vertex2;
#define CHUNK_LENGTH 16 #define CHUNK_LENGTH 16
#define CHUNK_RADIUS_H 8 // 8 #define CHUNK_RADIUS_H 5 // 8
#define CHUNK_RADIUS_V 8 // 4 #define CHUNK_RADIUS_V 5 // 4
#define TEXT_GAP_H 1 / 3.0 #define TEXT_GAP_H 1 / 3.0
#define TEXT_GAP_V 1 / 1.0 #define TEXT_GAP_V 1 / 1.0

7
gen.c
View file

@ -86,9 +86,6 @@ Chunk fetch_chunk(int x, int y, int z, int ci) {
fclose(fp); fclose(fp);
return chunk; return chunk;
} }
if (fp != 0) {
fclose(fp);
}
if (!fp) { if (!fp) {
fp = fopen(fname, "wb"); fp = fopen(fname, "wb");
} }
@ -124,6 +121,10 @@ Chunk fetch_chunk(int x, int y, int z, int ci) {
chunks2[i] = chunk; chunks2[i] = chunk;
if (!fp) {
return chunk;
}
fwrite(&chunk, sizeof(Chunk), 1, fp); fwrite(&chunk, sizeof(Chunk), 1, fp);
fclose(fp); fclose(fp);

33
main.c
View file

@ -169,13 +169,14 @@ int main(void) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double frames = 0.0; double frames = 0.0;
pthread_t thread_id; pthread_t thread_id = 0;
int is_render = 1; int is_render = 1;
glfwSetTime(7.5);
int last_render = 0;
vec3 oldPos = {pos[0], pos[1], pos[2]};
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
vec3 oldPos = {pos[0], pos[1], pos[2]};
glfwGetFramebufferSize(window, &width, &height); glfwGetFramebufferSize(window, &width, &height);
const float ratio = width / (float)height; const float ratio = width / (float)height;
@ -222,7 +223,7 @@ int main(void) {
int ay = (int)(oldPos[1] / CHUNK_LENGTH) - (int)(pos[1] / 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); 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; struct args args;
args.cube_count = &cube_count; args.cube_count = &cube_count;
@ -234,28 +235,32 @@ int main(void) {
args.vertex_buffer = vertex_buffer; args.vertex_buffer = vertex_buffer;
args.is_render = &is_render; args.is_render = &is_render;
if (thread_id != 0) last_render = 120;
pthread_join(thread_id, NULL);
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)); pthread_create(&(thread_id), NULL, render_chunks, (void *)&(args));
}
frames = 0;
glfwSetTime(0);
} }
frames = glfwGetTime(); last_render--;
//Sprintf("%f\n", frames);
glfwSwapBuffers(window); glfwSwapBuffers(window);
glfwPollEvents(); glfwPollEvents();
if (is_render == 2) { if (is_render == 2) {
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); 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]); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count*sizeof(Vertex), cube); glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(Vertex),
is_render = 1; cube);
if (thread_id != 0)
pthread_join(thread_id,NULL);
thread_id = 0;
} }
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);