Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Direct Sound

This topic is 6604 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I currently working on writing a sound engine for a game. The engine will interpret messages sent to it, and determine a sound file to use. It will then use directx to play the sound. Can someone please help me out with direct sound? And how do you preload sound files, so you don''t have to load them every time?

Share this post


Link to post
Share on other sites
Advertisement
A simple method of storing them and loading only once:

// All your sounds
enum {SND_DEATH, SND_INJURE, SND_FIRE, SND_FOOTSTEP, ...etc, etc... MAX_SOUNDS }; //etc

// ''Sound'' is a class which contains a buffer, and a Play() method, among others
Sound* allSounds[MAX_SOUNDS];

void InitSound()
{
// set up DirectSound first

// LoadSound loads in the sound and returns a pointer to it
allSounds[SND_DEATH] = LoadSound("sounds/death.wav");
allSounds[SND_INJURE] = LoadSound("sounds/injure.wav");
...etc...
}

and in your game, when you want to play a sound, for example, when the character is injured...

allSounds[SND_INJURE]->Play();

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I think it is better to hide your array of sounds totally within
your class.

So, instead of:

allSounds[SND_INJURE]->Play();

Yo could do:

MY_SOUND_CLASS->Play_Sound(SND_INJURE);

Share this post


Link to post
Share on other sites
Yep. The benefits to Anonymous''s method are that you can check for a NULL sound pointer in one place rather than many, and you can centralise some way of re-using sound buffers if needed. The benefits of my method are that it''s pretty simple to get started with. For my game I actually use a SoundManager class, but I was just suggesting a quick way to get results, not the best way.

Share this post


Link to post
Share on other sites
Thanks. This stuff all sounds good.
Could someone please explain what enum does, and how to use it. I''ve seen it before, but havn''t had time to figured it out yet.
And can anyone give me a little information on direct sound?
Thanks

Share this post


Link to post
Share on other sites
Enum is short for enumeration, or enumerated type. Basically you give it a list of identifiers and it assigns numbers to them in order for you. Example:

enum Directions { DIR_N, DIR_NE, DIR_E }; // etc

The above is somewhat equivalent to:

const int DIR_N = 0;
const int DIR_NE = 1;
const int DIR_E = 2;

or, in C,

#define DIR_N 0
#define DIR_NE 1
#define DIR_E 2

The benefits are that there is less typing involved, and that it automatically assigns you consecutive numbers, so no need to worry about accidentally repeating or missing one.

Howver, if you need to, you can specify individual values, and it continues counting from there. Example:

enum SomeASCII { SPACE = 32, LETTER_A = 65, LETTER_B, LETTER_C };

LETTER_B is obviously 66, LETTER_C is obviously 67, etc.

What''s more, (in C++ at least, not sure about C) you can create instances of enumerated types, and pass them to functions just like you would with an int. So you could declare:

void MyFunction(SomeASCII a_letter)
{
// do something
}

int main()
{
SomeASCII myLetter = LETTER_A;
MyFunction(LETTER_A);
}

This will provide better type-checking than if you just passed const ints around.

Hope that makes some sense

Share this post


Link to post
Share on other sites
If I want this to work throughout the entire program, where do I put the code for it? Do I write a function and call it, do I put it in the main funtion. Do I just leave as global code, or do I put it in a header and include the header(preferable)?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Treat your enumerations like your define constants. You can just put them in
include files if you want to use them globally in all your files. They are
pre-processed just like your defines and they do not take up any memory (unlike
real global variables which do).

Share this post


Link to post
Share on other sites
Another way to store the sounds is by using a hash table.

You store a large hash table in a class and have a public member function PlaySound(char *filename). You then hash the filename and check it against your hash table to see if the file has been loaded. If not, you load the file, add it to your hash table, and play it. It is then there for future use.

I don''t know if this is a good way, but it makes it so you don''t have to preload every sound you are going to use. I also don''t think the performance hit of loading a sound midgame is that great.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!