diff --git a/src/program.cpp b/src/program.cpp index 7bdc8b1c..e4e3c8ba 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -169,7 +169,7 @@ void spawnEnemy(float x, float y, float r, int baseLvl, int lvl) { } // Start the game in selected mode -void startGame(void* sender, std::string elementName) { +void startGame(Window* sender, std::string elementName) { cam->setW(1600); hud->reset(); @@ -457,7 +457,7 @@ void shutdownSystem() { delete splash; } -void backFromOptionsAndSave(void* sender, std::string elementName); +void backFromOptionsAndSave(Window* sender, std::string elementName); void refreshOptionsWindow() { const int l = videoManager->getVideoMode().Width * 0.1f; @@ -506,7 +506,7 @@ void refreshOptionsWindow() { 13 * h, TextManager::LEFT, TextManager::MIDDLE); } -void switchGameOption(void* sender, std::string elementName) { +void switchGameOption(Window* sender, std::string elementName) { map m; m["autoreload"] = &config->AutoReload; m["autopickup"] = &config->AutoWeaponPickup; @@ -520,7 +520,7 @@ void switchGameOption(void* sender, std::string elementName) { } } -void switchVolumeDown(void* sender, std::string elementName) { +void switchVolumeDown(Window* sender, std::string elementName) { if (elementName == "musicvolume") { if (config->MusicVolume > 0) { config->MusicVolume--; @@ -547,7 +547,7 @@ void switchVolumeDown(void* sender, std::string elementName) { refreshOptionsWindow(); } -void switchVolumeUp(void* sender, std::string elementName) { +void switchVolumeUp(Window* sender, std::string elementName) { if (elementName == "musicvolume") { if (config->MusicVolume <= 9) { config->MusicVolume++; @@ -574,7 +574,7 @@ void switchVolumeUp(void* sender, std::string elementName) { refreshOptionsWindow(); } -void switchResolutionDown(void* sender, std::string elementName) { +void switchResolutionDown(Window* sender, std::string elementName) { vector modes = videoManager->GetAvailableModes(); bool set = false; @@ -595,7 +595,7 @@ void switchResolutionDown(void* sender, std::string elementName) { refreshOptionsWindow(); } -void switchResolutionUp(void* sender, std::string elementName) { +void switchResolutionUp(Window* sender, std::string elementName) { vector modes = videoManager->GetAvailableModes(); bool set = false; @@ -618,7 +618,7 @@ void switchResolutionUp(void* sender, std::string elementName) { void refreshControlsMenuWindow(); -void changeControlStyle(void* sender, std::string elementName) { +void changeControlStyle(Window* sender, std::string elementName) { enum ControlStyle style = GetNextControlStyle(config->Control); config->Control = style; @@ -628,7 +628,7 @@ void changeControlStyle(void* sender, std::string elementName) { refreshControlsMenuWindow(); } -void controlsMenuWindowController(void* sender, std::string elementName); +void controlsMenuWindowController(Window* sender, std::string elementName); inline void addControlElement(Window* w, unsigned i, unsigned strN, unsigned lx, unsigned rx) { @@ -704,7 +704,7 @@ void drawWindows() { } } -void controlsMenuWindowController(void* sender, std::string elementName) { +void controlsMenuWindowController(Window* sender, std::string elementName) { Window *w = new Window(0.0f, 0.0f, config->Screen.Width, config->Screen.Height, 0.0f, 0.0f, 0.0f, 0.5f); @@ -757,9 +757,9 @@ void controlsMenuWindowController(void* sender, std::string elementName) { } void drawWindows(); -void showHighScores(void* sender, std::string); +void showHighScores(Window* sender, std::string); -void createControlsMenuWindow(void* sender, std::string elementName) { +void createControlsMenuWindow(Window* sender, std::string elementName) { Window *w = new Window(0.0f, 0.0f, config->Screen.Width, config->Screen.Height, 0.0f, 0.0f, 0.0f, 0.5f); @@ -770,7 +770,7 @@ void createControlsMenuWindow(void* sender, std::string elementName) { windows["options"]->CloseFlag = true; } -void resetControls(void* sender, std::string elementName) { +void resetControls(Window* sender, std::string elementName) { Configuration* config_default = new Configuration(fileUtility); for (int i = 0; i < InputHandler::GameInputEventsCount; i++) config->PlayerInputBinding[i] = config_default->PlayerInputBinding[i]; @@ -849,12 +849,12 @@ void createOptionsWindow() { refreshOptionsWindow(); } -void showOptions(void* sender, std::string elementName) { +void showOptions(Window* sender, std::string elementName) { windows["mainmenu"]->CloseFlag = true; createOptionsWindow(); } -void resumeGame(void* sender, std::string elementName) { +void resumeGame(Window* sender, std::string elementName) { Window* w = windows.find("mainmenu")->second; w->CloseFlag = true; switchGamePause(); @@ -880,7 +880,7 @@ void refreshMainMenuWindow() { TextManager::MIDDLE); } -void switchGameMode(void* sender, std::string elementName) { +void switchGameMode(Window* sender, std::string elementName) { switch (gameMode) { case GAMEMODE_SURVIVAL: gameMode = GAMEMODE_WAVES; @@ -913,7 +913,7 @@ void createMainMenuWindow() { refreshMainMenuWindow(); } -void highScoresWindowController(void* sender, std::string elementName) { +void highScoresWindowController(Window* sender, std::string elementName) { if (elementName == "back") { windows["highscores"]->CloseFlag = true; createMainMenuWindow(); @@ -995,7 +995,7 @@ void createHighscoresWindow() { windows["highscores"] = w; } -void showHighScores(void* sender, std::string elementName) { +void showHighScores(Window* sender, std::string elementName) { windows["mainmenu"]->CloseFlag = true; createHighscoresWindow(); } @@ -1012,7 +1012,7 @@ void unloadResources() { delete config; } -void backFromOptionsAndSave(void* sender, std::string elementName) { +void backFromOptionsAndSave(Window* sender, std::string elementName) { bool changeVideoMode = config->Screen.Width != tempConfig->Screen.Width || config->Screen.Height != tempConfig->Screen.Height; diff --git a/src/windows/CharStatsWindow.cpp b/src/windows/CharStatsWindow.cpp index 446e8dac..b1d9b138 100644 --- a/src/windows/CharStatsWindow.cpp +++ b/src/windows/CharStatsWindow.cpp @@ -28,21 +28,39 @@ const char* CharStatsWindow::paramIds[] = { const unsigned CharStatsWindow::paramIdsNumber = sizeof(CharStatsWindow::paramIds) / sizeof(char*); -void CharStatsWindow::onPlayerParamClickEvent(void* sender, string paramName) +void CharStatsWindow::onPlayerParamClickEvent(Window* sender, string paramName) { - CharStatsWindow* window = (CharStatsWindow*)sender; + CharStatsWindow* window = dynamic_cast(sender); + + if (NULL == window) { + std::cerr << "onPlayerParamClickEvent was called with unexpected sender" << std::endl; + return; + } + window->increasePlayerParam(paramName); } -void CharStatsWindow::onPerkHoverEvent(void* sender, string perkName) +void CharStatsWindow::onPerkHoverEvent(Window* sender, string perkName) { - CharStatsWindow* window = (CharStatsWindow*)sender; + CharStatsWindow* window = dynamic_cast(sender); + + if (NULL == window) { + std::cerr << "onPerkHoverEvent was called with unexpected sender" << std::endl; + return; + } + window->showPerkDetails(perkName); } -void CharStatsWindow::onPerkClickEvent(void* sender, string perkName) +void CharStatsWindow::onPerkClickEvent(Window* sender, string perkName) { - CharStatsWindow* window = (CharStatsWindow*)sender; + CharStatsWindow* window = dynamic_cast(sender); + + if (NULL == window) { + std::cerr << "onPerkClickEvent was called with unexpected sender" << std::endl; + return; + } + window->givePerkToPlayer(perkName); } diff --git a/src/windows/CharStatsWindow.h b/src/windows/CharStatsWindow.h index fe4268ee..69ab4902 100644 --- a/src/windows/CharStatsWindow.h +++ b/src/windows/CharStatsWindow.h @@ -20,9 +20,9 @@ class CharStatsWindow: public Window { static const char* paramIds[]; static const unsigned paramIdsNumber; - static void onPlayerParamClickEvent(void* sender, std::string paramName); - static void onPerkHoverEvent(void* sender, std::string perkName); - static void onPerkClickEvent(void* sender, std::string perkName); + static void onPlayerParamClickEvent(Window* sender, std::string paramName); + static void onPerkHoverEvent(Window* sender, std::string perkName); + static void onPerkClickEvent(Window* sender, std::string perkName); CharStatsWindow(Configuration* config, VideoManager* videoManager, Player* player); diff --git a/src/windows/MainMenuWindow.cpp b/src/windows/MainMenuWindow.cpp index 6722ead9..286e7ff6 100644 --- a/src/windows/MainMenuWindow.cpp +++ b/src/windows/MainMenuWindow.cpp @@ -35,9 +35,14 @@ void MainMenuWindow::exitGame() m_gameState->end(); } -void MainMenuWindow::onMenuItemClick(void* sender, string menuItem) +void MainMenuWindow::onMenuItemClick(Window* sender, string menuItem) { - MainMenuWindow* window = (MainMenuWindow*)sender; + MainMenuWindow* window = dynamic_cast(sender); + + if (NULL == window) { + std::cerr << "onMenuItemClick was called with unexpected sender" << std::endl; + return; + } if (menuItem == "exit") window->exitGame(); diff --git a/src/windows/MainMenuWindow.h b/src/windows/MainMenuWindow.h index 886b6114..26fe8583 100644 --- a/src/windows/MainMenuWindow.h +++ b/src/windows/MainMenuWindow.h @@ -14,7 +14,7 @@ class MainMenuWindow: public Window { void exitGame(); public: - static void onMenuItemClick(void* sender, std::string menuItem); + static void onMenuItemClick(Window* sender, std::string menuItem); MainMenuWindow(Configuration* config, GameState* gameState, TextManager* text); diff --git a/src/windows/Window.cpp b/src/windows/Window.cpp index a27589a6..0617ff84 100644 --- a/src/windows/Window.cpp +++ b/src/windows/Window.cpp @@ -53,8 +53,7 @@ void Window::removeElement(std::string name, bool remainHandler) { removeHandler(hdl_click, name); } -void Window::addHandler(HandlerType hdl, std::string elementName, void(*func)(void* sender, - std::string elementName)) { +void Window::addHandler(HandlerType hdl, std::string elementName, void(*func)(Window* sender, std::string elementName)) { removeHandler(hdl, elementName); if (hdl == hdl_click || hdl == hdl_lclick) m_lcHandlers[elementName] = func; @@ -66,17 +65,17 @@ void Window::addHandler(HandlerType hdl, std::string elementName, void(*func)(vo void Window::removeHandler(HandlerType hdl, std::string elementName) { if (hdl == hdl_all || hdl == hdl_click || hdl == hdl_lclick) { - std::map::iterator it = m_lcHandlers.find(elementName); + std::map::iterator it = m_lcHandlers.find(elementName); if (it != m_lcHandlers.end()) m_lcHandlers.erase(elementName); } if (hdl == hdl_all || hdl == hdl_click || hdl == hdl_rclick) { - std::map::iterator it = m_rcHandlers.find(elementName); + std::map::iterator it = m_rcHandlers.find(elementName); if (it != m_rcHandlers.end()) m_rcHandlers.erase(elementName); } if (hdl == hdl_all || hdl == hdl_move) { - std::map::iterator it = m_mvHandlers.find(elementName); + std::map::iterator it = m_mvHandlers.find(elementName); if (it != m_mvHandlers.end()) m_mvHandlers.erase(elementName); } @@ -118,7 +117,7 @@ void Window::process(InputHandler* input) { int gmy = input->mouseY; // Mouse hover handlers - std::map::const_iterator iter; + std::map::const_iterator iter; for (iter = m_mvHandlers.begin(); iter != m_mvHandlers.end(); ++iter) { std::map::iterator it = m_elements.find(iter->first); if (it != m_elements.end()) { diff --git a/src/windows/Window.h b/src/windows/Window.h index 59be909f..4b9bc1e9 100644 --- a/src/windows/Window.h +++ b/src/windows/Window.h @@ -11,9 +11,9 @@ class Window { protected: float m_left, m_top, m_right, m_bottom, m_r, m_g, m_b, m_a; std::map m_elements; - std::map m_lcHandlers; - std::map m_rcHandlers; - std::map m_mvHandlers; + std::map m_lcHandlers; + std::map m_rcHandlers; + std::map m_mvHandlers; public: enum HandlerType { hdl_all = 0, hdl_click, hdl_lclick, hdl_rclick, hdl_move @@ -31,8 +31,7 @@ class Window { TextManager::TextHAlignFlag halign, TextManager::TextVAlignFlag valign); void removeElement(std::string name, bool remainHandler); // void addHandler(HandlerType hdl, std::string elementName, void(*func)()); - void addHandler(HandlerType hdl, std::string elementName, void(*func)(void* sender, - std::string elementName)); + void addHandler(HandlerType hdl, std::string elementName, void(*func)(Window* sender, std::string elementName)); void removeHandler(HandlerType hdl, std::string elementName); void process(InputHandler* input); void draw();