• Create Account

## When to free sound

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1munchor  Members

101
Like
0Likes
Like

Posted 03 March 2012 - 04:48 AM

I'm using SDL_mixer to play a .wav sound, but I don't know when to free it.

I have this code:

Bullet::Bullet() {
//Create Bullet

/* Play the shot sound */
sound = Mix_LoadWAV("media/gunshot.wav"); //sound is a private property of Bullet

if (sound == NULL) {
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}

Mix_PlayChannel(0, sound, 0);
}


My problem is that if I free the sound right after I play it, it will not be played.
If I free the sound when the bullet is deleted (deconstructor), the sound stops before it is over, because I delete bullets whenever they leave the screen, and sometimes they leave the screen before the sound isn't over.

So, my doubt it, how can I handle loading the sound and freeing it without super RAM consumption? Thank you!

### #2SimonForsman  Members

7584
Like
0Likes
Like

Posted 03 March 2012 - 07:41 AM

I'm using SDL_mixer to play a .wav sound, but I don't know when to free it.

I have this code:

Bullet::Bullet() {
//Create Bullet

/* Play the shot sound */
sound = Mix_LoadWAV("media/gunshot.wav"); //sound is a private property of Bullet

if (sound == NULL) {
fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
}

Mix_PlayChannel(0, sound, 0);
}


My problem is that if I free the sound right after I play it, it will not be played.
If I free the sound when the bullet is deleted (deconstructor), the sound stops before it is over, because I delete bullets whenever they leave the screen, and sometimes they leave the screen before the sound isn't over.

So, my doubt it, how can I handle loading the sound and freeing it without super RAM consumption? Thank you!

1) Load the sound at the start of the level. (The same goes for textures, models, etc)
2) Free them at the end of the level

If sounds and models etc are the same across all levels then you can load them when the game starts instead (and free them when the game ends), you could also store a list of resources required for each level and then load the full list for level 1, at the end of level 1 you check the difference in the resource list for lvl 1 and 2 and free the resources that are no longer needed and then add any new additions.

All bullets can share 1 sound object so you don't need to load it once for each bullet.
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #3munchor  Members

101
Like
0Likes
Like

Posted 03 March 2012 - 08:42 AM

1) Load the sound at the start of the level. (The same goes for textures, models, etc)
2) Free them at the end of the level

If sounds and models etc are the same across all levels then you can load them when the game starts instead (and free them when the game ends), you could also store a list of resources required for each level and then load the full list for level 1, at the end of level 1 you check the difference in the resource list for lvl 1 and 2 and free the resources that are no longer needed and then add any new additions.

All bullets can share 1 sound object so you don't need to load it once for each bullet.

The problem is how can I play them from the Bullet class if I do that.

### #4SimonForsman  Members

7584
Like
0Likes
Like

Posted 03 March 2012 - 11:34 AM

1) Load the sound at the start of the level. (The same goes for textures, models, etc)
2) Free them at the end of the level

If sounds and models etc are the same across all levels then you can load them when the game starts instead (and free them when the game ends), you could also store a list of resources required for each level and then load the full list for level 1, at the end of level 1 you check the difference in the resource list for lvl 1 and 2 and free the resources that are no longer needed and then add any new additions.

All bullets can share 1 sound object so you don't need to load it once for each bullet.

The problem is how can I play them from the Bullet class if I do that.

keep a pointer to the sound in the bullet class, pass the sound by reference to the bullet constructor.

Or have a AudioPlayer class that all bullets have a pointer to and simply do something like
ap->playSound("GunShot");

the AudioPlayer then stores a map of identifiers (i used a string id in that example) and sounds. (This is a bit cleaner if you need one object to play multiple different sounds) and just plays whichever one is requested. (You could also pass in the position then to get stereo or 3D sound)
I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

### #5munchor  Members

101
Like
0Likes
Like

Posted 03 March 2012 - 01:37 PM

Thank you, the first alternative worked!

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.