简体   繁体   English

C ++-编码视频游戏的音效?

[英]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. 我已经做了很多研究,但是我不知道如何将.wav文件加载到内存中然后进行播放。 Also, I'd like to have the capability to simultaneously play multiple loaded .wav files. 另外,我希望能够同时播放多个已加载的.wav文件。

Currently, I load the .wav file into string buffer, then call: 目前,我将.wav文件加载到字符串缓冲区中,然后调用:

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

stows() is a function I created which converts a string into a wide string. stows()是我创建的将字符串转换为宽字符串的函数。

What am I doing wrong/what could I be doing better? 我在做错什么/我能做得更好?

EDIT: I'm trying to use SFML now. 编辑:我正在尝试使用SFML。 I followed the walkthrough here to a T, but I'm getting 9 linker errors when I attempt to compile. 我按照这里的演练进行操作直到出现T,但是尝试编译时出现9个链接器错误。 It doesn't matter if I use the static or dynamic dlls. 我使用静态或动态dll都没有关系。

EDIT2: I must have VS2010 32 bit. EDIT2:我必须拥有VS2010 32位。 I downloaded SFML 2.0 32 bit for VS2010 and now I'm down to 5 linker errors. 我为VS2010下载了SFML 2.0 32位,现在只有5个链接器错误。 It says all of them are occurring in sfml-audio-s.lib. 它说所有这些都发生在sfml-audio-s.lib中。

EDIT3: I got it to compile! EDIT3:我可以编译了! 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. 我使用VS进行调试,并确认可以正确打开WAV文件并将其存储在Sound对象中,但是当我调用play()方法时,没有任何反应。 Furthermore, I'm getting an unhandled exception every time I close the program. 此外,每次关闭程序时都会收到未处理的异常。

I'm reasonably convinced that PlaySound isn't able of mixing two sounds playing simultaneously. 我有理由相信PlaySound无法将两种声音同时播放。 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 : 您可以使一个声音中断另一个声音,并且可以在代码执行其他操作时使它“在后台”播放(使用SND_ASYNC ),但是文档非常清楚地说明“如果播放另一个声音,则当前声音停止”-在SND_NOSTOP部分:

If this flag is not specified, PlaySound attempts to stop any sound that is currently playing in the same process 如果未指定此标志,则PlaySound尝试停止在同一进程中当前正在播放的任何声音

(If you specify SND_NOSTOP , it doesn't play this sound if another one is already playing). (如果指定SND_NOSTOP ,则如果已经在播放另一个声音,则不会播放该声音)。

Link to full documentation: PlaySound . 链接到完整文档: 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) 恐怕我不完全知道如何同时使用多种声音来创建声音播放,因为我从未在PC上做到这一点(我使用过硬件调音台,而且我还编写了代码来合并来自文件,但几乎可以肯定不是您想要的)

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. 我根据在这里找到的教程制作了一个类来加载和播放.WAV文件,这非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM