lobby_menu.cpp builds, as does all of client/menu_scenes

This is great progress, but I think I should turn the common directory into a subnmodule, since
I've been constantly tabbing back and forward between this folder, and the debug folder, making
and committing identical edits to both.

 Scene list:
        > disconnected_screen.*pp
        > lobby_menu.*pp
        > main_menu.*pp
        > options_menu.*pp
        > splash_screen.*pp
        * world*.*pp

        * unfinished
        > building
This commit is contained in:
2015-08-14 21:47:04 +10:00
parent e8347cb9fb
commit 2f86511254
6 changed files with 109 additions and 97 deletions
+84 -78
View File
@@ -39,33 +39,30 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
accountIndex = -1; accountIndex = -1;
//setup the utility objects //setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp"); buttonImage.Load(GetRenderer(), config["dir.interface"] + "button.png");
image.SetClipH(image.GetClipH()/3); font = TTF_OpenFont(config["client.font"].c_str(), 12);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
//pass the utility objects //setup the buttons
search.SetImage(&image); searchButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
search.SetFont(&font); searchButton.SetText(GetRenderer(), font, "Search", {255, 255, 255, 255});
join.SetImage(&image); joinButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
join.SetFont(&font); joinButton.SetText(GetRenderer(), font, "Join", {255, 255, 255, 255});
back.SetImage(&image); backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
back.SetFont(&font); backButton.SetText(GetRenderer(), font, "Back", {255, 255, 255, 255});
//set the button positions //set the button positions (assumed)
search.SetX(50); searchButton.SetX(50);
search.SetY(50 + image.GetClipH() * 0); searchButton.SetY(50);
join.SetX(50); joinButton.SetX(50);
join.SetY(50 + image.GetClipH() * 1); joinButton.SetY(70);
back.SetX(50); backButton.SetX(50);
back.SetY(50 + image.GetClipH() * 2); backButton.SetY(90);
//set the button texts //pseudo-list selection
search.SetText("Search"); boundingBox = {300, 50, 200, 12};
join.SetText("Join");
back.SetText("Back");
//set the server list's position //DEBUG: hacked together a highlight box
listBox = {300, 50, 200, font.GetCharH()}; highlightImage.Create(GetRenderer(), 300, 12, {49, 150, 5, 255});
//Eat incoming packets //Eat incoming packets
while(network.Receive()); while(network.Receive());
@@ -75,7 +72,7 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
} }
LobbyMenu::~LobbyMenu() { LobbyMenu::~LobbyMenu() {
// TTF_CloseFont(font);
} }
//------------------------- //-------------------------
@@ -99,39 +96,25 @@ void LobbyMenu::FrameEnd() {
// //
} }
void ExampleScene::RenderFrame(SDL_Renderer* renderer) { void LobbyMenu::RenderFrame(SDL_Renderer* renderer) {
//TODO: (2) I need a proper UI system for the entire client and the editor //TODO: (2) I need a proper UI system for the entire client and the editor
//UI //UI
search.DrawTo(renderer); searchButton.DrawTo(renderer);
join.DrawTo(renderer); joinButton.DrawTo(renderer);
back.DrawTo(renderer); backButton.DrawTo(renderer);
//TODO: (3) draw headers for the server list //TODO: (3) draw headers for the server list
//TODO: (3) ping/delay displayed in the server list //TODO: (3) ping/delay displayed in the server list
for (int i = 0; i < serverInfo.size(); i++) { for (int i = 0; i < serverVector.size(); i++) {
//draw the selected server's highlight //draw the selected server's highlight
if (selection == &serverInfo[i]) { if (selection == &serverVector[i]) {
SDL_Rect r = { highlightImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
(Sint16)listBox.x, (Sint16)listBox.y,
(Uint16)listBox.w, (Uint16)listBox.h
};
r.y += i * listBox.h;
SDL_FillRect(screen, &r, SDL_MapRGB(screen->format, 49, 150, 5));
} }
//draw the server name //draw the server's info
font.DrawStringTo(serverInfo[i].name, screen, listBox.x, listBox.y + i*listBox.h); serverVector[i].nameImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
serverVector[i].playerCountImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
//draw the player count
std::ostringstream msg;
msg << serverInfo[i].playerCount;
font.DrawStringTo(msg.str(), screen, listBox.x + listBox.w, listBox.y + i*listBox.h);
//compatible?
if (!serverInfo[i].compatible) {
font.DrawStringTo("?", screen, listBox.x - font.GetCharW(), listBox.y + i*listBox.h);
}
} }
} }
@@ -139,34 +122,35 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
//Event handlers //Event handlers
//------------------------- //-------------------------
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& motion) { void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& event) {
search.MouseMotion(motion); searchButton.MouseMotion(event);
join.MouseMotion(motion); joinButton.MouseMotion(event);
back.MouseMotion(motion); backButton.MouseMotion(event);
} }
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& event) {
search.MouseButtonDown(button); searchButton.MouseButtonDown(event);
join.MouseButtonDown(button); joinButton.MouseButtonDown(event);
back.MouseButtonDown(button); backButton.MouseButtonDown(event);
} }
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& event) {
if (search.MouseButtonUp(button) == Button::State::HOVER) { if (searchButton.MouseButtonUp(event) == Button::State::RELEASED) {
SendBroadcastRequest(); SendBroadcastRequest();
} }
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { if (joinButton.MouseButtonUp(event) == Button::State::RELEASED && selection && selection->compatible) {
SendJoinRequest(); SendJoinRequest();
} }
else if (back.MouseButtonUp(button) == Button::State::HOVER) { if (backButton.MouseButtonUp(event) == Button::State::RELEASED) {
SetNextScene(SceneList::MAINMENU); SetSceneSignal(SceneSignal::MAINMENU);
} }
//has the user selected a server on the list? //has the user selected a server on the list?
BoundingBox tmpBox = listBox; BoundingBox tmpBox = boundingBox;
tmpBox.h *= serverInfo.size(); tmpBox.h *= serverVector.size(); //within the list bounds
if (tmpBox.CheckOverlap({button.x, button.y})) { if (tmpBox.CheckOverlap({event.x, event.y})) {
selection = &serverInfo[(button.y - listBox.y)/listBox.h]; //NOTE: this memory trick requires a vector
selection = &serverVector[(event.y - boundingBox.y)/boundingBox.h];
} }
else { else {
selection = nullptr; selection = nullptr;
@@ -177,15 +161,15 @@ void LobbyMenu::MouseWheel(SDL_MouseWheelEvent const& event) {
// //
} }
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) { void LobbyMenu::KeyDown(SDL_KeyboardEvent const& event) {
switch(key.keysym.sym) { switch(event.keysym.sym) {
case SDLK_ESCAPE: case SDLK_ESCAPE:
SetSceneSignal(SceneSignal::MAINMENU); SetSceneSignal(SceneSignal::MAINMENU);
break; break;
} }
} }
void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) { void LobbyMenu::KeyUp(SDL_KeyboardEvent const& event) {
// //
} }
@@ -226,17 +210,38 @@ void LobbyMenu::HandlePacket(SerialPacket* const argPacket) {
void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) { void LobbyMenu::HandleBroadcastResponse(ServerPacket* const argPacket) {
//extract the data //extract the data
ServerInformation server; ServerInfo newServer;
server.address = argPacket->srcAddress;
server.name = argPacket->name;
server.playerCount = argPacket->playerCount;
server.version = argPacket->version;
//Checking compatibility newServer.address = argPacket->srcAddress;
server.compatible = server.version == NETWORK_VERSION; newServer.name = argPacket->name;
newServer.playerCount = argPacket->playerCount;
newServer.version = argPacket->version;
newServer.compatible = newServer.version == NETWORK_VERSION;
//push //push
serverInfo.push_back(server); serverVector.push_back(newServer);
//BUGFIX: since TextLine lacks the memory management of Image, I'll wait until after the line is in the vector to handle these
//fancy colors
SDL_Color color;
if (newServer.compatible) {
color = {255, 255, 255, 255};
}
else {
color = {255, 0, 0, 255};
}
//fancy itoa
auto itoa_base10 = [](int i) -> std::string {
char str[20];
printf(str, "%d", i);
return std::string(str);
};
//text graphics
serverVector.back().nameImage.SetText(GetRenderer(), font, newServer.name, color);
serverVector.back().playerCountImage.SetText(GetRenderer(), font, itoa_base10(newServer.playerCount), color);
} }
void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) {
@@ -253,7 +258,7 @@ void LobbyMenu::HandleLoginResponse(ClientPacket* const argPacket) {
throw(std::runtime_error("Client index invalid during login")); throw(std::runtime_error("Client index invalid during login"));
} }
accountIndex = argPacket->accountIndex; accountIndex = argPacket->accountIndex;
SetNextScene(SceneList::WORLD); SetSceneSignal(SceneSignal::WORLD);
} }
void LobbyMenu::HandleJoinRejection(TextPacket* const argPacket) { void LobbyMenu::HandleJoinRejection(TextPacket* const argPacket) {
@@ -275,7 +280,7 @@ void LobbyMenu::SendBroadcastRequest() {
network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet);
//reset the server list //reset the server list
serverInfo.clear(); serverVector.clear();
selection = nullptr; selection = nullptr;
} }
@@ -291,6 +296,7 @@ void LobbyMenu::SendJoinRequest() {
void LobbyMenu::SendLoginRequest() { void LobbyMenu::SendLoginRequest() {
//NOTE: high cohesion //NOTE: high cohesion
//TODO: have a separate login screen
ClientPacket packet; ClientPacket packet;
packet.type = SerialPacketType::LOGIN_REQUEST; packet.type = SerialPacketType::LOGIN_REQUEST;
packet.clientIndex = clientIndex; packet.clientIndex = clientIndex;
+20 -12
View File
@@ -25,6 +25,9 @@
#include "image.hpp" #include "image.hpp"
#include "button.hpp" #include "button.hpp"
#include "bounding_box.hpp" #include "bounding_box.hpp"
#include "text_line.hpp"
#include "SDL2/SDL_ttf.h"
//utilities //utilities
#include "config_utility.hpp" #include "config_utility.hpp"
@@ -78,14 +81,13 @@ protected:
int& clientIndex; int& clientIndex;
int& accountIndex; int& accountIndex;
//members //define the list object
Image image; struct ServerInfo {
Button search; //graphics
Button join; TextLine nameImage;
Button back; TextLine playerCountImage;
//server list //networking
struct ServerInformation {
IPaddress address; IPaddress address;
std::string name; std::string name;
int playerCount; int playerCount;
@@ -93,10 +95,16 @@ protected:
bool compatible; bool compatible;
}; };
std::vector<ServerInformation> serverInfo; //members
Image buttonImage;
Image highlightImage;
TTF_Font* font = nullptr;
Button searchButton;
Button joinButton;
Button backButton;
//NOTE: a terrible hack std::vector<ServerInfo> serverVector;
//I'd love a proper gui system for this ServerInfo* selection = nullptr;
BoundingBox listBox;
ServerInformation* selection = nullptr; BoundingBox boundingBox;
}; };
-3
View File
@@ -6,9 +6,6 @@ CXXFLAGS+=-std=c++11 $(addprefix -I,$(INCLUDES))
#source #source
CXXSRC=$(wildcard *.cpp) CXXSRC=$(wildcard *.cpp)
#DEBUG: override the wildcard
CXXSRC=disconnected_screen.cpp main_menu.cpp options_menu.cpp splash_screen.cpp
#objects #objects
OBJDIR=obj OBJDIR=obj
OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o)) OBJ+=$(addprefix $(OBJDIR)/,$(CXXSRC:.cpp=.o))
+3 -2
View File
@@ -95,7 +95,7 @@ SDL_Texture* Image::Load(SDL_Renderer* renderer, std::string fname) {
return texture; return texture;
} }
SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) { SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h, SDL_Color blank) {
Free(); Free();
//make the texture //make the texture
@@ -124,8 +124,9 @@ SDL_Texture* Image::Create(SDL_Renderer* renderer, Uint16 w, Uint16 h) {
//blank (black) texture //blank (black) texture
SDL_SetRenderTarget(renderer, texture); SDL_SetRenderTarget(renderer, texture);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_SetRenderDrawColor(renderer, blank.r, blank.g, blank.b, blank.a);
SDL_RenderFillRect(renderer, nullptr); SDL_RenderFillRect(renderer, nullptr);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
SDL_SetRenderTarget(renderer, nullptr); SDL_SetRenderTarget(renderer, nullptr);
return texture; return texture;
+1 -1
View File
@@ -39,7 +39,7 @@ public:
Image& operator=(Image&&); Image& operator=(Image&&);
SDL_Texture* Load(SDL_Renderer* renderer, std::string fname); SDL_Texture* Load(SDL_Renderer* renderer, std::string fname);
SDL_Texture* Create(SDL_Renderer* renderer, Uint16 w, Uint16 h); SDL_Texture* Create(SDL_Renderer* renderer, Uint16 w, Uint16 h, SDL_Color blank = {0, 0, 0, 255});
SDL_Texture* CopyTexture(SDL_Renderer* renderer, SDL_Texture* ptr); SDL_Texture* CopyTexture(SDL_Renderer* renderer, SDL_Texture* ptr);
SDL_Texture* SetTexture(SDL_Texture*); SDL_Texture* SetTexture(SDL_Texture*);
SDL_Texture* GetTexture() const; SDL_Texture* GetTexture() const;
+1 -1
View File
@@ -33,7 +33,7 @@ public:
TextLine(); TextLine();
TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c) TextLine(SDL_Renderer* r, TTF_Font* f, std::string s, SDL_Color c)
{ SetText(r, f, s, c); } { SetText(r, f, s, c); }
~TextLine(); virtual ~TextLine();
void DrawTo(SDL_Renderer*, int posX, int posY); void DrawTo(SDL_Renderer*, int posX, int posY);