diff --git a/client/lobby.cpp b/client/lobby.cpp index f22c8c7..7c1658e 100644 --- a/client/lobby.cpp +++ b/client/lobby.cpp @@ -12,6 +12,9 @@ Lobby::Lobby() { #ifdef DEBUG cout << "entering Lobby" << endl; #endif + 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"); } Lobby::~Lobby() { @@ -37,7 +40,9 @@ void Lobby::FrameEnd() { } void Lobby::Render(SDL_Surface* const screen) { - // + refreshButton.DrawTo(screen); + joinButton.DrawTo(screen); + backButton.DrawTo(screen); } //------------------------- @@ -45,15 +50,27 @@ void Lobby::Render(SDL_Surface* const screen) { //------------------------- void Lobby::MouseMotion(SDL_MouseMotionEvent const& motion) { - // + refreshButton.MouseMotion(motion); + joinButton.MouseMotion(motion); + backButton.MouseMotion(motion); } void Lobby::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // + refreshButton.MouseButtonDown(button); + joinButton.MouseButtonDown(button); + backButton.MouseButtonDown(button); } void Lobby::MouseButtonUp(SDL_MouseButtonEvent const& button) { - // + if (refreshButton.MouseButtonUp(button) == Button::State::HOVER) { + //ping the server + } + if (joinButton.MouseButtonUp(button) == Button::State::HOVER) { + //join a server + } + if (backButton.MouseButtonUp(button) == Button::State::HOVER) { + QuitEvent(); + } } void Lobby::KeyDown(SDL_KeyboardEvent const& key) { diff --git a/client/lobby.hpp b/client/lobby.hpp index 9c7e262..1e773da 100644 --- a/client/lobby.hpp +++ b/client/lobby.hpp @@ -2,6 +2,12 @@ #define LOBBY_HPP_ #include "base_scene.hpp" +#include "service_locator.hpp" + +#include "config_utility.hpp" +#include "surface_manager.hpp" +#include "udp_network_utility.hpp" +#include "button.hpp" class Lobby : public BaseScene { public: @@ -17,11 +23,21 @@ protected: void Render(SDL_Surface* const); /* Event handlers */ + void QuitEvent() { SetNextScene(SceneList::MAINMENU); } 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&); + + //services + ConfigUtility* configUtil = ServiceLocator::Get(); + SurfaceManager* surfaceMgr = ServiceLocator::Get(); + UDPNetworkUtility* netUtil = ServiceLocator::Get(); + + Button refreshButton; + Button joinButton; + Button backButton; }; #endif diff --git a/client/main_menu.cpp b/client/main_menu.cpp index c250a1f..9c8aa06 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -12,6 +12,9 @@ MainMenu::MainMenu() { #ifdef DEBUG cout << "entering MainMenu" << endl; #endif + startButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Start"); + optionsButton.Setup(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Options"); + quitButton.Setup(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Quit"); } MainMenu::~MainMenu() { @@ -37,7 +40,9 @@ void MainMenu::FrameEnd() { } void MainMenu::Render(SDL_Surface* const screen) { - // + startButton.DrawTo(screen); + optionsButton.DrawTo(screen); + quitButton.DrawTo(screen); } //------------------------- @@ -45,15 +50,27 @@ void MainMenu::Render(SDL_Surface* const screen) { //------------------------- void MainMenu::MouseMotion(SDL_MouseMotionEvent const& motion) { - // + startButton.MouseMotion(motion); + optionsButton.MouseMotion(motion); + quitButton.MouseMotion(motion); } void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // + startButton.MouseButtonDown(button); + optionsButton.MouseButtonDown(button); + quitButton.MouseButtonDown(button); } void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { - // + if (startButton.MouseButtonUp(button) == Button::State::HOVER) { + SetNextScene(SceneList::LOBBY); + } + if (optionsButton.MouseButtonUp(button) == Button::State::HOVER) { + SetNextScene(SceneList::OPTIONSCREEN); + } + if (quitButton.MouseButtonUp(button) == Button::State::HOVER) { + QuitEvent(); + } } void MainMenu::KeyDown(SDL_KeyboardEvent const& key) { @@ -63,7 +80,3 @@ void MainMenu::KeyDown(SDL_KeyboardEvent const& key) { break; } } - -void MainMenu::KeyUp(SDL_KeyboardEvent const& key) { - // -} diff --git a/client/main_menu.hpp b/client/main_menu.hpp index 723ccba..2b576e7 100644 --- a/client/main_menu.hpp +++ b/client/main_menu.hpp @@ -2,6 +2,10 @@ #define MAINMENU_HPP_ #include "base_scene.hpp" +#include "service_locator.hpp" + +#include "surface_manager.hpp" +#include "button.hpp" class MainMenu : public BaseScene { public: @@ -21,7 +25,12 @@ protected: void MouseButtonDown(SDL_MouseButtonEvent const&); void MouseButtonUp(SDL_MouseButtonEvent const&); void KeyDown(SDL_KeyboardEvent const&); - void KeyUp(SDL_KeyboardEvent const&); + + SurfaceManager* surfaceMgr = ServiceLocator::Get(); + + Button startButton; + Button optionsButton; + Button quitButton; }; #endif diff --git a/client/option_screen.cpp b/client/option_screen.cpp index a7d265d..a375d9a 100644 --- a/client/option_screen.cpp +++ b/client/option_screen.cpp @@ -12,6 +12,7 @@ OptionScreen::OptionScreen() { #ifdef DEBUG cout << "entering OptionScreen" << endl; #endif + backButton.Setup(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "Back"); } OptionScreen::~OptionScreen() { @@ -24,20 +25,8 @@ OptionScreen::~OptionScreen() { //Frame loop //------------------------- -void OptionScreen::FrameStart() { - // -} - -void OptionScreen::Update(double delta) { - // -} - -void OptionScreen::FrameEnd() { - // -} - void OptionScreen::Render(SDL_Surface* const screen) { - // + backButton.DrawTo(screen); } //------------------------- @@ -45,15 +34,17 @@ void OptionScreen::Render(SDL_Surface* const screen) { //------------------------- void OptionScreen::MouseMotion(SDL_MouseMotionEvent const& motion) { - // + backButton.MouseMotion(motion); } void OptionScreen::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // + backButton.MouseButtonDown(button); } void OptionScreen::MouseButtonUp(SDL_MouseButtonEvent const& button) { - // + if (backButton.MouseButtonUp(button) == Button::State::HOVER) { + QuitEvent(); + } } void OptionScreen::KeyDown(SDL_KeyboardEvent const& key) { @@ -63,7 +54,3 @@ void OptionScreen::KeyDown(SDL_KeyboardEvent const& key) { break; } } - -void OptionScreen::KeyUp(SDL_KeyboardEvent const& key) { - // -} diff --git a/client/option_screen.hpp b/client/option_screen.hpp index a37a479..35cade9 100644 --- a/client/option_screen.hpp +++ b/client/option_screen.hpp @@ -2,6 +2,10 @@ #define OPTIONSCREEN_HPP_ #include "base_scene.hpp" +#include "service_locator.hpp" + +#include "surface_manager.hpp" +#include "button.hpp" class OptionScreen : public BaseScene { public: @@ -11,17 +15,17 @@ public: protected: /* Frame loop */ - void FrameStart(); - void Update(double delta); - void FrameEnd(); void Render(SDL_Surface* const); /* Event handlers */ + void QuitEvent() { SetNextScene(SceneList::MAINMENU); } 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&); + + SurfaceManager* surfaceMgr = ServiceLocator::Get(); + Button backButton; }; #endif diff --git a/libs/Codebase/button.cpp b/libs/Codebase/button.cpp index f9f066c..4d952af 100644 --- a/libs/Codebase/button.cpp +++ b/libs/Codebase/button.cpp @@ -23,15 +23,13 @@ #include -Button::Button(): - Button(0,0, nullptr, nullptr) -{ +Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) { + Setup(i, j, imageSurface, fontSurface, s); } -Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) { +void Button::Setup(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string s) { x = i; y = j; - state = State::NORMAL; SetSurfaces(imageSurface, fontSurface); diff --git a/libs/Codebase/button.hpp b/libs/Codebase/button.hpp index 14d47cd..3acb43e 100644 --- a/libs/Codebase/button.hpp +++ b/libs/Codebase/button.hpp @@ -34,9 +34,11 @@ public: NORMAL, HOVER, PRESSED }; - Button(); + Button() = default; Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = ""); + void Setup(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = ""); + //return the current state State MouseMotion(SDL_MouseMotionEvent const&); State MouseButtonDown(SDL_MouseButtonEvent const&); @@ -69,11 +71,11 @@ public: private: State CalcState(Sint16 x, Sint16 y, bool leftPressed); - Sint16 x, y; - Sint16 textX, textY; //prevent recalc every loop + Sint16 x = 0, y = 0; + Sint16 textX = 0, textY = 0; //prevent recalc every loop Image image; RasterFont font; - State state; + State state = State::NORMAL; std::string text; };