Brian Gradin
Brian Gradin

Reputation: 2275

C++ - Coding video game sound effects?

I've done a bunch of research, but I can't figure out how to load a .wav file into memory and then play it. Also, I'd like to have the capability to simultaneously play multiple loaded .wav files.

Currently, I load the .wav file into string buffer, then call:

PlaySound(stows(buffer).c_str(), NULL, SND_MEMORY);

stows() is a function I created which converts a string into a wide string.

What am I doing wrong/what could I be doing better?

EDIT: I'm trying to use SFML now. I followed the walkthrough here to a T, but I'm getting 9 linker errors when I attempt to compile. It doesn't matter if I use the static or dynamic dlls.

EDIT2: I must have VS2010 32 bit. I downloaded SFML 2.0 32 bit for VS2010 and now I'm down to 5 linker errors. It says all of them are occurring in sfml-audio-s.lib.

EDIT3: I got it to compile! However, it simply isn't playing anything. I used VS to debug and confirmed that I am opening the WAV file correctly and storing it in a Sound object, but when I call the play() method, nothing happens. Furthermore, I'm getting an unhandled exception every time I close the program.

Upvotes: 1

Views: 1707

Answers (2)

Brian Gradin
Brian Gradin

Reputation: 2275

I got it working! I ended up using direct sound. I made a class to load and play .WAV files based on a tutorial I found here, which was immensely helpful.

Upvotes: 0

Mats Petersson
Mats Petersson

Reputation: 129344

I'm reasonably convinced that PlaySound isn't able of mixing two sounds playing simultaneously. You can have one sound interrupt the other, and you can make it play "in the background" while your code is doing other things (using SND_ASYNC), but the documentation is pretty clearly stating that "if another sound is played, the current one stops" - in the section of SND_NOSTOP:

If this flag is not specified, PlaySound attempts to stop any sound that is currently playing in the same process

(If you specify SND_NOSTOP, it doesn't play this sound if another one is already playing).

Link to full documentation: PlaySound.

I'm afraid I don't know exactly how you create sound playing using multiple sounds simultaneously, as I've never done that on a PC (I have used hardware mixers, and I've also written code to merge two sounds from a file, but that's almost certainly not what you want)

Upvotes: 1

Related Questions