client to server contact

This commit is contained in:
Kayne Ruse
2013-11-16 23:46:59 +11:00
parent dc24d1b059
commit e176a60d28
7 changed files with 95 additions and 41 deletions
+15 -3
View File
@@ -55,10 +55,20 @@ ClientApplication::~ClientApplication() {
}
void ClientApplication::Init() {
if (SDL_Init(SDL_INIT_VIDEO))
throw(std::runtime_error("Failed to initialize SDL"));
//load the prerequisites
config.Load("rsc\\config.cfg");
//initialize SDL
if (SDL_Init(SDL_INIT_VIDEO)) {
throw(std::runtime_error("Failed to initialize SDL"));
}
BaseScene::SetScreen(800, 600);
//initialize SDL_net
if (SDLNet_Init()) {
throw(std::runtime_error("Failed to initialize SDL_net"));
}
network.Open(0, sizeof(NetworkPacket));
}
void ClientApplication::Proc() {
@@ -100,6 +110,8 @@ void ClientApplication::Proc() {
}
void ClientApplication::Quit() {
network.Close();
SDLNet_Quit();
SDL_Quit();
}
@@ -123,7 +135,7 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
activeScene = new OptionsMenu();
break;
case SceneList::LOBBYMENU:
activeScene = new LobbyMenu();
activeScene = new LobbyMenu(&config, &network);
break;
case SceneList::INWORLD:
activeScene = new InWorld();
+7
View File
@@ -25,6 +25,10 @@
#include "scene_list.hpp"
#include "base_scene.hpp"
#include "config_utility.hpp"
#include "network_packet.hpp"
#include "udp_network_utility.hpp"
class ClientApplication {
private:
ClientApplication();
@@ -44,6 +48,9 @@ private:
void UnloadScene();
BaseScene* activeScene = nullptr;
ConfigUtility config;
UDPNetworkUtility network;
};
#endif
+38 -10
View File
@@ -25,22 +25,35 @@
//Public access members
//-------------------------
LobbyMenu::LobbyMenu() {
LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2):
config(*arg1),
network(*arg2)
{
//setup the utility objects
image.LoadSurface("rsc\\graphics\\interface\\button_menu.bmp");
image.SetClipH(image.GetClipH()/3);
font.LoadSurface("rsc\\graphics\\fonts\\pk_white_8.bmp");
//pass the utility objects
backButton.SetImage(&image);
backButton.SetFont(&font);
search.SetImage(&image);
search.SetFont(&font);
join.SetImage(&image);
join.SetFont(&font);
back.SetImage(&image);
back.SetFont(&font);
//set the button positions
backButton.SetX(50);
backButton.SetY(50 + image.GetClipH() * 0);
search.SetX(50);
search.SetY(50 + image.GetClipH() * 0);
join.SetX(50);
join.SetY(50 + image.GetClipH() * 1);
back.SetX(50);
back.SetY(50 + image.GetClipH() * 2);
//set the button texts
backButton.SetText("Back");
search.SetText("Search");
join.SetText("Join");
back.SetText("Back");
}
LobbyMenu::~LobbyMenu() {
@@ -64,7 +77,9 @@ void LobbyMenu::FrameEnd() {
}
void LobbyMenu::Render(SDL_Surface* const screen) {
backButton.DrawTo(screen);
search.DrawTo(screen);
join.DrawTo(screen);
back.DrawTo(screen);
}
//-------------------------
@@ -72,15 +87,28 @@ void LobbyMenu::Render(SDL_Surface* const screen) {
//-------------------------
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
backButton.MouseMotion(motion);
search.MouseMotion(motion);
join.MouseMotion(motion);
back.MouseMotion(motion);
}
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
backButton.MouseButtonDown(button);
search.MouseButtonDown(button);
join.MouseButtonDown(button);
back.MouseButtonDown(button);
}
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (backButton.MouseButtonUp(button) == Button::State::HOVER) {
if (search.MouseButtonUp(button) == Button::State::HOVER) {
//ping the LAN
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::BROADCAST_REQUEST;
network.Send(config["server.host"].c_str(), config.Int("server.port"), reinterpret_cast<void*>(&packet), sizeof(NetworkPacket));
}
if (join.MouseButtonUp(button) == Button::State::HOVER) {
//join the selected server
}
if (back.MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::MAINMENU);
}
}
+22 -2
View File
@@ -28,10 +28,17 @@
#include "raster_font.hpp"
#include "button.hpp"
#include "config_utility.hpp"
#include "udp_network_utility.hpp"
#include "network_packet.hpp"
#include <vector>
class LobbyMenu : public BaseScene {
public:
//Public access members
LobbyMenu();
LobbyMenu() = delete;
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const);
~LobbyMenu();
protected:
@@ -48,10 +55,23 @@ protected:
void KeyDown(SDL_KeyboardEvent const&);
void KeyUp(SDL_KeyboardEvent const&);
//handles
ConfigUtility& config;
UDPNetworkUtility& network;
//members
Image image;
RasterFont font;
Button backButton;
Button search;
Button join;
Button back;
struct ServerInfo {
std::string name;
IPaddress address;
};
std::vector<ServerInfo> serverInfo;
};
#endif
+1 -1
View File
@@ -1,7 +1,7 @@
#config
COMMONDIR+=../common ../common/map ../common/network ../common/ui
COMMON+=../libcommon.a
LIB+=$(COMMON) -lmingw32 -lSDLmain -lSDL -llua -lsqlite3
LIB+=$(COMMON) -lSDL_net -lwsock32 -liphlpapi -lmingw32 -lSDLmain -lSDL
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR))
CFLAGS+=-DDEBUG $(addprefix -I,$(COMMONDIR))
+3 -16
View File
@@ -78,11 +78,6 @@ void ServerApplication::Init(int argc, char** argv) {
}
cout << "Thread safety confirmed" << endl;
if (running) {
throw(std::runtime_error("Multiple calls to ServerApplication::Init() is not allowed"));
}
running = true;
//load config
config.Load("rsc\\config.cfg");
@@ -125,18 +120,11 @@ void ServerApplication::Init(int argc, char** argv) {
throw(runtime_error("Failed to create the networkQueueThread"));
}
cout << "initialized networkQueueThread" << endl;
//debugging
NetworkPacket packet;
packet.meta.type = NetworkPacket::Type::PING;
strcpy(packet.serverInfo.name, "foo");
networkUtil.Send(config["server.host"].c_str(), config.Int("server.port"), &packet, sizeof(NetworkPacket));
}
void ServerApplication::Loop() {
//debugging
SDL_Delay(1000);
while(running) {
while(networkQueue.Size() > 0) {
try {
HandlePacket(networkQueue.Pop());
@@ -145,6 +133,7 @@ void ServerApplication::Loop() {
cerr << "Network Error: " << e.what() << endl;
}
};
}
}
void ServerApplication::Quit() {
@@ -165,14 +154,12 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
switch(packet.meta.type) {
case NetworkPacket::Type::PING:
//NOT USED
//debugging
cout << packet.serverInfo.name << endl;
break;
case NetworkPacket::Type::PONG:
//NOT USED
break;
case NetworkPacket::Type::BROADCAST_REQUEST:
//
cout << "Recieved a request" << endl;
break;
case NetworkPacket::Type::BROADCAST_RESPONSE:
//
+1 -1
View File
@@ -49,7 +49,7 @@ private:
void HandlePacket(NetworkPacket);
//members
bool running = false;
bool running = true;
ConfigUtility config;
//networking