Skip to content

Commit

Permalink
Implement position sending
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Mar 29, 2021
1 parent a46f592 commit a97351d
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 24 deletions.
45 changes: 26 additions & 19 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ static void *reciever_thread(void *unused)
return NULL;
}

static void update_view_matrix(ShaderProgram *prog, mat4x4 view)
{
mat4x4_translate(view, -client.pos.x, -client.pos.y, -client.pos.z);
glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
}

static void client_loop()
{
if(! glfwInit()) {
Expand Down Expand Up @@ -91,11 +97,9 @@ static void client_loop()
return;
}

v3f pos = {0.0f, 0.0f, 0.0f};

mat4x4 view, projection;

mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
update_view_matrix(prog, view);
mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);

glUseProgram(prog->id);
Expand All @@ -107,31 +111,33 @@ static void client_loop()
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);

bool view_changed = false;
bool pos_changed = false;

if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
view_changed = true;
pos.z -= 1.0f;
pos_changed = true;
client.pos.z -= 1.0f;
} else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
view_changed = true;
pos.z += 1.0f;
pos_changed = true;
client.pos.z += 1.0f;
} if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
view_changed = true;
pos.x -= 1.0f;
pos_changed = true;
client.pos.x -= 1.0f;
} else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
view_changed = true;
pos.x += 1.0f;
pos_changed = true;
client.pos.x += 1.0f;
} if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
view_changed = true;
pos.y -= 1.0f;
pos_changed = true;
client.pos.y -= 1.0f;
} else if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
view_changed = true;
pos.y += 1.0f;
pos_changed = true;
client.pos.y += 1.0f;
}

if (view_changed) {
mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
if (pos_changed) {
update_view_matrix(prog, view);
pthread_mutex_lock(&client.mtx);
(void) (write_u32(client.fd, SC_POS) && write_v3f32(client.fd, client.pos));
pthread_mutex_unlock(&client.mtx);
}

scene_render(client.scene, prog);
Expand Down Expand Up @@ -192,6 +198,7 @@ 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};

mapblock_meshgen_init(client.map, client.scene);

Expand Down
1 change: 1 addition & 0 deletions src/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ typedef struct Client
char *name;
Map *map;
Scene *scene;
v3f pos;
} Client;

void client_disconnect(bool send, const char *detail);
Expand Down
8 changes: 5 additions & 3 deletions src/mapgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,17 @@ void mapgen_init(Server *srv)
server->map->on_block_create = &generate_block;
}

#define RANGE 3

static void *mapgen_thread(void *cliptr)
{
Client *client = cliptr;

while (client->state != CS_DISCONNECTED) {
v3s32 pos = map_node_to_block_pos((v3s32) {client->pos.x, client->pos.y, client->pos.z}, NULL);
for (s32 x = pos.x - 5; x < pos.x + 5; x++)
for (s32 y = pos.y - 5; y < pos.y + 5; y++)
for (s32 z = pos.z - 5; z < pos.z + 5; z++)
for (s32 x = pos.x - RANGE; x <= pos.x + RANGE; x++)
for (s32 y = pos.y - RANGE; y <= pos.y + RANGE; y++)
for (s32 z = pos.z - RANGE; z <= pos.z + RANGE; z++)
map_get_block(client->server->map, (v3s32) {x, y, z}, true);
}

Expand Down
Binary file added src/old_map
Binary file not shown.
14 changes: 14 additions & 0 deletions src/servercommands.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,24 @@ static bool kick_handler(Client *client, bool good)
return true;
}

static bool pos_handler(Client *client, bool good)
{
v3f pos;

if (! read_v3f32(client->fd, &pos))
return false;

if (good)
client->pos = pos;

return true;
}

CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
{0},
{&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
{&auth_handler, "AUTH", CS_CREATED},
{&setnode_handler, "SETNODE", CS_ACTIVE},
{&kick_handler, "KICK", CS_ACTIVE},
{&pos_handler, "POS", CS_ACTIVE},
};
1 change: 1 addition & 0 deletions src/servercommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ typedef enum
SC_AUTH,
SC_SETNODE,
SC_KICK,
SC_POS,
SERVER_COMMAND_COUNT,
} ServerCommand;

Expand Down
24 changes: 24 additions & 0 deletions src/types.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,27 @@ DEFTYPES(8)
DEFTYPES(16)
DEFTYPES(32)
DEFTYPES(64)

#define DEFFLOAT(type) \
bool read_ ## type(int fd, type *buf) \
{ \
int n_read; \
if ((n_read = read(fd, buf, sizeof(type))) != sizeof(type)) { \
if (n_read == -1) \
perror("read"); \
return false; \
} \
return true; \
} \
bool write_ ## type(int fd, type val) \
{ \
if (write(fd, &val, sizeof(val)) == -1) { \
perror("write"); \
return false; \
} \
return true; \
} \
DEFVEC(type)

DEFFLOAT(f32)
DEFFLOAT(f64)
4 changes: 2 additions & 2 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ DEFTYPES(64)
typedef float f32;
typedef double f64;

DEFVEC(f32)
DEFVEC(f64)
DEFTYP(float, f32)
DEFTYP(double, f64)

typedef v2f32 v2f;
typedef v3f32 v3f;
Expand Down

0 comments on commit a97351d

Please sign in to comment.