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 0000000..842f309 Binary files /dev/null and b/rsc/graphics/interface/button.bmp differ