break and place blocks

This commit is contained in:
onezplpl 2024-07-18 13:46:29 -04:00
parent 227d642be6
commit ed720e0ea0
No known key found for this signature in database
GPG key ID: 7EC026A136F9EEC3
2 changed files with 61 additions and 54 deletions

86
cubes.c
View file

@ -26,10 +26,10 @@ void cubes_face(struct v3f *cube, int i, int x, int y, int z, int x2, int y2,
int z2, int *j, int is_text, struct chunk dat, int ci, int a3) { int z2, int *j, int is_text, struct chunk dat, int ci, int a3) {
struct v3i a = {{x, y, z}}; struct v3i a = {{x, y, z}};
struct v3f a2 = {{x, y, z}}; struct v3f a2 = {{x, y, z}};
int exists = 0;
if (is_text) { if (is_text) {
a.pos[i]--; a.pos[i]--;
int exists = 0;
int type = gen_cube(x, y, z, dat, ci, -1); int type = gen_cube(x, y, z, dat, ci, -1);
int typeB = gen_cube(a.pos[0], a.pos[1], a.pos[2], dat, ci, -1); int typeB = gen_cube(a.pos[0], a.pos[1], a.pos[2], dat, ci, -1);
exists = ((!!type) != (!!typeB)); exists = ((!!type) != (!!typeB));
@ -37,10 +37,6 @@ void cubes_face(struct v3f *cube, int i, int x, int y, int z, int x2, int y2,
a2.pos[0] = exists ? TEXT_GAP_H * (type ? (type - 1) : (typeB - 1)) : -1.0; a2.pos[0] = exists ? TEXT_GAP_H * (type ? (type - 1) : (typeB - 1)) : -1.0;
a2.pos[1] = exists ? TEXT_GAP_V * (i + (!!type) * 3) : -1.0; a2.pos[1] = exists ? TEXT_GAP_V * (i + (!!type) * 3) : -1.0;
a2.pos[2] = 0; a2.pos[2] = 0;
if (!exists) {
*j = (*j - 6 * a3);
return;
}
} }
struct v3f b = a2; struct v3f b = a2;
@ -65,6 +61,10 @@ void cubes_face(struct v3f *cube, int i, int x, int y, int z, int x2, int y2,
cube[*j + 3] = c; cube[*j + 3] = c;
cube[*j + 4] = b; cube[*j + 4] = b;
cube[*j + 5] = d; cube[*j + 5] = d;
if (is_text && !exists) {
*j = (*j - 6 * a3);
return;
}
} }
void *cubes_chunk(void *args) { void *cubes_chunk(void *args) {
@ -84,12 +84,8 @@ void *cubes_chunk(void *args) {
struct v3f *cube = args2.cube; struct v3f *cube = args2.cube;
struct v3f *text = args2.text; struct v3f *text = args2.text;
struct v3f vNul = {{-10.0, -10.0, -10.0}}; struct v3f vNul = {{-1.0, -1.0, -1.0}};
if (x2 < 0 || y2 < 0 || z2 < 0) { if (x2 < 0 || y2 < 0 || z2 < 0) {
for (int a = i; a < args2.i + (BLOCK_ALL * 18) * a3; a += 6 * a3) {
cube[a] = vNul;
text[a] = vNul;
}
return 0; return 0;
} }
@ -105,8 +101,11 @@ void *cubes_chunk(void *args) {
printf("Loading world... (%i / %i)\n", a2, CHUNK_ALL); printf("Loading world... (%i / %i)\n", a2, CHUNK_ALL);
if (i > CBLOCK_ALL * 18) if (i > CBLOCK_ALL * 18) {
i2[a2] = i;
return (void *)i; return (void *)i;
}
for (int y4 = 0; y4 < CHUNK_LENGTH; y4++) { for (int y4 = 0; y4 < CHUNK_LENGTH; y4++) {
for (int x4 = 0; x4 < CHUNK_LENGTH; x4++) { for (int x4 = 0; x4 < CHUNK_LENGTH; x4++) {
@ -120,17 +119,16 @@ void *cubes_chunk(void *args) {
cubes_face(cube, m, x3, y3, z3, x4, y4, z4, &i, 0, chunk, a, a3); cubes_face(cube, m, x3, y3, z3, x4, y4, z4, &i, 0, chunk, a, a3);
cubes_face(text, m, x3, y3, z3, x4, y4, z4, &i, 1, chunk, a, a3); cubes_face(text, m, x3, y3, z3, x4, y4, z4, &i, 1, chunk, a, a3);
i += 6 * a3; i += 6 * a3;
if (i > CBLOCK_ALL * 18) if (i > CBLOCK_ALL * 18) {
i2[a2] = i;
return (void *)i; return (void *)i;
} }
} }
} }
} }
for (int a = i; a < args2.i + (BLOCK_ALL * 18) * a3; a += 6 * a3) {
cube[a] = vNul;
text[a] = vNul;
} }
i2[a2] = i;
return (void *)i; return (void *)i;
} }
@ -149,12 +147,6 @@ void cubes_refresh(int x, int y, int z, struct chunk dat) {
int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO, int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
struct v3f *textO, int x, int y, int z, int is_purge, int *i2) { struct v3f *textO, int x, int y, int z, int is_purge, int *i2) {
struct v3f noth = {{-1, -1, -1}};
for (int i = 0; i < CBLOCK_ALL * 18; i++) {
cube[i] = noth;
text[i] = noth;
}
int i = 0; int i = 0;
x *= CHUNK_LENGTH; x *= CHUNK_LENGTH;
y *= CHUNK_LENGTH; y *= CHUNK_LENGTH;
@ -164,16 +156,21 @@ int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
struct args args[a3]; struct args args[a3];
int thread_return[a3]; int thread_return[a3];
if (is_purge) { if (is_purge) {
for (int k = 0; k < a3; k++) {
gen_init(); gen_init();
} }
}
if (!chunk_old) { if (!chunk_old) {
chunk_old = calloc((CHUNK_ALL), sizeof(struct chunk)); chunk_old = calloc((CHUNK_ALL), sizeof(struct chunk));
} }
pthread_t thread_id[a3]; pthread_t thread_id[a3];
int j = 0; int j = 0;
struct v3f vNul = {{-1.0, -1.0, -1.0}};
for (int i = 0; i < CBLOCK_ALL * 18; i++) {
cubeO[i] = vNul;
textO[i] = vNul;
}
for (int y2 = y - (CHUNK_RADIUS_V)*CHUNK_LENGTH; for (int y2 = y - (CHUNK_RADIUS_V)*CHUNK_LENGTH;
y2 <= y + (CHUNK_RADIUS_V)*CHUNK_LENGTH; y2 += CHUNK_LENGTH) { y2 <= y + (CHUNK_RADIUS_V)*CHUNK_LENGTH; y2 += CHUNK_LENGTH) {
@ -184,11 +181,11 @@ int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
z2 <= z + (CHUNK_RADIUS_H)*CHUNK_LENGTH; z2 += CHUNK_LENGTH) { z2 <= z + (CHUNK_RADIUS_H)*CHUNK_LENGTH; z2 += CHUNK_LENGTH) {
struct v3i pos = { struct v3i pos = {
{x + (((x2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) * {x - (((x2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) *
CHUNK_LENGTH, CHUNK_LENGTH,
y + (((y2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_V)) - CHUNK_RADIUS_V) * y - (((y2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_V)) - CHUNK_RADIUS_V) *
CHUNK_LENGTH, CHUNK_LENGTH,
z + (((z2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) * z - (((z2 / CHUNK_LENGTH) % (CHUNK_DIAMETER_H)) - CHUNK_RADIUS_H) *
CHUNK_LENGTH}}; CHUNK_LENGTH}};
int a2 = a % a3; int a2 = a % a3;
@ -203,17 +200,18 @@ int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
/*gen_chunk((void *)&args); */ /*gen_chunk((void *)&args); */
struct chunk old_chunk = chunk_old[a]; int ib = i;
struct chunk old_chunk = chunk_old[a];
int cached = int cached =
(old_chunk.x == pos.pos[0] / CHUNK_LENGTH && (old_chunk.x == pos.pos[0] / CHUNK_LENGTH &&
old_chunk.y == pos.pos[1] / CHUNK_LENGTH && old_chunk.y == pos.pos[1] / CHUNK_LENGTH &&
old_chunk.z == pos.pos[2] / CHUNK_LENGTH && old_chunk.exists != 0); old_chunk.z == pos.pos[2] / CHUNK_LENGTH && old_chunk.exists != 0);
if (cached) { if (cached) {
thread_return[a2] = i2[a2 / a3] - 6;
thread_id[a2] = -1; thread_id[a2] = -1;
ib = i2[a];
} else if (pos.pos[0] < 0 || pos.pos[1] < 0 || pos.pos[2] < 0) { } else if (pos.pos[0] < 0 || pos.pos[1] < 0 || pos.pos[2] < 0) {
thread_return[a2] = -1; i2[a] = -6;
thread_id[a2] = -1; thread_id[a2] = -1;
} else { } else {
pthread_create(&(thread_id[a2]), NULL, cubes_chunk, pthread_create(&(thread_id[a2]), NULL, cubes_chunk,
@ -226,26 +224,32 @@ int cubes_vert(struct v3f *cube, struct v3f *text, struct v3f *cubeO,
} }
for (int k = 0; k < a3; k++) { for (int k = 0; k < a3; k++) {
if (thread_id[k] == -1) if (thread_id[k] == -1) {
continue; continue;
}
pthread_join((thread_id[k]), (void **)&thread_return[k]); pthread_join((thread_id[k]), (void **)&thread_return[k]);
} }
int max = 0; int max = 0;
int min = 0xFFFFFFF; int min = 0xFFFFFFF;
for (int a = 0; a < a3; a++) { for (int a9 = 0; a9 < a3; a9++) {
max = (max > thread_return[a]) ? max : thread_return[a]; int ii = a9 + a - CHUNK_DIAMETER_H;
min = (min < thread_return[a]) ? min : thread_return[a]; /*for (int a10 = i2[ii];
a10 < (BLOCK_ALL * 18) *
((ii / CHUNK_DIAMETER_H + 1) * CHUNK_DIAMETER_H) && a10 <
CBLOCK_ALL * 18; a10 += 6 * a3) { cube[a10] = vNul; text[a10] = vNul;
}*/
max = (max > i2[ii]) ? max : i2[ii];
min = (min < i2[ii]) ? min : i2[ii];
} }
max += 6; if (max > i - 6 * a3) {
i2[(a / a3) - 1] = max;
if (max > i) {
memcpy(&(cubeO[j]), &(cube[i - 6 * a3]), memcpy(&(cubeO[j]), &(cube[i - 6 * a3]),
(max - i) * sizeof(struct v3f)); (max - (i - 6 * a3)) * sizeof(struct v3f));
memcpy(&(textO[j]), &(text[i - 6 * a3]), memcpy(&(textO[j]), &(text[i - 6 * a3]),
(max - i) * sizeof(struct v3f)); (max - (i - 6 * a3)) * sizeof(struct v3f));
j += max - i; j += max - (i - 6 * a3);
} }
i = (BLOCK_ALL * 18) * a; // i = max + 6; i = (BLOCK_ALL * 18) * a; // i = max + 6;
} }
} }

23
main.c
View file

@ -147,7 +147,7 @@ int main(void) {
struct v3f *cubeO = malloc(CTRI_ALL); struct v3f *cubeO = malloc(CTRI_ALL);
struct v3f *textO = malloc(CTRI_ALL); struct v3f *textO = malloc(CTRI_ALL);
int bleh = (CHUNK_DIAMETER_H) * (CHUNK_DIAMETER_V); int bleh = (CHUNK_DIAMETER_H) * (CHUNK_DIAMETER_V);
int *i2 = calloc(bleh, sizeof(int)); int *i2 = calloc(CHUNK_ALL, sizeof(int));
GLuint vertex_buffer[2]; GLuint vertex_buffer[2];
@ -217,7 +217,7 @@ int main(void) {
pos[1] += (state ? 1 : -1); pos[1] += (state ? 1 : -1);
} }
pos[1] += 15; pos[1] += 5;
// int x = 0, y = 25, z = 0; // int x = 0, y = 25, z = 0;
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -231,11 +231,13 @@ int main(void) {
struct chunk chunk = gen_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH, struct chunk chunk = gen_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH,
pos[2] / CHUNK_LENGTH, 0, 0); pos[2] / CHUNK_LENGTH, 0, 0);
is_render = 2;
vec3 dir_temp = {0, 0, 0}; vec3 dir_temp = {0, 0, 0};
vec3 direction; vec3 direction;
fr = 1; fr = 0;
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window)) {
@ -281,9 +283,9 @@ int main(void) {
args.text = &text; args.text = &text;
args.cubeO = &cubeO; args.cubeO = &cubeO;
args.textO = &textO; args.textO = &textO;
args.pos[0] = pos[0] + direction[0] / 400.0 * CHUNK_LENGTH; args.pos[0] = pos[0];
args.pos[1] = pos[1]; args.pos[1] = pos[1];
args.pos[2] = pos[2] + direction[2] / 400.0 * CHUNK_LENGTH; args.pos[2] = pos[2];
args.vertex_buffer = vertex_buffer; args.vertex_buffer = vertex_buffer;
args.is_render = &is_render; args.is_render = &is_render;
args.i2 = i2; args.i2 = i2;
@ -304,14 +306,15 @@ int main(void) {
glfwPollEvents(); glfwPollEvents();
if (is_render == 2) { if (is_render == 2) {
if (thread_id != 0) {
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[1]);
glBufferSubData(GL_ARRAY_BUFFER, 0, glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(struct v3f),
(long)cube_count * sizeof(struct v3f), textO); textO);
glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]); glBindBuffer(GL_ARRAY_BUFFER, vertex_buffer[0]);
glBufferSubData(GL_ARRAY_BUFFER, 0, glBufferSubData(GL_ARRAY_BUFFER, 0, (long)cube_count * sizeof(struct v3f),
(long)cube_count * sizeof(struct v3f), cubeO); cubeO);
if (thread_id != 0) {
pthread_join(thread_id, NULL); pthread_join(thread_id, NULL);
chunk = gen_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH, chunk = gen_chunk(pos[0] / CHUNK_LENGTH, pos[1] / CHUNK_LENGTH,
pos[2] / CHUNK_LENGTH, 0, 0); pos[2] / CHUNK_LENGTH, 0, 0);