From 7ebef76b243df54f3853c85263a19b6a2c251e92 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Tue, 24 Nov 2015 22:16:40 -0400 Subject: [PATCH] Added love.window.setScreenSaverEnabled and love.window.isScreenSaverEnabled (resolves issue #1088.) --- src/modules/event/sdl/Event.cpp | 6 ------ src/modules/graphics/opengl/SpriteBatch.cpp | 18 ++---------------- src/modules/window/Window.h | 3 +++ src/modules/window/sdl/Window.cpp | 16 ++++++++++++++++ src/modules/window/sdl/Window.h | 3 +++ src/modules/window/wrap_Window.cpp | 14 ++++++++++++++ src/modules/window/wrap_Window.h | 2 ++ 7 files changed, 40 insertions(+), 22 deletions(-) diff --git a/src/modules/event/sdl/Event.cpp b/src/modules/event/sdl/Event.cpp index c7decc61e..815bb51bd 100644 --- a/src/modules/event/sdl/Event.cpp +++ b/src/modules/event/sdl/Event.cpp @@ -556,12 +556,6 @@ Message *Event::convertWindowEvent(const SDL_Event &e) const { case SDL_WINDOWEVENT_FOCUS_GAINED: case SDL_WINDOWEVENT_FOCUS_LOST: - // Users won't expect the screensaver to activate if a game is in - // focus. Also, joystick input may not delay the screensaver timer. - if (e.window.event == SDL_WINDOWEVENT_FOCUS_GAINED) - SDL_DisableScreenSaver(); - else - SDL_EnableScreenSaver(); vargs.push_back(new Variant(e.window.event == SDL_WINDOWEVENT_FOCUS_GAINED)); msg = new Message("focus", vargs); break; diff --git a/src/modules/graphics/opengl/SpriteBatch.cpp b/src/modules/graphics/opengl/SpriteBatch.cpp index 6d2d12c33..6b4ff109a 100644 --- a/src/modules/graphics/opengl/SpriteBatch.cpp +++ b/src/modules/graphics/opengl/SpriteBatch.cpp @@ -53,23 +53,9 @@ SpriteBatch::SpriteBatch(Texture *texture, int size, Mesh::Usage usage) throw love::Exception("Invalid SpriteBatch size."); GLenum gl_usage = Mesh::getGLBufferUsage(usage); + size_t vertex_size = sizeof(Vertex) * 4 * size; - const size_t vertex_size = sizeof(Vertex) * 4 * size; - - try - { - array_buf = new GLBuffer(vertex_size, nullptr, GL_ARRAY_BUFFER, gl_usage, GLBuffer::MAP_EXPLICIT_RANGE_MODIFY); - } - catch (love::Exception &) - { - delete array_buf; - throw; - } - catch (std::bad_alloc &) - { - delete array_buf; - throw love::Exception("Out of memory."); - } + array_buf = new GLBuffer(vertex_size, nullptr, GL_ARRAY_BUFFER, gl_usage, GLBuffer::MAP_EXPLICIT_RANGE_MODIFY); } SpriteBatch::~SpriteBatch() diff --git a/src/modules/window/Window.h b/src/modules/window/Window.h index e70496144..3240bba37 100644 --- a/src/modules/window/Window.h +++ b/src/modules/window/Window.h @@ -137,6 +137,9 @@ public: virtual bool setIcon(love::image::ImageData *imgd) = 0; virtual love::image::ImageData *getIcon() = 0; + virtual void setScreenSaverEnabled(bool enable) = 0; + virtual bool isScreenSaverEnabled() const = 0; + virtual void minimize() = 0; virtual void maximize() = 0; diff --git a/src/modules/window/sdl/Window.cpp b/src/modules/window/sdl/Window.cpp index 9a6b18f78..d86f33f8e 100644 --- a/src/modules/window/sdl/Window.cpp +++ b/src/modules/window/sdl/Window.cpp @@ -67,6 +67,9 @@ Window::Window() if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) throw love::Exception("Could not initialize SDL video subsystem (%s)", SDL_GetError()); + // Make sure the screensaver doesn't activate by default. + setScreenSaverEnabled(false); + SDL_version version = {}; SDL_GetVersion(&version); hasSDL203orEarlier = (version.major == 2 && version.minor == 0 && version.patch <= 3); @@ -842,6 +845,19 @@ love::image::ImageData *Window::getIcon() return curMode.icon.get(); } +void Window::setScreenSaverEnabled(bool enable) +{ + if (enable) + SDL_EnableScreenSaver(); + else + SDL_DisableScreenSaver(); +} + +bool Window::isScreenSaverEnabled() const +{ + return SDL_IsScreenSaverEnabled() != SDL_FALSE; +} + void Window::minimize() { if (window != nullptr) diff --git a/src/modules/window/sdl/Window.h b/src/modules/window/sdl/Window.h index 1f83d9634..56ad1cf4a 100644 --- a/src/modules/window/sdl/Window.h +++ b/src/modules/window/sdl/Window.h @@ -70,6 +70,9 @@ public: bool setIcon(love::image::ImageData *imgd); love::image::ImageData *getIcon(); + void setScreenSaverEnabled(bool enable); + bool isScreenSaverEnabled() const; + void minimize(); void maximize(); diff --git a/src/modules/window/wrap_Window.cpp b/src/modules/window/wrap_Window.cpp index e805d29ea..ec3ecd240 100644 --- a/src/modules/window/wrap_Window.cpp +++ b/src/modules/window/wrap_Window.cpp @@ -329,6 +329,18 @@ int w_getIcon(lua_State *L) return 1; } +int w_setScreenSaverEnabled(lua_State *L) +{ + instance()->setScreenSaverEnabled(luax_toboolean(L, 1)); + return 0; +} + +int w_isScreenSaverEnabled(lua_State *L) +{ + luax_pushboolean(L, instance()->isScreenSaverEnabled()); + return 1; +} + int w_setTitle(lua_State *L) { std::string title = luax_checkstring(L, 1); @@ -509,6 +521,8 @@ static const luaL_Reg functions[] = { "getPosition", w_getPosition }, { "setIcon", w_setIcon }, { "getIcon", w_getIcon }, + { "setScreenSaverEnabled", w_setScreenSaverEnabled }, + { "isScreenSaverEnabled", w_isScreenSaverEnabled }, { "setTitle", w_setTitle }, { "getTitle", w_getTitle }, { "hasFocus", w_hasFocus }, diff --git a/src/modules/window/wrap_Window.h b/src/modules/window/wrap_Window.h index 324f39d97..fa0e0911a 100644 --- a/src/modules/window/wrap_Window.h +++ b/src/modules/window/wrap_Window.h @@ -43,6 +43,8 @@ int w_setPosition(lua_State *L); int w_getPosition(lua_State *L); int w_setIcon(lua_State *L); int w_getIcon(lua_State *L); +int w_setScreenSaverEnabled(lua_State *L); +int w_isScreenSaverEnabled(lua_State *L); int w_setTitle(lua_State *L); int w_getTitle(lua_State *L); int w_hasFocus(lua_State *L);