2
0
Эх сурвалжийг харах

Slightly reduced internal reliance on specific module backends

Alex Szpakowski 12 жил өмнө
parent
commit
3e57cfd174

+ 15 - 2
src/common/Module.cpp

@@ -54,9 +54,9 @@ void Module::registerInstance(Module *instance)
 	registry.insert(make_pair(name, instance));
 	registry.insert(make_pair(name, instance));
 }
 }
 
 
-Module *Module::getInstance(const char *name)
+Module *Module::getInstance(const std::string &name)
 {
 {
-	std::map<std::string, Module*>::iterator it = registry.find(std::string(name));
+	std::map<std::string, Module*>::const_iterator it = registry.find(name);
 
 
 	if (registry.end() == it)
 	if (registry.end() == it)
 		return NULL;
 		return NULL;
@@ -64,4 +64,17 @@ Module *Module::getInstance(const char *name)
 	return it->second;
 	return it->second;
 }
 }
 
 
+Module *Module::findInstance(const std::string &name)
+{
+	std::map<std::string, Module*>::const_iterator it;
+
+	for (it = registry.begin(); it != registry.end(); ++it)
+	{
+		if (it->first.find(name) == 0)
+			return it->second;
+	}
+
+	return NULL;
+}
+
 } // love
 } // love

+ 11 - 2
src/common/Module.h

@@ -59,9 +59,18 @@ public:
 	 * Retrieve module instance from internal registry. May return NULL
 	 * Retrieve module instance from internal registry. May return NULL
 	 * if module not registered.
 	 * if module not registered.
 	 * @param name The full name of the module.
 	 * @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
 }; // Module
 
 

+ 1 - 2
src/modules/thread/LuaThread.cpp

@@ -106,8 +106,7 @@ void LuaThread::onError()
 	if (error.empty())
 	if (error.empty())
 		return;
 		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)
 	if (!event)
 		return;
 		return;
 
 

+ 1 - 2
src/modules/window/Window.h

@@ -27,7 +27,6 @@
 // LOVE
 // LOVE
 #include "common/Module.h"
 #include "common/Module.h"
 #include "image/ImageData.h"
 #include "image/ImageData.h"
-#include "graphics/Graphics.h"
 
 
 namespace love
 namespace love
 {
 {
@@ -56,7 +55,7 @@ public:
 
 
 	virtual ~Window();
 	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 void getWindow(int &width, int &height, WindowFlags &flags) const = 0;
 
 
 	virtual bool checkWindowSize(int width, int height, bool fullscreen) const = 0;
 	virtual bool checkWindowSize(int width, int height, bool fullscreen) const = 0;

+ 7 - 5
src/modules/window/sdl/Window.cpp

@@ -20,6 +20,7 @@
 
 
 // LOVE
 // LOVE
 #include "common/config.h"
 #include "common/config.h"
+#include "graphics/Graphics.h"
 #include "Window.h"
 #include "Window.h"
 
 
 // SDL
 // 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 fullscreen = false;
 	bool vsync = true;
 	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.borderless = ((surface->flags & SDL_NOFRAME) != 0);
 	currentMode.flags.centered = centered;
 	currentMode.flags.centered = centered;
 
 
-	if (graphics)
-		graphics->setMode(width, height);
+	if (gfx)
+		gfx->setMode(width, height);
 
 
 	return true;
 	return true;
 }
 }

+ 1 - 1
src/modules/window/sdl/Window.h

@@ -38,7 +38,7 @@ public:
 	Window();
 	Window();
 	~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;
 	void getWindow(int &width, int &height, WindowFlags &flags) const;
 
 
 	bool checkWindowSize(int width, int height, bool fullscreen) const;
 	bool checkWindowSize(int width, int height, bool fullscreen) const;

+ 3 - 7
src/modules/window/wrap_Window.cpp

@@ -42,11 +42,9 @@ int w_setMode(lua_State *L)
 	int w = luaL_checkint(L, 1);
 	int w = luaL_checkint(L, 1);
 	int h = luaL_checkint(L, 2);
 	int h = luaL_checkint(L, 2);
 
 
-	graphics::Graphics *g = luax_optmodule<graphics::Graphics>(L, "graphics", MODULE_GRAPHICS_T);
-
 	if (lua_isnoneornil(L, 3))
 	if (lua_isnoneornil(L, 3))
 	{
 	{
-		luax_pushboolean(L, instance->setWindow(w, h, g, 0));
+		luax_pushboolean(L, instance->setWindow(w, h, 0));
 		return 1;
 		return 1;
 	}
 	}
 
 
@@ -63,7 +61,7 @@ int w_setMode(lua_State *L)
 
 
 	try
 	try
 	{
 	{
-		luax_pushboolean(L, instance->setWindow(w, h, g, &flags));
+		luax_pushboolean(L, instance->setWindow(w, h, &flags));
 	}
 	}
 	catch (love::Exception &e)
 	catch (love::Exception &e)
 	{
 	{
@@ -138,8 +136,6 @@ int w_getModes(lua_State *L)
 
 
 int w_toggleFullscreen(lua_State *L)
 int w_toggleFullscreen(lua_State *L)
 {
 {
-	graphics::Graphics *g = luax_optmodule<graphics::Graphics>(L, "graphics", MODULE_GRAPHICS_T);
-
 	int width, height;
 	int width, height;
 	WindowFlags flags;
 	WindowFlags flags;
 	instance->getWindow(width, height, flags);
 	instance->getWindow(width, height, flags);
@@ -147,7 +143,7 @@ int w_toggleFullscreen(lua_State *L)
 
 
 	try
 	try
 	{
 	{
-		luax_pushboolean(L, instance->setWindow(width, height, g, &flags));
+		luax_pushboolean(L, instance->setWindow(width, height, &flags));
 	}
 	}
 	catch (love::Exception &e)
 	catch (love::Exception &e)
 	{
 	{