diff --git a/src/common/Module.cpp b/src/common/Module.cpp index 8b7dda2d0..e464b4e0b 100644 --- a/src/common/Module.cpp +++ b/src/common/Module.cpp @@ -54,9 +54,9 @@ void Module::registerInstance(Module *instance) registry.insert(make_pair(name, instance)); } -Module *Module::getInstance(const char *name) +Module *Module::getInstance(const std::string &name) { - std::map::iterator it = registry.find(std::string(name)); + std::map::const_iterator it = registry.find(name); if (registry.end() == it) return NULL; @@ -64,4 +64,17 @@ Module *Module::getInstance(const char *name) return it->second; } +Module *Module::findInstance(const std::string &name) +{ + std::map::const_iterator it; + + for (it = registry.begin(); it != registry.end(); ++it) + { + if (it->first.find(name) == 0) + return it->second; + } + + return NULL; +} + } // love diff --git a/src/common/Module.h b/src/common/Module.h index 96eecdf8d..4b5e70c52 100644 --- a/src/common/Module.h +++ b/src/common/Module.h @@ -59,9 +59,18 @@ public: * Retrieve module instance from internal registry. May return NULL * if module not registered. * @param name The full name of the module. - * @returns Module instance of NULL if the module is not registered. + * @return Module instance or NULL if the module is not registered. */ - static Module *getInstance(const char *name); + static Module *getInstance(const std::string &name); + + /** + * Find the first module instance from the internal registry whose name + * starts with the supplied name. May return NULL if module is not + * registered or the supplied name is not part of any module name. + * @param name The partial name of the module. + * @return Module instance or NULL if the module is not registered. + **/ + static Module *findInstance(const std::string &name); }; // Module diff --git a/src/modules/thread/LuaThread.cpp b/src/modules/thread/LuaThread.cpp index 62ea2b97a..7b8f38dba 100644 --- a/src/modules/thread/LuaThread.cpp +++ b/src/modules/thread/LuaThread.cpp @@ -106,8 +106,7 @@ void LuaThread::onError() if (error.empty()) return; - // FIXME: We shouldn't specify any particular Event module implementation. - event::Event *event = (event::Event *) Module::getInstance("love.event.sdl"); + event::Event *event = (event::Event *) Module::findInstance("love.event."); if (!event) return; diff --git a/src/modules/window/Window.h b/src/modules/window/Window.h index 1a18d06e4..8148aeb72 100644 --- a/src/modules/window/Window.h +++ b/src/modules/window/Window.h @@ -27,7 +27,6 @@ // LOVE #include "common/Module.h" #include "image/ImageData.h" -#include "graphics/Graphics.h" namespace love { @@ -56,7 +55,7 @@ public: virtual ~Window(); - virtual bool setWindow(int width = 800, int height = 600, graphics::Graphics *graphics = 0, WindowFlags *flags = 0) = 0; + virtual bool setWindow(int width = 800, int height = 600, WindowFlags *flags = 0) = 0; virtual void getWindow(int &width, int &height, WindowFlags &flags) const = 0; virtual bool checkWindowSize(int width, int height, bool fullscreen) const = 0; diff --git a/src/modules/window/sdl/Window.cpp b/src/modules/window/sdl/Window.cpp index 9ec373026..99189bf45 100644 --- a/src/modules/window/sdl/Window.cpp +++ b/src/modules/window/sdl/Window.cpp @@ -20,6 +20,7 @@ // LOVE #include "common/config.h" +#include "graphics/Graphics.h" #include "Window.h" // SDL @@ -59,10 +60,11 @@ Window::_currentMode::_currentMode() { } -bool Window::setWindow(int width, int height, graphics::Graphics *graphics, WindowFlags *flags) +bool Window::setWindow(int width, int height, WindowFlags *flags) { - if (graphics) - graphics->unSetMode(); + graphics::Graphics *gfx = (graphics::Graphics *) Module::findInstance("love.graphics."); + if (gfx) + gfx->unSetMode(); bool fullscreen = false; bool vsync = true; @@ -191,8 +193,8 @@ bool Window::setWindow(int width, int height, graphics::Graphics *graphics, Wind currentMode.flags.borderless = ((surface->flags & SDL_NOFRAME) != 0); currentMode.flags.centered = centered; - if (graphics) - graphics->setMode(width, height); + if (gfx) + gfx->setMode(width, height); return true; } diff --git a/src/modules/window/sdl/Window.h b/src/modules/window/sdl/Window.h index 51615e7c0..8048a63ec 100644 --- a/src/modules/window/sdl/Window.h +++ b/src/modules/window/sdl/Window.h @@ -38,7 +38,7 @@ public: Window(); ~Window(); - bool setWindow(int width = 800, int height = 600, graphics::Graphics *graphics = 0, WindowFlags *flags = 0); + bool setWindow(int width = 800, int height = 600, WindowFlags *flags = 0); void getWindow(int &width, int &height, WindowFlags &flags) const; bool checkWindowSize(int width, int height, bool fullscreen) const; diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index 685723037..02bcea71f 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -42,11 +42,9 @@ int w_setMode(lua_State *L) int w = luaL_checkint(L, 1); int h = luaL_checkint(L, 2); - graphics::Graphics *g = luax_optmodule(L, "graphics", MODULE_GRAPHICS_T); - if (lua_isnoneornil(L, 3)) { - luax_pushboolean(L, instance->setWindow(w, h, g, 0)); + luax_pushboolean(L, instance->setWindow(w, h, 0)); return 1; } @@ -63,7 +61,7 @@ int w_setMode(lua_State *L) try { - luax_pushboolean(L, instance->setWindow(w, h, g, &flags)); + luax_pushboolean(L, instance->setWindow(w, h, &flags)); } catch (love::Exception &e) { @@ -138,8 +136,6 @@ int w_getModes(lua_State *L) int w_toggleFullscreen(lua_State *L) { - graphics::Graphics *g = luax_optmodule(L, "graphics", MODULE_GRAPHICS_T); - int width, height; WindowFlags flags; instance->getWindow(width, height, flags); @@ -147,7 +143,7 @@ int w_toggleFullscreen(lua_State *L) try { - luax_pushboolean(L, instance->setWindow(width, height, g, &flags)); + luax_pushboolean(L, instance->setWindow(width, height, &flags)); } catch (love::Exception &e) {