Cross-Platform Toggling Fullscreen in SDL 1.2

Since SDL_WM_ToggleFullScreen only works in X11 I created a custom header file for toggling fullscreen on an SDL window. But as I am a novice programmer I am worried that there might be errors in my code. Would someone that is more experience than I look over my code and let me know if I need to make some changes? The line I am most concerned about contains flags -= SDL_FULLSCREEN, I’m not sure if this is the correct way to remove a Uint32 flag.

Code:
// Cross-platform Toggle FullScreen for an SDL Window
// This code toggles fullscreen on an SDL window using the same parameters as the given window, e.g. window height, width and bpp

#ifndef _SDL_TOGGLE_FS_H
#define _SDL_TOGGLE_FS_H

#define SDL_WIN_SETTINGS surface->w, surface->h, surface->format->BitsPerPixel

// Check if SDL is already loaded
#ifndef _SDL_H
#include <SDL/SDL.h>
#endif

int IsFullScreen(SDL_Surface *surface)
{
if (surface->flags & SDL_FULLSCREEN) return 1; // return true if surface is fullscreen
return 0; // Return false if surface is windowed
}

int SDL_ToggleFS(SDL_Surface *surface)
{
Uint32 flags = surface->flags; // Get the video surface flags

if (IsFullScreen(surface))
{
    // Swith to WINDOWED mode
    if ((surface = SDL_SetVideoMode(SDL_WIN_SETTINGS, flags -= SDL_FULLSCREEN)) == NULL) return 0;
    return 1;
}

// Swith to FULLSCREEN mode
if ((surface = SDL_SetVideoMode(SDL_WIN_SETTINGS, flags|SDL_FULLSCREEN)) == NULL) return 0;
return 1;

}

#endif /* _SDL_TOGGLE_FS_H */------------------------
OS:
Windows XP Profession
Lubuntu/Ubuntu Linux 10.10

SDL uses Uint32 to represent 32 bits of binary data. These bit flags, like
SDL_FULLSCREEN, have a single bit set to 1 with the rest 0. For removing a
flag, I think you’d do a bitwise AND NOT. Something like:
flags &= ~SDL_FULLSCREEN

However, I don’t recommend using the assignment operator (=) in the
condition test for the flags variable. Just put the statement separately
for clarity:
flags &= ~SDL_FULLSCREEN;
if(surface = SDL_SetVideoMode(SDL_WIN_SETTINGS, flags) == NULL)
return 0;

I also don’t recommend (again, for clarity) using a #define for multiple
function arguments.

Jonny DOn Fri, Mar 4, 2011 at 2:24 AM, Deluge wrote:

Since SDL_WM_ToggleFullScreen only works in X11 I created a custom header
file for toggling fullscreen on an SDL window. But as I am a novice
programmer I am worried that there might be errors in my code. Would someone
that is more experience than I look over my code and let me know if I need
to make some changes? The line I am most concerned about contains flags -=
SDL_FULLSCREEN, I’m not sure if this is the correct way to remove a Uint32
flag.

Code:

// Cross-platform Toggle FullScreen for an SDL Window
// This code toggles fullscreen on an SDL window using the same parameters
as the given window, e.g. window height, width and bpp

#ifndef _SDL_TOGGLE_FS_H
#define _SDL_TOGGLE_FS_H

#define SDL_WIN_SETTINGS surface->w, surface->h,
surface->format->BitsPerPixel

// Check if SDL is already loaded
#ifndef _SDL_H
#include
#endif

int IsFullScreen(SDL_Surface *surface)
{
if (surface->flags & SDL_FULLSCREEN) return 1; // return true if
surface is fullscreen
return 0; // Return false if surface is windowed
}

int SDL_ToggleFS(SDL_Surface *surface)
{
Uint32 flags = surface->flags; // Get the video surface flags

if (IsFullScreen(surface))
{
    // Swith to WINDOWED mode
    if ((surface = SDL_SetVideoMode(SDL_WIN_SETTINGS, flags -=

SDL_FULLSCREEN)) == NULL) return 0;
return 1;
}

// Swith to FULLSCREEN mode
if ((surface = SDL_SetVideoMode(SDL_WIN_SETTINGS,

flags|SDL_FULLSCREEN)) == NULL) return 0;
return 1;
}

#endif /* _SDL_TOGGLE_FS_H */


OS:
Windows XP Profession
Lubuntu/Ubuntu Linux 10.10


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks, that’s much appreciated.------------------------
OS:
Windows XP Professional
Lubuntu/Ubuntu Linux 10.10

Here is the fixed version if anybody would like to use it:

Code:
// Cross-platform Toggle FullScreen for an SDL Window

#ifndef _SDL_TOGGLE_FS
#define _SDL_TOGGLE_FS

// Check if SDL is already loaded
#ifndef _SDL_H
#include <SDL/SDL.h>
#endif

int IsFullScreen(SDL_Surface *surface)
{
if (surface->flags & SDL_FULLSCREEN) return 1; // return true if surface is fullscreen
return 0; // Return false if surface is windowed
}

int SDL_ToggleFS(SDL_Surface *surface)
{
Uint32 flags = surface->flags; // Get the video surface flags

if (IsFullScreen(surface))
{
    // Swith to WINDOWED mode
    flags &= ~SDL_FULLSCREEN;
    if ((surface = SDL_SetVideoMode(surface->w, surface->h, surface->format->BitsPerPixel, flags)) == NULL) return 0;
    return 1;
}

// Swith to FULLSCREEN mode
flags = flags|SDL_FULLSCREEN;
if ((surface = SDL_SetVideoMode(surface->w, surface->h, surface->format->BitsPerPixel, flags)) == NULL) return 0;
return 1;

}

#endif------------------------
OS:
Windows XP Professional
Lubuntu/Ubuntu Linux 10.10

Don’t check if SDL.h has already been included, that is a really brittle way
to handle this. To optimise header inclusion, consider using precompiled
headers.

You are assigning to your parameter, which suggests you think that this will
cause the caller’s value to change. This is not the case, pointers are
passed by value so you are modifying a copy. Instead, pass a pointer to a
pointer, or better still return the new surface as the return value.

You can also avoid duplicate code by simply inverting the bit you want
without additional condition statements.

Something like the following:

SDL_Surface *SDL_ToggleFS(SDL_Surface *surface)
{
Uint32 flags = surface->flags;
flags ^= SDL_FULLSCREEN;
return SDL_SetVideoMode(surface->w, surface->h,
surface->format->BitsPerPixel, flags);
}On 12 April 2011 02:06, Deluge wrote:

Here is the fixed version if anybody would like to use it:

Thank you Brian, that was very helpful.------------------------
OS:
Windows XP Professional
Lubuntu/Ubuntu Linux 10.10