1 /** Example 001 HelloWorld
\r
3 This tutorial shows how to set up the IDE for using the Irrlicht Engine and how
\r
4 to write a simple HelloWorld program with it. The program will show how to use
\r
5 the basics of the VideoDriver, the GUIEnvironment, and the SceneManager.
\r
6 Microsoft Visual Studio is used as an IDE, but you will also be able to
\r
7 understand everything if you are using a different one or even another
\r
8 operating system than Windows.
\r
10 You have to include the header file <irrlicht.h> in order to use the engine. The
\r
11 header file can be found in the Irrlicht Engine SDK directory \c include. To let
\r
12 the compiler find this header file, the directory where it is located has to be
\r
13 added in your project as include path. This is different for every IDE and
\r
14 compiler you use. Let's explain shortly how to do this in Visual Studio 2010:
\r
16 - In Visual Studio 2010 select the Menu Project -> Properties. Select the
\r
17 "C/C++" - "General" option, and select the "Additional Include Directories".
\r
18 Add the \c include directory of the Irrlicht engine folder to the list of
\r
19 directories. Now the compiler will find the irrlicht.h header file. We also
\r
20 need the irrlicht.lib to be found, so select "Linker" - "General" and
\r
21 add the \c lib/Win64-visualStudio or \c lib/Win32-visualStudio directory
\r
22 to "Additional Library Directories". Which of the 2 Irrlicht versions you
\r
23 chose depends on the target platform for your application (win32 or x64).
\r
24 In your project properties you can see what your active solution platform
\r
25 is, you can use the same one for Irrlicht.
\r
27 To be able to use the Irrlicht.DLL file, we need to link with the Irrlicht.lib.
\r
28 In most IDE's you have to add irrlicht.lib (or irrlicht.a or irrlicht.so on
\r
29 Linux) to your Linker input files.
\r
31 For VisualStudio we can be lazy and use the pragma comment lib.
\r
32 We also want to get rid of the console window, which pops up when starting a
\r
33 program with main() (instead of WinMain). This is done by the second pragma.
\r
34 We could also use the WinMain method, though losing platform independence then.
\r
37 #pragma comment(lib, "Irrlicht.lib")
\r
38 #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
\r
42 That's it. With your IDE set up like this, you will now be able to develop
\r
43 applications with the Irrlicht Engine.
\r
47 After we have set up the IDE, the compiler will know where to find the Irrlicht
\r
48 Engine header files so we can include it now in our code.
\r
50 #include <irrlicht.h>
\r
53 That header just adds the getExampleMediaPath tool-functions to help locating
\r
54 the media we need. More about that later below.
\r
56 #include "exampleHelper.h"
\r
59 In the Irrlicht Engine, everything can be found in the namespace 'irr'. So if
\r
60 you want to use a class of the engine, you have to write irr:: before the name
\r
61 of the class. For example to use the IrrlichtDevice write: irr::IrrlichtDevice.
\r
62 To get rid of the irr:: in front of the name of every class, we tell the
\r
63 compiler that we use that namespace from now on, and we will not have to write
\r
65 Note that you never should do that in headers - otherwise you will pollute the
\r
66 namespace of every file including such a header. So in headers always write
\r
67 out the full names including all namespaces.
\r
69 using namespace irr;
\r
72 There are 5 sub namespaces in the Irrlicht Engine. Take a look at them, you can
\r
73 read a detailed description of them in the documentation by clicking on the top
\r
74 menu item 'Namespace List' or by using this link:
\r
75 http://irrlicht.sourceforge.net/docu/namespaces.html
\r
76 Like the irr namespace, we do not want these 5 sub namespaces now, to keep this
\r
77 example simple. Hence, we tell the compiler again that we do not want always to
\r
80 using namespace core;
\r
81 using namespace scene;
\r
82 using namespace video;
\r
84 using namespace gui;
\r
87 This is the main method. We can now use main() on every platform.
\r
92 The most important function of the engine is the createDevice()
\r
93 function. The IrrlichtDevice is created by it, which is the root
\r
94 object for doing anything with the engine. createDevice() has the
\r
95 following parameters:
\r
97 - driverType: Type of the video driver. This can currently be the Null-device,
\r
98 one of the two software renderers, D3D9, or OpenGL. In this
\r
99 example we use EDT_BURNINGSVIDEO, but to try out, you might want to
\r
100 change it to EDT_SOFTWARE, EDT_NULL, EDT_DIRECT3D9, or EDT_OPENGL.
\r
101 Generally you will want to use OpenGL or Direct3D as they are
\r
102 using your graphic card for calculations instead of the CPU and
\r
103 are way faster (and usually better looking). We just use one of the
\r
104 software renderers here as it even works when your graphic card driver
\r
105 isn't set up for 3d support.
\r
107 - windowSize: Size of the Window or screen in FullScreenMode to be
\r
108 created. In this example we use 640x480.
\r
110 - bits: Amount of color bits per pixel. This should be 16 or 32. The
\r
111 parameter is often ignored when running in windowed mode. More
\r
112 commonly you would chose 32 bit, again we're just playing it safe.
\r
114 - fullscreen: Specifies if we want the device to run in fullscreen mode
\r
117 - stencilbuffer: Specifies if we want to use the stencil buffer (you
\r
118 need it for drawing shadows).
\r
120 - vsync: Specifies if we want to have vsync enabled, this is only useful
\r
121 in fullscreen mode.
\r
123 - eventReceiver: An object to receive events. We do not want to use this
\r
124 parameter here, and set it to 0.
\r
126 Always check the return value to cope with unsupported drivers,
\r
129 IrrlichtDevice *device =
\r
130 createDevice( video::EDT_OGLES1, dimension2d<u32>(640, 480), 16,
\r
131 false, false, false, 0);
\r
137 Set the caption of the window to some nice text. Note that there is an
\r
138 'L' in front of the string. The Irrlicht Engine uses wide character
\r
139 strings when displaying text.
\r
141 device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");
\r
144 Get a pointer to the VideoDriver, the SceneManager and the graphical
\r
145 user interface environment, so that we do not always have to write
\r
146 device->getVideoDriver(), device->getSceneManager(), or
\r
147 device->getGUIEnvironment().
\r
149 IVideoDriver* driver = device->getVideoDriver();
\r
150 ISceneManager* smgr = device->getSceneManager();
\r
151 IGUIEnvironment* guienv = device->getGUIEnvironment();
\r
154 We add a hello world label to the window, using the GUI environment.
\r
155 The text is placed at the position (10,10) as top left corner and
\r
156 (260,22) as lower right corner.
\r
158 guienv->addStaticText(L"Hello World! This is Irrlicht with the burnings software renderer!",
\r
159 rect<s32>(10,10,260,22), true);
\r
162 Get a media path dedicated for your platform. Finding media files for your
\r
163 applications can be tricky. First you have 2 options - working with relative
\r
164 paths or working with absolute paths.
\r
166 On Windows a common solution is that your installer will write a key into
\r
167 the registry with the absolute path of wherever the user installed the
\r
168 media. And in your application you read out that key from the registry.
\r
169 On Linux a common solution is to use config file which is placed in some
\r
170 fixed location (for example in a . file/folder in the user home).
\r
172 But you can also work with relative paths - which is what we do here. There
\r
173 is a slight complication with relative paths as they are relative to your
\r
174 current working directory. And that depends on the way your application is
\r
175 started and it might change inside your application. But mostly it will be
\r
176 set to your executable on start so you can ignore that problem while
\r
179 When inside VisualStudio the current working directory is set to your
\r
180 project files location unless you overwrite Project properties - Debugging
\r
181 - Working Directory. In Irrlicht examples the media folder is on most
\r
182 platforms ../../media which works for the examples as it's relative to our
\r
183 project files as well as to the binary (.exe) files.
\r
185 Whatever you chose to find your base-folder for media - wrap it with some
\r
186 function and then you can improve the code to locate the media later on.
\r
188 const io::path mediaPath = getExampleMediaPath();
\r
191 To show something interesting, we load a Quake 2 model and display it.
\r
192 We get the Mesh from the Scene Manager with getMesh() and add a SceneNode
\r
193 to display the mesh with addAnimatedMeshSceneNode(). Check the return value
\r
194 of getMesh() to become aware of loading problems and other errors.
\r
196 Instead of writing the filename sydney.md2, it would also be possible
\r
197 to load a Maya object file (.obj), a complete Quake3 map (.bsp) or any
\r
198 other supported file format. By the way, that cool Quake 2 model
\r
199 called sydney was modeled by Brian Collins.
\r
201 IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "sydney.md2");
\r
207 IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
\r
210 To let the mesh look a little bit nicer, we change its material. We
\r
211 disable lighting because we do not have a dynamic light in here, and
\r
212 the mesh would be totally black otherwise. Then we set the frame loop,
\r
213 such that the predefined STAND animation is used. And last, we apply a
\r
214 texture to the mesh. Without it the mesh would be drawn using only a
\r
219 node->setMaterialFlag(EMF_LIGHTING, false);
\r
220 node->setMD2Animation(scene::EMAT_STAND);
\r
221 node->setMaterialTexture( 0, driver->getTexture(mediaPath + "sydney.bmp") );
\r
225 To look at the mesh, we place a camera into 3d space at the position
\r
226 (0, 30, -40). The camera looks from there to (0,5,0), which is
\r
227 approximately the place where our md2 model is.
\r
229 smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
\r
232 OK, now we have set up the scene, lets draw everything: We run the
\r
233 device in a while() loop, until the device does not want to run any
\r
234 more. This would be when the user closes the window or presses ALT+F4
\r
235 (or whatever keycode closes a window on your OS).
\r
237 while(device->run())
\r
240 Anything can be drawn between a beginScene() and an endScene()
\r
241 call. The beginScene() call clears the screen with a color and
\r
242 the depth buffer, if desired. Then we let the Scene Manager and
\r
243 the GUI Environment draw their content. With the endScene()
\r
244 call everything is presented on the screen.
\r
246 driver->beginScene(ECBF_COLOR | ECBF_DEPTH, SColor(255,100,101,140));
\r
251 driver->endScene();
\r
255 After we are done with the render loop, we have to delete the Irrlicht
\r
256 Device created before with createDevice(). In the Irrlicht Engine, you
\r
257 have to delete all objects you created with a method or function which
\r
258 starts with 'create'. The object is simply deleted by calling ->drop().
\r
259 See the documentation at irr::IReferenceCounted::drop() for more
\r
268 That's it. Compile and run.
\r