From f5abedaeebbae5dd8767555e7a5024740a4fae10 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 24 Jul 2015 21:40:55 -0300 Subject: [PATCH] Cleaned up the love.graphics code that deals with love.window to be more consistent with the rest of the codebase. --- src/modules/graphics/opengl/Graphics.cpp | 30 +++++++++++++----------- src/modules/graphics/opengl/Graphics.h | 2 +- src/modules/window/Window.cpp | 4 ---- src/modules/window/Window.h | 7 ------ src/modules/window/sdl/Window.cpp | 10 -------- src/modules/window/sdl/Window.h | 2 -- src/modules/window/wrap_Window.cpp | 7 ++++-- 7 files changed, 22 insertions(+), 40 deletions(-) diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index bea92a732..aa4612b43 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/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(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(); + if (currentWindow.get()) + { + int w, h; + love::window::WindowSettings wsettings; - 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(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); diff --git a/src/modules/graphics/opengl/Graphics.h b/src/modules/graphics/opengl/Graphics.h index ba9a97ac1..7116fe142 100644 --- a/src/modules/graphics/opengl/Graphics.h +++ b/src/modules/graphics/opengl/Graphics.h @@ -508,7 +508,7 @@ private: void checkSetDefaultFont(); - love::window::Window *currentWindow; + StrongRef currentWindow; StrongRef defaultFont; diff --git a/src/modules/window/Window.cpp b/src/modules/window/Window.cpp index c513bc4e2..0131b3c2d 100644 --- a/src/modules/window/Window.cpp +++ b/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() diff --git a/src/modules/window/Window.h b/src/modules/window/Window.h index 2afd40c08..8c70b1531 100644 --- a/src/modules/window/Window.h +++ b/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::Entry settingEntries[]; diff --git a/src/modules/window/sdl/Window.cpp b/src/modules/window/sdl/Window.cpp index 29de8424e..b4f982e8c 100644 --- a/src/modules/window/sdl/Window.cpp +++ b/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"; diff --git a/src/modules/window/sdl/Window.h b/src/modules/window/sdl/Window.h index eca5ad37e..d985f7b77 100644 --- a/src/modules/window/sdl/Window.h +++ b/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: diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index 0f2627861..cc4f767d0 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/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;