I'm writing some simple test audio code to make sure SDL_mixer works for my application, but in the code to change the volume when given certain characters from std::cin everything works except for subtracting 10 from the volume. Adding 10 works, subtracting 1 works, but subtracting 10 only subtracts 1 and not 10.
In the code below, the variable sound
is a struct containing 2 maps called mus
and eff
, indexed by strings and containing Mix_Music*
and Mix_Chunk
variables respectively. "Moon Patrol" is my music audio and "Pew" is my test effect audio. Also, sorry about the big if/else statement, I'm going to be using GLUT keys for input in the actual program.
Relevant Code:
Mix_PlayMusic(sound.mus["Moon Patrol"], -1);
char i = ' ';
std::cout << "i / k: volume +/- 1 | u / j: volume +/- 10 | q: quit | v: pew sound" << std::endl;
while (i != 'q')
{
std::cin >> i;
int cVolume = Mix_VolumeMusic(-1);
if (i == 'i') Mix_VolumeMusic(cVolume + ((1) ? cVolume < 128 : 0)); // +1 Works
else if (i == 'k') Mix_VolumeMusic(cVolume - (1 ? cVolume > 0 : 0)); // -1 Works
else if (i == 'u') Mix_VolumeMusic(cVolume + (10 ? cVolume + 10 <= 128 : 0)); // +10 Works
else if (i == 'j') Mix_VolumeMusic(cVolume - (10 ? cVolume > 9 : 0)); // -10 Doesn't Work
else if (i == 'p') std::cout << cVolume << std::endl; // Showing Volume Works
else if (i == 'v') Mix_PlayChannel(-1, sound.eff["Pew"], 0); // Effect Works
} // Exiting works
Things I've tried that didn't work:
cVolume
with Mix_VolumeMusic(-1)
everywhere10
in parentheses'j'
to 'f'
Things I've tried that worked as written (but not what I want):
10
to a 1
(code subtracted 1 as told)else if (i == 'j') Mix_VolumeMusic(cVolume - (10));// ? cVolume > 9 : 0));
(subtracts 10 correctly but doesn't protect from negative values)This shows that it's something about the ternary, but the code for subtracting 1 and subtracting 10 is essentially identical. Is there something I'm missing?
The ternary operator works like this:
variable = (condition) ? expressionTrue : expressionFalse;