Development/Tutorials/Games/KALEngine: Difference between revisions

    From KDE TechBase
    No edit summary
    No edit summary
    Line 79: Line 79:
    </code>
    </code>


    **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 :  
    -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 :  


    <code cppqt>
    <code cppqt>
    Line 85: Line 85:
    </code>
    </code>


    **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.
    -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 : [[http://ktank.free.fr/kalengine/ocean.wav ocean.wav]] and do like this :  
    If you play your own sound, download a short song : [[http://ktank.free.fr/kalengine/ocean.wav ocean.wav]] and do like this :  


    Line 95: Line 95:
    If you want to play music, please wait tutorial for KALStream.
    If you want to play music, please wait tutorial for KALStream.


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


    <b>KALSource::play()</b>
    <b>KALSource::play()</b>
    Line 102: Line 102:




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


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


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



    Revision as of 12:34, 13 February 2009

    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/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().


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