SDL2_Mixer:if (initted&flags != flags) // Warning, Always False?

Hi,

We are working on an update to our Windows®/Linux cross-platform SDL2 2-D game engine now.
We are getting the following warning when building the executable on Linux:


g++ -pipe -Wall -g -I/usr/include/SDL2 -D_REENTRANT -c src/audio.cpp -o src/audio.o
src/audio.cpp: In member function ‘void Audio::SetupAudio()’:
src/audio.cpp:67:23: warning: self-comparison always evaluates to false [-Wtautological-compare]
if (initted&flags != flags)
^~
src/audio.cpp:67:23: warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]


The code is the following on initialization of SDL2_Mixer taken from the official docs:
int flags=MIX_INIT_OGG|MIX_INIT_MOD;
int initted=Mix_Init(flags);
if (initted&flags != flags)

Any help would be appreciate, on Windows®, Code::Blocks does not show this warning…

Jesse
www.FallenAngelSoftware.com

my mistake, it has the same warning on Windows® Code::Blocks…

Well not sure what happened, but with the following I get a different warning:
if (initted&flags != flags)

\vmware-host\Shared Folders\VM-SHARE\GT-R_Engine\src\audio.cpp|67|warning: suggest parentheses around comparison in operand of ‘&’ [-Wparentheses]|

The code (and thus the documentation) is wrong, it should be if( (initted&flags) != flags ).

!= has a higher precedence than &, so, as the compiler warning says, the original code would mean if( initted & (flags != flags) ) which makes no sense.
The term you may wanna look up is “operator precedence”.

Hi,

I changed the code to the following:
int flags = MIX_INIT_OGG|MIX_INIT_MOD;
int initted = Mix_Init(flags);
if ( (initted&flags) != flags )
{

But it is always false and thus the music and sound effects don’t work?

Jesse

“Mix_OpenAudio()” has to be called BEFORE “Mix_Init(flags)”…

Here is the code:
int flags = MIX_INIT_OGG|MIX_INIT_MOD;
int initted = Mix_Init(flags);

if ( flags != initted )
{

Thanks!

Jesse