Having Trouble Switching From Windowed To Fullscreen using SDL2/OpenGL

Started by
17 comments, last by Rutin 6 years ago

I've created a game window of size 1280x720, and everything works great, until I want to change it to fullscreen mode. When I try to use SDL_SetWindowFullscreen(_window, SDL_WINDOW_FULLSCREEN), I get weird behavior where the window is rendered  near the middle of the screen and gets cut off. It ends up looking something like this:

iUky7uW.png

At first I thought it was a rendering bug, but I noticed that I can only move the mouse cursor within the game window area on my screen, so it's almost like the window itself is positioned oddly. One other thing I should note, when I set my game window to 800x600, it works just fine - it scales to fit and fills up the entire screen.

 

I have also tried SDL_WINDOW_FULLSCREEN_DESKTOP, but this also doesn't give me the desired behavior. This time it renders the screen in the bottom left corner, like this:

ztfDyHb.png

I can move the mouse cursor anywhere on screen, so this one may be a rendering issue. I noticed that if I call glViewport with different values I experimented with, it would change the position and size of

 

 

 

Advertisement

What platform is this running on? And are you running the very latest version of SDL 2?

I've had a lot of fun with SDL window handling on Retina MacBook displays in the past.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

It's on Windows 7, and yeah I just updated to SDL2.0.8 to see if that would fix it, but it didn't.

1 hour ago, JackOfCandles said:

It's on Windows 7, and yeah I just updated to SDL2.0.8 to see if that would fix it, but it didn't.

Can you please post the exact code you're using to setup your window and render out display.

I don't use SDL anymore since SDL1, but I made a quick project with your res of 1280x720, and toggled full-screen after window creation and it doesn't do anything strange.


// CREATE WINDOW + FULLSCREEN
window = SDL_CreateWindow("Empty", 0, 0, 1280, 720, SDL_WINDOW_FULLSCREEN);

or


// CREATE WINDOW
window = SDL_CreateWindow("Empty", 0, 0, 1280, 720, SDL_WINDOW_SHOWN);

// FULLSCREEN
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);

 

Programmer and 3D Artist

1 hour ago, Rutin said:

Can you please post the exact code you're using to setup your window and render out display.

I don't use SDL anymore since SDL1, but I made a quick project with your res of 1280x720, and toggled full-screen after window creation and it doesn't do anything strange.



// CREATE WINDOW + FULLSCREEN
window = SDL_CreateWindow("Empty", 0, 0, 1280, 720, SDL_WINDOW_FULLSCREEN);

or



// CREATE WINDOW
window = SDL_CreateWindow("Empty", 0, 0, 1280, 720, SDL_WINDOW_SHOWN);

// FULLSCREEN
SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);

 

 

It's mostly the same, except I'm centering it and  setting the SDL_WINDOW_OPENGL flag:
 


    //Create the window via SDL
    _window = SDL_CreateWindow("Engine",
                               SDL_WINDOWPOS_CENTERED,
                               SDL_WINDOWPOS_CENTERED,
                               _screenWidth,
                               _screenHeight,
                               SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);

And then I set the window to fullscreen after, exactly as you've done in the second code snippet.

I made the window with openGL using the same code above, no problem.

Can you make a minimal project that replicates the problem, then post your code here so we can compile it.

Programmer and 3D Artist

10 hours ago, Rutin said:

I made the window with openGL using the same code above, no problem.

Can you make a minimal project that replicates the problem, then post your code here so we can compile it.

 

Here's the a basic window creation app I wrote that will toggle between fullscreen and windowed mode when it detects a key press. Even with this minimal code, the problem still arises.


#include <SDL.h>

int main(int argc, char *args[]) {
  
	SDL_Window* window;

	window = SDL_CreateWindow("Test",
                                  SDL_WINDOWPOS_CENTERED,
                                  SDL_WINDOWPOS_CENTERED,
                                  1280,
                                  720,
                                  SDL_WINDOW_SHOWN);
	
	SDL_Event event;

	//While the user hasn't quit, run the main loop.
	bool quit = false;
	bool isFullscreen = false;

	while (quit == false) {
		while (SDL_PollEvent(&event)) {
			switch (event.type) {

			case SDL_KEYDOWN:

				if (isFullscreen == false) {
					SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
				}
				else {
					SDL_SetWindowFullscreen(window, 0);
				}

				isFullscreen = !isFullscreen;

				break;

			case SDL_QUIT:
				quit = true;
				break;
			}
		}
	}
  
	return 0;
}

 

Oh! That's an easy fix. :)

The problem you're having is this: 

You need to have 


SDL_Surface* screenSurface;

Then set it before the update:


screenSurface = SDL_GetWindowSurface(window);
SDL_UpdateWindowSurface(window);

Here is your code edited, sorry if I didn't put it in the right place as I don't use SDL2 and it's been a long time since I touched SDL.


#include <SDL.h>

int main(int argc, char *args[]) {

	SDL_Window* window;
	SDL_Surface* screenSurface;

	window = SDL_CreateWindow("Test",
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		1280,
		720,
		SDL_WINDOW_SHOWN);


	SDL_Event event;

	//While the user hasn't quit, run the main loop.
	bool quit = false;
	bool isFullscreen = false;

	while (quit == false) {
		while (SDL_PollEvent(&event)) {
			switch (event.type) {

			case SDL_KEYDOWN:

				if (isFullscreen == false) {
					SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
				}
				else {
					SDL_SetWindowFullscreen(window, 0);
				}

				isFullscreen = !isFullscreen;

				break;

			case SDL_QUIT:
				quit = true;
				break;
			}
		}

		screenSurface = SDL_GetWindowSurface(window);

		SDL_UpdateWindowSurface(window);
	}

	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}

 

Let me know if this fixes your problem.

Programmer and 3D Artist

24 minutes ago, Rutin said:

Oh! That's an easy fix. :)

The problem you're having is this: 

You need to have 



SDL_Surface* screenSurface;

Then set it before the update:



screenSurface = SDL_GetWindowSurface(window);
SDL_UpdateWindowSurface(window);

Here is your code edited, sorry if I didn't put it in the right place as I don't use SDL2 and it's been a long time since I touched SDL.



#include <SDL.h>

int main(int argc, char *args[]) {

	SDL_Window* window;
	SDL_Surface* screenSurface;

	window = SDL_CreateWindow("Test",
		SDL_WINDOWPOS_CENTERED,
		SDL_WINDOWPOS_CENTERED,
		1280,
		720,
		SDL_WINDOW_SHOWN);


	SDL_Event event;

	//While the user hasn't quit, run the main loop.
	bool quit = false;
	bool isFullscreen = false;

	while (quit == false) {
		while (SDL_PollEvent(&event)) {
			switch (event.type) {

			case SDL_KEYDOWN:

				if (isFullscreen == false) {
					SDL_SetWindowFullscreen(window, SDL_WINDOW_FULLSCREEN);
				}
				else {
					SDL_SetWindowFullscreen(window, 0);
				}

				isFullscreen = !isFullscreen;

				break;

			case SDL_QUIT:
				quit = true;
				break;
			}
		}

		screenSurface = SDL_GetWindowSurface(window);

		SDL_UpdateWindowSurface(window);
	}

	SDL_DestroyWindow(window);
	SDL_Quit();

	return 0;
}

 

Let me know if this fixes your problem.

Unfortunately it does not fix the problem. The mouse cursor is still constrained to a particular region of the screen, like the window itself is positioned incorrectly.

That's extremely odd. When I run your minimal code I get the same problem you're having, but when I add in:


screenSurface = SDL_GetWindowSurface(window);
SDL_UpdateWindowSurface(window);

Everything works perfectly fine... Did you try to make a clean EMPTY project?

Follow this guide 100% then paste my code in and run it.

http://lazyfoo.net/tutorials/SDL/01_hello_SDL/windows/msvsnet2010u/index.php

If you're not using Visual Studio let me know.

If you're still getting the same problem, then I would strongly suggest uploading the project file and posting on the SDL forums, also stating Windows 7.

https://discourse.libsdl.org

Just an FYI, I was using SDL2 2.0.7. Maybe there is an issue with 2.0.8? Try using 2.0.7 maybe?

SDL2-devel-2.0.7-VC.zip

Programmer and 3D Artist

This topic is closed to new replies.

Advertisement