Smoothed naming and other conventions
This commit is contained in:
+10
-10
@@ -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<void*>(&packet), sizeof(Packet));
|
||||
netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast<void*>(&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<void*>(&packet), sizeof(Packet));
|
||||
netUtil->Send(&server->address, reinterpret_cast<void*>(&packet), sizeof(PacketData));
|
||||
}
|
||||
|
||||
+10
-11
@@ -15,11 +15,6 @@
|
||||
#include <map>
|
||||
#include <string>
|
||||
|
||||
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<std::string, Button*> 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<std::string, Button*> buttonMap;
|
||||
|
||||
//the list of servers on the screen
|
||||
std::vector<Server> serverVector;
|
||||
Server* selectedServer = nullptr;
|
||||
std::vector<ServerData> serverVector;
|
||||
ServerData* selectedServer = nullptr;
|
||||
SDL_Rect listBox;
|
||||
};
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ MainMenu::~MainMenu() {
|
||||
for (auto it : buttonMap) {
|
||||
delete it.second;
|
||||
}
|
||||
buttonMap.clear();
|
||||
#ifdef DEBUG
|
||||
cout << "leaving MainMenu" << endl;
|
||||
#endif
|
||||
|
||||
@@ -31,15 +31,12 @@ protected:
|
||||
virtual void KeyDown(SDL_KeyboardEvent const&);
|
||||
virtual void KeyUp(SDL_KeyboardEvent const&);
|
||||
|
||||
//utilities
|
||||
typedef std::map<std::string, Button*> ButtonMap;
|
||||
|
||||
//singletons
|
||||
//globals
|
||||
ConfigUtility* configUtil;
|
||||
SurfaceManager* surfaceMgr;
|
||||
|
||||
//members
|
||||
ButtonMap buttonMap;
|
||||
std::map<std::string, Button*> buttonMap;
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -26,8 +26,9 @@ private:
|
||||
void LoadScene(SceneList sceneIndex);
|
||||
void UnloadScene();
|
||||
|
||||
BaseScene* activeScene;
|
||||
BaseScene* activeScene = nullptr;
|
||||
|
||||
//globals
|
||||
ConfigUtility configUtil;
|
||||
SurfaceManager surfaceMgr;
|
||||
UDPNetworkUtility netUtil;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
+6
-4
@@ -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
|
||||
|
||||
@@ -85,10 +85,8 @@ struct Movement {
|
||||
//this state of this is great
|
||||
//-------------------------
|
||||
|
||||
union Packet {
|
||||
Packet () {}
|
||||
~Packet() {}
|
||||
|
||||
union PacketData {
|
||||
PacketData() {};
|
||||
PacketList type = PacketList::NONE;
|
||||
|
||||
//connections
|
||||
|
||||
+1
-1
@@ -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!!!!!
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
@@ -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
@@ -1,4 +1,4 @@
|
||||
#include "server.hpp"
|
||||
#include "server_application.hpp"
|
||||
|
||||
#include <stdexcept>
|
||||
#include <iostream>
|
||||
@@ -10,7 +10,7 @@ int main(int, char**) {
|
||||
cout << "Beginning server" << endl;
|
||||
#endif
|
||||
try {
|
||||
Server app;
|
||||
ServerApplication app;
|
||||
app.Init();
|
||||
app.Proc();
|
||||
app.Quit();
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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
|
||||
@@ -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();
|
||||
}
|
||||
@@ -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
|
||||
@@ -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 <iostream>
|
||||
|
||||
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<void*>(&packet), netUtil.GetInData(), sizeof(Packet));
|
||||
memcpy(reinterpret_cast<void*>(&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<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
|
||||
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
|
||||
}
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user