Browse Source

Cleaned up the love.graphics code that deals with love.window to be more consistent with the rest of the codebase.

Alex Szpakowski 10 years ago
parent
commit
f5abedaeeb

+ 16 - 14
src/modules/graphics/opengl/Graphics.cpp

@@ -24,7 +24,6 @@
 #include "common/Vector.h"
 
 #include "Graphics.h"
-#include "window/sdl/Window.h"
 #include "font/Font.h"
 #include "Polyline.h"
 
@@ -50,7 +49,8 @@ namespace opengl
 {
 
 Graphics::Graphics()
-	: quadIndices(nullptr)
+	: currentWindow(Module::getInstance<love::window::Window>(Module::M_WINDOW))
+	, quadIndices(nullptr)
 	, width(0)
 	, height(0)
 	, created(false)
@@ -62,20 +62,21 @@ Graphics::Graphics()
 	states.reserve(10);
 	states.push_back(DisplayState());
 
-	currentWindow = love::window::sdl::Window::createSingleton();
-
-	int w, h;
-	love::window::WindowSettings wsettings;
+	if (currentWindow.get())
+	{
+		int w, h;
+		love::window::WindowSettings wsettings;
 
-	currentWindow->getWindow(w, h, wsettings);
+		currentWindow->getWindow(w, h, wsettings);
 
-	if (currentWindow->isOpen())
-		setMode(w, h, wsettings.sRGB);
+		if (currentWindow->isOpen())
+			setMode(w, h, wsettings.sRGB);
+	}
 }
 
 Graphics::~Graphics()
 {
-	// We do this manually so the love objects get released before the window.
+	// We do this manually so the graphics objects are released before the window.
 	states.clear();
 	defaultFont.set(nullptr);
 
@@ -87,8 +88,6 @@ Graphics::~Graphics()
 
 	if (quadIndices)
 		delete quadIndices;
-
-	currentWindow->release();
 }
 
 const char *Graphics::getName() const
@@ -238,6 +237,8 @@ void Graphics::setViewportSize(int width, int height)
 
 bool Graphics::setMode(int width, int height, bool &sRGB)
 {
+	currentWindow.set(Module::getInstance<love::window::Window>(Module::M_WINDOW));
+
 	this->width = width;
 	this->height = height;
 
@@ -355,7 +356,7 @@ bool Graphics::isActive() const
 {
 	// The graphics module is only completely 'active' if there's a window, a
 	// context, and the active variable is set.
-	return active && isCreated() && currentWindow && currentWindow->isOpen();
+	return active && isCreated() && currentWindow.get() && currentWindow->isOpen();
 }
 
 static void APIENTRY debugCB(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei /*len*/, const GLchar *msg, const GLvoid* /*usr*/)
@@ -538,7 +539,8 @@ void Graphics::present()
 	glBindRenderbuffer(GL_RENDERBUFFER, info.info.uikit.colorbuffer);
 #endif
 
-	currentWindow->swapBuffers();
+	if (currentWindow.get())
+		currentWindow->swapBuffers();
 
 	// Restore the currently active canvas, if there is one.
 	setCanvas(canvases);

+ 1 - 1
src/modules/graphics/opengl/Graphics.h

@@ -508,7 +508,7 @@ private:
 
 	void checkSetDefaultFont();
 
-	love::window::Window *currentWindow;
+	StrongRef<love::window::Window> currentWindow;
 
 	StrongRef<Font> defaultFont;
 

+ 0 - 4
src/modules/window/Window.cpp

@@ -26,12 +26,8 @@ namespace love
 namespace window
 {
 
-Window *Window::singleton = nullptr;
-
 Window::~Window()
 {
-	if (singleton == this)
-		singleton = nullptr;
 }
 
 void Window::swapBuffers()

+ 0 - 7
src/modules/window/Window.h

@@ -175,9 +175,6 @@ public:
 
 	virtual void requestAttention(bool continuous) = 0;
 
-	//virtual static Window *createSingleton() = 0;
-	// No virtual statics, of course, but you are supposed to implement this static.
-
 	static bool getConstant(const char *in, Setting &out);
 	static bool getConstant(Setting in, const char *&out);
 
@@ -187,10 +184,6 @@ public:
 	static bool getConstant(const char *in, MessageBoxType &out);
 	static bool getConstant(MessageBoxType in, const char *&out);
 
-protected:
-
-	static Window *singleton;
-
 private:
 
 	static StringMap<Setting, SETTING_MAX_ENUM>::Entry settingEntries[];

+ 0 - 10
src/modules/window/sdl/Window.cpp

@@ -994,16 +994,6 @@ void Window::requestAttention(bool continuous)
 	// TODO: Linux?
 }
 
-love::window::Window *Window::createSingleton()
-{
-	if (!singleton)
-		singleton = new Window();
-	else
-		singleton->retain();
-
-	return singleton;
-}
-
 const char *Window::getName() const
 {
 	return "love.window.sdl";

+ 0 - 2
src/modules/window/sdl/Window.h

@@ -104,8 +104,6 @@ public:
 
 	void requestAttention(bool continuous);
 
-	static love::window::Window *createSingleton();
-
 	const char *getName() const;
 
 private:

+ 5 - 2
src/modules/window/wrap_Window.cpp

@@ -529,8 +529,11 @@ static const luaL_Reg functions[] =
 
 extern "C" int luaopen_love_window(lua_State *L)
 {
-	Window *instance = nullptr;
-	luax_catchexcept(L, [&](){ instance = sdl::Window::createSingleton(); });
+	Window *instance = instance();
+	if (instance == nullptr)
+		luax_catchexcept(L, [&](){ instance = new love::window::sdl::Window(); });
+	else
+		instance->retain();
 
 	WrappedModule w;
 	w.module = instance;