Uint32 Flags?

Sorry I couldn't think of a better title, but hopefully you'll understand. I've worked with the SDL library for quite some time and whenever you have to set the video mode, there are flags that that tell whether to go fullscreen, use OpenGL, or whatever such as SDL_OPENGL | SDL_FULLSCREEN.

What I want to know is how would I break down these flags if I wanted to know if the Uint32 variable contained the flag for fullscreen or if it contained the flag for something else?
Let's take a look at it:

1
2
3
Uint32 Flags = SDL_OPENGL | SDL_FULLSCREEN;
bool IsOpenGL = (Flags & SDL_OPENGL) == SDL_OPENGL;
bool IsFullScreen = (Flags & SDL_FULLSCREEN) == SDL_FULLSCREEN;


Enough? ^

Beware: You cannot use random values for SDL_OPENGL and SDL_FULLSCREEN and so on.

They must be left-shifted of at least a bit from each other, like:

0x00000001
0x00000002
0x00000004
0x00000008
0x00000010
0x00000020
0x00000040
0x00000080
0x00000100
...and so on
Last edited on
You could also compare against 0 instead of the flag if you prefer.
1
2
bool IsOpenGL = (Flags & SDL_OPENGL) != 0;
bool IsFullScreen = (Flags & SDL_FULLSCREEN) != 0;

or you could leave out the comparasion agains zero because that is the default integer to bool conversion anyway.
1
2
bool IsOpenGL = (Flags & SDL_OPENGL);
bool IsFullScreen = (Flags & SDL_FULLSCREEN);


If you don't want to use a variable and just check it inside an if statement it would like like
if ((Flags & SDL_OPENGL) == SDL_OPENGL), or
if ((Flags & SDL_OPENGL) != 0), or
if (Flags & SDL_OPENGL).

EDIT:
EssGeEich's way is better if you have combined flags with more than one bit set.
const Uint32 OPENGL_AND_FULLSCREEN = SDL_OPENGL | SDL_FULLSCREEN;

Now this would not work.
bool IsOpenGLAndFullScreen = (Flags & OPENGL_AND_FULLSCREEN);

IsOpenGLAndFullScreen would be set to true if at least one of the two flags are true. By using EssGeEich's way it would work correctly.
bool IsOpenGLAndFullScreen = (Flags & OPENGL_AND_FULLSCREEN) == OPENGL_AND_FULLSCREEN;
Last edited on
Oh thank you that worked perfectly!

SDL_OPENGL and SDL_FULLSCREEN aren't values I set, they're values set by the SDL library, but that's useful information if I wanted to do something like that.
Yeah, I told you in case you wanted to do your enumeration. Almost everyone uses this kind of enumeration for bitmasks, so it should work almost everywhere.

Also feel free to use Peter87's code: It works almost the same, and it's shorter.
Last edited on
Topic archived. No new replies allowed.