Tutorial 1.HelloWorld |
This Tutorial shows how to set up the IDE for using the
Irrlicht Engine and how to write a simple HelloWorld program
with it. The program will show how to use the basics of
the VideoDriver, the GUIEnvironment and the SceneManager.
|
Lets start! | |||||||||||||||
After we have set up the IDE, the compiler will know where to find the Irrlicht Engine header files so we can include it now into our code.
In the Irrlicht Engine, everything can be found in the namespace 'irr'. So if you want to use a class of the engine, you'll have to type an irr:: before the name of the class. For example, to use the IrrlichtDevice, write: irr::IrrlichtDevice. To avoid having to put irr:: before of the name of every class, we tell the compiler that we use that namespace.
There are 5 sub-namespaces in the Irrlicht Engine. Take a look at them: you can read a detailed description of them in the documentation by clicking on the top menu item 'Namespace List'. To keep this example simple, we don't want to have to specify the name spaces, Hence:
To be able to use the Irrlicht.DLL file, we need to link with the Irrlicht.lib. We could set this option in the project settings, but to make it easy we use a pragma comment:
Now the main method: to keep this example simple we use int main(), which can be used on any platform. However, on Windows platforms, we could also use the WinMain method if we would want to get rid of the console window which pops up when starting a program with main().
The most important function of the engine is the 'createDevice' function. The Irrlicht Device, which is the root object for doing everything with the engine, can be created with it. createDevice() has 7 parameters:
Now we set the caption of the window to some nice text. Note that there is a 'L' in front of the string: the Irrlicht Engine uses wide character strings when displaying text.
Now we store a pointer to the video driver, the SceneManager, and the graphical user interface environment so that we do not always have to write device->getVideoDriver(), device->getSceneManager(), and device->getGUIEnvironment().
We add a hello world label to the window using the GUI environment. The text is placed at the position (10,10) as top left corner and (200,22) as lower right corner.
To display something interesting, we load a Quake 2
model and display it. We only have to get the Mesh from
the Scene Manager with getMesh() and add a SceneNode
to display the mesh with addAnimatedMeshSceneNode().
Instead of loading a Quake2 file (.md2), it is also
possible to load a Maya object file (.obj), a complete
Quake3 map (.bsp), or a Milshape file (.ms3d).
To make the mesh look a little bit nicer, we change its material a little bit: we disable lighting because we do not have a dynamic light in here and the mesh would be totally black. Then we set the frame loop so that the animation is looped between the frames 0 and 310. Then, at last, we apply a texture to the mesh. Without it the mesh would be drawn using only a solid color.
To look at the mesh, we place a camera into 3d space at the position (0, 10, -40). The camera looks from there to (0,5,0).
Ok. Now that we have set up the scene, let's draw everything: we run the device in a while() loop until the device does not want to run any more. This would be when the user closes the window or presses ALT+F4 in Windows.
Everything must be drawn between a beginScene() and an endScene() call. The beginScene clears the screen with a color and also the depth buffer, if desired. Then we let the Scene Manager and the GUI environment draw their content. With the endScene() call, everything is presented on the screen.
After we are finished, we have to delete the Irrlicht Device created earlier with createDevice(). With the Irrlicht Engine, you should delete all objects you created with a method or function that starts with 'create'. The object is deleted simply by calling ->drop(). See the documentation for more information.
That's it. Compile and run.
|
Possible Errors or Problems | |||||
Visual Studio
Solution: You may have set the include directory improperly in the Visual Studio options. See above for information on setting it.
Solution: You may have set the library directory improperly.
See above for information on
setting it. Compiler independent problems
Solution: You may have forgotten to copy the Irrlicht.dll file from Irrlicht\bin\VisualStudio to the directory the tutorial's project file is in. If the tutorial compiles and runs successfully but produces errors in the console like:
Or:
Solution: The file listed in the error message cannot
be found. Ensure that the directory specified in the
main.cpp exists and is where the file is located. |