diff --git a/client/client_application.cpp b/client/client_application.cpp index 37006eb..35ed2bb 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -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(); diff --git a/client/client_application.hpp b/client/client_application.hpp index 861ef03..bf8af4b 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -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 diff --git a/client/lobby_menu.cpp b/client/lobby_menu.cpp index f337d52..809787d 100644 --- a/client/lobby_menu.cpp +++ b/client/lobby_menu.cpp @@ -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(&packet), sizeof(NetworkPacket)); + } + if (join.MouseButtonUp(button) == Button::State::HOVER) { + //join the selected server + } + if (back.MouseButtonUp(button) == Button::State::HOVER) { SetNextScene(SceneList::MAINMENU); } } diff --git a/client/lobby_menu.hpp b/client/lobby_menu.hpp index a32a0f0..c50e8cc 100644 --- a/client/lobby_menu.hpp +++ b/client/lobby_menu.hpp @@ -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 + 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; }; #endif diff --git a/client/makefile b/client/makefile index 218f19d..ad32d17 100644 --- a/client/makefile +++ b/client/makefile @@ -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)) diff --git a/server/server_application.cpp b/server/server_application.cpp index cfc8c06..e9c4869 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -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,26 +120,20 @@ 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(networkQueue.Size() > 0) { - try { - HandlePacket(networkQueue.Pop()); - } - catch(exception& e) { - cerr << "Network Error: " << e.what() << endl; - } - }; + while(running) { + while(networkQueue.Size() > 0) { + try { + HandlePacket(networkQueue.Pop()); + } + catch(exception& e) { + 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: // diff --git a/server/server_application.hpp b/server/server_application.hpp index 8a28400..19a8bfd 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -49,7 +49,7 @@ private: void HandlePacket(NetworkPacket); //members - bool running = false; + bool running = true; ConfigUtility config; //networking