So I am making a basic 2D fighting game using C++. And for sound/audio effects I am using mciSendString()
mciSendString(TEXT("setaudio sounds\\character_select.mp3 volume to 500"), NULL, 0, NULL);
This above code works fine. It sets the volume of the sound to 500; however, I do not want to hard-code the volume value. I want it to progressively get smaller without having to copy and paste the same line over and over with just a smaller integer value for volume.
mciSendString(TEXT("setaudio sounds\\character_select.mp3 volume to " + volume ), NULL, 0, NULL);
I want to do something like this. Where instead of having a hard-coded 500 I could have a variable with any integer value in it. However when I run it I get no errors and the audio continues playing like it normally would at 1000 instead of 500.
What would I do to fix this?
Firstly you cannot use the TEXT()
macro with variables. It's only meant to be used with character array literals like "Hello"
and places an L
before the literal, depending on which option (multibyte wide strings or UTF8) is used to compile the code.
For the latter option you can use a std::string
variable to compose your command:
std::ostringstream oss;
int volume = 300;
oss << "setaudio sounds\\character_select.mp3 volume to " << volume;
std::string cmd = oss.str();
mciSendString(cmd.c_str(), NULL, 0, NULL);
For the other option, you need to use std::wstring
, std::wostringstream
accordingly.
I had the same issue with mcisendstring since you are using c++ I recommend you do as I did previously to work this issue out
make a function called mcicommand as follow:
#include <string>
string mcicommand(string volume){
string command = "setaudio song volume to ";
int found = command.find_last_of(" ");
command = command.substr(0, found) + " " + volume;
return command;
}
now whenever you want to call upon this function with mcisendstring do this
string volume;
cin >> volume;
vol_command = mcicommand(volume)
mcisendstring(vol_command.c_str , NULL, 0, NULL);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.