init_input(&client, window);
+ struct timespec ts, ts_old;
+ clock_gettime(CLOCK_REALTIME, &ts_old);
+
while (! glfwWindowShouldClose(window) && client.state != CS_DISCONNECTED && ! interrupted) {
+ clock_gettime(CLOCK_REALTIME, &ts);
+ f64 dtime = (f64) (ts.tv_sec - ts_old.tv_sec) + (f64) (ts.tv_nsec - ts_old.tv_nsec) / 1000000000.0;
+ ts_old = ts;
+
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
process_input();
+ clientplayer_tick(&client.player, dtime);
scene_render(client.scene, prog);
+#include "camera.h"
#include "client.h"
#include "clientplayer.h"
-void clientplayer_send_pos(ClientPlayer *player)
+static void 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);
}
+
+void clientplayer_tick(ClientPlayer *player, f64 dtime)
+{
+ v3f old_pos = player->pos;
+
+ player->pos.x += player->velocity.x * dtime;
+ player->pos.y += player->velocity.y * dtime;
+ player->pos.z += player->velocity.z * dtime;
+
+ if (old_pos.x != player->pos.x || old_pos.y != player->pos.y || old_pos.z != player->pos.z) {
+ send_pos(player);
+ set_camera_position(player->pos);
+ }
+}
set_camera_angle(input.client->player.yaw, input.client->player.pitch);
}
-static bool move(int forward, int backward, vec3 speed)
+static bool move(int forward, int backward, vec3 dir)
{
f32 sign;
+ f32 speed = 10.0f;
if (glfwGetKey(input.window, forward) == GLFW_PRESS)
sign = +1.0f;
else
return false;
- input.client->player.pos.x += speed[0] * sign;
- input.client->player.pos.y += speed[1] * sign;
- input.client->player.pos.z += speed[2] * sign;
+ input.client->player.velocity.x += dir[0] * speed * sign;
+ input.client->player.velocity.y += dir[1] * speed * sign;
+ input.client->player.velocity.z += dir[2] * speed * sign;
return true;
}
void process_input()
{
- bool moved_forward = move(GLFW_KEY_W, GLFW_KEY_S, movement_dirs.front);
- bool moved_up = move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, movement_dirs.up);
- 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->player.pos);
- clientplayer_send_pos(&input.client->player);
- }
+ input.client->player.velocity.x = 0.0f;
+ input.client->player.velocity.y = 0.0f;
+ input.client->player.velocity.z = 0.0f;
+
+ move(GLFW_KEY_W, GLFW_KEY_S, movement_dirs.front);
+ move(GLFW_KEY_SPACE, GLFW_KEY_LEFT_SHIFT, movement_dirs.up);
+ move(GLFW_KEY_D, GLFW_KEY_A, movement_dirs.right);
}
void init_input(Client *client, GLFWwindow *window)