From 843053d3074182251b0f1800041d145e28ea4436 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Thu, 13 Jun 2013 12:26:27 +1000 Subject: [PATCH] Implemented the server list box, still messy --- client/lobby.cpp | 53 ++++++++++++++++++++++++++++++++--- client/lobby.hpp | 24 ++++++++++++++-- server/server_application.cpp | 3 +- 3 files changed, 72 insertions(+), 8 deletions(-) diff --git a/client/lobby.cpp b/client/lobby.cpp index 7c1658e..8e67f43 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -15,6 +15,18 @@ Lobby::Lobby() { refreshButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Refresh"); joinButton.Setup(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Join"); backButton.Setup(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back"); + + font.SetSurface(surfaceMgr->Get("font")); + listBox.x = 280; + listBox.y = 50; + listBox.w = GetScreen()->w - listBox.x - 50; + listBox.h = font.GetCharH(); + + serverList.push_back({"foo",{0,0}}); + serverList.push_back({"bar",{0,0}}); + serverList.push_back({"foobar",{0,0}}); + + BroadcastNetwork(); } Lobby::~Lobby() { @@ -43,6 +55,16 @@ void Lobby::Render(SDL_Surface* const screen) { refreshButton.DrawTo(screen); joinButton.DrawTo(screen); backButton.DrawTo(screen); + + for (int i = 0; i < serverList.size(); i++) { + if (selectedServer == &serverList[i]) { + //draw the highlight box + SDL_Rect r = listBox; + r.y += i * font.GetCharH(); + SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 255, 127, 39)); + } + font.DrawStringTo(serverList[i].name, screen, listBox.x, listBox.y + i * font.GetCharH()); + } } //------------------------- @@ -63,14 +85,29 @@ void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) { void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (refreshButton.MouseButtonUp(button) == Button::State::HOVER) { - //ping the server + BroadcastNetwork(); } - if (joinButton.MouseButtonUp(button) == Button::State::HOVER) { - //join a server + else if (joinButton.MouseButtonUp(button) == Button::State::HOVER) { + //TODO: join a server } - if (backButton.MouseButtonUp(button) == Button::State::HOVER) { + else if (backButton.MouseButtonUp(button) == Button::State::HOVER) { QuitEvent(); } + else if ( + //clicked within bounds TODO: make the damn collision system + button.x > listBox.x && + button.y > listBox.y && + button.x < listBox.x + listBox.w && + button.y < listBox.y + (listBox.h * serverList.size()) + ) + { + //selecting a server + selectedServer = &serverList[(button.y - listBox.y) / listBox.h]; + } + else { + //lose focus on a server + selectedServer = nullptr; + } } void Lobby::KeyDown(SDL_KeyboardEvent const& key) { @@ -84,3 +121,11 @@ void Lobby::KeyDown(SDL_KeyboardEvent const& key) { void Lobby::KeyUp(SDL_KeyboardEvent const& key) { // } + +//------------------------- +//Utilities +//------------------------- + +void Lobby::BroadcastNetwork() { + // +} \ No newline at end of file diff --git a/client/lobby.hpp b/client/lobby.hpp index 1e773da..625b5af 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -8,21 +8,30 @@ #include "surface_manager.hpp" #include "udp_network_utility.hpp" #include "button.hpp" +#include "raster_font.hpp" + +#include +#include + +struct ServerEntry { + std::string name; + IPaddress address; +}; class Lobby : public BaseScene { public: - /* Public access members */ + //Public access members Lobby(); ~Lobby(); protected: - /* Frame loop */ + //Frame loop void FrameStart(); void Update(double delta); void FrameEnd(); void Render(SDL_Surface* const); - /* Event handlers */ + //Event handlers void QuitEvent() { SetNextScene(SceneList::MAINMENU); } void MouseMotion(SDL_MouseMotionEvent const&); void MouseButtonDown(SDL_MouseButtonEvent const&); @@ -30,14 +39,23 @@ protected: void KeyDown(SDL_KeyboardEvent const&); void KeyUp(SDL_KeyboardEvent const&); + //utilities + void BroadcastNetwork(); + //services ConfigUtility* configUtil = ServiceLocator::Get(); SurfaceManager* surfaceMgr = ServiceLocator::Get(); UDPNetworkUtility* netUtil = ServiceLocator::Get(); + //members Button refreshButton; Button joinButton; Button backButton; + + RasterFont font; + SDL_Rect listBox; + std::vector serverList; + ServerEntry* selectedServer = nullptr; }; #endif diff --git a/server/server_application.cpp b/server/server_application.cpp index b318f89..0dadac0 100644 --- a/server/server_application.cpp +++ b/server/server_application.cpp @@ -28,6 +28,7 @@ void ServerApplication::Init() { if (configUtil.Int("server.port") <= 0) { throw(runtime_error("Cannot open the server on an invalid port or port 0")); } + cout << configUtil["server.name"] << endl; cout << "Opening on port " << configUtil["server.port"] << endl; netUtil.Open(configUtil.Int("server.port"), sizeof(Packet)); @@ -116,7 +117,7 @@ void ServerApplication::Broadcast(BroadcastRequest& bcast) { //respond to a broadcast request with the server's data Packet p; p.type = PacketType::BROADCAST_RESPONSE; - snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil.CString("servername")); + snprintf(p.broadcastResponse.name, PACKET_STRING_SIZE, "%s", configUtil.CString("server.name")); //TODO version information netUtil.Send(&netUtil.GetInPacket()->address, &p, sizeof(Packet)); } \ No newline at end of file