1. Bart van Strien
  2. love-experiments

Commits

Alex Szpakowski  committed c4a285e

Added love.window.setFullscreen and removed love.window.toggleFullscreen.

setFullscreen has an optional fullscreen type argument, and when switching back and forth between windowed and fullscreen it'll preserve the original size of the non-fullscreen window.

  • Participants
  • Parent commits adccfa0
  • Branches SDL2

Comments (0)

Files changed (5)

File src/modules/window/Window.h

View file
  • Ignore whitespace
 	virtual bool setWindow(int width = 800, int height = 600, WindowFlags *flags = 0) = 0;
 	virtual void getWindow(int &width, int &height, WindowFlags &flags) = 0;
 
+	virtual bool setFullscreen(bool fullscreen, FullscreenType fstype) = 0;
+	virtual bool setFullscreen(bool fullscreen) = 0;
+
 	virtual bool onWindowResize(int width, int height) = 0;
 
 	virtual int getDisplayCount() const = 0;

File src/modules/window/sdl/Window.cpp

View file
  • Ignore whitespace
 	// Use the desktop resolution if a width or height of 0 is specified.
 	if (width == 0 || height == 0)
 	{
-		SDL_DisplayMode mode;
+		SDL_DisplayMode mode = {};
 		SDL_GetDesktopDisplayMode(f.display, &mode);
 		width = mode.w;
 		height = mode.h;
 	flags = curMode.flags;
 }
 
+bool Window::setFullscreen(bool fullscreen, Window::FullscreenType fstype)
+{
+	if (!window)
+		return false;
+
+	WindowFlags newflags = curMode.flags;
+	newflags.fullscreen = fullscreen;
+	newflags.fstype = fstype;
+
+	Uint32 sdlflags = 0;
+
+	if (fullscreen)
+	{
+		if (fstype == FULLSCREEN_TYPE_DESKTOP)
+			sdlflags = SDL_WINDOW_FULLSCREEN_DESKTOP;
+		else
+		{
+			sdlflags = SDL_WINDOW_FULLSCREEN;
+
+			SDL_DisplayMode mode = {};
+			mode.w = curMode.width;
+			mode.h = curMode.height;
+
+			SDL_GetClosestDisplayMode(SDL_GetWindowDisplayIndex(window), &mode, &mode);
+			SDL_SetWindowDisplayMode(window, &mode);
+		}
+	}
+
+	bool success = SDL_SetWindowFullscreen(window, sdlflags) == 0;
+
+	if (context)
+		SDL_GL_MakeCurrent(window, context);
+
+	updateWindowFlags(newflags);
+	return success;
+}
+
+bool Window::setFullscreen(bool fullscreen)
+{
+	return setFullscreen(fullscreen, curMode.flags.fstype);
+}
+
 int Window::getDisplayCount() const
 {
 	return SDL_GetNumVideoDisplays();
 {
 	if (fullscreen)
 	{
-		SDL_DisplayMode mode, closest;
-		memset(&mode, 0, sizeof(SDL_DisplayMode));
-		mode.w = width;
-		mode.h = height;
-		SDL_GetClosestDisplayMode(displayindex, &mode, &closest);
+		SDL_DisplayMode closest = {};
+		closest.w = width;
+		closest.h = height;
+		SDL_GetClosestDisplayMode(displayindex, &closest, &closest);
 
-		return (mode.w == closest.w && mode.h == closest.h);
+		return (width == closest.w && height == closest.h);
 	}
 	else
 	{
-		SDL_DisplayMode mode;
+		SDL_DisplayMode mode = {};
 		SDL_GetDesktopDisplayMode(displayindex, &mode);
 
 		return (width <= mode.w && height <= mode.h);
 {
 	std::vector<WindowSize> sizes;
 
-	SDL_DisplayMode mode;
+	SDL_DisplayMode mode = {};
 	std::vector<WindowSize>::const_iterator it;
 	for (int i = 0; i < SDL_GetNumDisplayModes(displayindex); i++)
 	{
 {
 	if (displayindex >= 0 && displayindex < getDisplayCount())
 	{
-		SDL_DisplayMode mode;
+		SDL_DisplayMode mode = {};
 		SDL_GetDesktopDisplayMode(displayindex, &mode);
 		width = mode.w;
 		height = mode.h;

File src/modules/window/sdl/Window.h

View file
  • Ignore whitespace
 	bool setWindow(int width = 800, int height = 600, WindowFlags *flags = 0);
 	void getWindow(int &width, int &height, WindowFlags &flags);
 
+	bool setFullscreen(bool fullscreen, FullscreenType fstype);
+	bool setFullscreen(bool fullscreen);
+
 	bool onWindowResize(int width, int height);
 
 	int getDisplayCount() const;

File src/modules/window/wrap_Window.cpp

View file
  • Ignore whitespace
 	return 1;
 }
 
-int w_toggleFullscreen(lua_State *L)
+int w_setFullscreen(lua_State *L)
 {
-	int width, height;
+	bool fullscreen = luax_toboolean(L, 1);
+	Window::FullscreenType fstype = Window::FULLSCREEN_TYPE_MAX_ENUM;
+
+	const char *typestr = lua_isnoneornil(L, 2) ? 0 : lua_tostring(L, 2);
+	if (typestr && !Window::getConstant(typestr, fstype))
+		return luaL_error(L, "Invalid fullscreen type: %s", typestr);
+
+	bool success = false;
+	if (fstype == Window::FULLSCREEN_TYPE_MAX_ENUM)
+		success = instance->setFullscreen(fullscreen);
+	else
+		success = instance->setFullscreen(fullscreen, fstype);
+
+	luax_pushboolean(L, success);
+	return 1;
+}
+
+int w_isFullscreen(lua_State *L)
+{
+	int w, h;
 	WindowFlags flags;
-	instance->getWindow(width, height, flags);
+	instance->getWindow(w, h, flags);
 
-	flags.fullscreen = !flags.fullscreen;
+	const char *typestr;
+	if (!Window::getConstant(flags.fstype, typestr))
+		luaL_error(L, "Unknown fullscreen type.");
 
-	try
-	{
-		luax_pushboolean(L, instance->setWindow(width, height, &flags));
-	}
-	catch (love::Exception &e)
-	{
-		return luaL_error(L, "%s", e.what());
-	}
-
-	return 1;
+	luax_pushboolean(L, flags.fullscreen);
+	lua_pushstring(L, typestr);
+	return 2;
 }
 
 int w_isCreated(lua_State *L)
 	{ "setMode", w_setMode },
 	{ "getMode", w_getMode },
 	{ "getModes", w_getModes },
-	{ "toggleFullscreen", w_toggleFullscreen },
+	{ "setFullscreen", w_setFullscreen },
+	{ "isFullscreen", w_isFullscreen },
 	{ "isCreated", w_isCreated },
 	{ "getWidth", w_getWidth },
 	{ "getHeight", w_getHeight },

File src/modules/window/wrap_Window.h

View file
  • Ignore whitespace
 int w_setMode(lua_State *L);
 int w_getMode(lua_State *L);
 int w_getModes(lua_State *L);
-int w_toggleFullscreen(lua_State *L);
+int w_setFullscreen(lua_State *L);
+int w_isFullscreen(lua_State *L);
 int w_isCreated(lua_State *L);
 int w_getWidth(lua_State *L);
 int w_getHeight(lua_State *L);