I am learning how to write SDL program in C++, and I came across this code:
SDL_Renderer *ren =
SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_R
These are flags which you can set. In this instance |
refers to the bitwise operator.
In your example, this conveniently allows you to combine multiple flags through a single parameter.
Suppose the two flags have the following values:
SDL_RENDERER_SOFTWARE = 1 // Binary 0001
SDL_RENDERER_ACCELERATED = 2 // Binary 0010
SDL_RENDERER_PRESENTVSYNC = 4 // Binary 0100
A logic bitwise OR of the two, would leave you with the value 6 for the flag
.
We can easily determine from this value which flags have been set using bitwise AND.:
flag = SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC // flag = 6 (0110)
flag & SDL_RENDERER_SOFTWARE == 0 // Not set
flag & SDL_RENDERER_ACCELERATED == 2 // Set
flag & SDL_RENDERER_PRESENTVSYNC == 4 // Set
Note that it's important here for the flags to be powers of two, to ensure all flag combinations result in a unique value.