The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Issue Toggling Fullscreen on SDL2 on WinXP
bazz


Joined: 25 Oct 2010
Posts: 31
Hello there, I am writing code that is so far multiplatform between WinXP and MacOSX Mountain Lion.

I recently migrated from SDL 1.2 to SDL2, because fullscreen was crashing on my Mac in SDL1.2 (on OSX10.8.2 Mountain Lion). So I migrated my project to SDL2, which fixed all fullscreen issues on the Mac.

While testing under my Mac,. I wrote the following routine to toggle Fullscreen from my SDL_Event EventHandler. I check for a certain key combo and then will call SDL_ToggleFS().

I'll put the code below, but in easy descriptions all it really does is use SDL_GetWindowFlags to check for SDL_WINDOW_FULLSCREEN. based on that check a call to SDL_SetWindowFullscreen is made. I do not actually alter the flags themselves. Anyways, this code works flawlessly on my Mac. But I just recently went back to upgrade the windows side of things to SDL2. Installing all the libraries etc. Everything is functioning except fullscreen (how ironic). Well, if I START the game in fullscreen/windowed it is fine. But if I try to toggle from windowed to fullscreen, all that happens is my screen resolution changes, but then all i see is my desktop screen. The game does not freeze. If I toggle back to windowed mode, it turns back into a window, but the screen stays black forever. Again the game is still operating and it can contemplate my input. so I can quit the game or fire bullets etc. I just cant see anything...

anyways, here's an exact code snippet of my ToggleFullscreen

Code:
/// Queries the Screen to see if it's set to Fullscreen or Not
/// @return SDL_FALSE if windowed, SDL_TRUE if fullscreen
SDL_bool IsFullScreen(SDL_Window *win)
{
   Uint32 flags = SDL_GetWindowFlags(win);
   
    if (flags & SDL_WINDOW_FULLSCREEN) return SDL_TRUE; // return SDL_TRUE if fullscreen
   
   return SDL_FALSE; // Return SDL_FALSE if windowed
}

/// Toggles On/Off FullScreen
/// @returns -1 on error, 1 on Set fullscreen successful, 0 on Set Windowed successful
int SDL_ToggleFS(SDL_Window *win)

    if (IsFullScreen(win))
    {
        // Swith to WINDOWED mode
        if (SDL_SetWindowFullscreen(win, SDL_FALSE) < 0)
      {
         std::cout<<"Setting windowed failed : "<<SDL_GetError()<<std::endl;
         return -1;
      }
      
        return 0;
    }
   
    // Swith to FULLSCREEN mode
    if (SDL_SetWindowFullscreen(win, SDL_TRUE) < 0)
   {
      std::cout<<"Setting fullscreen failed : "<<SDL_GetError()<<std::endl;
      return -1;
   }
   
   return 1;
}
bazz


Joined: 25 Oct 2010
Posts: 31
Some Research I did:
http://forums.libsdl.org/viewtopic.php?t=8787 - In Accordance to this forum posting, I tried re-updating my Textures when I decided to Toggle Fullscreen. This did not help...
I tried variations on SDL_SetWindowDisplayMode before and after the Toggle, including using SDL_GetWindowDisplayMode

I tried researching for some sort of "Standard example" online, but I could not find much. I even researched into MAME's SDL2 code. (it was an attempt anyways), whose program toggles fullscreen nicely (though I just realized i only know this from experience in Mac OSX)

---------------------------------------------------------------------------------------------------------
Anyways, I finally have found something that at least offers some sort of solution (though it certainly requires some working new components into my game to work properly). Anybody please confirm if this is really what I need to do to get this fullscreen toggling working multiplatform..

EDIT This doesn't work.. It's buggy as all hell!!!!
What works is this :
Code:
SDL_ToggleFS(Window);
SDL_DestroyRenderer(Renderer);
Renderer = GetRenderer(Window, SDL_RENDERER_ACCELERATED); // (Window, -1,
Menu_Background->UpdateTexture(Renderer);
//... now have to re-update all textures


Even tho it works, I cannot tell if I am doing MORE THAN I HAVE TO .

Here in this example I was experimenting. I really want confirmation before going ahead with this, because I do not currently have any kind of Texture Manager that tracks every single Texture I am currently using... it would require a lot of work..
Dominus


Joined: 13 Oct 2009
Posts: 127
Just a word ofgeneral advice, it seems that the forum is not forwarding everything to the mailing list, which most people with the neccessary knowledge are using.
But even it were toforward everything, it certainly got to be lacking in forwarding edits to forum posts.
So, perhaps consider using the mailing list or at least not editing your posts.
bazz


Joined: 25 Oct 2010
Posts: 31
Thanks for the heads up Dominus.
bazz


Joined: 25 Oct 2010
Posts: 31
For those on the email list.. Basically I figured out that Destroying and creating a new Renderer after the call to Toggle the Fullscreen (SDL_SetWindowFullscreen) actually led to some pretty weird bugs...

So I truly cannot believe that there is no standard code out there for doing something as simple as a FullScreen toggle. How come I cannot find anything out there?? My God.
monkey0506


Joined: 13 Mar 2014
Posts: 1
I know it's been a month, but I came across this thread while googling the same problem. Destroying and creating renderers every time I toggled felt rather wrong, and as bazz noted it can cause other unforeseen issues.

I'd actually highly recommend to anyone reading to stop using SDL_WINDOW_FULLSCREEN in favor of SDL2's lovely new SDL_WINDOW_FULLSCREEN_DESKTOP. This lets you keep the same logical resolution without having to worry about whether the monitor supports it as a native resolution, and SDL will automagically handle any necessary scaling and/or letterboxing to keep the same aspect ratio. Very Happy Here's what your function should look like bazz:

Code:
int SDL_ToggleFS(SDL_Window *win)
{
    Uint32 flags = (SDL_GetWindowFlags(win) ^ SDL_WINDOW_FULLSCREEN_DESKTOP);
    if (SDL_SetWindowFullscreen(win, flags) < 0) // NOTE: this takes FLAGS as the second param, NOT true/false!
    {
        std::cout << "Toggling fullscreen mode failed: " << SDL_GetError() << std::endl;
        return -1;
    }
    int w = 640, h = 480; // TODO: UPDATE ME
    if ((flags & SDL_WINDOW_FULLSCREEN_DESKTOP) != 0)
    {
        SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear");
        SDL_RenderSetLogicalSize(Renderer, w, h); // TODO: pass renderer as param maybe?
        return 1;
    }
    SDL_SetWindowSize(win, w, h);
    return 0;
}


You'll also want to update your IsFullScreen method to check against the right flag of course. And for what it's worth, I sorted this out for myself by reading the SDL2 Migration Guide. Even if you've already read through it, another glance couldn't hurt. Cool
macrofeet


Joined: 29 Apr 2012
Posts: 20
Thanks for code bazz
macrofeet


Joined: 29 Apr 2012
Posts: 20
Also thanks monkey0506

int SDL_ToggleFS(SDL_Window *win, int w, int h)
{
Uint32 flags = (SDL_GetWindowFlags(win) ^ SDL_WINDOW_FULLSCREEN_DESKTOP);
if (SDL_SetWindowFullscreen(win, flags) < 0) return -1; // NOTE: this takes FLAGS as the second param, NOT true/false!
SDL_SetWindowSize(win, w, h);
return 0;
}