Sophie

Sophie

distrib > Mandriva > 2010.1 > x86_64 > media > contrib-release > by-pkgid > 58828b263d8f56d90ac336dea07a4586 > files > 732

irrlicht-doc-1.6.1-1mdv2010.1.x86_64.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1">
<title>Irrlicht Engine: Tutorial 1: HelloWorld</title>
<link href="doxygen.css" rel="stylesheet" type="text/css">
</head><body>
<table class="irrlicht" >
  <tr valign="middle"> 
    <td><font size="2"><a class="qindex" href="index.html"><font color="#FFFFFF">Home</font></a> 
      | <a class="qindex" href="namespaces.html"><font color="#FFFFFF">Namespaces</font></a> 
      | <a class="qindex" href="hierarchy.html"><font color="#FFFFFF">Hierarchy</font></a> 
      | <a class="qindex" href="classes.html"><font color="#FFFFFF">Alphabetical 
      List</font></a> | <a class="qindex" href="annotated.html"><font color="#FFFFFF"> 
      Class list</font></a> | <a class="qindex" href="files.html"><font color="#FFFFFF">Files</font></a> 
      | <a class="qindex" href="namespacemembers.html"><font color="#FFFFFF"> 
      Namespace&nbsp;Members</font></a> | <a class="qindex" href="functions.html"><font color="#FFFFFF">Class 
      members</font></a> | <a class="qindex" href="globals.html"><font color="#FFFFFF">File 
      members</font></a> | <a class="qindex" href="pages.html"><font color="#FFFFFF">Tutorials</font></a></font> </td>
  </tr>
</table>
<!-- Generated by Doxygen 1.5.6 -->
<div class="contents">
<h1><a class="anchor" name="example001">Tutorial 1: HelloWorld </a></h1><div align="center">
<img src="001shot.jpg" alt="001shot.jpg">
</div>
 <p>
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. Microsoft Visual Studio is used as an IDE, but you will also be able to understand everything if you are using a different one or even another operating system than windows.<p>
You have to include the header file &lt;<a class="el" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt; in order to use the engine. The header file can be found in the Irrlicht Engine SDK directory <code>include</code>. To let the compiler find this header file, the directory where it is located has to be specified. This is different for every IDE and compiler you use. Let's explain shortly how to do this in Microsoft Visual Studio:<p>
<ul>
<li>If you use Version 6.0, select the Menu Extras -&gt; Options. Select the directories tab, and select the 'Include' Item in the combo box. Add the <code>include</code> directory of the irrlicht engine folder to the list of directories. Now the compiler will find the <a class="el" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">Irrlicht.h</a> header file. We also need the irrlicht.lib to be found, so stay in that dialog, select 'Libraries' in the combo box and add the <code>lib/VisualStudio</code> directory. <div align="center">
<img src="vc6optionsdir.jpg" alt="vc6optionsdir.jpg">
</div>
  <div align="center">
<img src="vc6include.jpg" alt="vc6include.jpg">
</div>
 </li></ul>
<p>
<ul>
<li>If your IDE is Visual Studio .NET, select Tools -&gt; Options. Select the projects entry and then select VC++ directories. Select 'show directories for include files' in the combo box, and add the <code>include</code> directory of the irrlicht engine folder to the list of directories. Now the compiler will find the <a class="el" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">Irrlicht.h</a> header file. We also need the irrlicht.lib to be found, so stay in that dialog, select 'show directories for Library files' and add the <code>lib/VisualStudio</code> directory. <div align="center">
<img src="vcnetinclude.jpg" alt="vcnetinclude.jpg">
</div>
 </li></ul>
<p>
That's it. With your IDE set up like this, you will now be able to develop applications with the Irrlicht Engine.<p>
Lets start!<p>
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 in our code. <div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
</pre></div><p>
In the Irrlicht Engine, everything can be found in the namespace 'irr'. So if you want to use a class of the engine, you have to write <a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a>:: before the name of the class. For example to use the IrrlichtDevice write: <a class="el" href="classirr_1_1_irrlicht_device.html" title="The Irrlicht device. You can create it with createDevice() or createDeviceEx().">irr::IrrlichtDevice</a>. To get rid of the <a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a>:: in front of the name of every class, we tell the compiler that we use that namespace from now on, and we will not have to write <a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a>:: anymore. <div class="fragment"><pre class="fragment"><span class="keyword">using namespace </span>irr;
</pre></div><p>
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' or by using this link: <a href="http://irrlicht.sourceforge.net/docu/namespaces.html">http://irrlicht.sourceforge.net/docu/namespaces.html</a> Like the <a class="el" href="namespaceirr.html" title="Everything in the Irrlicht Engine can be found in this namespace.">irr</a> namespace, we do not want these 5 sub namespaces now, to keep this example simple. Hence, we tell the compiler again that we do not want always to write their names. <div class="fragment"><pre class="fragment"><span class="keyword">using namespace </span>core;
<span class="keyword">using namespace </span>scene;
<span class="keyword">using namespace </span>video;
<span class="keyword">using namespace </span>io;
<span class="keyword">using namespace </span>gui;
</pre></div><p>
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 lib for VisualStudio. On Windows platforms, we have to get rid of the console window, which pops up when starting a program with main(). This is done by the second pragma. We could also use the WinMain method, though losing platform independence then. <div class="fragment"><pre class="fragment"><span class="preprocessor">#ifdef _IRR_WINDOWS_</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")</span>
<span class="preprocessor">#endif</span>
</pre></div><p>
This is the main method. We can now use main() on every platform. <div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
{
</pre></div><p>
The most important function of the engine is the <a class="el" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice()</a> function. The IrrlichtDevice is created by it, which is the root object for doing anything with the engine. <a class="el" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice()</a> has 7 parameters:<p>
<ul>
<li>deviceType: Type of the device. This can currently be the Null-device, one of the two software renderers, D3D8, D3D9, or OpenGL. In this example we use EDT_SOFTWARE, but to try out, you might want to change it to EDT_BURNINGSVIDEO, EDT_NULL, EDT_DIRECT3D8, EDT_DIRECT3D9, or EDT_OPENGL.</li></ul>
<p>
<ul>
<li>windowSize: Size of the Window or screen in FullScreenMode to be created. In this example we use 640x480.</li></ul>
<p>
<ul>
<li>bits: Amount of color bits per pixel. This should be 16 or 32. The parameter is often ignored when running in windowed mode.</li></ul>
<p>
<ul>
<li>fullscreen: Specifies if we want the device to run in fullscreen mode or not.</li></ul>
<p>
<ul>
<li>stencilbuffer: Specifies if we want to use the stencil buffer (for drawing shadows).</li></ul>
<p>
<ul>
<li>vsync: Specifies if we want to have vsync enabled, this is only useful in fullscreen mode.</li></ul>
<p>
<ul>
<li>eventReceiver: An object to receive events. We do not want to use this parameter here, and set it to 0.</li></ul>
<p>
Always check the return value to cope with unsupported drivers, dimensions, etc. <div class="fragment"><pre class="fragment">        IrrlichtDevice *device =
                <a class="code" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice</a>( <a class="code" href="namespaceirr_1_1video.html#e35a6de6d436c76107ad157fe42356d01598cd235a1a6bd052e2011b559e8995" title="The Irrlicht Engine Software renderer.">video::EDT_SOFTWARE</a>, dimension2d&lt;u32&gt;(640, 480), 16,
                        <span class="keyword">false</span>, <span class="keyword">false</span>, <span class="keyword">false</span>, 0);

        <span class="keywordflow">if</span> (!device)
                <span class="keywordflow">return</span> 1;
</pre></div><p>
Set the caption of the window to some nice text. Note that there is an 'L' in front of the string. The Irrlicht Engine uses wide character strings when displaying text. <div class="fragment"><pre class="fragment">        device-&gt;setWindowCaption(L<span class="stringliteral">"Hello World! - Irrlicht Engine Demo"</span>);
</pre></div><p>
Get a pointer to the VideoDriver, the SceneManager and the graphical user interface environment, so that we do not always have to write device-&gt;getVideoDriver(), device-&gt;getSceneManager(), or device-&gt;getGUIEnvironment(). <div class="fragment"><pre class="fragment">        IVideoDriver* driver = device-&gt;getVideoDriver();
        ISceneManager* smgr = device-&gt;getSceneManager();
        IGUIEnvironment* guienv = device-&gt;getGUIEnvironment();
</pre></div><p>
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 (260,22) as lower right corner. <div class="fragment"><pre class="fragment">        guienv-&gt;addStaticText(L<span class="stringliteral">"Hello World! This is the Irrlicht Software renderer!"</span>,
                rect&lt;s32&gt;(10,10,260,22), <span class="keyword">true</span>);
</pre></div><p>
To show 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(). We check the return value of getMesh() to become aware of loading problems and other errors.<p>
Instead of writing the filename sydney.md2, it would also be possible to load a Maya object file (.obj), a complete Quake3 map (.bsp) or any other supported file format. By the way, that cool Quake 2 model called sydney was modelled by Brian Collins. <div class="fragment"><pre class="fragment">        IAnimatedMesh* mesh = smgr-&gt;getMesh(<span class="stringliteral">"../../media/sydney.md2"</span>);
        <span class="keywordflow">if</span> (!mesh)
        {
                device-&gt;drop();
                <span class="keywordflow">return</span> 1;
        }
        IAnimatedMeshSceneNode* node = smgr-&gt;addAnimatedMeshSceneNode( mesh );
</pre></div><p>
To let the mesh look a little bit nicer, we change its material. We disable lighting because we do not have a dynamic light in here, and the mesh would be totally black otherwise. Then we set the frame loop, such that the predefined STAND animation is used. And last, we apply a texture to the mesh. Without it the mesh would be drawn using only a color. <div class="fragment"><pre class="fragment">        <span class="keywordflow">if</span> (node)
        {
                node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#8a3bc00ae8137535b9fbc5f40add70d3cea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">EMF_LIGHTING</a>, <span class="keyword">false</span>);
                node-&gt;setMD2Animation(<a class="code" href="namespaceirr_1_1scene.html#08d4a84966e1d2886d0d57e4acbb4f1935893ae58423c41dace23ad13e262e2c">scene::EMAT_STAND</a>);
                node-&gt;setMaterialTexture( 0, driver-&gt;getTexture(<span class="stringliteral">"../../media/sydney.bmp"</span>) );
        }
</pre></div><p>
To look at the mesh, we place a camera into 3d space at the position (0, 30, -40). The camera looks from there to (0,5,0), which is approximately the place where our md2 model is. <div class="fragment"><pre class="fragment">        smgr-&gt;addCameraSceneNode(0, <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">vector3df</a>(0,30,-40), <a class="code" href="namespaceirr_1_1core.html#06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">vector3df</a>(0,5,0));
</pre></div><p>
Ok, now we have set up the scene, lets 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 (or whatever keycode closes a window). <div class="fragment"><pre class="fragment">        <span class="keywordflow">while</span>(device-&gt;run())
        {
</pre></div><p>
Anything can be drawn between a beginScene() and an endScene() call. The beginScene() call clears the screen with a color and 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. <div class="fragment"><pre class="fragment">                driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, SColor(255,100,101,140));

                smgr-&gt;drawAll();
                guienv-&gt;drawAll();

                driver-&gt;endScene();
        }
</pre></div><p>
After we are done with the render loop, we have to delete the Irrlicht Device created before with <a class="el" href="namespaceirr.html#baf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice()</a>. In the Irrlicht Engine, you have to delete all objects you created with a method or function which starts with 'create'. The object is simply deleted by calling -&gt;drop(). See the documentation at <a class="el" href="classirr_1_1_i_reference_counted.html#fb169a857e0d2cdb96b8821cb9bff17a" title="Drops the object. Decrements the reference counter by one.">irr::IReferenceCounted::drop()</a> for more information. <div class="fragment"><pre class="fragment">        device-&gt;drop();

        <span class="keywordflow">return</span> 0;
}
</pre></div><p>
That's it. Compile and run. </div>
<hr size="1">
<address style="align: right;">
<small> </small>
</address>
<table width="100%" border="0" cellspacing="0" cellpadding="2">
  <tr> 
    <td width="0"> <div align="left"><small><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="irrlicht.png" alt="The Irrlicht Engine" align="middle" border=0 width=88 height=31></a></small></div></td>
    <td> <div align="left"><small><em><font size="2">The <a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht 
        Engine</a> Documentation &copy; 2003-2009 by Nikolaus Gebhardt. Generated 
        on Sun Jan 10 09:24:06 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> 
        (1.5.6)</font></em></small></div></td>
  </tr>
</table>
<address style="align: right;">
</address>
</body>
</html>