KDE TechBase
  • Page
  • Discussion
  • Edit
  • History
KDE TechBase is a Wiki - You can help! Please contribute! Questions?
Please ask development related questions in the KDE Community Forum.

Development/Tutorials/Games/KALEngine

< Development | Tutorials
KALEngine tutorial
Tutorial Series   KALEngine developement
Prerequisites   None
What's Next   Nothing at the moment
Further Reading   KGLEngine2d's code

Contents

  • 1 Abstract
  • 2 Installation
  • 3 Hello Word
  • 4 Multi-channel
  • 5 3d sound

[edit] 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 openAL library. You also get all the other openAL goodness like 3D sounds, effects and multichannel.

[edit] Installation

1: You need to have the following libraries installed.

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

If you're using a Debian-based distribution, you can install them that way :

sudo apt-get install libalut0-dev libopenal1-dev libsndfile1-dev libvorbis-dev libvorbisfile libogg-dev

2: Build and install KALEngine from svn :

svn co svn://anonsvn.kde.org/home/kde/trunk/playground/games/KALEngine/
  • cmake -DCMAKE_INSTALL_PREFIX=$(kde-config --prefix)
  • make
  • sudo make install

[edit] Hello Word

In this example, we will play a "Hello Word". We don't need to specify any sound file, openAL provides a default sound itself.

#include <KALEngine>
#include <KALSource>
#include <KDebug>
 
int main(int argc, char **argv)
{
// create an OpenAL object
KALEngine soundEngine;
// initialize OpenAL context and device
soundEngine.init();
 
// create a sound, since we haven't specified any filename in the constructor, it will play the "Hello World"
KALSource *sound = new KALSource();
 
// play the sound
sound->play();
 
// print the elapsed time until the sound has finished playing
while (sound->status() == AL_PLAYING)
{
kDebug() << sound->elapsedTime();
}
 
// clear the buffer
delete sound;
// ...and close the device
soundEngine.shutdown();
 
return 0;
}

- First, we initialize the sound engine. The init() function can optionally takes one argument, the device name. You can retrieve the list of devices, using this static function :

QStringList devicesList().

- Then, we create a KALsource. KALSource can be used for playing small sounds(shoot, scream, tic, walk, etc). It will load the entire file inside a buffer, so be careful if you try to play a big file using it.

If you want to try playing something else than the default "Hello World", you can download this short simple : [ocean.wav] and replace the sound object definition by:

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

You can only use it to play wav file at the moment, but we hope to bring ogg support soon. For playing music, please wait until KALStream tutorial is up.

- The sound can now be played by simply calling play(). This function is asynchronous, meaning your program doesn't have to wait until the sound has finished playing to continue.

- Then, to know when the sound has ended, we create a loop using status() which returns ever AL_PLAYING or AL_STOPED.

- Inside the loop, we print the time since the sound started playing using elapsedTime(). It returns an ALfloat with the elapsed time expressed in seconds.

- Finally, we have to clear the KALSource buffer by deleting the KALSource pointer and close the device by calling shutdown().

[edit] Multi-channel

sound1->play(); sound2->play();

...to develop

[edit] 3d sound

KALSource->setPosition(x,y,z) ...to develop



Tip
noframe
For more information on how OpenAL works, see the documentation [OpenAL programmers Guide ]



TODO
noframe
Create KALStream


Retrieved from "http://techbase.kde.org/Development/Tutorials/Games/KALEngine"
Category: Tutorial

Navigation

  • Home
  • Help
  • Recent changes

Sections

  • Getting started
  • Development
  • Schedules
  • Policies
  • Contribute
  • Projects

Toolbox

  • What links here
  • Related changes
  • Special pages
  • Printable version
  • Permanent link

Personal tools

  • 38.107.191.99
  • Talk for this IP
  • Log in / create account
  • Login with OpenID
Creative Commons License SA 3.0 as well as the GNU Free Documentation License 1.2
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal