Browse Source

The vsync field in t.window and love.window.setMode is now an integer.

0 disables vsync, 1 sets it to sync every screen refreshes, 2 sets it to sync every 2 screen refreshes (effectively setting the framerate to half the monitor's refresh rate), etc.

As a special case, -1 also attempts to use adaptive vsync (vsync enabled when FPS is >= monitor's refresh rate, disabled whe not) if the driver supports it.

--HG--
branch : minor
Alex Szpakowski 9 years ago
parent
commit
dd2a448099

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

@@ -203,7 +203,7 @@ struct WindowSettings
 {
 {
 	bool fullscreen = false;
 	bool fullscreen = false;
 	Window::FullscreenType fstype = Window::FULLSCREEN_DESKTOP;
 	Window::FullscreenType fstype = Window::FULLSCREEN_DESKTOP;
-	bool vsync = true;
+	int vsync = 1;
 	int msaa = 0;
 	int msaa = 0;
 	bool resizable = false;
 	bool resizable = false;
 	int minwidth = 1;
 	int minwidth = 1;

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

@@ -483,7 +483,12 @@ bool Window::setWindow(int width, int height, WindowSettings *settings)
 
 
 	SDL_RaiseWindow(window);
 	SDL_RaiseWindow(window);
 
 
-	SDL_GL_SetSwapInterval(f.vsync ? 1 : 0);
+	SDL_GL_SetSwapInterval(f.vsync);
+
+	// Check if adaptive vsync was requested but not supported, and fall back
+	// to regular vsync if so.
+	if (f.vsync == -1 && SDL_GL_GetSwapInterval() != -1)
+		SDL_GL_SetSwapInterval(1);
 
 
 	updateSettings(f);
 	updateSettings(f);
 
 
@@ -568,7 +573,7 @@ void Window::updateSettings(const WindowSettings &newsettings)
 	SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &samples);
 	SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &samples);
 
 
 	settings.msaa = (buffers > 0 ? samples : 0);
 	settings.msaa = (buffers > 0 ? samples : 0);
-	settings.vsync = SDL_GL_GetSwapInterval() != 0;
+	settings.vsync = SDL_GL_GetSwapInterval();
 
 
 	SDL_DisplayMode dmode = {};
 	SDL_DisplayMode dmode = {};
 	SDL_GetCurrentDisplayMode(settings.display, &dmode);
 	SDL_GetCurrentDisplayMode(settings.display, &dmode);

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

@@ -82,7 +82,6 @@ static int readWindowSettings(lua_State *L, int idx, WindowSettings &settings)
 	lua_pop(L, 1);
 	lua_pop(L, 1);
 
 
 	settings.fullscreen = luax_boolflag(L, idx, settingName(Window::SETTING_FULLSCREEN), settings.fullscreen);
 	settings.fullscreen = luax_boolflag(L, idx, settingName(Window::SETTING_FULLSCREEN), settings.fullscreen);
-	settings.vsync = luax_boolflag(L, idx, settingName(Window::SETTING_VSYNC), settings.vsync);
 	settings.msaa = luax_intflag(L, idx, settingName(Window::SETTING_MSAA), settings.msaa);
 	settings.msaa = luax_intflag(L, idx, settingName(Window::SETTING_MSAA), settings.msaa);
 	settings.resizable = luax_boolflag(L, idx, settingName(Window::SETTING_RESIZABLE), settings.resizable);
 	settings.resizable = luax_boolflag(L, idx, settingName(Window::SETTING_RESIZABLE), settings.resizable);
 	settings.minwidth = luax_intflag(L, idx, settingName(Window::SETTING_MIN_WIDTH), settings.minwidth);
 	settings.minwidth = luax_intflag(L, idx, settingName(Window::SETTING_MIN_WIDTH), settings.minwidth);
@@ -92,6 +91,13 @@ static int readWindowSettings(lua_State *L, int idx, WindowSettings &settings)
 	settings.display = luax_intflag(L, idx, settingName(Window::SETTING_DISPLAY), settings.display+1) - 1;
 	settings.display = luax_intflag(L, idx, settingName(Window::SETTING_DISPLAY), settings.display+1) - 1;
 	settings.highdpi = luax_boolflag(L, idx, settingName(Window::SETTING_HIGHDPI), settings.highdpi);
 	settings.highdpi = luax_boolflag(L, idx, settingName(Window::SETTING_HIGHDPI), settings.highdpi);
 
 
+	lua_getfield(L, idx, settingName(Window::SETTING_VSYNC));
+	if (lua_isnumber(L, -1))
+		settings.vsync = (int) lua_tointeger(L, -1);
+	else if (lua_isboolean(L, -1))
+		settings.vsync = lua_toboolean(L, -1);
+	lua_pop(L, 1);
+
 	lua_getfield(L, idx, settingName(Window::SETTING_X));
 	lua_getfield(L, idx, settingName(Window::SETTING_X));
 	lua_getfield(L, idx, settingName(Window::SETTING_Y));
 	lua_getfield(L, idx, settingName(Window::SETTING_Y));
 	settings.useposition = !(lua_isnoneornil(L, -2) && lua_isnoneornil(L, -1));
 	settings.useposition = !(lua_isnoneornil(L, -2) && lua_isnoneornil(L, -1));
@@ -122,7 +128,7 @@ int w_setMode(lua_State *L)
 	// Defaults
 	// Defaults
 	settings.fstype = Window::FULLSCREEN_DESKTOP;
 	settings.fstype = Window::FULLSCREEN_DESKTOP;
 	settings.fullscreen = false;
 	settings.fullscreen = false;
-	settings.vsync = true;
+	settings.vsync = 1;
 	settings.msaa = 0;
 	settings.msaa = 0;
 	settings.resizable = false;
 	settings.resizable = false;
 	settings.minwidth = 1;
 	settings.minwidth = 1;
@@ -186,7 +192,7 @@ int w_getMode(lua_State *L)
 	luax_pushboolean(L, settings.fullscreen);
 	luax_pushboolean(L, settings.fullscreen);
 	lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
 	lua_setfield(L, -2, settingName(Window::SETTING_FULLSCREEN));
 
 
-	luax_pushboolean(L, settings.vsync);
+	lua_pushinteger(L, settings.vsync);
 	lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
 	lua_setfield(L, -2, settingName(Window::SETTING_VSYNC));
 
 
 	lua_pushinteger(L, settings.msaa);
 	lua_pushinteger(L, settings.msaa);