From 24e48dec535467395441d9050784ebdb0f2b4a3a Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Fri, 17 May 2013 19:32:45 +1000 Subject: [PATCH] very buggy system working, but only works properly with one client at a time due to recv blocking --- client/lobby.cpp | 19 ++++++++++++++++++- client/lobby.hpp | 17 +++++++++++++---- client/main_menu.cpp | 2 +- client/scene_manager.cpp | 11 +++++++---- client/test_systems.cpp | 23 ++++++++++++++++++++++- client/test_systems.hpp | 20 +++++++++++++------- server/player.cpp | 9 +++++++++ server/player.hpp | 31 +++++++++++++++++++++++++------ server/player_manager.cpp | 2 ++ server/player_manager.hpp | 19 +++++++++++++++++++ server/server.cpp | 32 +++++++++++++++++++------------- server/server.hpp | 10 +++++----- 12 files changed, 153 insertions(+), 42 deletions(-) create mode 100644 server/player.cpp create mode 100644 server/player_manager.cpp diff --git a/client/lobby.cpp b/client/lobby.cpp index 26591ab..bc790f8 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -1,5 +1,6 @@ #include "lobby.hpp" +#include #include using namespace std; @@ -8,10 +9,26 @@ using namespace std; //Public access members //------------------------- -Lobby::Lobby() { +Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, TCPSocket* sock) { #ifdef DEBUG cout << "entering Lobby" << endl; #endif + configUtil = cUtil; + surfaceMgr = sMgr; + socket = sock; + + //ping the network, ping the preset "phone home" servers + //generate the server list + //eventually + + try { + socket->Open(configUtil->CString("ip"), configUtil->Integer("port")); + } + catch(exception& e) { + cerr << "Network Error: " << e.what() << endl; + } + + SetNextScene(SceneList::TESTSYSTEMS); } Lobby::~Lobby() { diff --git a/client/lobby.hpp b/client/lobby.hpp index 8749ab9..a9eac7d 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -3,25 +3,34 @@ #include "base_scene.hpp" +#include "config_utility.hpp" +#include "surface_manager.hpp" +#include "network.hpp" + class Lobby : public BaseScene { public: - /* Public access members */ - Lobby(); + //Public access members + Lobby(ConfigUtility*, SurfaceManager*, TCPSocket*); virtual ~Lobby(); protected: - /* Frame loop */ + //Frame loop virtual void FrameStart(); virtual void FrameEnd(); virtual void Update(); virtual void Render(SDL_Surface* const); - /* Event handlers */ + //Event handlers virtual void MouseMotion(SDL_MouseMotionEvent const&); virtual void MouseButtonDown(SDL_MouseButtonEvent const&); virtual void MouseButtonUp(SDL_MouseButtonEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&); + + //members + ConfigUtility* configUtil; + SurfaceManager* surfaceMgr; + TCPSocket* socket; }; #endif diff --git a/client/main_menu.cpp b/client/main_menu.cpp index 0e1c2b2..6d7afe3 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -70,7 +70,7 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) { //TODO - SetNextScene(SceneList::TESTSYSTEMS); + SetNextScene(SceneList::LOBBY); cout << "start" << endl; } if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) { diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index 192b3a5..43ab804 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -38,6 +38,8 @@ void SceneManager::Init() { configUtil.Load("rsc/config.cfg"); + NetworkInit(); + //set the screen from the config file int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; if (configUtil.Boolean("screen.f")) { @@ -66,6 +68,7 @@ void SceneManager::Proc() { void SceneManager::Quit() { UnloadScene(); + NetworkQuit(); SDL_Quit(); } @@ -80,7 +83,7 @@ void SceneManager::LoadScene(SceneList sceneIndex) { //add scene creation calls here #ifdef DEBUG case SceneList::TESTSYSTEMS: - activeScene = new TestSystems(&configUtil, &surfaceMgr); + activeScene = new TestSystems(&configUtil, &surfaceMgr, &socket); break; #endif @@ -94,11 +97,11 @@ void SceneManager::LoadScene(SceneList sceneIndex) { case SceneList::INGAME: activeScene = new InGame(&configUtil, &surfaceMgr); break; + case SceneList::LOBBY: + activeScene = new Lobby(&configUtil, &surfaceMgr, &socket); + break; #ifdef DEBUG - case SceneList::LOBBY: - activeScene = new Lobby(); - break; case SceneList::COMBAT: activeScene = new Combat(); break; diff --git a/client/test_systems.cpp b/client/test_systems.cpp index d610c5c..6476d2d 100644 --- a/client/test_systems.cpp +++ b/client/test_systems.cpp @@ -8,12 +8,13 @@ using namespace std; //Public access members //------------------------- -TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) { +TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr, TCPSocket* sock) { #ifdef DEBUG cout << "entering TestSystems" << endl; #endif configUtil = cUtil; surfaceMgr = sMgr; + socket = sock; playerCounter = currentPlayer = 0; @@ -21,10 +22,13 @@ TestSystems::TestSystems(ConfigUtility* cUtil, SurfaceManager* sMgr) { playerMgr.New(playerCounter++, surfaceMgr->Get("elliot")); playerMgr.New(playerCounter++, surfaceMgr->Get("coa")); playerMgr.New(playerCounter++, surfaceMgr->Get("coa")); + + font.SetSurface(surfaceMgr->Get("font")); } TestSystems::~TestSystems() { playerMgr.DeleteAll(); + socket->Close(); #ifdef DEBUG cout << "leaving TestSystems" << endl; #endif @@ -47,8 +51,16 @@ void TestSystems::Update() { playerMgr.UpdateAll(delta.GetDelta()); } +string IToS(int i) { + char buffer[20]; + memset(buffer, 0, 20); + sprintf(buffer, "%d", i); + return string(buffer); +} + void TestSystems::Render(SDL_Surface* const screen) { playerMgr.DrawAllTo(screen); + font.DrawStringTo("FPS: " + IToS(frameRate.GetFrameRate()), screen, 16, 16); } //------------------------- @@ -75,15 +87,19 @@ void TestSystems::KeyDown(SDL_KeyboardEvent const& key) { case SDLK_w: playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); + SendMessage("move up"); break; case SDLK_s: playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH); + SendMessage("move down"); break; case SDLK_a: playerMgr[currentPlayer]->WalkInDirection(Direction::WEST); + SendMessage("move left"); break; case SDLK_d: playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); + SendMessage("move right"); break; case SDLK_1: @@ -147,3 +163,8 @@ void TestSystems::SwitchToPlayer(int index) { playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); } } + +void TestSystems::SendMessage(std::string s) { + s += ';'; + socket->Send(s.c_str(), s.length()); +} \ No newline at end of file diff --git a/client/test_systems.hpp b/client/test_systems.hpp index 518843d..0906278 100644 --- a/client/test_systems.hpp +++ b/client/test_systems.hpp @@ -3,19 +3,21 @@ #include "base_scene.hpp" -#include "player_manager.hpp" - -#include "delta.hpp" -#include "frame_rate.hpp" #include "config_utility.hpp" #include "surface_manager.hpp" +#include "network.hpp" + +#include "player_manager.hpp" +#include "delta.hpp" +#include "frame_rate.hpp" +#include "raster_font.hpp" #include class TestSystems : public BaseScene { public: //Public access members - TestSystems(ConfigUtility*, SurfaceManager*); + TestSystems(ConfigUtility*, SurfaceManager*, TCPSocket*); virtual ~TestSystems(); protected: @@ -35,14 +37,18 @@ protected: //utilities void NewPlayer(int index, std::string avatarName, int x, int y); void SwitchToPlayer(int index); + void SendMessage(std::string); //members + ConfigUtility* configUtil; + SurfaceManager* surfaceMgr; + TCPSocket* socket; + PlayerManager playerMgr; Delta delta; FrameRate frameRate; - ConfigUtility* configUtil; - SurfaceManager* surfaceMgr; + RasterFont font; int playerCounter; int currentPlayer; diff --git a/server/player.cpp b/server/player.cpp new file mode 100644 index 0000000..bc00649 --- /dev/null +++ b/server/player.cpp @@ -0,0 +1,9 @@ +#include "player.hpp" + +Player::Player(int id) : clientID(id) { + // +} + +void Player::Update(int delta) { + position += motion * delta; +} \ No newline at end of file diff --git a/server/player.hpp b/server/player.hpp index de0c599..f471ae8 100644 --- a/server/player.hpp +++ b/server/player.hpp @@ -5,18 +5,37 @@ #include -//TODO - class Player { public: - Player(); - ~Player(); + Player(int id); + ~Player() = default; void Update(int); - Vector2 GetPosition(); + int GetClientID() const { + return clientID; + } + + Vector2 SetPosition(Vector2 v) { + return position = v; + } + Vector2 GetPosition() const { + return position; + } + Vector2 SetMotion(Vector2 v) { + return motion = v; + } + Vector2 GetMotion() const { + return motion; + } + std::string SetAvatarName(std::string s) { + return avatarName = s; + } + std::string GetAvatarName() { + return avatarName; + } private: - int clientID; + const int clientID; Vector2 position; Vector2 motion; std::string avatarName; diff --git a/server/player_manager.cpp b/server/player_manager.cpp new file mode 100644 index 0000000..616c934 --- /dev/null +++ b/server/player_manager.cpp @@ -0,0 +1,2 @@ +#include "player_manager.hpp" + diff --git a/server/player_manager.hpp b/server/player_manager.hpp index f6f0356..962beab 100644 --- a/server/player_manager.hpp +++ b/server/player_manager.hpp @@ -1,4 +1,23 @@ #ifndef PLAYERMANAGER_H_ #define PLAYERMANAGER_H_ +#include "player.hpp" + +#include +#include + +class PlayerManager { +public: + PlayerManager() = default; + ~PlayerManager() = default; +private: + //utilities + typedef std::map PlayerMap; + + //members + PlayerMap playerMap; + int maxPlayers; + int ticker; +}; + #endif diff --git a/server/server.cpp b/server/server.cpp index b347687..14d85bc 100644 --- a/server/server.cpp +++ b/server/server.cpp @@ -2,17 +2,10 @@ #include #include +#include using namespace std; -Server::Server() { - running = false; -} - -Server::~Server() { - // -} - void Server::Init() { NetworkInit(); @@ -34,7 +27,7 @@ void Server::Proc() { } void Server::Quit() { - for (auto it : sockVec) { + for (auto it : socketList) { it->Close(); delete it; } @@ -46,12 +39,25 @@ void Server::HandleInput() { //accept new connections TCPSocket* sock = new TCPSocket; if (servSock.Accept(sock)) { - sockVec.push_back(sock); + socketList.push_back(sock); + } + else { + delete sock; } //accept updates from the clients - //... + string input; + for_each(socketList.begin(), socketList.end(), [&input](TCPSocket* sock){ //why use for_each & lamdas?? to give logan a brain hemorrhage + char buffer[512]; + memset(buffer, 0, 512); + sock->Recv(buffer, 512); + input += buffer; + }); //read the updates from the clients into internal containers - //... + if (input.size()) { + cout << "dumping input from the network" << endl; + cout << input << endl; + input.clear(); + } } void Server::UpdateWorld() { @@ -65,7 +71,7 @@ void Server::HandleOutput() { //... //selective updates to existing connectons string s = "hello world"; - for (auto it : sockVec) { + for (auto it : socketList) { it->Send(s.c_str(), s.length()); } } diff --git a/server/server.hpp b/server/server.hpp index 13f4736..fa709a2 100644 --- a/server/server.hpp +++ b/server/server.hpp @@ -4,12 +4,12 @@ #include "config_utility.hpp" #include "network.hpp" -#include +#include class Server { public: - Server(); - ~Server(); + Server() = default; + ~Server() = default; void Init(); void Proc(); @@ -19,10 +19,10 @@ public: void UpdateWorld(); void HandleOutput(); private: - bool running; + bool running = false; ConfigUtility config; TCPServerSocket servSock; - std::vector sockVec; + std::list socketList; }; #endif