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;
#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

7
gen.c
View file

@ -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);

37
main.c
View file

@ -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;
@ -222,7 +223,7 @@ int main(void) {
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);