How to create a Computer Game - Part 2
In
the first part of this tutorial it was shown to you how to use a 3D world editor to create a game world and save it, to write
C++ code which loads and displays that world, use an IDE and a compiler to transform your code into machine code and create an actual executable game.exe from it.
In this part of the tutorial, we are going to extend this basic game stub with some sound and music, to make it feel more like a game.
1. Adding sound using irrKlang
In part1 of this tutorial of this tutorial, you should already have downloaded and extracted irrKlang 1.0.2, the free audio library. If you haven't, please do this now.
1.1 Adding paths to CodeBlocks
Just as we did it with the Irrlicht engine library before, we need to tell CodeBlocks and the compiler that we want to use irrKlang and where the files are so it will be able to find them in the compiling process: Again open the dialog Build -> Compiler Settings -> Linker, to add the irrKlang lib named libirrKlang.a which can be found in the place where you extracted the irrKlang zip archive and in the sub folder lib\win32-gcc:

add the libirrKlang.a library to
CodeBlocks.
Same with the include files on the 'directories' tab, just add the 'include' path from the SDK to the list of paths to be included in the build:

add the irrKlang include path to
CodeBlocks.
That's it, now the compiler will find everything and we are able to use the sound engine freely in our game.
2. Extending the code to play some music and sound
To make the game more interesting, we now add some few lines into the existing code to play a file named music.mp3 at the start of the game and a sound named sound.wav once the user presses the key 'S'.
Just open the file main.cpp in Codeblocks unless it isn't already open, and replace the code from the last part of the tutorial with the following, extended version:
#include <irrlicht.h>
#include <iostream>
#include <direct.h>
#include <irrklang.h>
using namespace irr;
IrrlichtDevice* Device = 0;
irrklang::ISoundEngine* SoundEngine = 0;
class EventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{
// close app when user presses escape
if (event.EventType == EET_KEY_INPUT_EVENT)
{
if(event.KeyInput.Key == KEY_ESCAPE)
Device->closeDevice();
if (event.KeyInput.Key == KEY_KEY_S &&
!event.KeyInput.PressedDown)
SoundEngine->play2D("mygame/sound.wav");
}
return false;
}
};
int main()
{
Device = createDevice(video::EDT_OPENGL,
core::dimension2d<s32>(640, 480));
SoundEngine = irrklang::createIrrKlangDevice();
chdir(".."); // change to the main irredit directory
EventReceiver receiver;
Device->setEventReceiver(&receiver);
Device->setWindowCaption(L"A Game");
scene::ISceneManager* smgr = Device->getSceneManager();
SoundEngine->play2D("mygame/music.mp3"); // play music
smgr->loadScene("scenes/game.irr");
smgr->addCameraSceneNodeFPS(); // create fps camera
Device->getCursorControl()->setVisible(false);
while(Device->run())
if (Device->isWindowActive())
{
Device->getVideoDriver()->beginScene(true,
true, video::SColor(0,200,200,200));
smgr->drawAll();
Device->getVideoDriver()->endScene();
}
Device->drop();
SoundEngine->drop();
return 0;
}
To test if everything works, just build the code using the menu command Build -> Build or by pressing Ctrl + F9. The message log should print that everything compiled fine and that no error occurred.
3. Copying the necessary files
Before you can start the game with sound, you need to copy some missing files: The dll file which is the sound engine and some sound files we want to play.
Go into the directory where you extracted the irrKlang SDK and copy the file irrKlang.dll and ikpMP3.dll located in bin\win32-gcc to the directory where your game is, the 'mygame' directory you should have created in part 1 of the tutorial.
In addition, you need two sound files: A music file in mp3 format and a sound file as wav file. Just take a random mp3 file, rename it to 'music.mp3' and copy it to your game folder. Do the same with a sound wav file. Your game folder should look like this afterwards:

the game folder after all necessary file have been copied there
Note: the irrKlang and irrlicht SDK contain multiple irrlicht.dll and irrklang.dll files for different compilers and IDEs. Be sure that you copied the right ones, the ones from the folder bin/Win32-gcc.
4. Starting your new game
If you haven't compiled the game, do this now: Click the menu command Build -> Build or press Ctrl + F9. The message log should print that everything compiled fine and that no error occurred. Then, a file named console.exe or similar should be generated in your game folder. You can directly start it by double clicking it or by pressing Strl + F10 in the CodeBlocks IDE.
Congratulations, your game is running, and you have music and sound when pressing the key 'S'.

your game in action, now even with sound and music
In the next part of this tutorial, you will be shown how to make your 'game' more interactive and behave unlike a 3D flight simulatation with dwarves and more like a game.
Add a comment: Comments on article 'How to create a Computer Game'
Related pages