diff --git a/client/lobby.cpp b/client/lobby.cpp index f033db7..eece608 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -63,9 +63,9 @@ void Lobby::Update() { void Lobby::Receive() { //dump to the console - Packet packet; + PacketData packet; while(netUtil->Receive()) { - memcpy(&packet, netUtil->GetInData(), sizeof(Packet)); + memcpy(&packet, netUtil->GetInData(), sizeof(PacketData)); switch(packet.type) { case PacketList::PONG: PushServer(&packet); @@ -149,24 +149,24 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) { void Lobby::PingNetwork() { //ping the network - Packet packet; + PacketData packet; packet.type = PacketList::PING; - netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast(&packet), sizeof(Packet)); + netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast(&packet), sizeof(PacketData)); //reset the server list // serverVector.clear(); } -void Lobby::PushServer(Packet* packet) { - Server s; +void Lobby::PushServer(PacketData* packet) { + ServerData s; s.name = packet->pong.metadata; - s.add = netUtil->GetInPacket()->address; + s.address = netUtil->GetInPacket()->address; serverVector.push_back(s); } -void Lobby::JoinRequest(Server* server) { - Packet packet; +void Lobby::JoinRequest(ServerData* server) { + PacketData packet; packet.type = PacketList::JOINREQUEST; snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); snprintf(packet.joinRequest.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); - netUtil->Send(&server->add, reinterpret_cast(&packet), sizeof(Packet)); + netUtil->Send(&server->address, reinterpret_cast(&packet), sizeof(PacketData)); } diff --git a/client/lobby.hpp b/client/lobby.hpp index 0fed73b..1fc14ae 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -15,11 +15,6 @@ #include #include -struct Server { - std::string name; - IPaddress add; -}; - class Lobby : public BaseScene { public: //Public access members @@ -42,10 +37,14 @@ protected: virtual void KeyUp(SDL_KeyboardEvent const&); //utilities + struct ServerData { + std::string name; + IPaddress address; + }; + void PingNetwork(); - void PushServer(Packet*); - void JoinRequest(Server*); - typedef std::map ButtonMap; + void PushServer(PacketData*); + void JoinRequest(ServerData*); //members ConfigUtility* configUtil = nullptr; @@ -53,11 +52,11 @@ protected: UDPNetworkUtility* netUtil = nullptr; RasterFont font; - ButtonMap buttonMap; + std::map buttonMap; //the list of servers on the screen - std::vector serverVector; - Server* selectedServer = nullptr; + std::vector serverVector; + ServerData* selectedServer = nullptr; SDL_Rect listBox; }; diff --git a/client/main_menu.cpp b/client/main_menu.cpp index 758d5fa..15cbc73 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -24,6 +24,7 @@ MainMenu::~MainMenu() { for (auto it : buttonMap) { delete it.second; } + buttonMap.clear(); #ifdef DEBUG cout << "leaving MainMenu" << endl; #endif diff --git a/client/main_menu.hpp b/client/main_menu.hpp index 7d082ce..993c7db 100644 --- a/client/main_menu.hpp +++ b/client/main_menu.hpp @@ -31,15 +31,12 @@ protected: virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&); - //utilities - typedef std::map ButtonMap; - - //singletons + //globals ConfigUtility* configUtil; SurfaceManager* surfaceMgr; //members - ButtonMap buttonMap; + std::map buttonMap; }; #endif diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index 3cccd78..d56eb58 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -25,7 +25,7 @@ //------------------------- SceneManager::SceneManager() { - activeScene = nullptr; + // } SceneManager::~SceneManager() { @@ -41,7 +41,7 @@ void SceneManager::Init() { } configUtil.Load("rsc/config.cfg"); - netUtil.Open(0, sizeof(Packet)); + netUtil.Open(0, sizeof(PacketData)); //set the screen from the config file int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; @@ -103,12 +103,12 @@ void SceneManager::LoadScene(SceneList sceneIndex) { case SceneList::MAINMENU: activeScene = new MainMenu(&configUtil, &surfaceMgr); break; - case SceneList::INGAME: - activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil); - break; case SceneList::LOBBY: activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil); break; + case SceneList::INGAME: + activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil); + break; #ifdef DEBUG case SceneList::COMBAT: diff --git a/client/scene_manager.hpp b/client/scene_manager.hpp index d604a72..1583f0c 100644 --- a/client/scene_manager.hpp +++ b/client/scene_manager.hpp @@ -26,8 +26,9 @@ private: void LoadScene(SceneList sceneIndex); void UnloadScene(); - BaseScene* activeScene; + BaseScene* activeScene = nullptr; + //globals ConfigUtility configUtil; SurfaceManager surfaceMgr; UDPNetworkUtility netUtil; diff --git a/client/splash.cpp b/client/splash.cpp index 5678cbb..922d358 100644 --- a/client/splash.cpp +++ b/client/splash.cpp @@ -12,8 +12,6 @@ Splash::Splash(ConfigUtility* cUtil, SurfaceManager* sMgr) { #ifdef DEBUG cout << "entering Splash" << endl; #endif - loaded = false; - start = clock(); configUtil = cUtil; surfaceMgr = sMgr; diff --git a/client/splash.hpp b/client/splash.hpp index 5630ede..a79ee0a 100644 --- a/client/splash.hpp +++ b/client/splash.hpp @@ -19,12 +19,14 @@ protected: void LoadResources(); - bool loaded; - time_t start; - + //globals ConfigUtility* configUtil; SurfaceManager* surfaceMgr; - Image* logo; + + //members + bool loaded = false; + time_t start = clock(); + Image* logo = nullptr; }; #endif diff --git a/common/packet_list.hpp b/common/packet_list.hpp index d5e754a..b6cebc8 100644 --- a/common/packet_list.hpp +++ b/common/packet_list.hpp @@ -85,10 +85,8 @@ struct Movement { //this state of this is great //------------------------- -union Packet { - Packet () {} - ~Packet() {} - +union PacketData { + PacketData() {}; PacketList type = PacketList::NONE; //connections diff --git a/docs/TODO.txt b/docs/TODO.txt index ed714dd..7be0f57 100644 --- a/docs/TODO.txt +++ b/docs/TODO.txt @@ -1,2 +1,2 @@ -Build an interface for the lobby Have multiple players moving around the server at the same time +keep the docs up to date!!!!! \ No newline at end of file diff --git a/docs/networking outline.txt b/docs/networking outline.txt index 6c30afe..7cafbb6 100644 --- a/docs/networking outline.txt +++ b/docs/networking outline.txt @@ -80,13 +80,46 @@ end Networking protocol: //connections - ping - ping the server - pong - a response to a ping, carries the server name - join request - from client to server, this is the initial contact the client makes. it carries all of the client's information, like the handle, avatar, etc. - join confirm - the response to a join request, this makes the client enter the game proper, and carries the client's playerID. - disconnect - from either the client or server, his officially ends communications between the two programs + ping: + ping the server + pong: + a response to a ping + join request: + from client to server, this is the initial contact the client makes. it carries all of the client's information, like the handle, avatar, etc. + join confirm: + the response to a join request, this makes the client enter the game proper, and carries the client's playerID. + disconnect: + from either the client or server, his officially ends communications between the two programs + + //information control + synchronize: + update both the server and client + new player: + a new player enters the world. carries all player info + delete player: + a player leaves the world, carries the player ID + movement: + this is the initial position and motion of a player in the world. it is relayed to all clients, and progresses using delta progression + +------------------------- + +Server: + ClientData: + int playerID + int channel + str handle + str avatar + vec position + vec motion + end +end + +Client: + Lobby: + ServerData: + name + address + end + end +end - //player controls - new player - a new player enters the world. carries all player info - delete player - a player leaves the world, carries the player ID - movement - this is the initial position and motion of a player in the world. it is relayed to all clients, and progresses using delta progression diff --git a/docs/pseudocode.txt b/docs/pseudocode.txt index 1eef49b..8a8af18 100644 --- a/docs/pseudocode.txt +++ b/docs/pseudocode.txt @@ -50,17 +50,3 @@ Receive: player update: PlayerManager.Update(message) end - -------------------------- - -TCP systems - -TCPNetworkManager: //NOPE - Init //opens the TCP server socket, and allocates the socket set - Quit //close the TCP server socket, and free the socket set - AcceptConnections //accept new connections - Send //send to a specific client, closing the connection if an error occured - SendAll //send to all clients - CheckSockets //non-blocking - GetSocket() //get a specific socket - total connections in the packet \ No newline at end of file diff --git a/server/client_data.hpp b/server/client_data.hpp new file mode 100644 index 0000000..994e60b --- /dev/null +++ b/server/client_data.hpp @@ -0,0 +1,28 @@ +#ifndef CLIENTDATA_HPP_ +#define CLIENTDATA_HPP_ + +#include "vector2.hpp" + +#include + +struct ClientData { + ClientData() = default; + ClientData(int ID, int ch, std::string h, std::string a) { + playerID = ID; + channel = ch; + handle = h; + avatar = a; + } + void Update(int delta) { + position += motion * delta; + } + + int playerID = -1; + int channel = -1; + Vector2 position; + Vector2 motion; + std::string handle; + std::string avatar; +}; + +#endif diff --git a/server/main.cpp b/server/main.cpp index 4e58cee..c098d11 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -1,4 +1,4 @@ -#include "server.hpp" +#include "server_application.hpp" #include #include @@ -10,7 +10,7 @@ int main(int, char**) { cout << "Beginning server" << endl; #endif try { - Server app; + ServerApplication app; app.Init(); app.Proc(); app.Quit(); diff --git a/server/player.cpp b/server/player.cpp deleted file mode 100644 index 3747639..0000000 --- a/server/player.cpp +++ /dev/null @@ -1,12 +0,0 @@ -#include "player.hpp" - -Player::Player(int ID, int ch, std::string h, std::string a) { - playerID = ID; - channel = ch; - handle = h; - avatar = a; -} - -void Player::Update(int delta) { - position += motion * delta; -} \ No newline at end of file diff --git a/server/player.hpp b/server/player.hpp deleted file mode 100644 index 81dcedb..0000000 --- a/server/player.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#ifndef PLAYER_HPP_ -#define PLAYER_HPP_ - -#include "vector2.hpp" - -#include - -class Player { -public: - Player() = default; - Player(int playerID, int channel, std::string handle, std::string avatar); - void Update(int delta); - - int SetPlayerID(int id) {return playerID = id;} - int GetPlayerID() const {return playerID;} - int SetChannel(int ch) {return channel = ch;} - int GetChannel() const {return channel;} - - Vector2 SetPosition(Vector2 v) {return position = v;} - Vector2 ShiftPosition(Vector2 v) {return position += v;} - Vector2 GetPosition() const {return position;} - Vector2 SetMotion(Vector2 v) {return motion = v;} - Vector2 ShiftMotion(Vector2 v) {return motion += v;} - Vector2 GetMotion() const {return motion;} - - std::string SetHandle(std::string s) {return handle = s;} - std::string GetHandle() const {return handle;} - std::string SetAvatar(std::string s) {return avatar = s;} - std::string GetAvatar() const {return avatar;} -private: - int playerID = -1; - int channel = -1; //for networking - Vector2 position; - Vector2 motion; - std::string handle; - std::string avatar; -}; - -#endif diff --git a/server/player_manager.cpp b/server/player_manager.cpp deleted file mode 100644 index 25fd2ab..0000000 --- a/server/player_manager.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include "player_manager.hpp" - -#include - -void PlayerManager::UpdateAll(int delta) { - for (auto it : playerMap) { - it.second->Update(delta); - } -} - -Player* PlayerManager::New(int playerID, int channel, std::string handle, std::string avatar) { - if (playerMap.find(playerID) != playerMap.end()) { - throw(std::runtime_error("Player ID already exists")); - } - return playerMap[playerID] = new Player(playerID, channel, handle, avatar); -} - -Player* PlayerManager::Get(int playerID) { - for (auto it : playerMap) { - if (it.second->GetPlayerID() == playerID) { - return it.second; - } - } - return nullptr; -} - -void PlayerManager::Delete(int playerID) { - for (auto it : playerMap) { - if (it.second->GetPlayerID() == playerID) { - delete it.second; - playerMap.erase(playerID); - return; - } - } -} - -void PlayerManager::DeleteAll() { - for (auto it : playerMap) { - delete it.second; - } - playerMap.clear(); -} diff --git a/server/player_manager.hpp b/server/player_manager.hpp deleted file mode 100644 index 99c6705..0000000 --- a/server/player_manager.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef PLAYERMANAGER_H_ -#define PLAYERMANAGER_H_ - -#include "player.hpp" - -#include -#include - -class PlayerManager { -private: - //utilities - typedef std::map PlayerMap; -public: - PlayerManager() = default; - PlayerManager(int max) {SetMaxPlayers(max);} - ~PlayerManager() {DeleteAll();} - - void UpdateAll(int delta); - - Player* New(int playerID, int channel, std::string handle, std::string avatar); - Player* Get(int playerID); - void Delete(int playerID); - - void DeleteAll(); - - PlayerMap* GetPlayerMap() {return &playerMap;} - int SetMaxPlayers(int max) {return maxPlayers = max;} - int GetMaxPlayers() const {return maxPlayers;} -private: - //utilities - //... - - //members - PlayerMap playerMap; - int maxPlayers = 0; -}; - -#endif diff --git a/server/server.hpp b/server/server.hpp deleted file mode 100644 index 5759e38..0000000 --- a/server/server.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#ifndef SERVER_HPP_ -#define SERVER_HPP_ - -#include "delta.hpp" -#include "packet_list.hpp" - -#include "config_utility.hpp" -#include "udp_network_utility.hpp" -#include "player_manager.hpp" - -class Server { -public: - Server() = default; - ~Server() = default; - - void Init(); - void Proc(); - void Quit(); - -private: - void HandleInput(); - void UpdateWorld(); - void HandleOutput(); - - //network commands - void Ping(Packet*); - void JoinRequest(Packet*); - void Disconnect(Packet*); - void Movement(Packet*); - - bool running = false; - Delta delta; - - ConfigUtility configUtil; - UDPNetworkUtility netUtil; - PlayerManager playerMgr; - - int uniqueIndex = 0; -}; - -#endif diff --git a/server/server.cpp b/server/server_application.cpp similarity index 69% rename from server/server.cpp rename to server/server_application.cpp index fd13580..6e5f8fc 100644 --- a/server/server.cpp +++ b/server/server_application.cpp @@ -1,21 +1,20 @@ -#include "server.hpp" +#include "server_application.hpp" #include #include using namespace std; -void Server::Init() { +void ServerApplication::Init() { if (SDLNet_Init()) { throw(runtime_error("Failed to initialize SDL_net")); } configUtil.Load("config.cfg"); - netUtil.Open(configUtil.Integer("server.port"), sizeof(Packet)); - playerMgr.SetMaxPlayers(SDLNET_MAX_UDPCHANNELS); + netUtil.Open(configUtil.Integer("server.port"), sizeof(PacketData)); running = true; } -void Server::Proc() { +void ServerApplication::Proc() { while(running) { HandleInput(); UpdateWorld(); @@ -27,18 +26,18 @@ void Server::Proc() { } } -void Server::Quit() { +void ServerApplication::Quit() { netUtil.Close(); SDLNet_Quit(); } -void Server::HandleInput() { +void ServerApplication::HandleInput() { //accept new connections //accept updates from the clients //read the updates from the clients into internal containers - Packet packet; + PacketData packet; while(netUtil.Receive()) { - memcpy(reinterpret_cast(&packet), netUtil.GetInData(), sizeof(Packet)); + memcpy(reinterpret_cast(&packet), netUtil.GetInData(), sizeof(PacketData)); switch(packet.type) { case PacketList::PING: Ping(&packet); @@ -56,15 +55,17 @@ void Server::HandleInput() { } } -void Server::UpdateWorld() { +void ServerApplication::UpdateWorld() { //update internals ie. // ai // loot drops delta.Calculate(); - playerMgr.UpdateAll(delta.GetDelta()); + for (auto it : clientMap) { + it.second.Update(delta.GetDelta()); + } } -void Server::HandleOutput() { +void ServerApplication::HandleOutput() { //send all information to new connections //selective updates to existing connectons } @@ -73,14 +74,14 @@ void Server::HandleOutput() { //network commands //------------------------- -void Server::Ping(Packet* packet) { +void ServerApplication::Ping(PacketData* packet) { //respond to pings with the server name packet->type = PacketList::PONG; sprintf(packet->pong.metadata, "%s",configUtil.CString("servername")); - netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast(packet), sizeof(Packet)); + netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast(packet), sizeof(PacketData)); } -void Server::JoinRequest(Packet* packet) { +void ServerApplication::JoinRequest(PacketData* packet) { //TODO cout << "Join request..." << endl; // if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) { @@ -91,10 +92,10 @@ void Server::JoinRequest(Packet* packet) { // cout << ch << endl; } -void Server::Disconnect(Packet* packet) { +void ServerApplication::Disconnect(PacketData* packet) { //TODO } -void Server::Movement(Packet* packet) { +void ServerApplication::Movement(PacketData* packet) { //TODO } diff --git a/server/server_application.hpp b/server/server_application.hpp new file mode 100644 index 0000000..99df2e1 --- /dev/null +++ b/server/server_application.hpp @@ -0,0 +1,47 @@ +#ifndef SERVERAPPLICATION_HPP_ +#define SERVERAPPLICATION_HPP_ + +#include "client_data.hpp" + +#include "delta.hpp" +#include "packet_list.hpp" +#include "config_utility.hpp" +#include "udp_network_utility.hpp" + +#include +#include + +class ServerApplication { +public: + ServerApplication() = default; + ~ServerApplication() = default; + + void Init(); + void Proc(); + void Quit(); + +private: + void HandleInput(); + void UpdateWorld(); + void HandleOutput(); + + //network commands + void Ping(PacketData*); + void JoinRequest(PacketData*); + void Disconnect(PacketData*); + void Movement(PacketData*); + + bool running = false; + Delta delta; + + //globals + ConfigUtility configUtil; + UDPNetworkUtility netUtil; + + //members + std::map clientMap; + int maxClients = SDLNET_MAX_UDPCHANNELS; + int uniqueIndex = 0; +}; + +#endif