Development/Tutorials/Games/KALEngine

< Development‎ | Tutorials
Revision as of 13:31, 13 February 2009 by Dridk (Talk | contribs)

Jump to: navigation, search
KALEngine tutorial
Tutorial Series   KALEngine developement
Prerequisites   None
What's Next   Nothing at the moment
Further Reading   KGLEngine2d's code

Abstract

Phonon is not really designed to handle multiple sounds at the same time. KALEngine is made for games and create one thread per sound, using the excellent openAL librairy. you also get all the other openAL goodness like 3D sounds, effects and multichannel.

Installation

1: Be sure you have installed dependent librairy.

  • openAL ( sound api)
  • alut (openal toolkit)
  • libSndFile (load sound file).

You can install them, from your package manager. for exemple in kubuntu :

sudo apt-get install libalut0-dev libopenal1-dev libsndfile1-dev

2: compile and install kalengine from svn : svn co svn://anonsvn.kde.org/home/kde/trunk/trunk/playground/games/KALEngine/

  • cmake
  • make
  • sudo make install

Hello Word

In this exemple, we will play an "hello word". We don't need any sound, it's the default exemple of openAL.

  1. include <KALEngine>
  2. include <KALSource>
  3. include <KDebug>

int main(int argc, char **argv) {

   KALEngine snd; //create an openAL object
   snd.init(); // Initialize OpenAL Context + Device!
   KALSource *son = new KALSource(); // create a sample, if no argument, It will play an "hello world"
   son->play(); // play the sound!!


   while (son->getStatus() == AL_PLAYING) // wait while sound is playing
   {
     kDebug()<<son->getElapsedTime(); //print the elapsed time.
   }


   delete son; //when it finished! Don't forget to clear the buffer!!
   snd.shutdown(); // and close the device!!


   return 0;

}


-At the first line, we initialize the sound engine. KALengine::init(QString device) take in arguments the deviceName. If you want recover the list of device, you can call the static function :

QStringList KALEngine::getDevicesList().

-At the second time, we create a KALsource. KALSource is the base of openAL, for playing small sound, like shoot, scream, tic, walk ..... It load all the file sound inside a buffer! Then, be carefull if you try to load a Big music with it. If you play your own sound, download a short song : [ocean.wav] and do like this :

 KALSource *son = new KALSource("ocean.wav");

So, currently, it can only play wav file. But, in few time, maybe 1 day..., it will play ogg files. If you want to play music, please wait tutorial for KALStream.

- And now, we can play the sound by calling :

KALSource::play()

This work like a thread! It doesn't wait the end of the song, the programm continue to work.


Then, we must to create a loop for waiting the end of the song. KALSource->getStatus() return the status of the song. AL_PLAYING or AL_STOPED.

-For wait inside the loop, we can print the elapsed time of playing by call : KALSource::getElapsedTime(). It return in second the duration.

- At the end, we must clear the buffer of the KALSource , by calling : delete son. And close the device by calling : KALEngine::shutdown().


Ktip.png
 
Tip
For more information on how the openAL works, look the documentation [openAL programmers Guide ]

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal