]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Add movement keys; Fix array_search deadlock; Remove mapgen height limit
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Mar 2021 10:15:15 +0000 (12:15 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sun, 28 Mar 2021 10:15:15 +0000 (12:15 +0200)
src/array.c
src/client.c
src/mapgen.c

index 321d6740d33d49fdc7d9490212c392549d8c57d5..1d648f23878f51aa3b14bc2c7d72bb939a08b148 100644 (file)
@@ -50,23 +50,23 @@ void array_append(Array *array, void *elem)
 ArraySearchResult array_search(Array *array, void *search)
 {
        assert(array->cmp);
-       size_t min, max, index;
+       size_t low, high, index;
 
-       min = index = 0;
-       max = array->siz;
+       low = index = 0;
+       high = array->siz;
 
-       while (min < max) {
-               index = min;
+       while (low < high) {
+               index = low;
 
-               size_t mid = (max + min) / 2;
+               size_t mid = (low + high) / 2;
                s8 state = array->cmp(search, (char *) array->ptr + mid * array->membsiz);
 
                if (state == 0)
                        return (ArraySearchResult) {true, mid};
                else if (state > 0)
-                       max = mid;
+                       high = mid;
                else
-                       min = mid;
+                       low = mid + 1;
        }
 
        return (ArraySearchResult) {false, index};
index 557ba041788dce35a906ea367083348312242b14..d32575d0d2c6981b1723e33452e537e0b6441e4c 100644 (file)
@@ -91,24 +91,60 @@ static void client_loop()
                return;
        }
 
+       v3f pos = {0.0f, 0.0f, 0.0f};
+
        mat4x4 view, projection;
 
-       mat4x4_identity(view);  // ToDo: camera
-       mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 100.0f);
+       mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
+       mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
 
        glUseProgram(prog->id);
        glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
        glUniformMatrix4fv(prog->loc_projection, 1, GL_FALSE, projection[0]);
 
+       bool e_was_pressed = false;
+
        while (! glfwWindowShouldClose(window) && client.state != CS_DISCONNECTED && ! interrupted) {
                glClear(GL_COLOR_BUFFER_BIT);
                glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
 
-               if (glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS) {
+               bool e_is_pressed = glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS;
+
+               if (e_is_pressed && ! e_was_pressed) {
                        pthread_mutex_lock(&client.mtx);
-                       (void) (write_u32(client.fd, SC_GETBLOCK) && write_v3s32(client.fd, (v3s32) {0, 0, 0}));
+                       (void) (write_u32(client.fd, SC_GETBLOCK) && write_v3s32(client.fd, map_node_to_block_pos((v3s32) {pos.x, pos.y, pos.z}, NULL)));
                        pthread_mutex_unlock(&client.mtx);
-               };
+               }
+
+               e_was_pressed = e_is_pressed;
+
+               bool view_changed = false;
+
+               if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
+                       view_changed = true;
+                       pos.z -= 1.0f;
+               } else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
+                       view_changed = true;
+                       pos.z += 1.0f;
+               } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
+                       view_changed = true;
+                       pos.x -= 1.0f;
+               } else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
+                       view_changed = true;
+                       pos.x += 1.0f;
+               } if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
+                       view_changed = true;
+                       pos.y -= 1.0f;
+               } else if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
+                       view_changed = true;
+                       pos.y += 1.0f;
+               }
+
+               if (view_changed) {
+                       printf("%f %f %f\n", pos.x, pos.y, pos.z);
+                       mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
+                       glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
+               }
 
                scene_render(client.scene, prog);
 
index 6e24f2e167964911ac13e5f01f4cee62afca32da..7bbcfc9e1f1ed2905cc8f6deb3246e17f849bdd7 100644 (file)
@@ -4,10 +4,8 @@
 // mapgen prototype
 static void generate_block(MapBlock *block)
 {
-       if (block->pos.y < 0 && rand() % 2 == 0) {
-               ITERATE_MAPBLOCK {
-                       block->data[x][y][z] = map_node_create(rand() % 4 + 1);
-               }
+       ITERATE_MAPBLOCK {
+               block->data[x][y][z] = map_node_create(rand() % 4 + 1);
        }
        block->ready = true;
 }