From 182101b5921a307c9eab92c1d6f3a41f2cd07560 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 13 Aug 2014 06:19:07 +1000 Subject: [PATCH 1/3] Minor tweaks --- common/map/region_pager_base.cpp | 5 +++-- common/network/udp_network_utility.cpp | 2 +- todo.txt | 5 ----- 3 files changed, 4 insertions(+), 8 deletions(-) diff --git a/common/map/region_pager_base.cpp b/common/map/region_pager_base.cpp index 8c31df1..f288ef0 100644 --- a/common/map/region_pager_base.cpp +++ b/common/map/region_pager_base.cpp @@ -91,8 +91,9 @@ Region* RegionPagerBase::CreateRegion(int x, int y) { } void RegionPagerBase::UnloadRegion(int x, int y) { - //custom loop, not FindRegion() - regionList.remove_if([x, y](Region& region) -> bool { return region.GetX() == x && region.GetY() == y; }); + regionList.remove_if([x, y](Region& region) -> bool { + return region.GetX() == x && region.GetY() == y; + }); } void RegionPagerBase::UnloadAll() { diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index ad97a1c..2b90df8 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -25,7 +25,7 @@ #include -//BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network +//NOTE: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network //NOTE: don't confuse SerialPacket with UDPpacket void UDPNetworkUtility::Open(int port) { diff --git a/todo.txt b/todo.txt index f3394b6..c5f5afd 100644 --- a/todo.txt +++ b/todo.txt @@ -5,14 +5,9 @@ TODO: make the whole thing more fault tolerant TODO: Authentication TODO: server is slaved to the client -TODO: I completely forgot about status ailments TODO: Time delay for requesting region packets TODO: command line parameters overriding config.cfg settings ---Battle System-- - -TODO - --Requirements-- The enemies need AI scripts From 74f809a8014ce9c17d1f30fcd9501385fbce50e3 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 13 Aug 2014 06:53:43 +1000 Subject: [PATCH 2/3] UDPNetworkUtility is a singleton, disabled InCombat scene I found incombat to be way too finicky to keep up to date, so I disabled it for now. --- client/client_application.cpp | 18 +++++++++--------- client/client_application.hpp | 1 - client/main.cpp | 9 +++++++-- client/scenes/clean_up.cpp | 2 -- client/scenes/clean_up.hpp | 3 +-- .../{in_combat.cpp => in_combat.cpp.txt} | 0 .../{in_combat.hpp => in_combat.hpp.txt} | 0 client/scenes/in_world.cpp | 2 -- client/scenes/in_world.hpp | 3 +-- client/scenes/lobby_menu.cpp | 7 +------ client/scenes/lobby_menu.hpp | 8 ++------ common/network/udp_network_utility.cpp | 2 +- common/network/udp_network_utility.hpp | 17 +++++++++++------ server/main.cpp | 9 +++++++-- server/server_application.hpp | 2 +- 15 files changed, 41 insertions(+), 42 deletions(-) rename client/scenes/{in_combat.cpp => in_combat.cpp.txt} (100%) rename client/scenes/{in_combat.hpp => in_combat.hpp.txt} (100%) diff --git a/client/client_application.cpp b/client/client_application.cpp index d50dcd2..16ad96c 100644 --- a/client/client_application.cpp +++ b/client/client_application.cpp @@ -38,7 +38,7 @@ #include "options_menu.hpp" #include "lobby_menu.hpp" #include "in_world.hpp" -#include "in_combat.hpp" +//#include "in_combat.hpp" #include "clean_up.hpp" //------------------------- @@ -66,7 +66,7 @@ void ClientApplication::Init(int argc, char** argv) { if (SDLNet_Init()) { throw(std::runtime_error("Failed to initialize SDL_net")); } - network.Open(0); + UDPNetworkUtility::GetSingleton().Open(0); std::cout << "Initialized SDL_net" << std::endl; //------------------------- @@ -151,7 +151,7 @@ void ClientApplication::Proc() { void ClientApplication::Quit() { std::cout << "Shutting down" << std::endl; - network.Close(); + UDPNetworkUtility::GetSingleton().Close(); SDLNet_Quit(); SDL_Quit(); std::cout << "Clean exit" << std::endl; @@ -176,16 +176,16 @@ void ClientApplication::LoadScene(SceneList sceneIndex) { activeScene = new OptionsMenu(); break; case SceneList::LOBBYMENU: - activeScene = new LobbyMenu(&network, &clientIndex, &accountIndex); + activeScene = new LobbyMenu(&clientIndex, &accountIndex); break; case SceneList::INWORLD: - activeScene = new InWorld(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); - break; - case SceneList::INCOMBAT: - activeScene = new InCombat(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new InWorld(&clientIndex, &accountIndex, &characterIndex, &characterMap); break; +// case SceneList::INCOMBAT: +// activeScene = new InCombat(&clientIndex, &accountIndex, &characterIndex, &characterMap); +// break; case SceneList::CLEANUP: - activeScene = new CleanUp(&network, &clientIndex, &accountIndex, &characterIndex, &characterMap); + activeScene = new CleanUp(&clientIndex, &accountIndex, &characterIndex, &characterMap); break; default: throw(std::logic_error("Failed to recognize the scene index")); diff --git a/client/client_application.hpp b/client/client_application.hpp index eea274f..773243f 100644 --- a/client/client_application.hpp +++ b/client/client_application.hpp @@ -52,7 +52,6 @@ private: BaseScene* activeScene = nullptr; //shared parameters - UDPNetworkUtility network; int clientIndex = -1; int accountIndex = -1; int characterIndex = -1; diff --git a/client/main.cpp b/client/main.cpp index 57154b6..0903053 100644 --- a/client/main.cpp +++ b/client/main.cpp @@ -23,6 +23,7 @@ //singletons #include "config_utility.hpp" +#include "udp_network_utility.hpp" #include #include @@ -32,18 +33,22 @@ using namespace std; int main(int argc, char** argv) { try { //create the singletons - ClientApplication::Create(); ConfigUtility::Create(); + UDPNetworkUtility::Create(); //call the server's routines + ClientApplication::Create(); ClientApplication& app = ClientApplication::GetSingleton(); + app.Init(argc, argv); app.Proc(); app.Quit(); + ClientApplication::Delete(); + //delete the singletons ConfigUtility::Delete(); - ClientApplication::Delete(); + UDPNetworkUtility::Delete(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; diff --git a/client/scenes/clean_up.cpp b/client/scenes/clean_up.cpp index 87846d5..c5b5d0f 100644 --- a/client/scenes/clean_up.cpp +++ b/client/scenes/clean_up.cpp @@ -31,13 +31,11 @@ //------------------------- CleanUp::CleanUp( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), characterIndex(*argCharacterIndex), diff --git a/client/scenes/clean_up.hpp b/client/scenes/clean_up.hpp index 3ba76ce..ac39b44 100644 --- a/client/scenes/clean_up.hpp +++ b/client/scenes/clean_up.hpp @@ -45,7 +45,6 @@ class CleanUp : public BaseScene { public: //Public access members CleanUp( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, @@ -68,7 +67,7 @@ protected: void KeyUp(SDL_KeyboardEvent const&); //shared parameters - UDPNetworkUtility& network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; int& characterIndex; diff --git a/client/scenes/in_combat.cpp b/client/scenes/in_combat.cpp.txt similarity index 100% rename from client/scenes/in_combat.cpp rename to client/scenes/in_combat.cpp.txt diff --git a/client/scenes/in_combat.hpp b/client/scenes/in_combat.hpp.txt similarity index 100% rename from client/scenes/in_combat.hpp rename to client/scenes/in_combat.hpp.txt diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index dac3bce..8dbd0e6 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -35,13 +35,11 @@ //------------------------- InWorld::InWorld( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, CharacterMap* argCharacterMap ): - network(*argNetwork), clientIndex(*argClientIndex), accountIndex(*argAccountIndex), characterIndex(*argCharacterIndex), diff --git a/client/scenes/in_world.hpp b/client/scenes/in_world.hpp index 8dddfdf..c72ff73 100644 --- a/client/scenes/in_world.hpp +++ b/client/scenes/in_world.hpp @@ -49,7 +49,6 @@ class InWorld : public BaseScene { public: //Public access members InWorld( - UDPNetworkUtility* const argNetwork, int* const argClientIndex, int* const argAccountIndex, int* const argCharacterIndex, @@ -92,7 +91,7 @@ protected: void UpdateMap(); //shared parameters - UDPNetworkUtility& network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; int& characterIndex; diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 2e71a89..4b7b356 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -30,12 +30,7 @@ //Public access members //------------------------- -LobbyMenu::LobbyMenu( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex - ): - network(*argNetwork), +LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): clientIndex(*argClientIndex), accountIndex(*argAccountIndex) { diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index 96afaa0..43f2c99 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -40,11 +40,7 @@ class LobbyMenu : public BaseScene { public: //Public access members - LobbyMenu( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex - ); + LobbyMenu(int* const argClientIndex, int* const argAccountIndex); ~LobbyMenu(); protected: @@ -68,7 +64,7 @@ protected: //shared parameters ConfigUtility& config = ConfigUtility::GetSingleton(); - UDPNetworkUtility& network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); int& clientIndex; int& accountIndex; diff --git a/common/network/udp_network_utility.cpp b/common/network/udp_network_utility.cpp index 2b90df8..ad97a1c 100644 --- a/common/network/udp_network_utility.cpp +++ b/common/network/udp_network_utility.cpp @@ -25,7 +25,7 @@ #include -//NOTE: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network +//BUGFIX: memset() is used before sending a packet to remove old data; you don't want to send sensitive data over the network //NOTE: don't confuse SerialPacket with UDPpacket void UDPNetworkUtility::Open(int port) { diff --git a/common/network/udp_network_utility.hpp b/common/network/udp_network_utility.hpp index 4ebdd98..d812ad7 100644 --- a/common/network/udp_network_utility.hpp +++ b/common/network/udp_network_utility.hpp @@ -22,15 +22,15 @@ #ifndef UDPNETWORKUTILITY_HPP_ #define UDPNETWORKUTILITY_HPP_ +//common +#include "serial_packet.hpp" +#include "singleton.hpp" + +//APIs #include "SDL/SDL_net.h" -#include "serial_packet.hpp" - -class UDPNetworkUtility { +class UDPNetworkUtility : public Singleton { public: - UDPNetworkUtility() = default; - ~UDPNetworkUtility() = default; - void Open(int port); void Close(); @@ -65,6 +65,11 @@ public: return socket; } private: + friend Singleton; + + UDPNetworkUtility() = default; + ~UDPNetworkUtility() = default; + UDPsocket socket = nullptr; UDPpacket* packet = nullptr; }; diff --git a/server/main.cpp b/server/main.cpp index 4fbc24a..71ebb7c 100644 --- a/server/main.cpp +++ b/server/main.cpp @@ -23,6 +23,7 @@ //singletons #include "config_utility.hpp" +#include "udp_network_utility.hpp" #include #include @@ -33,17 +34,21 @@ int main(int argc, char** argv) { try { //create the singletons ConfigUtility::Create(); - ServerApplication::Create(); + UDPNetworkUtility::Create(); //call the server's routines + ServerApplication::Create(); ServerApplication& app = ServerApplication::GetSingleton(); + app.Init(argc, argv); app.Proc(); app.Quit(); + ServerApplication::Delete(); + //delete the singletons ConfigUtility::Delete(); - ServerApplication::Delete(); + UDPNetworkUtility::Delete(); } catch(exception& e) { cerr << "Fatal exception thrown: " << e.what() << endl; diff --git a/server/server_application.hpp b/server/server_application.hpp index 6c99d2d..17b6e41 100644 --- a/server/server_application.hpp +++ b/server/server_application.hpp @@ -85,7 +85,7 @@ private: //APIs and utilities sqlite3* database = nullptr; lua_State* luaState = nullptr; - UDPNetworkUtility network; + UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); ConfigUtility& config = ConfigUtility::GetSingleton(); //simple tables From 1776583e01db4e9dda3a8dd358ac6b65f295e276 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 13 Aug 2014 08:08:16 +1000 Subject: [PATCH 3/3] Added the escape hotkey, refined BoundingBox a bit LobbyMenu also searches for a server as soon as you enter, so you don't have to press an extra button, a good feature. --- client/scenes/clean_up.cpp | 6 +- client/scenes/in_combat.cpp.txt | 220 ------------------------------ client/scenes/in_combat.hpp.txt | 94 ------------- client/scenes/in_world.cpp | 7 + client/scenes/lobby_menu.cpp | 74 ++++++---- client/scenes/lobby_menu.hpp | 9 +- client/scenes/options_menu.cpp | 6 +- common/utilities/bounding_box.hpp | 3 +- common/utilities/check_bounds.cpp | 40 ------ common/utilities/check_bounds.hpp | 30 ---- todo.txt | 1 + 11 files changed, 72 insertions(+), 418 deletions(-) delete mode 100644 client/scenes/in_combat.cpp.txt delete mode 100644 client/scenes/in_combat.hpp.txt delete mode 100644 common/utilities/check_bounds.cpp delete mode 100644 common/utilities/check_bounds.hpp diff --git a/client/scenes/clean_up.cpp b/client/scenes/clean_up.cpp index c5b5d0f..416a1c8 100644 --- a/client/scenes/clean_up.cpp +++ b/client/scenes/clean_up.cpp @@ -124,7 +124,11 @@ void CleanUp::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void CleanUp::KeyDown(SDL_KeyboardEvent const& key) { - // + switch(key.keysym.sym) { + case SDLK_ESCAPE: + SetNextScene(SceneList::MAINMENU); + break; + } } void CleanUp::KeyUp(SDL_KeyboardEvent const& key) { diff --git a/client/scenes/in_combat.cpp.txt b/client/scenes/in_combat.cpp.txt deleted file mode 100644 index 358bd08..0000000 --- a/client/scenes/in_combat.cpp.txt +++ /dev/null @@ -1,220 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013, 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#include "in_combat.hpp" - -#include "channels.hpp" -#include "utility.hpp" -#include "config_utility.hpp" - -#include - -//------------------------- -//Public access members -//------------------------- - -InCombat::InCombat( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ): - network(*argNetwork), - clientIndex(*argClientIndex), - accountIndex(*argAccountIndex), - characterIndex(*argCharacterIndex), - characterMap(*argCharacterMap) -{ -/* //setup the utility objects - buttonImage.LoadSurface(config["dir.interface"] + "button_menu.bmp"); - buttonImage.SetClipH(buttonImage.GetClipH()/3); - font.LoadSurface(config["dir.fonts"] + "pk_white_8.bmp"); - - //pass the utility objects - backButton.SetImage(&buttonImage); - backButton.SetFont(&font); - - //set the button positions - backButton.SetX(50); - backButton.SetY(50 + buttonImage.GetClipH() * 0); - - //set the button texts - backButton.SetText("Back"); - - //request a sync - RequestSynchronize(); -*/ - //debug - // -} - -InCombat::~InCombat() { - // -} - -//------------------------- -//Frame loop -//------------------------- - -void InCombat::FrameStart() { - // -} - -void InCombat::Update(double delta) { - //suck in and process all waiting packets - SerialPacket* packetBuffer = static_cast(malloc(MAX_PACKET_SIZE)); - while(network.Receive(packetBuffer)) { - HandlePacket(packetBuffer); - } - free(static_cast(packetBuffer)); - - //TODO: more -} - -void InCombat::FrameEnd() { - // -} - -void InCombat::RenderFrame() { - SDL_FillRect(GetScreen(), 0, 0); - Render(GetScreen()); - SDL_Flip(GetScreen()); - fps.Calculate(); -} - -void InCombat::Render(SDL_Surface* const screen) { - //TODO: draw the background - - //TODO: draw the characters - - //TODO: draw the enemies - - //TODO: draw the UI -} - -//------------------------- -//Event handlers -//------------------------- - -void InCombat::QuitEvent() { - //exit the game AND the server - RequestDisconnect(); - SetNextScene(SceneList::QUIT); -} - -void InCombat::MouseMotion(SDL_MouseMotionEvent const& motion) { - // -} - -void InCombat::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // -} - -void InCombat::MouseButtonUp(SDL_MouseButtonEvent const& button) { - // -} - -void InCombat::KeyDown(SDL_KeyboardEvent const& key) { - // -} - -void InCombat::KeyUp(SDL_KeyboardEvent const& key) { - // -} - -//------------------------- -//Network handlers -//------------------------- - -void InCombat::HandlePacket(SerialPacket* const argPacket) { - switch(argPacket->type) { - case SerialPacketType::DISCONNECT: - HandleDisconnect(argPacket); - break; - //handle errors - default: - throw(std::runtime_error(std::string() + "Unknown SerialPacketType encountered in InCombat: " + to_string_custom(static_cast(argPacket->type)) )); - break; - } -} - -void InCombat::HandleDisconnect(SerialPacket* const) { - SetNextScene(SceneList::CLEANUP); -} - -//TODO: more network handlers - -//------------------------- -//Server control -//------------------------- - -void InCombat::RequestSynchronize() { - ClientPacket newPacket; - - //request a sync - newPacket.type = SerialPacketType::SYNCHRONIZE; - newPacket.clientIndex = clientIndex; - newPacket.accountIndex = accountIndex; - - network.SendTo(Channels::SERVER, &newPacket); -} - -void InCombat::SendPlayerUpdate() { - CharacterPacket newPacket; - - //pack the packet - newPacket.type = SerialPacketType::CHARACTER_UPDATE; - - newPacket.characterIndex = characterIndex; - //handle, avatar - newPacket.accountIndex = accountIndex; -// newPacket.roomIndex = localCharacter->roomIndex; -// newPacket.origin = localCharacter->origin; -// newPacket.motion = localCharacter->motion; -// newPacket.stats = localCharacter->stats; - - //TODO: gameplay components: equipment, items, buffs, debuffs - - network.SendTo(Channels::SERVER, &newPacket); -} - -void InCombat::RequestDisconnect() { - ClientPacket newPacket; - - //send a disconnect request - newPacket.type = SerialPacketType::DISCONNECT; - newPacket.clientIndex = clientIndex; - newPacket.accountIndex = accountIndex; - - network.SendTo(Channels::SERVER, &newPacket); -} - -void InCombat::RequestShutdown() { - ClientPacket newPacket; - - //send a shutdown request - newPacket.type = SerialPacketType::SHUTDOWN; - newPacket.clientIndex = clientIndex; - newPacket.accountIndex = accountIndex; - - network.SendTo(Channels::SERVER, &newPacket); -} diff --git a/client/scenes/in_combat.hpp.txt b/client/scenes/in_combat.hpp.txt deleted file mode 100644 index 2f5cd20..0000000 --- a/client/scenes/in_combat.hpp.txt +++ /dev/null @@ -1,94 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2013, 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#ifndef INCOMBAT_HPP_ -#define INCOMBAT_HPP_ - -//network -#include "udp_network_utility.hpp" - -//graphics -#include "image.hpp" -#include "raster_font.hpp" -#include "button.hpp" - -//common -#include "frame_rate.hpp" - -#include "character.hpp" - -//client -#include "base_scene.hpp" - -class InCombat : public BaseScene { -public: - //Public access members - InCombat( - UDPNetworkUtility* const argNetwork, - int* const argClientIndex, - int* const argAccountIndex, - int* const argCharacterIndex, - CharacterMap* argCharacterMap - ); - ~InCombat(); - -protected: - //Frame loop - void FrameStart(); - void Update(double delta); - void FrameEnd(); - void RenderFrame(); - void Render(SDL_Surface* const); - - //Event handlers - void QuitEvent(); - void MouseMotion(SDL_MouseMotionEvent const&); - void MouseButtonDown(SDL_MouseButtonEvent const&); - void MouseButtonUp(SDL_MouseButtonEvent const&); - void KeyDown(SDL_KeyboardEvent const&); - void KeyUp(SDL_KeyboardEvent const&); - - //Network handlers - void HandlePacket(SerialPacket* const); - void HandleDisconnect(SerialPacket* const); - - //Server control - void RequestSynchronize(); - void SendPlayerUpdate(); - void RequestDisconnect(); - void RequestShutdown(); - - //shared parameters - UDPNetworkUtility& network; - int& clientIndex; - int& accountIndex; - int& characterIndex; - CharacterMap& characterMap; - - //graphics - //TODO: graphics - - //UI - //TODO: UI - FrameRate fps; -}; - -#endif diff --git a/client/scenes/in_world.cpp b/client/scenes/in_world.cpp index 8dbd0e6..ff0f67b 100644 --- a/client/scenes/in_world.cpp +++ b/client/scenes/in_world.cpp @@ -205,6 +205,13 @@ void InWorld::KeyDown(SDL_KeyboardEvent const& key) { return; } + //hotkeys + switch(key.keysym.sym) { + case SDLK_ESCAPE: + RequestDisconnect(); + break; + } + //player movement Vector2 motion = localCharacter->GetMotion(); switch(key.keysym.sym) { diff --git a/client/scenes/lobby_menu.cpp b/client/scenes/lobby_menu.cpp index 4b7b356..d866159 100644 --- a/client/scenes/lobby_menu.cpp +++ b/client/scenes/lobby_menu.cpp @@ -65,6 +65,9 @@ LobbyMenu::LobbyMenu(int* const argClientIndex, int* const argAccountIndex): //BUGFIX: Eat incoming packets while(network.Receive()); + + //Initial broadcast + SendBroadcastRequest(); } LobbyMenu::~LobbyMenu() { @@ -104,7 +107,10 @@ void LobbyMenu::Render(SDL_Surface* const screen) { for (int i = 0; i < serverInfo.size(); i++) { //draw the selected server's highlight if (selection == &serverInfo[i]) { - SDL_Rect r = listBox; + 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, 255, 127, 39)); } @@ -142,39 +148,19 @@ void LobbyMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (search.MouseButtonUp(button) == Button::State::HOVER) { - //broadcast to the network, or a specific server - SerialPacket packet; - packet.type = SerialPacketType::BROADCAST_REQUEST; - network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); - - //reset the server list - serverInfo.clear(); - selection = nullptr; + SendBroadcastRequest(); } - else if (join.MouseButtonUp(button) == Button::State::HOVER && selection != nullptr && selection->compatible) { - //pack the packet - ClientPacket packet; - packet.type = SerialPacketType::JOIN_REQUEST; - strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); - - //join the selected server - network.SendTo(&selection->address, &packet); - selection = nullptr; + SendJoinRequest(); } - else if (back.MouseButtonUp(button) == Button::State::HOVER) { SetNextScene(SceneList::MAINMENU); } - else if ( - //has the user selected a server on the list? - //TODO: replace with regular collision checker - button.x > listBox.x && - button.x < listBox.x + listBox.w && - button.y > listBox.y && - button.y < listBox.y + listBox.h * serverInfo.size() - ) { + //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]; } else { @@ -183,7 +169,11 @@ void LobbyMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void LobbyMenu::KeyDown(SDL_KeyboardEvent const& key) { - // + switch(key.keysym.sym) { + case SDLK_ESCAPE: + SetNextScene(SceneList::MAINMENU); + break; + } } void LobbyMenu::KeyUp(SDL_KeyboardEvent const& key) { @@ -237,4 +227,30 @@ void LobbyMenu::HandleJoinResponse(ClientPacket* const argPacket) { strncpy(newPacket.avatar, config["client.avatar"].c_str(), PACKET_STRING_SIZE); newPacket.accountIndex = accountIndex; network.SendTo(Channels::SERVER, &newPacket); -} \ No newline at end of file +} + +//------------------------- +//server control +//------------------------- + +void LobbyMenu::SendBroadcastRequest() { + //broadcast to the network, or a specific server + SerialPacket packet; + packet.type = SerialPacketType::BROADCAST_REQUEST; + network.SendTo(config["server.host"].c_str(), config.Int("server.port"), &packet); + + //reset the server list + serverInfo.clear(); + selection = nullptr; +} + +void LobbyMenu::SendJoinRequest() { + //pack the packet + ClientPacket packet; + packet.type = SerialPacketType::JOIN_REQUEST; + strncpy(packet.username, config["client.username"].c_str(), PACKET_STRING_SIZE); + + //join the selected server + network.SendTo(&selection->address, &packet); + selection = nullptr; +} diff --git a/client/scenes/lobby_menu.hpp b/client/scenes/lobby_menu.hpp index 43f2c99..7020ac8 100644 --- a/client/scenes/lobby_menu.hpp +++ b/client/scenes/lobby_menu.hpp @@ -26,6 +26,7 @@ #include "image.hpp" #include "raster_font.hpp" #include "button.hpp" +#include "bounding_box.hpp" //utilities #include "config_utility.hpp" @@ -62,6 +63,10 @@ protected: void HandleBroadcastResponse(ServerPacket* const); void HandleJoinResponse(ClientPacket* const); + //server control + void SendBroadcastRequest(); + void SendJoinRequest(); + //shared parameters ConfigUtility& config = ConfigUtility::GetSingleton(); UDPNetworkUtility& network = UDPNetworkUtility::GetSingleton(); @@ -86,9 +91,9 @@ protected: std::vector serverInfo; - //a terrible hack, forgive me + //NOTE: a terrible hack //I'd love a proper gui system for this - SDL_Rect listBox; + BoundingBox listBox; ServerInformation* selection = nullptr; }; diff --git a/client/scenes/options_menu.cpp b/client/scenes/options_menu.cpp index 4a991f7..539cd44 100644 --- a/client/scenes/options_menu.cpp +++ b/client/scenes/options_menu.cpp @@ -92,7 +92,11 @@ void OptionsMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { } void OptionsMenu::KeyDown(SDL_KeyboardEvent const& key) { - // + switch(key.keysym.sym) { + case SDLK_ESCAPE: + SetNextScene(SceneList::MAINMENU); + break; + } } void OptionsMenu::KeyUp(SDL_KeyboardEvent const& key) { diff --git a/common/utilities/bounding_box.hpp b/common/utilities/bounding_box.hpp index 1d32887..ac7daf2 100644 --- a/common/utilities/bounding_box.hpp +++ b/common/utilities/bounding_box.hpp @@ -32,6 +32,7 @@ public: int w, h; BoundingBox() = default; + BoundingBox(int i, int j): x(i), y(j), w(0), h(0) {}; BoundingBox(int i, int j, int k, int l): x(i), y(j), w(k), h(l) {}; ~BoundingBox() = default; BoundingBox& operator=(BoundingBox const&) = default; @@ -62,7 +63,7 @@ public: }; //This is explicitly a POD -static_assert(std::is_pod::value, "BoundingBox is not a POD"); +static_assert(std::is_pod::value, "BoundingBox is not a POD"); #include "vector2.hpp" diff --git a/common/utilities/check_bounds.cpp b/common/utilities/check_bounds.cpp deleted file mode 100644 index b0d42f8..0000000 --- a/common/utilities/check_bounds.cpp +++ /dev/null @@ -1,40 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#include "check_bounds.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point) { - return !( - point.x < origin.x || - point.y < origin.y || - point.x >= origin.x + bound.x || - point.y >= origin.y + bound.y - ); -} - -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo) { - return !( - originOne.x >= originTwo.x + boundTwo.x || - originOne.x + boundOne.x >= originTwo.x || - originOne.y >= originTwo.y + boundTwo.y || - originOne.y + boundOne.y >= originTwo.y - ); -} diff --git a/common/utilities/check_bounds.hpp b/common/utilities/check_bounds.hpp deleted file mode 100644 index e02d6cd..0000000 --- a/common/utilities/check_bounds.hpp +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright: (c) Kayne Ruse 2014 - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any damages - * arising from the use of this software. - * - * Permission is granted to anyone to use this software for any purpose, - * including commercial applications, and to alter it and redistribute it - * freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you must not - * claim that you wrote the original software. If you use this software - * in a product, an acknowledgment in the product documentation would be - * appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and must not be - * misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. -*/ -#ifndef CHECKBOUNDS_HPP_ -#define CHECKBOUNDS_HPP_ - -#include "vector2.hpp" - -bool checkPoint(Vector2 const& origin, Vector2 const& bound, Vector2 const& point); -bool checkOverlap(Vector2 const& originOne, Vector2 const& boundOne, Vector2 const& originTwo, Vector2 const& boundTwo); - -#endif diff --git a/todo.txt b/todo.txt index c5f5afd..a1f8458 100644 --- a/todo.txt +++ b/todo.txt @@ -1,5 +1,6 @@ TODO: encapsulate the data structures TODO: Get the rooms working +TODO: A proper logging system TODO: make the whole thing more fault tolerant TODO: Authentication