From 4cd725cee61ddc1c8a8c242c412cd2ec6c60c69b Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Sat, 18 Jul 2015 13:22:55 -0300 Subject: [PATCH] Added love.window.close (resolves issue #1059), and renamed love.window.isCreated to love.window.isOpen. --- src/modules/graphics/opengl/Graphics.cpp | 4 +- src/modules/window/Window.h | 4 +- src/modules/window/sdl/Window.cpp | 82 ++++++++++-------------- src/modules/window/sdl/Window.h | 6 +- src/modules/window/wrap_Window.cpp | 13 +++- src/modules/window/wrap_Window.h | 3 +- 6 files changed, 54 insertions(+), 58 deletions(-) diff --git a/src/modules/graphics/opengl/Graphics.cpp b/src/modules/graphics/opengl/Graphics.cpp index cd52f805f..dd0d48311 100644 --- a/src/modules/graphics/opengl/Graphics.cpp +++ b/src/modules/graphics/opengl/Graphics.cpp @@ -69,7 +69,7 @@ Graphics::Graphics() currentWindow->getWindow(w, h, wsettings); - if (currentWindow->isCreated()) + if (currentWindow->isOpen()) setMode(w, h, wsettings.sRGB); } @@ -355,7 +355,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->isCreated(); + return active && isCreated() && currentWindow && currentWindow->isOpen(); } static void APIENTRY debugCB(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei /*len*/, const GLchar *msg, const GLvoid* /*usr*/) diff --git a/src/modules/window/Window.h b/src/modules/window/Window.h index e0738011b..2afd40c08 100644 --- a/src/modules/window/Window.h +++ b/src/modules/window/Window.h @@ -112,6 +112,8 @@ public: virtual bool setWindow(int width = 800, int height = 600, WindowSettings *settings = nullptr) = 0; virtual void getWindow(int &width, int &height, WindowSettings &settings) = 0; + virtual void close() = 0; + virtual bool setFullscreen(bool fullscreen, FullscreenType fstype) = 0; virtual bool setFullscreen(bool fullscreen) = 0; @@ -128,7 +130,7 @@ public: virtual void setPosition(int x, int y, int displayindex) = 0; virtual void getPosition(int &x, int &y, int &displayindex) = 0; - virtual bool isCreated() const = 0; + virtual bool isOpen() const = 0; virtual void setWindowTitle(const std::string &title) = 0; virtual const std::string &getWindowTitle() const = 0; diff --git a/src/modules/window/sdl/Window.cpp b/src/modules/window/sdl/Window.cpp index c7eab31c9..29de8424e 100644 --- a/src/modules/window/sdl/Window.cpp +++ b/src/modules/window/sdl/Window.cpp @@ -52,7 +52,7 @@ namespace sdl { Window::Window() - : created(false) + : open(false) , mouseGrabbed(false) , window(nullptr) , context(nullptr) @@ -65,17 +65,7 @@ Window::Window() Window::~Window() { - if (context) - { - graphics::Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); - if (gfx != nullptr) - gfx->unSetMode(); - - SDL_GL_DeleteContext(context); - } - - if (window) - SDL_DestroyWindow(window); + close(); SDL_QuitSubSystem(SDL_INIT_VIDEO); } @@ -345,22 +335,12 @@ bool Window::createWindowAndContext(int x, int y, int w, int h, Uint32 windowfla } } - if (context) - { - SDL_GL_DeleteContext(context); - context = nullptr; - } - - if (window) - { - SDL_DestroyWindow(window); - SDL_FlushEvent(SDL_WINDOWEVENT); - window = nullptr; - } + close(); return false; } + open = true; return true; } @@ -439,33 +419,11 @@ bool Window::setWindow(int width, int height, WindowSettings *settings) x = y = SDL_WINDOWPOS_UNDEFINED_DISPLAY(f.display); } - graphics::Graphics *gfx = Module::getInstance(Module::M_GRAPHICS); - if (gfx != nullptr) - gfx->unSetMode(); - - if (context) - { - SDL_GL_DeleteContext(context); - context = nullptr; - } - - if (window) - { - SDL_DestroyWindow(window); - window = nullptr; - - // The old window may have generated pending events which are no longer - // relevant. Destroy them all! - SDL_FlushEvent(SDL_WINDOWEVENT); - } - - created = false; + close(); if (!createWindowAndContext(x, y, width, height, sdlflags, f.msaa, f.sRGB)) return false; - created = true; - // Make sure the window keeps any previously set icon. setIcon(curMode.icon.get()); @@ -484,6 +442,7 @@ bool Window::setWindow(int width, int height, WindowSettings *settings) updateSettings(f); + auto gfx = Module::getInstance(Module::M_GRAPHICS); if (gfx != nullptr) gfx->setMode(curMode.pixelwidth, curMode.pixelheight, curMode.settings.sRGB); @@ -584,6 +543,31 @@ void Window::getWindow(int &width, int &height, WindowSettings &settings) settings = curMode.settings; } +void Window::close() +{ + auto gfx = Module::getInstance(Module::M_GRAPHICS); + if (gfx != nullptr) + gfx->unSetMode(); + + if (context) + { + SDL_GL_DeleteContext(context); + context = nullptr; + } + + if (window) + { + SDL_DestroyWindow(window); + window = nullptr; + + // The old window may have generated pending events which are no longer + // relevant. Destroy them all! + SDL_FlushEvent(SDL_WINDOWEVENT); + } + + open = false; +} + bool Window::setFullscreen(bool fullscreen, Window::FullscreenType fstype) { if (!window) @@ -730,9 +714,9 @@ void Window::getPosition(int &x, int &y, int &displayindex) } } -bool Window::isCreated() const +bool Window::isOpen() const { - return created; + return open; } void Window::setWindowTitle(const std::string &title) diff --git a/src/modules/window/sdl/Window.h b/src/modules/window/sdl/Window.h index 9f1eda42a..eca5ad37e 100644 --- a/src/modules/window/sdl/Window.h +++ b/src/modules/window/sdl/Window.h @@ -44,6 +44,8 @@ public: bool setWindow(int width = 800, int height = 600, WindowSettings *settings = nullptr); void getWindow(int &width, int &height, WindowSettings &settings); + void close(); + bool setFullscreen(bool fullscreen, FullscreenType fstype); bool setFullscreen(bool fullscreen); @@ -60,7 +62,7 @@ public: void setPosition(int x, int y, int displayindex); void getPosition(int &x, int &y, int &displayindex); - bool isCreated() const; + bool isOpen() const; void setWindowTitle(const std::string &title); const std::string &getWindowTitle() const; @@ -139,7 +141,7 @@ private: } curMode; - bool created; + bool open; bool mouseGrabbed; diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index 9d8595ec3..0f2627861 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -260,12 +260,18 @@ int w_getFullscreen(lua_State *L) return 2; } -int w_isCreated(lua_State *L) +int w_isOpen(lua_State *L) { - luax_pushboolean(L, instance()->isCreated()); + luax_pushboolean(L, instance()->isOpen()); return 1; } +int w_close(lua_State * /*L*/) +{ + instance()->close(); + return 0; +} + int w_getDesktopDimensions(lua_State *L) { int width = 0, height = 0; @@ -499,7 +505,8 @@ static const luaL_Reg functions[] = { "getFullscreenModes", w_getFullscreenModes }, { "setFullscreen", w_setFullscreen }, { "getFullscreen", w_getFullscreen }, - { "isCreated", w_isCreated }, + { "isOpen", w_isOpen }, + { "close", w_close }, { "getDesktopDimensions", w_getDesktopDimensions }, { "setPosition", w_setPosition }, { "getPosition", w_getPosition }, diff --git a/src/modules/window/wrap_Window.h b/src/modules/window/wrap_Window.h index 882be9964..324f39d97 100644 --- a/src/modules/window/wrap_Window.h +++ b/src/modules/window/wrap_Window.h @@ -36,7 +36,8 @@ int w_getMode(lua_State *L); int w_getFullscreenModes(lua_State *L); int w_setFullscreen(lua_State *L); int w_getFullscreen(lua_State *L); -int w_isCreated(lua_State *L); +int w_isOpen(lua_State *L); +int w_close(lua_State *L); int w_getDesktopDimensions(lua_State *L); int w_setPosition(lua_State *L); int w_getPosition(lua_State *L);