]> git.lizzy.rs Git - irrlicht.git/blob - examples/01.HelloWorld/main.cpp
Merging r6173 through r6179 from trunk to ogl-es branch
[irrlicht.git] / examples / 01.HelloWorld / main.cpp
1 /** Example 001 HelloWorld\r
2 \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
9 \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
15 \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
26 \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
30 \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
35 */\r
36 #ifdef _MSC_VER\r
37 #pragma comment(lib, "Irrlicht.lib")\r
38 #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")\r
39 #endif\r
40 \r
41 /*\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
44 \r
45 Lets start!\r
46 \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
49 */\r
50 #include <irrlicht.h>\r
51 \r
52 /*\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
55 */\r
56 #include "exampleHelper.h"\r
57 \r
58 /*\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
64 irr:: anymore.\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
68 */\r
69 using namespace irr;\r
70 \r
71 /*\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
78 write their names.\r
79 */\r
80 using namespace core;\r
81 using namespace scene;\r
82 using namespace video;\r
83 using namespace io;\r
84 using namespace gui;\r
85 \r
86 /*\r
87 This is the main method. We can now use main() on every platform.\r
88 */\r
89 int main()\r
90 {\r
91         /*\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
96 \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
106 \r
107         - windowSize: Size of the Window or screen in FullScreenMode to be\r
108            created. In this example we use 640x480.\r
109 \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
113 \r
114         - fullscreen: Specifies if we want the device to run in fullscreen mode\r
115            or windowed.\r
116 \r
117         - stencilbuffer: Specifies if we want to use the stencil buffer (you\r
118                 need it for drawing shadows).\r
119 \r
120         - vsync: Specifies if we want to have vsync enabled, this is only useful\r
121            in fullscreen mode.\r
122 \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
125 \r
126         Always check the return value to cope with unsupported drivers,\r
127         dimensions, etc.\r
128         */\r
129         IrrlichtDevice *device =\r
130                 createDevice( video::EDT_OGLES1, dimension2d<u32>(640, 480), 16,\r
131                         false, false, false, 0);\r
132 \r
133         if (!device)\r
134                 return 1;\r
135 \r
136         /*\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
140         */\r
141         device->setWindowCaption(L"Hello World! - Irrlicht Engine Demo");\r
142 \r
143         /*\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
148         */\r
149         IVideoDriver* driver = device->getVideoDriver();\r
150         ISceneManager* smgr = device->getSceneManager();\r
151         IGUIEnvironment* guienv = device->getGUIEnvironment();\r
152 \r
153         /*\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
157         */\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
160 \r
161         /*\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
165 \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
171 \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
177         developing.\r
178 \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
184 \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
187         */\r
188         const io::path mediaPath = getExampleMediaPath();\r
189 \r
190         /*\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
195 \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
200         */\r
201         IAnimatedMesh* mesh = smgr->getMesh(mediaPath + "sydney.md2");\r
202         if (!mesh)\r
203         {\r
204                 device->drop();\r
205                 return 1;\r
206         }\r
207         IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );\r
208 \r
209         /*\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
215         color.\r
216         */\r
217         if (node)\r
218         {\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
222         }\r
223 \r
224         /*\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
228         */\r
229         smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));\r
230 \r
231         /*\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
236         */\r
237         while(device->run())\r
238         {\r
239                 /*\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
245                 */\r
246                 driver->beginScene(ECBF_COLOR | ECBF_DEPTH, SColor(255,100,101,140));\r
247 \r
248                 smgr->drawAll();\r
249                 guienv->drawAll();\r
250 \r
251                 driver->endScene();\r
252         }\r
253 \r
254         /*\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
260         information.\r
261         */\r
262         device->drop();\r
263 \r
264         return 0;\r
265 }\r
266 \r
267 /*\r
268 That's it. Compile and run.\r
269 **/\r