client to server contact
This commit is contained in:
@@ -55,10 +55,20 @@ ClientApplication::~ClientApplication() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::Init() {
|
void ClientApplication::Init() {
|
||||||
if (SDL_Init(SDL_INIT_VIDEO))
|
//load the prerequisites
|
||||||
throw(std::runtime_error("Failed to initialize SDL"));
|
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);
|
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() {
|
void ClientApplication::Proc() {
|
||||||
@@ -100,6 +110,8 @@ void ClientApplication::Proc() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void ClientApplication::Quit() {
|
void ClientApplication::Quit() {
|
||||||
|
network.Close();
|
||||||
|
SDLNet_Quit();
|
||||||
SDL_Quit();
|
SDL_Quit();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,7 +135,7 @@ void ClientApplication::LoadScene(SceneList sceneIndex) {
|
|||||||
activeScene = new OptionsMenu();
|
activeScene = new OptionsMenu();
|
||||||
break;
|
break;
|
||||||
case SceneList::LOBBYMENU:
|
case SceneList::LOBBYMENU:
|
||||||
activeScene = new LobbyMenu();
|
activeScene = new LobbyMenu(&config, &network);
|
||||||
break;
|
break;
|
||||||
case SceneList::INWORLD:
|
case SceneList::INWORLD:
|
||||||
activeScene = new InWorld();
|
activeScene = new InWorld();
|
||||||
|
|||||||
@@ -25,6 +25,10 @@
|
|||||||
#include "scene_list.hpp"
|
#include "scene_list.hpp"
|
||||||
#include "base_scene.hpp"
|
#include "base_scene.hpp"
|
||||||
|
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
#include "network_packet.hpp"
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
|
||||||
class ClientApplication {
|
class ClientApplication {
|
||||||
private:
|
private:
|
||||||
ClientApplication();
|
ClientApplication();
|
||||||
@@ -44,6 +48,9 @@ private:
|
|||||||
void UnloadScene();
|
void UnloadScene();
|
||||||
|
|
||||||
BaseScene* activeScene = nullptr;
|
BaseScene* activeScene = nullptr;
|
||||||
|
|
||||||
|
ConfigUtility config;
|
||||||
|
UDPNetworkUtility network;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+38
-10
@@ -25,22 +25,35 @@
|
|||||||
//Public access members
|
//Public access members
|
||||||
//-------------------------
|
//-------------------------
|
||||||
|
|
||||||
LobbyMenu::LobbyMenu() {
|
LobbyMenu::LobbyMenu(ConfigUtility* const arg1, UDPNetworkUtility* const arg2):
|
||||||
|
config(*arg1),
|
||||||
|
network(*arg2)
|
||||||
|
{
|
||||||
//setup the utility objects
|
//setup the utility objects
|
||||||
image.LoadSurface("rsc\\graphics\\interface\\button_menu.bmp");
|
image.LoadSurface("rsc\\graphics\\interface\\button_menu.bmp");
|
||||||
image.SetClipH(image.GetClipH()/3);
|
image.SetClipH(image.GetClipH()/3);
|
||||||
font.LoadSurface("rsc\\graphics\\fonts\\pk_white_8.bmp");
|
font.LoadSurface("rsc\\graphics\\fonts\\pk_white_8.bmp");
|
||||||
|
|
||||||
//pass the utility objects
|
//pass the utility objects
|
||||||
backButton.SetImage(&image);
|
search.SetImage(&image);
|
||||||
backButton.SetFont(&font);
|
search.SetFont(&font);
|
||||||
|
join.SetImage(&image);
|
||||||
|
join.SetFont(&font);
|
||||||
|
back.SetImage(&image);
|
||||||
|
back.SetFont(&font);
|
||||||
|
|
||||||
//set the button positions
|
//set the button positions
|
||||||
backButton.SetX(50);
|
search.SetX(50);
|
||||||
backButton.SetY(50 + image.GetClipH() * 0);
|
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
|
//set the button texts
|
||||||
backButton.SetText("Back");
|
search.SetText("Search");
|
||||||
|
join.SetText("Join");
|
||||||
|
back.SetText("Back");
|
||||||
}
|
}
|
||||||
|
|
||||||
LobbyMenu::~LobbyMenu() {
|
LobbyMenu::~LobbyMenu() {
|
||||||
@@ -64,7 +77,9 @@ void LobbyMenu::FrameEnd() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void LobbyMenu::Render(SDL_Surface* const screen) {
|
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) {
|
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) {
|
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) {
|
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);
|
SetNextScene(SceneList::MAINMENU);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+22
-2
@@ -28,10 +28,17 @@
|
|||||||
#include "raster_font.hpp"
|
#include "raster_font.hpp"
|
||||||
#include "button.hpp"
|
#include "button.hpp"
|
||||||
|
|
||||||
|
#include "config_utility.hpp"
|
||||||
|
#include "udp_network_utility.hpp"
|
||||||
|
#include "network_packet.hpp"
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
class LobbyMenu : public BaseScene {
|
class LobbyMenu : public BaseScene {
|
||||||
public:
|
public:
|
||||||
//Public access members
|
//Public access members
|
||||||
LobbyMenu();
|
LobbyMenu() = delete;
|
||||||
|
LobbyMenu(ConfigUtility* const, UDPNetworkUtility* const);
|
||||||
~LobbyMenu();
|
~LobbyMenu();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
@@ -48,10 +55,23 @@ protected:
|
|||||||
void KeyDown(SDL_KeyboardEvent const&);
|
void KeyDown(SDL_KeyboardEvent const&);
|
||||||
void KeyUp(SDL_KeyboardEvent const&);
|
void KeyUp(SDL_KeyboardEvent const&);
|
||||||
|
|
||||||
|
//handles
|
||||||
|
ConfigUtility& config;
|
||||||
|
UDPNetworkUtility& network;
|
||||||
|
|
||||||
//members
|
//members
|
||||||
Image image;
|
Image image;
|
||||||
RasterFont font;
|
RasterFont font;
|
||||||
Button backButton;
|
Button search;
|
||||||
|
Button join;
|
||||||
|
Button back;
|
||||||
|
|
||||||
|
struct ServerInfo {
|
||||||
|
std::string name;
|
||||||
|
IPaddress address;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<ServerInfo> serverInfo;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
+1
-1
@@ -1,7 +1,7 @@
|
|||||||
#config
|
#config
|
||||||
COMMONDIR+=../common ../common/map ../common/network ../common/ui
|
COMMONDIR+=../common ../common/map ../common/network ../common/ui
|
||||||
COMMON+=../libcommon.a
|
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))
|
CXXFLAGS+=-std=c++11 -DDEBUG $(addprefix -I,$(COMMONDIR))
|
||||||
CFLAGS+=-DDEBUG $(addprefix -I,$(COMMONDIR))
|
CFLAGS+=-DDEBUG $(addprefix -I,$(COMMONDIR))
|
||||||
|
|
||||||
|
|||||||
@@ -78,11 +78,6 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
}
|
}
|
||||||
cout << "Thread safety confirmed" << endl;
|
cout << "Thread safety confirmed" << endl;
|
||||||
|
|
||||||
if (running) {
|
|
||||||
throw(std::runtime_error("Multiple calls to ServerApplication::Init() is not allowed"));
|
|
||||||
}
|
|
||||||
running = true;
|
|
||||||
|
|
||||||
//load config
|
//load config
|
||||||
config.Load("rsc\\config.cfg");
|
config.Load("rsc\\config.cfg");
|
||||||
|
|
||||||
@@ -125,18 +120,11 @@ void ServerApplication::Init(int argc, char** argv) {
|
|||||||
throw(runtime_error("Failed to create the networkQueueThread"));
|
throw(runtime_error("Failed to create the networkQueueThread"));
|
||||||
}
|
}
|
||||||
cout << "initialized networkQueueThread" << endl;
|
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() {
|
void ServerApplication::Loop() {
|
||||||
//debugging
|
//debugging
|
||||||
SDL_Delay(1000);
|
while(running) {
|
||||||
|
|
||||||
while(networkQueue.Size() > 0) {
|
while(networkQueue.Size() > 0) {
|
||||||
try {
|
try {
|
||||||
HandlePacket(networkQueue.Pop());
|
HandlePacket(networkQueue.Pop());
|
||||||
@@ -145,6 +133,7 @@ void ServerApplication::Loop() {
|
|||||||
cerr << "Network Error: " << e.what() << endl;
|
cerr << "Network Error: " << e.what() << endl;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void ServerApplication::Quit() {
|
void ServerApplication::Quit() {
|
||||||
@@ -165,14 +154,12 @@ void ServerApplication::HandlePacket(NetworkPacket packet) {
|
|||||||
switch(packet.meta.type) {
|
switch(packet.meta.type) {
|
||||||
case NetworkPacket::Type::PING:
|
case NetworkPacket::Type::PING:
|
||||||
//NOT USED
|
//NOT USED
|
||||||
//debugging
|
|
||||||
cout << packet.serverInfo.name << endl;
|
|
||||||
break;
|
break;
|
||||||
case NetworkPacket::Type::PONG:
|
case NetworkPacket::Type::PONG:
|
||||||
//NOT USED
|
//NOT USED
|
||||||
break;
|
break;
|
||||||
case NetworkPacket::Type::BROADCAST_REQUEST:
|
case NetworkPacket::Type::BROADCAST_REQUEST:
|
||||||
//
|
cout << "Recieved a request" << endl;
|
||||||
break;
|
break;
|
||||||
case NetworkPacket::Type::BROADCAST_RESPONSE:
|
case NetworkPacket::Type::BROADCAST_RESPONSE:
|
||||||
//
|
//
|
||||||
|
|||||||
@@ -49,7 +49,7 @@ private:
|
|||||||
void HandlePacket(NetworkPacket);
|
void HandlePacket(NetworkPacket);
|
||||||
|
|
||||||
//members
|
//members
|
||||||
bool running = false;
|
bool running = true;
|
||||||
ConfigUtility config;
|
ConfigUtility config;
|
||||||
|
|
||||||
//networking
|
//networking
|
||||||
|
|||||||
Reference in New Issue
Block a user