SDl:Tutorials:Complete 2D Engine Sound Core
From GPWiki
The wiki is now hosted by GameDev.NET at wiki.gamedev.net. All gpwiki.org content has been moved to the new server. However, the GPWiki forums are still active! Come say hello.
[edit] Sound Core Overview[edit] What Should the Sound Core do?As always think of what you want it to do. In my case I want to have streaming music and sound effects. Since this is a 2D engine it wouldn't make much sense to have position based sound so I will be leaving that out. [edit] How the Sound Core is Laid outFollowing on the same strategy as the graphics core I want to have a resource class and a manager class to handle all the resources.
[edit] Explanation of the Subsystems[edit] cSoundcSound is a class to represent a sound resource, it should handle loading, playing and all of the functionality we wish to have for a sound file. class cSound { public: cSound(); ~cSound(); bool Load(std::string filename); void SetRepeat(int count); void Play(); void Pause(); void Stop(); private: Aluint m_Buffer; } For the most part the sound resource is straight forward. 'Load' will open a file and put it into the sound buffer, false will be returned on a failure to load the file. 'SetRepeat' is handy for music and some effect files that you wish to be repeated, I have defined -1 as an infinite loop, 0 as not repeat and anything else as that number of times to repeat. 'Play', 'Stop' and 'Pause' do what you think they would. 'Aluint' is an openAL buffer type in case you are wondering. [edit] cSoundManagerThe sound manager handles all sound resources, to keep consistency this class will look very similar to the animation manager. class cSoundManager { public: SoundManager(); ~SoundManager(); uint CreateSound( std::string Filename); void Remove(uint Sound); void RemoveAll(); void Pause(uint Sound); void PauseAll(); void Stop(uint Sound); void StopAll(); void Play(uint Sound); void PlayAll(); void SetRepeations(uint Sound, int count); private: std::map< uint, cSound > m_Sounds; }; If you can't remember how the animation manager was laid out go take a look at it and compare it to the sound manager. Isn't it great to have a unified approach to both sound and graphics? Anyways everything here should be self explanatory. [edit] Conclusion[edit] Final NotesThe sound core can be a very complex beast but I decided to make it very simple. Maybe next time I make an engine I will include 3D sound and then the real fun can begin. [edit] About the SourceAs will all the other tutorials you can get the whole source code here.
This tutorial was written by Seoushi. Do you like this tutorial? Have any questions or comments? Let me know or you can ask the forums. |


