Sophie

Sophie

distrib > Mandriva > 2011.0 > i586 > by-pkgid > a16d689bc65aac5d987d5129109e6de5 > files > 805

irrlicht-doc-1.7.2-1.i586.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 6: 2D Graphics</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.6.2 -->
<div class="contents">


<h1><a class="anchor" id="example006">Tutorial 6: 2D Graphics </a></h1><div align="center">
<img src="006shot.jpg" alt="006shot.jpg"/>
</div>
 <p>This Tutorial shows how to do 2d graphics with the Irrlicht Engine. It shows how to draw images, keycolor based sprites, transparent rectangles, and different fonts. You may consider this useful if you want to make a 2d game with the engine, or if you want to draw a cool interface or head up display for your 3d game.</p>
<p>As always, I include the header files, use the irr namespace, and tell the linker to link with the .lib file. </p>
<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>
<span class="preprocessor">#include &quot;<a class="code" href="driver_choice_8h.html">driverChoice.h</a>&quot;</span>

<span class="keyword">using namespace </span>irr;

<span class="preprocessor">#ifdef _MSC_VER</span>
<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, &quot;Irrlicht.lib&quot;)</span>
<span class="preprocessor">#endif</span>
</pre></div><p>At first, we let the user select the driver type, then start up the engine, set a caption, and get a pointer to the video driver. </p>
<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
{
        <span class="comment">// ask user for driver</span>
        <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType=driverChoiceConsole();
        <span class="keywordflow">if</span> (driverType==<a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0ae685cada50f8c100403134d932d0414c" title="No driver, just for counting the elements.">video::EDT_COUNT</a>)
                <span class="keywordflow">return</span> 1;

        <span class="comment">// create device</span>

        IrrlichtDevice *device = <a class="code" href="namespaceirr.html#abaf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the...">createDevice</a>(driverType,
                core::dimension2d&lt;u32&gt;(512, 384));

        <span class="keywordflow">if</span> (device == 0)
                <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>

        device-&gt;setWindowCaption(L<span class="stringliteral">&quot;Irrlicht Engine - 2D Graphics Demo&quot;</span>);

        video::IVideoDriver* driver = device-&gt;getVideoDriver();
</pre></div><p>All 2d graphics in this example are put together into one texture, 2ddemo.png. Because we want to draw colorkey based sprites, we need to load this texture and tell the engine, which part of it should be transparent based on a colorkey.</p>
<p>In this example, we don't tell it the color directly, we just say "Hey Irrlicht Engine, you'll find the color I want at position (0,0) on the texture.". Instead, it would be also possible to call driver-&gt;makeColorKeyTexture(images, video::SColor(0,0,0,0)), to make e.g. all black pixels transparent. Please note that makeColorKeyTexture just creates an alpha channel based on the color. </p>
<div class="fragment"><pre class="fragment">        video::ITexture* images = driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/2ddemo.png&quot;</span>);
        driver-&gt;makeColorKeyTexture(images, core::position2d&lt;s32&gt;(0,0));
</pre></div><p>To be able to draw some text with two different fonts, we first load them. Ok, we load just one. As the first font we just use the default font which is built into the engine. Also, we define two rectangles which specify the position of the images of the red imps (little flying creatures) in the texture. </p>
<div class="fragment"><pre class="fragment">        gui::IGUIFont* font = device-&gt;getGUIEnvironment()-&gt;getBuiltInFont();
        gui::IGUIFont* font2 =
                device-&gt;getGUIEnvironment()-&gt;getFont(<span class="stringliteral">&quot;../../media/fonthaettenschweiler.bmp&quot;</span>);

        core::rect&lt;s32&gt; imp1(349,15,385,78);
        core::rect&lt;s32&gt; imp2(387,15,423,78);
</pre></div><p>Prepare a nicely filtering 2d render mode for special cases. </p>
<div class="fragment"><pre class="fragment">        driver-&gt;getMaterial2D().TextureLayer[0].BilinearFilter=<span class="keyword">true</span>;
        driver-&gt;getMaterial2D().AntiAliasing=<a class="code" href="namespaceirr_1_1video.html#aa8647c2a52bdd3bc15ee773e8f2b149dade3c9a3d46cbedc5304cfa869ab593fd" title="All typical anti-alias and smooth modes.">video::EAAM_FULL_BASIC</a>;
</pre></div><p>Everything is prepared, now we can draw everything in the draw loop, between the begin scene and end scene calls. In this example, we are just doing 2d graphics, but it would be no problem to mix them with 3d graphics. Just try it out, and draw some 3d vertices or set up a scene with the scene manager and draw it. </p>
<div class="fragment"><pre class="fragment">        <span class="keywordflow">while</span>(device-&gt;run() &amp;&amp; driver)
        {
                <span class="keywordflow">if</span> (device-&gt;isWindowActive())
                {
                        <a class="code" href="namespaceirr.html#a0416a53257075833e7002efd0a18e804" title="32 bit unsigned variable.">u32</a> time = device-&gt;getTimer()-&gt;getTime();

                        driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, video::SColor(255,120,102,136));
</pre></div><p>First, we draw 3 sprites, using the alpha channel we created with makeColorKeyTexture. The last parameter specifies that the drawing method should use this alpha channel. The last-but-one parameter specifies a color, with which the sprite should be colored. (255,255,255,255) is full white, so the sprite will look like the original. The third sprite is drawn with the red channel modulated based on the time. </p>
<div class="fragment"><pre class="fragment">                        <span class="comment">// draw fire &amp; dragons background world</span>
                        driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(50,50),
                                core::rect&lt;s32&gt;(0,0,342,224), 0,
                                video::SColor(255,255,255,255), <span class="keyword">true</span>);

                        <span class="comment">// draw flying imp</span>
                        driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(164,125),
                                (time/500 % 2) ? imp1 : imp2, 0,
                                video::SColor(255,255,255,255), <span class="keyword">true</span>);

                        <span class="comment">// draw second flying imp with colorcylce</span>
                        driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(270,105),
                                (time/500 % 2) ? imp1 : imp2, 0,
                                video::SColor(255,(time) % 255,255,255), <span class="keyword">true</span>);
</pre></div><p>Drawing text is really simple. The code should be self explanatory. </p>
<div class="fragment"><pre class="fragment">                        <span class="comment">// draw some text</span>
                        <span class="keywordflow">if</span> (font)
                                font-&gt;draw(L<span class="stringliteral">&quot;This demo shows that Irrlicht is also capable of drawing 2D graphics.&quot;</span>,
                                        core::rect&lt;s32&gt;(130,10,300,50),
                                        video::SColor(255,255,255,255));

                        <span class="comment">// draw some other text</span>
                        <span class="keywordflow">if</span> (font2)
                                font2-&gt;draw(L<span class="stringliteral">&quot;Also mixing with 3d graphics is possible.&quot;</span>,
                                        core::rect&lt;s32&gt;(130,20,300,60),
                                        video::SColor(255,time % 255,time % 255,255));
</pre></div><p>Next, we draw the Irrlicht Engine logo (without using a color or an alpha channel). Since we slightly scale the image we use the prepared filter mode. </p>
<div class="fragment"><pre class="fragment">                        driver-&gt;enableMaterial2D();
                        driver-&gt;draw2DImage(images, core::rect&lt;s32&gt;(10,10,108,48),
                                core::rect&lt;s32&gt;(354,87,442,118));
                        driver-&gt;enableMaterial2D(<span class="keyword">false</span>);
</pre></div><p>Finally draw a half-transparent rect under the mouse cursor. </p>
<div class="fragment"><pre class="fragment">                        core::position2d&lt;s32&gt; m = device-&gt;getCursorControl()-&gt;getPosition();
                        driver-&gt;draw2DRectangle(video::SColor(100,255,255,255),
                                core::rect&lt;s32&gt;(m.X-20, m.Y-20, m.X+20, m.Y+20));

                        driver-&gt;endScene();
                }
        }

        device-&gt;drop();

        <span class="keywordflow">return</span> 0;
}
</pre></div><p>That's all. I hope it was not too difficult. </p>
</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-2010 by Nikolaus Gebhardt. Generated 
        on Sun Oct 24 12:41:59 2010 by <a href="http://www.doxygen.org" target="_blank">Doxygen</a> 
        (1.6.2)</font></em></small></div></td>
  </tr>
</table>
<address style="align: right;">
</address>
</body>
</html>