From 35abb02e2fb9bba032f8496c145170775349c26d Mon Sep 17 00:00:00 2001 From: onezplpl Date: Wed, 17 Jul 2024 16:09:04 -0400 Subject: [PATCH] cleanup --- control.c | 108 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ control.h | 5 +++ cubes.c | 6 +-- gen.c | 5 --- main.c | 103 ++++----------------------------------------------- 5 files changed, 121 insertions(+), 106 deletions(-) create mode 100644 control.c create mode 100644 control.h diff --git a/control.c b/control.c new file mode 100644 index 0000000..4a26ad2 --- /dev/null +++ b/control.c @@ -0,0 +1,108 @@ +#include "cubes.h" +#include "gen.h" +#include "linmath.h" +#include + +int is_space; + +int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) { + vec3 closestVert = {floor(pos[0] + 0.5), floor(pos[1] + 0.5), + floor(pos[2] + 0.5)}; + if (col) { + double xd = dir_temp[0] / (closestVert[0] - (pos[0])); + double yd = dir_temp[1] / (closestVert[1] - (pos[1])); + double zd = dir_temp[2] / (closestVert[2] - (pos[2])); + if (xd < 0 && n == 0) { + dir_temp[0] = 0; + pos[0] = closestVert[0]; + return 1; + } + if (yd < 0 && n == 1) { + dir_temp[1] = 0; + pos[1] = closestVert[1]; + return 1; + } + if (zd < 0 && n == 2) { + dir_temp[2] = 0; + pos[2] = closestVert[2]; + return 1; + } + } + + return 0; +} + +struct v3f controlHandler(double cx, double cy, vec3 pos, vec3 dir_temp, + GLFWwindow *window, struct chunk chunk) { + vec3 direction = {cosf(cy) * sinf(cx) * 0.1, sinf(cy) * 0.1, + cosf(cy) * cosf(cx) * 0.1}; + vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.1, 0, + cosf(cx - 3.14f / 2.0f) * 0.1}; + + vec3 front = {sinf(cx) * 0.1, 0, cosf(cx) * 0.1}; + vec3 pos_temp = {0, 0, 0}; + + vec3_dup(pos_temp, pos); + + if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { + vec3_sub(dir_temp, dir_temp, front); + } + + if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { + vec3_add(dir_temp, dir_temp, front); + } + + if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { + vec3_sub(dir_temp, dir_temp, direction); + } + + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { + vec3_add(dir_temp, dir_temp, direction); + } + + if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { + vec3_add(dir_temp, dir_temp, right); + } + + if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { + vec3_sub(dir_temp, dir_temp, right); + } + + vec3_scale(dir_temp, dir_temp, 0.6); + + vec3 off = {0, 0, 0}; + int col = 0; + for (int a = -2; a < 0; a++) { + off[1] = a; + col = col || cube_exists(pos[0] + off[0], pos[1] + off[1], pos[2] + off[2], + chunk, 0); + } + + vec3_add(pos, pos, dir_temp); + dir_temp[1] -= 0.05; + if (collision(chunk, pos, dir_temp, col, 1)) { + col = 0; + for (int a = -2; a < 0; a++) { + off[1] = a; + col = col || cube_exists(pos[0] + off[0], pos[1] + off[1], + pos[2] + off[2], chunk, 0); + } + + if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { + is_space = 3; + } + } + + if (is_space > 0) { + dir_temp[1] = 0.8; + } + is_space--; + + collision(chunk, pos, dir_temp, col, 0); + collision(chunk, pos, dir_temp, col, 2); + + struct v3f dirOut; + vec3_dup(dirOut.pos, direction); + + return dirOut; +} \ No newline at end of file diff --git a/control.h b/control.h new file mode 100644 index 0000000..3e75bc6 --- /dev/null +++ b/control.h @@ -0,0 +1,5 @@ +#include "linmath.h" +#include "cubes.h" +#include +struct v3f controlHandler(double cx, double cy, vec3 pos, vec3 dir_temp, + GLFWwindow *window, struct chunk chunk); \ No newline at end of file diff --git a/cubes.c b/cubes.c index a2d3ace..a27a954 100644 --- a/cubes.c +++ b/cubes.c @@ -95,11 +95,7 @@ void *gen_chunk(void *args) { // int is_text = args2.is_text; - int the_j = a2; /*(((z2 / CHUNK_LENGTH) % CHUNK_DIAMETER_H) + - ((x2 / CHUNK_LENGTH) % CHUNK_DIAMETER_H) * CHUNK_DIAMETER_H + - ((y2 / CHUNK_LENGTH) % CHUNK_DIAMETER_V) * CHUNK_DIAMETER_H * - CHUNK_DIAMETER_H) % - (CHUNK_ALL);*/ + int the_j = a2; struct chunk chunk = fetch_chunk(x2 / CHUNK_LENGTH, y2 / CHUNK_LENGTH, z2 / CHUNK_LENGTH, a, 0); diff --git a/gen.c b/gen.c index a830e42..6fe5d8d 100644 --- a/gen.c +++ b/gen.c @@ -158,14 +158,9 @@ struct chunk fetch_chunk(int x, int y, int z, int ci, int h) { } void purge_chunks(int ci) { - /* - free(chunks); - chunks = calloc(CBUF_ALL, sizeof(struct chunk)); - */ if (chunks == NULL) { chunks = calloc(CHUNK_ALL, sizeof(struct chunk)); } - // memset(chunks, 0, sizeof(struct chunk) *CHUNK_ALL); } void free_chunks() { free(chunks); } diff --git a/main.c b/main.c index 42e0634..fc485b4 100644 --- a/main.c +++ b/main.c @@ -14,6 +14,7 @@ #include "gl.h" #include "linmath.h" #include "stb_image.h" +#include "control.h" static const char *vertex_shader_text = "#version 330\n" @@ -106,33 +107,6 @@ void *render_chunks(void *args) { return NULL; } -int collision(struct chunk chunk, vec3 pos, vec3 dir_temp, int col, int n) { - vec3 closestVert = {floor(pos[0] + 0.5), floor(pos[1] + 0.5), - floor(pos[2] + 0.5)}; - if (col) { - double xd = dir_temp[0] / (closestVert[0] - (pos[0])); - double yd = dir_temp[1] / (closestVert[1] - (pos[1])); - double zd = dir_temp[2] / (closestVert[2] - (pos[2])); - if (xd < 0 && n == 0) { - dir_temp[0] = 0; - pos[0] = closestVert[0]; - return 1; - } - if (yd < 0 && n == 1) { - dir_temp[1] = 0; - pos[1] = closestVert[1]; - return 1; - } - if (zd < 0 && n == 2) { - dir_temp[2] = 0; - pos[2] = closestVert[2]; - return 1; - } - } - - return 0; -} - #define WinMain main int main(void) { @@ -239,6 +213,8 @@ int main(void) { vec3 dir_temp = {0, 0, 0}; + vec3 direction; + while (!glfwWindowShouldClose(window)) { glfwGetFramebufferSize(window, &width, &height); @@ -253,74 +229,17 @@ int main(void) { mat4x4_perspective(p, 45.0 / 180.0 * M_PI, ratio, 0.1, 1000); // TODO: make control code its own thing - vec3 direction = {cosf(cy) * sinf(cx) * 0.1, sinf(cy) * 0.1, - cosf(cy) * cosf(cx) * 0.1}; - vec3 right = {sinf(cx - 3.14f / 2.0f) * 0.1, 0, - cosf(cx - 3.14f / 2.0f) * 0.1}; - vec3 front = {sinf(cx) * 0.1, 0, cosf(cx) * 0.1}; - vec3 pos_temp = {0, 0, 0}; - - vec3_dup(pos_temp, pos); - - if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) { - vec3_sub(dir_temp, dir_temp, front); - } - - if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) { - vec3_add(dir_temp, dir_temp, front); - } - - if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) { - vec3_sub(dir_temp, dir_temp, direction); - } - - if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { - vec3_add(dir_temp, dir_temp, direction); - } - - if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) { - vec3_add(dir_temp, dir_temp, right); - } - - if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) { - vec3_sub(dir_temp, dir_temp, right); - } - - vec3_scale(dir_temp, dir_temp, 0.6); - - vec3 off = {0, 0, 0}; - int col = 0; - for (int a = -2; a < 0; a++) { - off[1] = a; - col = col || cube_exists(pos[0] + off[0], pos[1] + off[1], - pos[2] + off[2], chunk, 0); - } - - vec3_add(pos, pos, dir_temp); - dir_temp[1] -= 0.07; - if (collision(chunk, pos, dir_temp, col, 1)) { - col = 0; - for (int a = -2; a < 0; a++) { - off[1] = a; - col = col || cube_exists(pos[0] + off[0], pos[1] + off[1], - pos[2] + off[2], chunk, 0); - } - - if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) { - dir_temp[1] = 3; - } - } - - collision(chunk, pos, dir_temp, col, 0); - collision(chunk, pos, dir_temp, col, 2); + vec3 direction; + + vec3_dup(direction,controlHandler(cx, cy, pos, dir_temp, + window, chunk).pos); vec3 dir_pos; vec3_scale(direction, direction, 100.0); vec3_add(dir_pos, direction, pos); vec3 up = {0, 1, 0}; - // vec3_mul_cross(up, right, direction); mat4x4_look_at(v, pos, dir_pos, up); mat4x4_mul(mvp, p, v); @@ -394,14 +313,6 @@ int main(void) { glBindVertexArray(vertex_array); glDrawArrays(GL_TRIANGLES, 0, cube_count); - - /*/ - for (int ch = 0; ch < bleh; ch++) { - int o = ch * BLOCK_ALL * 18 * (CHUNK_DIAMETER_H); - if (i2[ch] < o) continue; - glDrawArrays(GL_TRIANGLES, o, i2[ch] - o); - } - */ } free(cube);