very buggy system working, but only works properly with one client at a time due to recv blocking

This commit is contained in:
Kayne Ruse
2013-05-17 19:32:45 +10:00
parent 738320e88e
commit 24e48dec53
12 changed files with 153 additions and 42 deletions
+18 -1
View File
@@ -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
View File
@@ -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
+1 -1
View File
@@ -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) {
+7 -4
View File
@@ -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
View File
@@ -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
View File
@@ -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;