diff --git a/client/lobby.cpp b/client/lobby.cpp index 5ad8e3e..d55a4d6 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -20,17 +20,19 @@ Lobby::Lobby(ConfigUtility* cUtil, SurfaceManager* sMgr, UDPNetworkUtility* nUti //members font.SetSurface(surfaceMgr->Get("font")); - pingButton.SetSurfaces(surfaceMgr->Get("button"), surfaceMgr->Get("font")); - pingButton.SetText("Refresh"); - pingButton.SetX(50); - pingButton.SetY(50); - //ping the network, ping the preset "phone home" servers - //generate the server list - //eventually + buttonMap["ping"] = new Button(50, 50 , surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Refresh"); + buttonMap["join"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Join"); + buttonMap["back"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back"); + + //ping the network + PingNetwork(); } Lobby::~Lobby() { + for (auto it : buttonMap) { + delete it.second; + } #ifdef DEBUG cout << "leaving Lobby" << endl; #endif @@ -67,9 +69,16 @@ void Lobby::Receive() { } void Lobby::Render(SDL_Surface* const screen) { - pingButton.DrawTo(screen); + for (auto it : buttonMap) { + it.second->DrawTo(screen); + } + //draw the server list, highlighting the selected server for (int i = 0; i < serverVector.size(); i++) { - font.DrawStringTo(serverVector[i].name, screen, 50, 16*i + 100); + if (selectedServer && selectedServer == &serverVector[i]) { + SDL_Rect clip = {250, Sint16(16*i + 50), Uint16(screen->w - 250), 16}; + SDL_FillRect(screen, &clip, SDL_MapRGB(screen->format, 255, 127, 39)); + } + font.DrawStringTo(serverVector[i].name, screen, 250, 16*i + 50); } } @@ -78,23 +87,43 @@ void Lobby::Render(SDL_Surface* const screen) { //------------------------- void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) { - pingButton.MouseMotion(motion); + for (auto it : buttonMap) { + it.second->MouseMotion(motion); + } } void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) { - pingButton.MouseButtonDown(button); + for (auto it : buttonMap) { + it.second->MouseButtonDown(button); + } } void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) { - if (pingButton.MouseButtonUp(button) == Button::State::HOVER) { + if (buttonMap["ping"]->MouseButtonUp(button) == Button::State::HOVER) { PingNetwork(); } + if (buttonMap["join"]->MouseButtonUp(button) == Button::State::HOVER) { + //join... + if (selectedServer) { + cout << "joining server: " << selectedServer->name << endl; + } + } + if (buttonMap["back"]->MouseButtonUp(button) == Button::State::HOVER) { + SetNextScene(SceneList::MAINMENU); + } + //select a server + if (button.x >= 250 && button.y >= 50 && button.y < serverVector.size() * 16 + 50) { + selectedServer = &serverVector[(button.y-50)/16]; + } + else { + selectedServer = nullptr; + } } void Lobby::KeyDown(SDL_KeyboardEvent const& key) { switch(key.keysym.sym) { case SDLK_ESCAPE: - QuitEvent(); + SetNextScene(SceneList::MAINMENU); break; } } @@ -113,7 +142,7 @@ void Lobby::PingNetwork() { packet.type = PacketList::PING; netUtil->Send("255.255.255.255", configUtil->Integer("server.port"), reinterpret_cast(&packet), sizeof(Packet)); //reset the server list - serverVector.clear(); +// serverVector.clear(); } void Lobby::PushServer(Packet* packet) { diff --git a/client/lobby.hpp b/client/lobby.hpp index dcc4ee3..5d68ff2 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -12,6 +12,7 @@ #include "button.hpp" #include +#include #include struct Server { @@ -43,6 +44,7 @@ protected: //utilities void PingNetwork(); void PushServer(Packet*); + typedef std::map ButtonMap; //members ConfigUtility* configUtil = nullptr; @@ -50,9 +52,10 @@ protected: UDPNetworkUtility* netUtil = nullptr; RasterFont font; - Button pingButton; + ButtonMap buttonMap; std::vector serverVector; + Server* selectedServer = nullptr; }; #endif diff --git a/client/main_menu.cpp b/client/main_menu.cpp index 6d7afe3..758d5fa 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -15,9 +15,9 @@ MainMenu::MainMenu(ConfigUtility* cUtil, SurfaceManager* sMgr) { configUtil = cUtil; surfaceMgr = sMgr; - buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "start"); - buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "options"); - buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "quit"); + buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start"); + buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options"); + buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit"); } MainMenu::~MainMenu() { @@ -69,18 +69,13 @@ void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) { - //TODO SetNextScene(SceneList::LOBBY); - cout << "start" << endl; } if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) { //TODO - cout << "options" << endl; } if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) { - //TODO QuitEvent(); - cout << "quit" << endl; } } diff --git a/screenshots/select your poison.png b/screenshots/select your poison.png new file mode 100644 index 0000000..303b3df Binary files /dev/null and b/screenshots/select your poison.png differ