# SDL Toggle Fullscreen

## Recommended Posts

NDR008    122
Hi, So after finding that SDL_WM_ToggleFullScreen() doesn't work well I am unsure about the way I implemented my solution:
[source="cpp"]
bool toggle_fullscreen(bool fullscreen) {
SDL_FreeSurface(g_pDisplaySurface);
SDL_Surface* temp;

if (fullscreen == false) {temp = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_DOUBLEBUF | SDL_ANYFORMAT | SDL_FULLSCREEN);}
else {temp = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,  SDL_DOUBLEBUF | SDL_ANYFORMAT);}

g_pDisplaySurface = temp;
fullscreen = !fullscreen;

return(fullscreen);
}


on keypress.... fullscreen = toggle_fullscreen(fullscreen); The above works, what I am unsure, is the following needed: 1) Free old DisplaySurface 2) Create new tempSurface 3) Set DisplaySurface points to new surface Or could I call SDL_SetVideoMode(... flags); straight away without concerning myself with its return value? [Edited by - NDR008 on August 12, 2008 8:49:52 PM]

##### Share on other sites
bschneid    126
I don't believe the temp surface is necessary. To create the original display surface you probably did something like g_pDisplaySurface = SDL_SetVideoMode(...). If you didn't need the temp surface then, you shouldn't need it now.

I do think it is probably a good idea to free the old surface (as you have).

##### Share on other sites
NDR008    122
This is strange, I did a quick experiment:

[source="cpp"]bool toggle_fullscreen(bool fullscreen) {    if (fullscreen == false) {SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, SDL_DOUBLEBUF | SDL_ANYFORMAT | SDL_FULLSCREEN);}  else {SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0,  SDL_DOUBLEBUF | SDL_ANYFORMAT);}  fullscreen = !fullscreen;    return(fullscreen);}

Without even ding g_pDisplaySurface = SDL_SetVideoMode.... however the screen did change. Does SDL_SetVideo always perform functions on the surface created on its first call? I.e. I never need to use its return pointer?

##### Share on other sites
bschneid    126
Hmm. I can't find any good documentation online about the function. Can you still draw to the resized surface?

##### Share on other sites
NDR008    122
Yeah, same here, tried searching online documentations, forums and mailing lists.

Yeah i can draw to the surface after switching modes, so I suspect that SetVideoMode only creates a new surface the first time. There after it performs actions with the same surface.

Or do g_pDisplay = SetVideoMode(...) JUST in case things change with future SDL libraries.

##### Share on other sites
Kylotan    9857
Your display surface pointer will presumably point to some memory that the graphics card uses for the screen. When you resize the screen, it changes the properties of that surface, but it is unlikely to move it. Using the return value is better practice though, in case the above doesn't hold for some reason.

You can always get the current display surface with SDL_GetVideoSurface anyway.

##### Share on other sites
scjohnno    236
Also, you should not free the surface returned by SDL_SetVideoMode. From here:

The surface returned is freed by SDL_Quit and should not be freed by the caller. Note: This rule includes consecutive calls to SDL_SetVideoMode (i.e. resize or resolution change) - the pre-existing surface will be released automatically.