In my application I want the user to be able to set various properties from the settings, such as whether the application is fullscreen or whether VSync is enabled, however when creating my window and renderer the flags are passed into a function like this:
renderer = SDL_CreateRenderer(
window,
-1,
SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);
When starting I just made a simple if statement to determine whether to enable a flag
// Creates a new SDL hardware renderer using the default graphics device
if (VSync)
{
renderer = SDL_CreateRenderer(
window,
-1,
SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);
}
else
{
renderer = SDL_CreateRenderer(
window,
-1,
SDL_RendererFlags.SDL_RENDERER_ACCELERATED);
}
This very clearly has some problems, like if I wanted to add a second flag I would have to check if one was enabled and not the other, or if both, or neither, and the code would quickly become a mess. However I'm not sure what to pass in that will handle all cases. I thought of making a list of ints, running through all the settings, and adding the integer value of the respective enum flag like so
List<int> windowFlags = new();
windowFlags.Add((int)SDL_WindowFlags.SDL_WINDOW_SHOWN);
if (IsFullscreen)
windowFlags.Add((int)SDL_WindowFlags.SDL_WINDOW_FULLSCREEN
List<int> rendererFlags = new();
rendererFlags.Add((int)SDL_RendererFlags.SDL_RENDERER_ACCELERATED);
if (VSync)
rendererFlags.Add((int)SDL_RendererFlags.SDL_RENDERER_PRESENTVSYNC);
but I don't know how to convert this list of integers into the flags separated by the | operator (which to be honest I don't understand the function of to begin with). I also do not really like this idea either, as I'd much prefer to pass in the flags like this (I don't think this is possible though)
renderer = SDL_CreateRenderer(
window,
-1,
SDL_RendererFlags.SDL_RENDERER_ACCELERATED |
VSync); // Maybe possible with a ternary operator setup?
If anybody could give me some insight please let me know. Thanks!
CodePudding user response:
Something like this should work:
int flags = 0;
foreach (int flag in rendererFlags)
{
flags |= flag;
}
Reference: Bitwise OR operator on MSDN