Skip to content

Commit

Permalink
v1.stable
Browse files Browse the repository at this point in the history
  • Loading branch information
collinalexbell committed Nov 24, 2024
1 parent 273487c commit c6f6f66
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 27 deletions.
15 changes: 15 additions & 0 deletions include/MultiPlayer/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
#include <enet/enet.h>
#include <thread>
#include <atomic>
#include <glm/glm.hpp>

namespace MultiPlayer {

struct PlayerUpdate
{
uint32_t playerID;
glm::vec3 position;
glm::vec3 front;
};

enum CHANNEL_TYPE
{
PLAYER_UPDATE = 1
};

class Server
{
public:
Expand All @@ -21,6 +34,8 @@ class Server
ENetHost* server;
std::atomic<bool> isRunning;
std::thread pollThread;
PlayerUpdate getPlayerUpdateFromEvent(ENetEvent&);
void broadcastPlayerUpdate(PlayerUpdate&);
};

}
58 changes: 34 additions & 24 deletions src/MultiPlayer/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,37 @@ Server::Start(int port)
return true;
}

PlayerUpdate
Server::getPlayerUpdateFromEvent(ENetEvent& event)
{
PlayerUpdate update;
glm::vec3* data = reinterpret_cast<glm::vec3*>(event.packet->data);
update.position = data[0];
update.front = data[1];
update.playerID = event.peer->connectID;
return update;
}

void
Server::broadcastPlayerUpdate(PlayerUpdate& update)
{
ENetPacket* packet =
enet_packet_create(NULL,
sizeof(glm::vec3) * 2 + sizeof(uint32_t),
ENET_PACKET_FLAG_UNSEQUENCED);

memcpy(packet->data, &update.position, sizeof(glm::vec3));
memcpy(static_cast<unsigned char*>(packet->data) + sizeof(glm::vec3),
&update.front,
sizeof(glm::vec3));
memcpy(static_cast<unsigned char*>(packet->data) + sizeof(glm::vec3) * 2,
&update.playerID,
sizeof(uint32_t));

enet_host_broadcast(server, 1, packet);
enet_host_flush(server);
}

void
Server::PollLoop()
{
Expand All @@ -66,30 +97,9 @@ Server::PollLoop()
clients.push_back(playerId);
} break;
case ENET_EVENT_TYPE_RECEIVE:
// Handle received data
if (event.channelID == 1) { // Player data channel
glm::vec3* data = reinterpret_cast<glm::vec3*>(event.packet->data);
glm::vec3 position = data[0];
glm::vec3 front = data[1];
uint32_t playerID = event.peer->connectID;

ENetPacket* packet =
enet_packet_create(NULL,
sizeof(glm::vec3) * 2 + sizeof(uint32_t),
ENET_PACKET_FLAG_UNSEQUENCED);

memcpy(packet->data, &position, sizeof(glm::vec3));
memcpy(static_cast<unsigned char*>(packet->data) +
sizeof(glm::vec3),
&front,
sizeof(glm::vec3));
memcpy(static_cast<unsigned char*>(packet->data) +
sizeof(glm::vec3) * 2,
&playerID,
sizeof(uint32_t));

enet_host_broadcast(server, 1, packet);
enet_host_flush(server);
if (event.channelID == PLAYER_UPDATE) {
auto update = getPlayerUpdateFromEvent(event);
broadcastPlayerUpdate(update);
}

enet_packet_destroy(event.packet);
Expand Down
3 changes: 0 additions & 3 deletions todo.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ issues, but in a simple txt file
- stop move (can probably be the same key)
- have app blit to the screen when moving
- update the app coordinates
- kill app
- name the workspace
- click to focus



# Tasks
Expand Down

0 comments on commit c6f6f66

Please sign in to comment.