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;
//setup the utility objects
image.LoadSurface(config["dir.interface"] + "button_menu.bmp");
image.SetClipH(image.GetClipH()/3);
font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp");
buttonImage.Load(GetRenderer(), config["dir.interface"] + "button.png");
font = TTF_OpenFont(config["client.font"].c_str(), 12);
//pass the utility objects
search.SetImage(&image);
search.SetFont(&font);
join.SetImage(&image);
join.SetFont(&font);
back.SetImage(&image);
back.SetFont(&font);
//setup the buttons
searchButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
searchButton.SetText(GetRenderer(), font, "Search", {255, 255, 255, 255});
joinButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
joinButton.SetText(GetRenderer(), font, "Join", {255, 255, 255, 255});
backButton.SetBackgroundTexture(GetRenderer(), buttonImage.GetTexture());
backButton.SetText(GetRenderer(), font, "Back", {255, 255, 255, 255});
//set the button positions
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 positions (assumed)
searchButton.SetX(50);
searchButton.SetY(50);
joinButton.SetX(50);
joinButton.SetY(70);
backButton.SetX(50);
backButton.SetY(90);
//set the button texts
search.SetText("Search");
join.SetText("Join");
back.SetText("Back");
//pseudo-list selection
boundingBox = {300, 50, 200, 12};
//set the server list's position
listBox = {300, 50, 200, font.GetCharH()};
//DEBUG: hacked together a highlight box
highlightImage.Create(GetRenderer(), 300, 12, {49, 150, 5, 255});
//Eat incoming packets
while(network.Receive());
@@ -75,7 +72,7 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex):
}
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
//UI
search.DrawTo(renderer);
join.DrawTo(renderer);
back.DrawTo(renderer);
searchButton.DrawTo(renderer);
joinButton.DrawTo(renderer);
backButton.DrawTo(renderer);
//TODO: (3) draw headers for 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
if (selection == &serverInfo[i]) {
SDL_Rect r = {
(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));
if (selection == &serverVector[i]) {
highlightImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
}
//draw the server name
font.DrawStringTo(serverInfo[i].name, screen, listBox.x, listBox.y + i*listBox.h);
//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);
}
//draw the server's info
serverVector[i].nameImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
serverVector[i].playerCountImage.DrawTo(renderer, boundingBox.x, boundingBox.y + boundingBox.h * i);
}
}
@@ -139,34 +122,35 @@ void ExampleScene::RenderFrame(SDL_Renderer* renderer) {
//Event handlers
//-------------------------
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& motion) {
search.MouseMotion(motion);
join.MouseMotion(motion);
back.MouseMotion(motion);
void LobbyMenu::MouseMotion(SDL_MouseMotionEvent const& event) {
searchButton.MouseMotion(event);
joinButton.MouseMotion(event);
backButton.MouseMotion(event);
}
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) {
search.MouseButtonDown(button);
join.MouseButtonDown(button);
back.MouseButtonDown(button);
void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& event) {
searchButton.MouseButtonDown(event);
joinButton.MouseButtonDown(event);
backButton.MouseButtonDown(event);
}
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) {
if (search.MouseButtonUp(button) == Button::State::HOVER) {
void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& event) {
if (searchButton.MouseButtonUp(event) == Button::State::RELEASED) {
SendBroadcastRequest();
}
else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) {
if (joinButton.MouseButtonUp(event) == Button::State::RELEASED && selection && selection->compatible) {
SendJoinRequest();
}
else if (back.MouseButtonUp(button) == Button::State::HOVER) {
SetNextScene(SceneList::MAINMENU);
if (backButton.MouseButtonUp(event) == Button::State::RELEASED) {
SetSceneSignal(SceneSignal::MAINMENU);
}
//has the user selected a server on the list?
BoundingBox tmpBox = listBox;
tmpBox.h *= serverInfo.size();
if (tmpBox.CheckOverlap({button.x, button.y})) {
selection = &serverInfo[(button.y - listBox.y)/listBox.h];
BoundingBox tmpBox = boundingBox;
tmpBox.h *= serverVector.size(); //within the list bounds
if (tmpBox.CheckOverlap({event.x, event.y})) {
//NOTE: this memory trick requires a vector
selection = &serverVector[(event.y - boundingBox.y)/boundingBox.h];
}
else {
selection = nullptr;
@@ -177,15 +161,15 @@ void LobbyMenu::MouseWheel(SDL_MouseWheelEvent const& event) {
//
}
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) {
switch(key.keysym.sym) {
void LobbyMenu::KeyDown(SDL_KeyboardEvent const& event) {
switch(event.keysym.sym) {
case SDLK_ESCAPE:
SetSceneSignal(SceneSignal::MAINMENU);
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) {
//extract the data
ServerInformation server;
server.address = argPacket->srcAddress;
server.name = argPacket->name;
server.playerCount = argPacket->playerCount;
server.version = argPacket->version;
ServerInfo newServer;
//Checking compatibility
server.compatible = server.version == NETWORK_VERSION;
newServer.address = argPacket->srcAddress;
newServer.name = argPacket->name;
newServer.playerCount = argPacket->playerCount;
newServer.version = argPacket->version;
newServer.compatible = newServer.version == NETWORK_VERSION;
//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) {
@@ -253,7 +258,7 @@ void LobbyMenu::HandleLoginResponse(ClientPacket* const argPacket) {
throw(std::runtime_error("Client index invalid during login"));
}
accountIndex = argPacket->accountIndex;
SetNextScene(SceneList::WORLD);
SetSceneSignal(SceneSignal::WORLD);
}
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);
//reset the server list
serverInfo.clear();
serverVector.clear();
selection = nullptr;
}
@@ -291,6 +296,7 @@ void LobbyMenu::SendJoinRequest() {
void LobbyMenu::SendLoginRequest() {
//NOTE: high cohesion
//TODO: have a separate login screen
ClientPacket packet;
packet.type = SerialPacketType::LOGIN_REQUEST;
packet.clientIndex = clientIndex;