From 18380eb2e6443c2736b4958b01e7ba2fe2cfa318 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Fri, 28 Apr 2017 15:28:18 -0400 Subject: ui menu fix --- config/controls.dat | 4 +-- include/ui_menu.hpp | 12 ++++++++ include/world.hpp | 2 ++ main.cpp | 2 +- src/engine.cpp | 1 + src/ui_menu.cpp | 86 +++++++++++++++++++++++++++-------------------------- src/window.cpp | 1 + src/world.cpp | 4 +++ 8 files changed, 67 insertions(+), 45 deletions(-) diff --git a/config/controls.dat b/config/controls.dat index b5a4720..c0414cd 100644 --- a/config/controls.dat +++ b/config/controls.dat @@ -1,6 +1,6 @@ 119 97 100 -1073742049 1073742048 -100 +1073742049 +101 diff --git a/include/ui_menu.hpp b/include/ui_menu.hpp index 5288161..cf7ffd6 100644 --- a/include/ui_menu.hpp +++ b/include/ui_menu.hpp @@ -53,6 +53,18 @@ public: void gotoParent(void); }; +class SDLReceiver : public entityx::System, public entityx::Receiver +{ +public: + static bool clicked; + + void configure(entityx::EventManager& ev) + { ev.subscribe(*this); } + void receive(const MainSDLEvent& mse); + void update(entityx::EntityManager& en, entityx::EventManager& ev, entityx::TimeDelta dt) override + { (void)en, (void)ev, (void)dt; } +}; + namespace ui { namespace menu { menuItem createButton(vec2 l, dim2 d, Color c, const char* t, MenuAction f); diff --git a/include/world.hpp b/include/world.hpp index 163676d..2af4642 100644 --- a/include/world.hpp +++ b/include/world.hpp @@ -176,6 +176,8 @@ public: static void load(const std::string& file); void fight(entityx::Entity entity); + + void die(void); }; #endif // WORLD_HPP_ diff --git a/main.cpp b/main.cpp index 429bc7f..5cd4a44 100644 --- a/main.cpp +++ b/main.cpp @@ -185,10 +185,10 @@ int main(int argc, char *argv[]) // exit Mix_HaltMusic(); - Mix_CloseAudio(); unloadTextures(); + game::engine.getSystem()->die(); game::engine.getSystem()->die(); return 0; // Calls everything passed to atexit diff --git a/src/engine.cpp b/src/engine.cpp index dc7aa77..970904b 100644 --- a/src/engine.cpp +++ b/src/engine.cpp @@ -40,6 +40,7 @@ void Engine::init(void) { systems.add(); systems.add(); + systems.add(); systems.configure(); diff --git a/src/ui_menu.cpp b/src/ui_menu.cpp index b49f0e9..08d45e7 100644 --- a/src/ui_menu.cpp +++ b/src/ui_menu.cpp @@ -9,10 +9,38 @@ #include -extern vec2 offset; - static Menu* currentMenu = nullptr; +void SDLReceiver::receive(const MainSDLEvent& mse) +{ + switch (mse.event.type) { + case SDL_QUIT: + game::endGame(); + return; + break; + case SDL_MOUSEMOTION: + //ui::premouse.x = e.motion.x; + //ui::premouse.y = e.motion.y; + break; + case SDL_MOUSEBUTTONUP: + if (mse.event.button.button & SDL_BUTTON_LEFT) + clicked = true; + break; + case SDL_KEYUP: + if (currentMenu != nullptr && mse.event.key.keysym.sym == SDLK_ESCAPE) { + currentMenu->gotoParent(); + return; + } + break; + default: + break; + } +} + +bool SDLReceiver::clicked = false; + +extern vec2 offset; + static Menu pauseMenu; static Menu optionsMenu; static Menu controlsMenu; @@ -216,10 +244,6 @@ namespace ui { auto& SCREEN_WIDTH = game::SCREEN_WIDTH; auto& SCREEN_HEIGHT = game::SCREEN_HEIGHT; - SDL_Event e; - - bool clicked = false; - Render::useShader(&Render::textShader); game::config::update(); @@ -230,31 +254,6 @@ namespace ui { mouse.y = (offset.y+SCREEN_HEIGHT/2)-ui::premouse.y; //custom event polling for menu's so all other events are ignored - while(SDL_PollEvent(&e)) { - switch (e.type) { - case SDL_QUIT: - game::endGame(); - return; - break; - case SDL_MOUSEMOTION: - premouse.x=e.motion.x; - premouse.y=e.motion.y; - break; - case SDL_MOUSEBUTTONUP: - if (e.button.button & SDL_BUTTON_LEFT) { - clicked = true; - } - break; - case SDL_KEYUP: - if (SDL_KEY == SDLK_ESCAPE) { - currentMenu->gotoParent(); - return; - } - break; - default:break; - } - } - static float cMult = 1.0f; static const ColorTex back (Color(0, 0, 0, 204)); @@ -283,18 +282,21 @@ namespace ui { cMult = 0.6f; //if the mouse is over the button and clicks - if (clicked) { - switch(m.member) { - case 0: //normal button - m.button.func(); - break; - case -1: - currentMenu = m.child; - break; - case -2: - currentMenu->gotoParent(); - default:break; + if (SDLReceiver::clicked) { + switch (m.member) { + case 0: //normal button + m.button.func(); + break; + case -1: + currentMenu = m.child; + break; + case -2: + currentMenu->gotoParent(); + default: + break; } + + SDLReceiver::clicked = false; } } diff --git a/src/window.cpp b/src/window.cpp index 7d8f8e7..df584be 100644 --- a/src/window.cpp +++ b/src/window.cpp @@ -35,6 +35,7 @@ WindowSystem::WindowSystem(void) } Mix_AllocateChannels(8); atexit(Mix_Quit); + atexit(Mix_CloseAudio); // create the SDL window object window = SDL_CreateWindow(WINDOW_TITLE, diff --git a/src/world.cpp b/src/world.cpp index e87d6d0..29f77a2 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -561,6 +561,10 @@ WorldSystem::WorldSystem(void) } WorldSystem::~WorldSystem(void) +{ +} + +void WorldSystem::die(void) { // SDL2_mixer's object if (bgmObj != nullptr) -- cgit v1.2.3