From 30d163ec80b1414b326c0518df79f62da4b63125 Mon Sep 17 00:00:00 2001 From: Kayne Ruse Date: Wed, 15 May 2013 23:20:47 +1000 Subject: [PATCH] Implemented main menu and buttons --- client/base_scene.cpp | 1 + client/button.cpp | 6 ++--- client/button.hpp | 6 ++--- client/in_game.cpp | 30 +++++++++++++++++++---- client/in_game.hpp | 1 + client/main_menu.cpp | 38 ++++++++++++++++++++++++++---- client/main_menu.hpp | 25 +++++++++++++++++--- client/scene_manager.cpp | 16 ++++--------- client/scene_manager.hpp | 9 +++++-- client/splash.cpp | 9 ++++--- rsc/config.cfg | 1 + rsc/graphics/interface/button.bmp | Bin 0 -> 90054 bytes 12 files changed, 108 insertions(+), 34 deletions(-) create mode 100644 rsc/graphics/interface/button.bmp diff --git a/client/base_scene.cpp b/client/base_scene.cpp index 7e1db60..240269d 100644 --- a/client/base_scene.cpp +++ b/client/base_scene.cpp @@ -79,6 +79,7 @@ void BaseScene::RunFrame() { FrameStart(); HandleEvents(); Update(); + SDL_FillRect(screen, 0, 0); Render(screen); SDL_Flip(screen); FrameEnd(); diff --git a/client/button.cpp b/client/button.cpp index 3b035b7..debbf6d 100644 --- a/client/button.cpp +++ b/client/button.cpp @@ -41,7 +41,7 @@ Button::Button(Sint16 i, Sint16 j, SDL_Surface* imageSurface, SDL_Surface* fontS SetText(s); } -Button::State Button::MouseMotion(SDL_MouseMotionEvent& motion) { +Button::State Button::MouseMotion(SDL_MouseMotionEvent const& motion) { if (motion.state & SDL_BUTTON_LMASK) { return CalcState(motion.x, motion.y, true); } @@ -51,14 +51,14 @@ Button::State Button::MouseMotion(SDL_MouseMotionEvent& motion) { return state; } -Button::State Button::MouseButtonDown(SDL_MouseButtonEvent& button) { +Button::State Button::MouseButtonDown(SDL_MouseButtonEvent const& button) { if (button.button == SDL_BUTTON_LEFT) { return CalcState(button.x, button.y, true); } return state; } -Button::State Button::MouseButtonUp(SDL_MouseButtonEvent& button) { +Button::State Button::MouseButtonUp(SDL_MouseButtonEvent const& button) { if (button.button == SDL_BUTTON_LEFT) { return CalcState(button.x, button.y, false); } diff --git a/client/button.hpp b/client/button.hpp index eaa8579..cfb5d71 100644 --- a/client/button.hpp +++ b/client/button.hpp @@ -38,9 +38,9 @@ public: Button(Sint16 x, Sint16 y, SDL_Surface* imageSurface, SDL_Surface* fontSurface, std::string text = ""); //return the current state - State MouseMotion(SDL_MouseMotionEvent&); - State MouseButtonDown(SDL_MouseButtonEvent&); - State MouseButtonUp(SDL_MouseButtonEvent&); + State MouseMotion(SDL_MouseMotionEvent const&); + State MouseButtonDown(SDL_MouseButtonEvent const&); + State MouseButtonUp(SDL_MouseButtonEvent const&); State GetState() const { return state; } diff --git a/client/in_game.cpp b/client/in_game.cpp index 37f3f43..dd45999 100644 --- a/client/in_game.cpp +++ b/client/in_game.cpp @@ -49,7 +49,6 @@ void InGame::Update() { } void InGame::Render(SDL_Surface* const screen) { - SDL_FillRect(screen, 0, 0); playerMgr.DrawAllTo(screen); } @@ -89,16 +88,16 @@ void InGame::KeyDown(SDL_KeyboardEvent const& key) { break; case SDLK_1: - currentPlayer = 0; + SwitchToPlayer(0); break; case SDLK_2: - currentPlayer = 1; + SwitchToPlayer(1); break; case SDLK_3: - currentPlayer = 2; + SwitchToPlayer(2); break; case SDLK_4: - currentPlayer = 3; + SwitchToPlayer(3); break; } } @@ -128,3 +127,24 @@ void InGame::NewPlayer(int index, std::string avatarName, int x, int y) { Player* p = playerMgr.New(index, surfaceMgr[avatarName]); p->SetPosition(Vector2(x, y)); } + +void InGame::SwitchToPlayer(int index) { + //dirty hacks for smooth movement + playerMgr[currentPlayer]->SetMotion(Vector2(0,0)); + currentPlayer = index; + + Uint8* key = SDL_GetKeyState(NULL); + + if (key[SDLK_w]) { + playerMgr[currentPlayer]->WalkInDirection(Direction::NORTH); + } + if (key[SDLK_s]) { + playerMgr[currentPlayer]->WalkInDirection(Direction::SOUTH); + } + if (key[SDLK_a]) { + playerMgr[currentPlayer]->WalkInDirection(Direction::WEST); + } + if (key[SDLK_d]) { + playerMgr[currentPlayer]->WalkInDirection(Direction::EAST); + } +} \ No newline at end of file diff --git a/client/in_game.hpp b/client/in_game.hpp index cf83ab7..40a06e1 100644 --- a/client/in_game.hpp +++ b/client/in_game.hpp @@ -32,6 +32,7 @@ protected: //utilities void NewPlayer(int index, std::string avatarName, int x, int y); + void SwitchToPlayer(int index); //members Delta delta; diff --git a/client/main_menu.cpp b/client/main_menu.cpp index 69820bc..d0fa948 100644 --- a/client/main_menu.cpp +++ b/client/main_menu.cpp @@ -12,9 +12,20 @@ MainMenu::MainMenu() { #ifdef DEBUG cout << "entering MainMenu" << endl; #endif + configUtil = GetSingletonPtr(); + surfaceMgr = GetSingletonPtr(); + + surfaceMgr->Load("button", configUtil->String("interface") + "/button.bmp"); + + buttonMap["start"] = new Button(50, 50, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "start"); + buttonMap["options"] = new Button(50, 100, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "options"); + buttonMap["quit"] = new Button(50, 150, surfaceMgr->Get("button"), surfaceMgr->Get("font"), "quit"); } MainMenu::~MainMenu() { + for (auto it : buttonMap) { + delete it.second; + } #ifdef DEBUG cout << "leaving MainMenu" << endl; #endif @@ -37,7 +48,9 @@ void MainMenu::Update() { } void MainMenu::Render(SDL_Surface* const screen) { - // + for (auto it : buttonMap) { + it.second->DrawTo(screen); + } } //------------------------- @@ -45,15 +58,32 @@ void MainMenu::Render(SDL_Surface* const screen) { //------------------------- void MainMenu::MouseMotion(SDL_MouseMotionEvent const& motion) { - // + for (auto it : buttonMap) { + it.second->MouseMotion(motion); + } } void MainMenu::MouseButtonDown(SDL_MouseButtonEvent const& button) { - // + for (auto it : buttonMap) { + it.second->MouseButtonDown(button); + } } void MainMenu::MouseButtonUp(SDL_MouseButtonEvent const& button) { - // + if (buttonMap["start"]->MouseButtonUp(button) == Button::State::HOVER) { + //TODO + SetNextScene(SceneList::INGAME); + cout << "start" << endl; + } + if (buttonMap["options"]->MouseButtonUp(button) == Button::State::HOVER) { + //TODO + cout << "options" << endl; + } + if (buttonMap["quit"]->MouseButtonUp(button) == Button::State::HOVER) { + //TODO + SetNextScene(SceneList::QUIT); + cout << "quit" << endl; + } } void MainMenu::KeyDown(SDL_KeyboardEvent const& key) { diff --git a/client/main_menu.hpp b/client/main_menu.hpp index 1b26b3a..00d9914 100644 --- a/client/main_menu.hpp +++ b/client/main_menu.hpp @@ -3,25 +3,44 @@ #include "base_scene.hpp" +#include "singleton.hpp" +#include "config_utility.hpp" +#include "surface_manager.hpp" + +#include "button.hpp" + +#include +#include + class MainMenu : public BaseScene { public: - /* Public access members */ + //Public access members MainMenu(); virtual ~MainMenu(); protected: - /* Frame loop */ + //Frame loop virtual void FrameStart(); virtual void FrameEnd(); virtual void Update(); virtual void Render(SDL_Surface* const); - /* Event handlers */ + //Event handlers virtual void MouseMotion(SDL_MouseMotionEvent const&); virtual void MouseButtonDown(SDL_MouseButtonEvent const&); virtual void MouseButtonUp(SDL_MouseButtonEvent const&); virtual void KeyDown(SDL_KeyboardEvent const&); virtual void KeyUp(SDL_KeyboardEvent const&); + + //utilities + typedef std::map ButtonMap; + + //singletons + ConfigUtility* configUtil; + SurfaceManager* surfaceMgr; + + //members + ButtonMap buttonMap; }; #endif diff --git a/client/scene_manager.cpp b/client/scene_manager.cpp index 62d666e..1ef4626 100644 --- a/client/scene_manager.cpp +++ b/client/scene_manager.cpp @@ -1,8 +1,5 @@ #include "scene_manager.hpp" -#include "singleton.hpp" -#include "config_utility.hpp" - #include //------------------------- @@ -39,19 +36,16 @@ void SceneManager::Init() { if (SDL_Init(SDL_INIT_VIDEO)) throw(std::runtime_error("Failed to initialize SDL")); - GetSingletonPtr()->Load("rsc/config.cfg"); + configUtil = GetSingletonPtr(); + + configUtil->Load("rsc/config.cfg"); //set the screen from the config file int flags = SDL_HWSURFACE|SDL_DOUBLEBUF; - if (GetSingletonPtr()->Boolean("screen.f")) { + if (configUtil->Boolean("screen.f")) { flags |= SDL_FULLSCREEN; } - BaseScene::SetScreen( - GetSingletonPtr()->Integer("screen.w"), - GetSingletonPtr()->Integer("screen.h"), - 0, - flags - ); + BaseScene::SetScreen(configUtil->Integer("screen.w"),configUtil->Integer("screen.h"),0,flags); } void SceneManager::Proc() { diff --git a/client/scene_manager.hpp b/client/scene_manager.hpp index 2890ccc..edf1da0 100644 --- a/client/scene_manager.hpp +++ b/client/scene_manager.hpp @@ -4,11 +4,14 @@ #include "scene_list.hpp" #include "base_scene.hpp" +#include "singleton.hpp" +#include "config_utility.hpp" + #include "SDL/SDL.h" class SceneManager { public: - /* Public access members */ + //Public access members SceneManager(); ~SceneManager(); @@ -17,11 +20,13 @@ public: void Quit(); private: - /* Private access members */ + //Private access members void LoadScene(SceneList sceneIndex); void UnloadScene(); BaseScene* activeScene; + + ConfigUtility* configUtil; }; #endif diff --git a/client/splash.cpp b/client/splash.cpp index 4eff19e..44593b2 100644 --- a/client/splash.cpp +++ b/client/splash.cpp @@ -30,6 +30,8 @@ Splash::~Splash() { void Splash::RunFrame() { //skip any event handling here + SDL_Event event; + while(SDL_PollEvent(&event)); //draw the logo in the middle of the screen int x = (GetScreen()->w - logo->GetClipW()) / 2; @@ -40,15 +42,16 @@ void Splash::RunFrame() { if (!loaded) { LoadResources(); + loaded = true; } if (clock() - start > CLOCKS_PER_SEC*3) { - SetNextScene(SceneList::INGAME); + SetNextScene(SceneList::MAINMENU); } } void Splash::LoadResources() { - //load the resources during the splash screen + //load the global resources here + surfaceMgr->Load("font", configUtil->String("fonts") + "/pokemon_dark_font.bmp"); //TODO - loaded = true; } \ No newline at end of file diff --git a/rsc/config.cfg b/rsc/config.cfg index c4b3754..4c528c5 100644 --- a/rsc/config.cfg +++ b/rsc/config.cfg @@ -10,6 +10,7 @@ fonts = rsc/graphics/fonts logos = rsc/graphics/logos sprites = rsc/graphics/sprites tilesets = rsc/graphics/tilesets +interface = rsc/graphics/interface #debugging debug = true diff --git a/rsc/graphics/interface/button.bmp b/rsc/graphics/interface/button.bmp new file mode 100644 index 0000000000000000000000000000000000000000..842f309b0649f609bdb26af70858b8e335edd771 GIT binary patch literal 90054 zcmeI*!Hr`@5P;#?1UM4{5fA`34#YqxNJPMi1RxV6G^9fU!-HOxEN{tFy{h9kvaNQz zx(}az(B9s6Km7UQr?-FqzP;YxT<`bm{pbQKg9ivqe@#B@~%=qe~I76o**i0P^j&{a@aEDGpq5z|#6psS#;SQOCJBBrZC zKvzLwu_&OcMNC(PfUbhVVo^X>iPV1auV? z7K;M9TEui!2=LO@qRVX-Kn zt3^y#g@CSt!eUWCSBsdg3ISaOg~g(Pt`;#}6#}{n3X4SnT`gj|Dg<;D6c&pDx?03^ zRS4)RC@dBQbhU`-su0jsP*^Mq=xPzuRUz=$)wPa&3j((Vo}xNmZ{Ox1flJ^qs;5VK zJ^o}(;GTd-5Z-fh>V-f=<$qZ(l>F2ch_2rL{_rBOB5)$S(mJ=DukSyD4?q$pyR24u2L48Ljhe40Ub{Tbd|E$ z917@a2@w=qhEgITX;<5YX{dKvyY?&7pv+|8^@l$KO9H3OE3I?i`TFI*#Ox5L3p}=TtwmL*Gx=H| z(mGwe9`-ANy1*$)S9LLy6#-pUl+1Jmbk!9zUlGt%MafK8Kv!Kc^A!PIRg}zh1$5OF zGhY$VRYl26S3p-?G4mAxT~(CKbOm(P6*FHE&{ajrOjkfxT`}_&0bNy;%yb2G)fF>e z5ztjd$xK&3S6wml6#-pUl+1Jmbk!9zUlGt%MafK8Kv!Kc^A!PIRg}zh1$5OFGhY$V zRYl26S3p-?G4mAxT~(CKbOm(P6*FHE&{ajrOjkfxT`}_&0bNy;%yb2G)fF>e5ztjd z$xK&3S6wml6#-pUl+1Jmbk!9zUlGt%MafK8Kv!Kc^A!PIRg}zh1$5OFGhY$VRYl26 zS3p-?G4mCH$F8nNnN*ILA@Ew@3Msle3u(O;^XvZ#oChtPoSq)(b$;?m;GV!0)%E8Q MgvY_>qQHsjAN!htu>b%7 literal 0 HcmV?d00001