]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Move player code to its own file
authorElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 11:25:55 +0000 (13:25 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Sat, 10 Jul 2021 11:40:35 +0000 (13:40 +0200)
src/Makefile
src/camera.c
src/camera.h
src/client.c
src/client.h
src/clientplayer.c [new file with mode: 0644]
src/clientplayer.h [new file with mode: 0644]
src/input.c
src/mesh.h
src/util.c

index af1678ebd502ecb628cc9b38cf3ae107579ef6b5..089576571947036c3edd1771197d3a1fdcf58df0 100644 (file)
@@ -1,6 +1,6 @@
 COMMON = array.o list.o map.o signal.o util.o types.o node.o queue.o
 SERVER = $(COMMON) server.o servercommands.o servermap.o perlin.o facecache.o mapgen.o mapdb.o
-CLIENT = $(COMMON) camera.o client.o clientcommands.o clientmap.o clientnode.o input.o mesh.o scene.o shaders.o blockmesh.o texture.o
+CLIENT = $(COMMON) camera.o client.o clientcommands.o clientmap.o clientnode.o clientplayer.o input.o mesh.o scene.o shaders.o blockmesh.o texture.o
 LIBRARIES = -lpthread -lm -lz
 FLAGS = -g -fmax-errors=4
 
index ac675bd50166225d60dc22525141a71b3a6e1855..6341c02e9066ba491afe9045da27b8bf63f461f8 100644 (file)
@@ -33,7 +33,7 @@ void set_camera_position(v3f pos)
        update_camera();
 }
 
-void set_camera_angle(float yaw, float pitch)
+void set_camera_angle(f32 yaw, f32 pitch)
 {
        camera.front[0] = movement_dirs.front[0] = cos(yaw) * cos(pitch);
        camera.front[1] = sin(pitch);
index b2af78cb583208ad898d08a6499a9aab97ef5b63..c2aaa2098c5404916f7860d13198dab7f9785d76 100644 (file)
@@ -9,7 +9,7 @@
 
 void init_camera(GLFWwindow *window, ShaderProgram *prog);
 void set_camera_position(v3f pos);
-void set_camera_angle(float yaw, float pitch);
+void set_camera_angle(f32 yaw, f32 pitch);
 void set_window_size(int width, int height);
 
 extern struct movement_dirs
index 62e6ab3904eda96b195fa6b1cb24b2f21507787d..c7aea0b0d6cbcdc96022efb68c57c2b826d8d840 100644 (file)
@@ -92,8 +92,8 @@ static void client_loop()
 
        init_camera(window, prog);
 
-       set_camera_position(client.pos);
-       set_camera_angle(client.yaw, client.pitch);
+       set_camera_position(client.player.pos);
+       set_camera_angle(client.player.yaw, client.player.pitch);
 
        set_window_size(width, height);
 
@@ -164,8 +164,9 @@ static void client_start(int fd)
        client.name = NULL;
        client.map = map_create();
        client.scene = scene_create();
-       client.pos = (v3f) {0.0f, 0.0f, 0.0f};
-       client.yaw = client.pitch = 0.0;
+       client.player.client = &client;
+       client.player.pos = (v3f) {0.0f, 0.0f, 0.0f};
+       client.player.yaw = client.player.pitch = 0.0;
 
        clientmap_init(&client);
 
index 3dd0f3d0abc3c21b88dd67bf76a2d317ecf859d1..8e7e28f1cdb0d6008f1b2169823e5b0cde3f1c6e 100644 (file)
@@ -5,6 +5,7 @@
 #include <pthread.h>
 #include "servercommands.h"
 #include "clientcommands.h"
+#include "clientplayer.h"
 #include "map.h"
 #include "network.h"
 #include "scene.h"
@@ -23,8 +24,7 @@ typedef struct Client
        char *name;
        Map *map;
        Scene *scene;
-       v3f pos;
-       double yaw, pitch;
+       ClientPlayer player;
 } Client;
 
 void client_disconnect(bool send, const char *detail);
diff --git a/src/clientplayer.c b/src/clientplayer.c
new file mode 100644 (file)
index 0000000..7dc3eca
--- /dev/null
@@ -0,0 +1,9 @@
+#include "client.h"
+#include "clientplayer.h"
+
+void clientplayer_send_pos(ClientPlayer *player)
+{
+       pthread_mutex_lock(&player->client->mtx);
+       (void) (write_u32(player->client->fd, SC_POS) && write_v3f32(player->client->fd, player->pos));
+       pthread_mutex_unlock(&player->client->mtx);
+}
diff --git a/src/clientplayer.h b/src/clientplayer.h
new file mode 100644 (file)
index 0000000..d8cc50a
--- /dev/null
@@ -0,0 +1,15 @@
+#ifndef _CLIENTPLAYER_H_
+#define _CLIENTPLAYER_H_
+
+#include "types.h"
+
+typedef struct
+{
+       struct Client *client;
+       v3f pos;
+       f32 yaw, pitch;
+} ClientPlayer;
+
+void clientplayer_send_pos(ClientPlayer *player);
+
+#endif
index 5725bccc9844c4ef18d071fc066e7d3b70572a0c..6cd76ff41d920159473462cf17273d534ca7b988 100644 (file)
@@ -18,17 +18,17 @@ static void cursor_pos_callback(__attribute__((unused)) GLFWwindow* window, doub
        last_x = current_x;
        last_y = current_y;
 
-       input.client->yaw += (float) delta_x * M_PI / 180.0f / 8.0f;
-       input.client->pitch -= (float) delta_y * M_PI / 180.0f / 8.0f;
+       input.client->player.yaw += (f32) delta_x * M_PI / 180.0f / 8.0f;
+       input.client->player.pitch -= (f32) delta_y * M_PI / 180.0f / 8.0f;
 
-       input.client->pitch = fmax(fmin(input.client->pitch, 89.0f), -89.0f);
+       input.client->player.pitch = fmax(fmin(input.client->player.pitch, 89.0f), -89.0f);
 
-       set_camera_angle(input.client->yaw, input.client->pitch);
+       set_camera_angle(input.client->player.yaw, input.client->player.pitch);
 }
 
 static bool move(int forward, int backward, vec3 speed)
 {
-       float sign;
+       f32 sign;
 
        if (glfwGetKey(input.window, forward) == GLFW_PRESS)
                sign = +1.0f;
@@ -37,9 +37,9 @@ static bool move(int forward, int backward, vec3 speed)
        else
                return false;
 
-       input.client->pos.x += speed[0] * sign;
-       input.client->pos.y += speed[1] * sign;
-       input.client->pos.z += speed[2] * sign;
+       input.client->player.pos.x += speed[0] * sign;
+       input.client->player.pos.y += speed[1] * sign;
+       input.client->player.pos.z += speed[2] * sign;
 
        return true;
 }
@@ -51,11 +51,8 @@ void process_input()
        bool moved_right = move(GLFW_KEY_D, GLFW_KEY_A, movement_dirs.right);
 
        if (moved_forward || moved_up || moved_right) {
-               set_camera_position(input.client->pos);
-
-               pthread_mutex_lock(&input.client->mtx);
-               (void) (write_u32(input.client->fd, SC_POS) && write_v3f32(input.client->fd, input.client->pos));
-               pthread_mutex_unlock(&input.client->mtx);
+               set_camera_position(input.client->player.pos);
+               clientplayer_send_pos(&input.client->player);
        }
 }
 
index 5726ba85538748b41921a051079d769b3736b40c..e7471e81b7552c66e1098fd33b9f644d286a2c45 100644 (file)
@@ -38,7 +38,7 @@ typedef struct
 typedef struct
 {
        v3f pos, rot, scale;
-       float angle;
+       f32 angle;
        mat4x4 transform;
        bool remove;
        Mesh **meshes;
index c6c7c0a801909c24c1cbd4c39b9583198fc23173..bbfd0bd621d0595ccd581613cd4caa7e31f953a9 100644 (file)
@@ -54,5 +54,5 @@ v3f html_to_v3f(const char *html)
 {
        unsigned int r, g, b;
        sscanf(html, "#%2x%2x%2x", &r, &g, &b);
-       return (v3f) {(float) r / 255.0f, (float) g / 255.0f, (float) b / 255.0f};
+       return (v3f) {(f32) r / 255.0f, (f32) g / 255.0f, (f32) b / 255.0f};
 }