very buggy system working, but only works properly with one client at a time due to recv blocking
This commit is contained in:
+18
-1
@@ -1,5 +1,6 @@
|
||||
#include "lobby.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <iostream>
|
||||
|
||||
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() {
|
||||
|
||||
+13
-4
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
+22
-1
@@ -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());
|
||||
}
|
||||
+13
-7
@@ -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 <string>
|
||||
|
||||
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;
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "player.hpp"
|
||||
|
||||
Player::Player(int id) : clientID(id) {
|
||||
//
|
||||
}
|
||||
|
||||
void Player::Update(int delta) {
|
||||
position += motion * delta;
|
||||
}
|
||||
+25
-6
@@ -5,18 +5,37 @@
|
||||
|
||||
#include <string>
|
||||
|
||||
//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;
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
#include "player_manager.hpp"
|
||||
|
||||
@@ -1,4 +1,23 @@
|
||||
#ifndef PLAYERMANAGER_H_
|
||||
#define PLAYERMANAGER_H_
|
||||
|
||||
#include "player.hpp"
|
||||
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
class PlayerManager {
|
||||
public:
|
||||
PlayerManager() = default;
|
||||
~PlayerManager() = default;
|
||||
private:
|
||||
//utilities
|
||||
typedef std::map<std::string, Player*> PlayerMap;
|
||||
|
||||
//members
|
||||
PlayerMap playerMap;
|
||||
int maxPlayers;
|
||||
int ticker;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
+19
-13
@@ -2,17 +2,10 @@
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
+5
-5
@@ -4,12 +4,12 @@
|
||||
#include "config_utility.hpp"
|
||||
#include "network.hpp"
|
||||
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
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<TCPSocket*> sockVec;
|
||||
std::list<TCPSocket*> socketList;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user