Sign in to follow this  
NDR008

SDL Toggle Fullscreen

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this