Scrolling Game Development Kit Forum
SGDK Version 2 => Help, Errors, FAQ => Topic started by: Jam0864 on 2009-04-10, 08:40:54 PM
-
The first time a sound is played it is delayed since it's not preloaded. For the project I'm working on, it's crucial that it is preloaded.
I tried preloading all the sounds at startup by playing them all at once, but that crashed the project.
Is there something I can do the preload the sounds?
-
Did you try the LoadSound function? It should be an option not far from PlaySound.
-
Didn't see that. :-[
Thanks. ;)
-
Hey guys, I know I'm late on this topic, but maybe you can help me out.
In my game, it's not 100% necessary to preload the sounds, but it sure would be nice if the game didn't stop each time a new music is loaded. So I did like Jam0864 and I called LoadSound() for each music I have. The thing is, I've got 12 mp3 and all together they weight around 45 megs. I compressed them as much as I could usign Audacity and the Lame mp3 encoder. It used to be 75 megs, so I'm pretty happy it dropped to 45 but it's still huge.
I was wondering if there was a way to load the music asynchronously, so when the game starts, I would load every music and they will play when they are loaded without any freeze. If a music is supposed to be played when it is not loaded, the game would go on without music until it is loaded or it would stop until this precise music is loaded, whichever is easier to do.
Any suggestions? Can someone help me out with this?
Thanks! ;D
-
So what's the problem with using LoadSound on your current project's sounds? Does it take too long with 45 MB of sounds, or does it take too much memory or what?
-
It's very long. It was already taking quite long before I added the music, but now it's over the top. At least it doesn't crash or freeze.
-
You could try creating functions "StartLoadSound" and "EndLoadSound" which would be similar to many asynchronous functions in the .NET framework. The "Start" function begins a background thread to run the process, but returns immediately after the thread is started. The "EndLoadSound" returns the final result of the thread when it is complete. StartLoadSound would return an object which can be passed to EndLoadSound so that EndLoadSound knows which sound/thread you are referring to. This object can also be used to check whether the thread is ready to finish yet. Hm, it looks like .NET even provides a generic way to call a function asynchronously. See http://msdn.microsoft.com/en-us/magazine/cc301332.aspx (http://msdn.microsoft.com/en-us/magazine/cc301332.aspx). You may want to start with the conclusion and work your way backwards until you find what you need :). (There's a lot of introduction)
I guess the key is knowing how to use BeginInvoke and EndInvoke. http://shiman.wordpress.com/2008/09/10/c-net-delegates-asynchronous-invocation-begininvoke-method/ (http://shiman.wordpress.com/2008/09/10/c-net-delegates-asynchronous-invocation-begininvoke-method/)
-
Hey, thanks a lot for pointing this out for me! :)