Smoothed naming and other conventions

This commit is contained in:
Kayne Ruse
2013-05-24 00:51:04 +10:00
parent cce1a874bf
commit 0b2af1d80f
21 changed files with 176 additions and 257 deletions
+10 -10
View File
@@ -63,9 +63,9 @@ void Lobby::Update() {
void Lobby::Receive() { void Lobby::Receive() {
//dump to the console //dump to the console
Packet packet; PacketData packet;
while(netUtil->Receive()) { while(netUtil->Receive()) {
memcpy(&packet, netUtil->GetInData(), sizeof(Packet)); memcpy(&packet, netUtil->GetInData(), sizeof(PacketData));
switch(packet.type) { switch(packet.type) {
case PacketList::PONG: case PacketList::PONG:
PushServer(&packet); PushServer(&packet);
@@ -149,24 +149,24 @@ void Lobby::KeyUp(SDL_KeyboardEvent const& key) {
void Lobby::PingNetwork() { void Lobby::PingNetwork() {
//ping the network //ping the network
Packet packet; PacketData packet;
packet.type = PacketList::PING; packet.type = PacketList::PING;
netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast<void*>(&packet), sizeof(Packet)); netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast<void*>(&packet), sizeof(PacketData));
//reset the server list //reset the server list
// serverVector.clear(); // serverVector.clear();
} }
void Lobby::PushServer(Packet* packet) { void Lobby::PushServer(PacketData* packet) {
Server s; ServerData s;
s.name = packet->pong.metadata; s.name = packet->pong.metadata;
s.add = netUtil->GetInPacket()->address; s.address = netUtil->GetInPacket()->address;
serverVector.push_back(s); serverVector.push_back(s);
} }
void Lobby::JoinRequest(Server* server) { void Lobby::JoinRequest(ServerData* server) {
Packet packet; PacketData packet;
packet.type = PacketList::JOINREQUEST; packet.type = PacketList::JOINREQUEST;
snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle")); snprintf(packet.joinRequest.handle, PACKET_STRING_SIZE, "%s", configUtil->CString("handle"));
snprintf(packet.joinRequest.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar")); snprintf(packet.joinRequest.avatar, PACKET_STRING_SIZE, "%s", configUtil->CString("avatar"));
netUtil->Send(&server->add, reinterpret_cast<void*>(&packet), sizeof(Packet)); netUtil->Send(&server->address, reinterpret_cast<void*>(&packet), sizeof(PacketData));
} }
+10 -11
View File
@@ -15,11 +15,6 @@
#include <map> #include <map>
#include <string> #include <string>
struct Server {
std::string name;
IPaddress add;
};
class Lobby : public BaseScene { class Lobby : public BaseScene {
public: public:
//Public access members //Public access members
@@ -42,10 +37,14 @@ protected:
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//utilities //utilities
struct ServerData {
std::string name;
IPaddress address;
};
void PingNetwork(); void PingNetwork();
void PushServer(Packet*); void PushServer(PacketData*);
void JoinRequest(Server*); void JoinRequest(ServerData*);
typedef std::map<std::string, Button*> ButtonMap;
//members //members
ConfigUtility* configUtil = nullptr; ConfigUtility* configUtil = nullptr;
@@ -53,11 +52,11 @@ protected:
UDPNetworkUtility* netUtil = nullptr; UDPNetworkUtility* netUtil = nullptr;
RasterFont font; RasterFont font;
ButtonMap buttonMap; std::map<std::string, Button*> buttonMap;
//the list of servers on the screen //the list of servers on the screen
std::vector<Server> serverVector; std::vector<ServerData> serverVector;
Server* selectedServer = nullptr; ServerData* selectedServer = nullptr;
SDL_Rect listBox; SDL_Rect listBox;
}; };
+1
View File
@@ -24,6 +24,7 @@ MainMenu::~MainMenu() {
for (auto it : buttonMap) { for (auto it : buttonMap) {
delete it.second; delete it.second;
} }
buttonMap.clear();
#ifdef DEBUG #ifdef DEBUG
cout << "leaving MainMenu" << endl; cout << "leaving MainMenu" << endl;
#endif #endif
+2 -5
View File
@@ -31,15 +31,12 @@ protected:
virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&);
virtual void KeyUp(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&);
//utilities //globals
typedef std::map<std::string, Button*> ButtonMap;
//singletons
ConfigUtility* configUtil; ConfigUtility* configUtil;
SurfaceManager* surfaceMgr; SurfaceManager* surfaceMgr;
//members //members
ButtonMap buttonMap; std::map<std::string, Button*> buttonMap;
}; };
#endif #endif
+5 -5
View File
@@ -25,7 +25,7 @@
//------------------------- //-------------------------
SceneManager::SceneManager() { SceneManager::SceneManager() {
activeScene = nullptr; //
} }
SceneManager::~SceneManager() { SceneManager::~SceneManager() {
@@ -41,7 +41,7 @@ void SceneManager::Init() {
} }
configUtil.Load("rsc/config.cfg"); configUtil.Load("rsc/config.cfg");
netUtil.Open(0, sizeof(Packet)); netUtil.Open(0, sizeof(PacketData));
//set the screen from the config file //set the screen from the config file
int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; int flags = SDL_HWSURFACE|SDL_DOUBLEBUF;
@@ -103,12 +103,12 @@ void SceneManager::LoadScene(SceneList sceneIndex) {
case SceneList::MAINMENU: case SceneList::MAINMENU:
activeScene = new MainMenu(&configUtil, &surfaceMgr); activeScene = new MainMenu(&configUtil, &surfaceMgr);
break; break;
case SceneList::INGAME:
activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil);
break;
case SceneList::LOBBY: case SceneList::LOBBY:
activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil); activeScene = new Lobby(&configUtil, &surfaceMgr, &netUtil);
break; break;
case SceneList::INGAME:
activeScene = new InGame(&configUtil, &surfaceMgr, &netUtil);
break;
#ifdef DEBUG #ifdef DEBUG
case SceneList::COMBAT: case SceneList::COMBAT:
+2 -1
View File
@@ -26,8 +26,9 @@ private:
void LoadScene(SceneList sceneIndex); void LoadScene(SceneList sceneIndex);
void UnloadScene(); void UnloadScene();
BaseScene* activeScene; BaseScene* activeScene = nullptr;
//globals
ConfigUtility configUtil; ConfigUtility configUtil;
SurfaceManager surfaceMgr; SurfaceManager surfaceMgr;
UDPNetworkUtility netUtil; UDPNetworkUtility netUtil;
-2
View File
@@ -12,8 +12,6 @@ Splash::Splash(ConfigUtility* cUtil, SurfaceManager* sMgr) {
#ifdef DEBUG #ifdef DEBUG
cout << "entering Splash" << endl; cout << "entering Splash" << endl;
#endif #endif
loaded = false;
start = clock();
configUtil = cUtil; configUtil = cUtil;
surfaceMgr = sMgr; surfaceMgr = sMgr;
+6 -4
View File
@@ -19,12 +19,14 @@ protected:
void LoadResources(); void LoadResources();
bool loaded; //globals
time_t start;
ConfigUtility* configUtil; ConfigUtility* configUtil;
SurfaceManager* surfaceMgr; SurfaceManager* surfaceMgr;
Image* logo;
//members
bool loaded = false;
time_t start = clock();
Image* logo = nullptr;
}; };
#endif #endif
+2 -4
View File
@@ -85,10 +85,8 @@ struct Movement {
//this state of this is great //this state of this is great
//------------------------- //-------------------------
union Packet { union PacketData {
Packet () {} PacketData() {};
~Packet() {}
PacketList type = PacketList::NONE; PacketList type = PacketList::NONE;
//connections //connections
+1 -1
View File
@@ -1,2 +1,2 @@
Build an interface for the lobby
Have multiple players moving around the server at the same time Have multiple players moving around the server at the same time
keep the docs up to date!!!!!
+42 -9
View File
@@ -80,13 +80,46 @@ end
Networking protocol: Networking protocol:
//connections //connections
ping - ping the server ping:
pong - a response to a ping, carries the server name ping the server
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. pong:
join confirm - the response to a join request, this makes the client enter the game proper, and carries the client's playerID. a response to a ping
disconnect - from either the client or server, his officially ends communications between the two programs 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
-14
View File
@@ -50,17 +50,3 @@ Receive:
player update: player update:
PlayerManager.Update(message) PlayerManager.Update(message)
end 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
+28
View File
@@ -0,0 +1,28 @@
#ifndef CLIENTDATA_HPP_
#define CLIENTDATA_HPP_
#include "vector2.hpp"
#include <string>
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
+2 -2
View File
@@ -1,4 +1,4 @@
#include "server.hpp" #include "server_application.hpp"
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
@@ -10,7 +10,7 @@ int main(int, char**) {
cout << "Beginning server" << endl; cout << "Beginning server" << endl;
#endif #endif
try { try {
Server app; ServerApplication app;
app.Init(); app.Init();
app.Proc(); app.Proc();
app.Quit(); app.Quit();
-12
View File
@@ -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;
}
-39
View File
@@ -1,39 +0,0 @@
#ifndef PLAYER_HPP_
#define PLAYER_HPP_
#include "vector2.hpp"
#include <string>
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
-42
View File
@@ -1,42 +0,0 @@
#include "player_manager.hpp"
#include <stdexcept>
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();
}
-38
View File
@@ -1,38 +0,0 @@
#ifndef PLAYERMANAGER_H_
#define PLAYERMANAGER_H_
#include "player.hpp"
#include <map>
#include <string>
class PlayerManager {
private:
//utilities
typedef std::map<int, Player*> 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
-41
View File
@@ -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
@@ -1,21 +1,20 @@
#include "server.hpp" #include "server_application.hpp"
#include <stdexcept> #include <stdexcept>
#include <iostream> #include <iostream>
using namespace std; using namespace std;
void Server::Init() { void ServerApplication::Init() {
if (SDLNet_Init()) { if (SDLNet_Init()) {
throw(runtime_error("Failed to initialize SDL_net")); throw(runtime_error("Failed to initialize SDL_net"));
} }
configUtil.Load("config.cfg"); configUtil.Load("config.cfg");
netUtil.Open(configUtil.Integer("server.port"), sizeof(Packet)); netUtil.Open(configUtil.Integer("server.port"), sizeof(PacketData));
playerMgr.SetMaxPlayers(SDLNET_MAX_UDPCHANNELS);
running = true; running = true;
} }
void Server::Proc() { void ServerApplication::Proc() {
while(running) { while(running) {
HandleInput(); HandleInput();
UpdateWorld(); UpdateWorld();
@@ -27,18 +26,18 @@ void Server::Proc() {
} }
} }
void Server::Quit() { void ServerApplication::Quit() {
netUtil.Close(); netUtil.Close();
SDLNet_Quit(); SDLNet_Quit();
} }
void Server::HandleInput() { void ServerApplication::HandleInput() {
//accept new connections //accept new connections
//accept updates from the clients //accept updates from the clients
//read the updates from the clients into internal containers //read the updates from the clients into internal containers
Packet packet; PacketData packet;
while(netUtil.Receive()) { while(netUtil.Receive()) {
memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(Packet)); memcpy(reinterpret_cast<void*>(&packet), netUtil.GetInData(), sizeof(PacketData));
switch(packet.type) { switch(packet.type) {
case PacketList::PING: case PacketList::PING:
Ping(&packet); Ping(&packet);
@@ -56,15 +55,17 @@ void Server::HandleInput() {
} }
} }
void Server::UpdateWorld() { void ServerApplication::UpdateWorld() {
//update internals ie. //update internals ie.
// ai // ai
// loot drops // loot drops
delta.Calculate(); 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 //send all information to new connections
//selective updates to existing connectons //selective updates to existing connectons
} }
@@ -73,14 +74,14 @@ void Server::HandleOutput() {
//network commands //network commands
//------------------------- //-------------------------
void Server::Ping(Packet* packet) { void ServerApplication::Ping(PacketData* packet) {
//respond to pings with the server name //respond to pings with the server name
packet->type = PacketList::PONG; packet->type = PacketList::PONG;
sprintf(packet->pong.metadata, "%s",configUtil.CString("servername")); sprintf(packet->pong.metadata, "%s",configUtil.CString("servername"));
netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(Packet)); netUtil.Send(&netUtil.GetInPacket()->address, reinterpret_cast<void*>(packet), sizeof(PacketData));
} }
void Server::JoinRequest(Packet* packet) { void ServerApplication::JoinRequest(PacketData* packet) {
//TODO //TODO
cout << "Join request..." << endl; cout << "Join request..." << endl;
// if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) { // if (playerMgr.GetPlayerMap()->size() >= playerMgr.GetMaxPlayers()) {
@@ -91,10 +92,10 @@ void Server::JoinRequest(Packet* packet) {
// cout << ch << endl; // cout << ch << endl;
} }
void Server::Disconnect(Packet* packet) { void ServerApplication::Disconnect(PacketData* packet) {
//TODO //TODO
} }
void Server::Movement(Packet* packet) { void ServerApplication::Movement(PacketData* packet) {
//TODO //TODO
} }
+47
View File
@@ -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 <map>
#include <string>
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<int, ClientData> clientMap;
int maxClients = SDLNET_MAX_UDPCHANNELS;
int uniqueIndex = 0;
};
#endif