tutorial.html 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <html>
  2. <head>
  3. <title>Irrlicht Engine Tutorial</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. </head>
  6. <body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">
  7. <br>
  8. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  9. <tr>
  10. <td bgcolor="#666699" width="10"><b><a href="http://irrlicht.sourceforge.net" target="_blank"><img src="../../media/irrlichtlogo.jpg" width="88" height="31" border="0"></a></b></td>
  11. <td bgcolor="#666699" width="100%">
  12. <div align="center">
  13. <div align="center"></div>
  14. <div align="left"><b><font color="#FFFFFF">Tutorial 6. 2D Graphics</font></b></div>
  15. </div>
  16. </td>
  17. </tr>
  18. <tr bgcolor="#eeeeff">
  19. <td height="90" colspan="2">
  20. <div align="left">
  21. <p>This Tutorial shows how to do 2d graphics with the Irrlicht Engine.
  22. It shows how to draw images, keycolor based sprites, transparent rectangles
  23. and different fonts. You will may consider this useful if you want to
  24. make a 2d game with the engine, or if you want to draw a cool interface
  25. or head up display for your 3d game.</p>
  26. <p>The program which is described here will look like this:</p>
  27. <p align="center"><img src="../../media/006shot.jpg" width="259" height="204"><br>
  28. </p>
  29. </div>
  30. </td>
  31. </tr>
  32. </table>
  33. <br>
  34. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  35. <tr>
  36. <td bgcolor="#666699"> <div align="center"><b><font color="#FFFFFF"></font></b></div>
  37. <b><font color="#FFFFFF">Lets start!</font></b></td>
  38. </tr>
  39. <tr>
  40. <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
  41. <p>As always, I include the header files, use the irr namespace, and tell
  42. the linker to link with the .lib file. </p>
  43. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  44. <tr>
  45. <td> <pre>#include &lt;irrlicht.h&gt;<br>#include &lt;iostream&gt;<br><br>using namespace irr;</pre>
  46. <pre>#pragma comment(lib, &quot;Irrlicht.lib&quot;)
  47. </pre></td>
  48. </tr>
  49. </table>
  50. <p>At first, we let the user select the driver type, then start up the
  51. engine, set a caption, and get a pointer to the video driver.</p>
  52. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  53. <tr>
  54. <td> <pre>int main()<br>{<br> // let user select driver type<br> video::E_DRIVER_TYPE driverType;<br><br> printf(&quot;Please select the driver you want for this example:\n&quot;\<br> &quot; (a) Direct3D 9.0c\n (b) Direct3D 8.1\n (c) OpenGL 1.5\n&quot;\<br> &quot; (d) Software Renderer\n (e) Apfelbaum Software Renderer\n&quot;\<br> &quot; (f) NullDevice\n (otherKey) exit\n\n&quot;);<br><br> char i;<br> std::cin &gt;&gt; i;<br><br> switch(i)<br> {<br> case 'a': driverType = video::EDT_DIRECT3D9;break;<br> case 'b': driverType = video::EDT_DIRECT3D8;break;<br> case 'c': driverType = video::EDT_OPENGL; break;<br> case 'd': driverType = video::EDT_SOFTWARE; break;<br> case 'e': driverType = video::EDT_BURNINGSVIDEO;break;<br> case 'f': driverType = video::EDT_NULL; break;<br> default: return 0;<br> } <br><br> // create device</pre>
  55. <pre> IrrlichtDevice *device = createDevice(driverType,
  56. core::dimension2d&lt;s32&gt;(512, 384));</pre>
  57. <pre> if (device == 0)
  58. return 1;
  59. <br> device-&gt;setWindowCaption(L&quot;Irrlicht Engine - 2D Graphics Demo&quot;);</pre>
  60. <pre> video::IVideoDriver* driver = device-&gt;getVideoDriver();</pre></td>
  61. </tr>
  62. </table>
  63. <p> All 2d graphics in this example are put together into one texture,
  64. 2ddemo.bmp. Because we want to draw colorkey based sprites, we need
  65. to load this texture and tell the engine, which part of it should be
  66. transparent based on a colorkey. In this example, we don't tell it the
  67. color directly, we just say &quot;Hey Irrlicht Engine, you'll find the
  68. color I want at position (0,0) on the texture.&quot;. Instead, it would
  69. be also possible to call <font face="Courier New, Courier, mono">driver-&gt;makeColorKeyTexture(images,
  70. video::SColor(0,0,0,0))</font>, to make e.g. all black pixels transparent.
  71. Please note, that makeColorKeyTexture just creates an alpha channel
  72. based on the color. </p>
  73. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  74. <tr>
  75. <td> <pre>video::ITexture* images = driver-&gt;getTexture(&quot;../../media/2ddemo.bmp&quot;);<br>driver-&gt;makeColorKeyTexture(images, core::position2d&lt;s32&gt;(0,0));</pre></td>
  76. </tr>
  77. </table>
  78. <p>To be able to draw some text with two different fonts, we load them.
  79. Ok, we load just one, as first font we just use the default font which
  80. is built into the engine.<br>
  81. Also, we define two rectangles, which specify the position of the images
  82. of the red imps (little flying creatures) in the texture.</p>
  83. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  84. <tr>
  85. <td> <pre>gui::IGUIFont* font = device-&gt;getGUIEnvironment()-&gt;getBuiltInFont();<br>gui::IGUIFont* font2 = device-&gt;getGUIEnvironment()-&gt;getFont(
  86. &quot;../../media/fonthaettenschweiler.bmp&quot;);</pre>
  87. <pre>core::rect&lt;s32&gt; imp1(349,15,385,78);
  88. core::rect&lt;s32&gt; imp2(387,15,423,78);</pre></td>
  89. </tr>
  90. </table>
  91. <p>Everything is prepared, now we can draw everything in the draw loop,
  92. between the begin scene and end scene calls. In this example, we are
  93. just doing 2d graphics, but it would be no problem to mix them with
  94. 3d graphics. Just try it out, and draw some 3d vertices or set up a
  95. scene with the scene manager and draw it.</p>
  96. </div>
  97. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  98. <tr>
  99. <td> <pre>while(device-&gt;run() &amp;&amp; driver)<br>{<br> if (device-&gt;isWindowActive())<br> {<br> u32 time = device-&gt;getTimer()-&gt;getTime();<br> driver-&gt;beginScene(true, true, video::SColor(0,120,102,136));
  100. </pre></td>
  101. </tr>
  102. </table>
  103. <p> First, we draw 3 sprites, using the alpha channel we created with makeColorKeyTexture.
  104. The last parameter specifiys that the drawing method should use thiw alpha
  105. channel. The parameter before the last one specifies a color, with wich
  106. the sprite should be colored. (255,255,255,255) is full white, so the
  107. sprite will look like the original. The third sprite is drawed colored
  108. based on the time. </p>
  109. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  110. <tr>
  111. <td><pre>// draw fire &amp; dragons background world<br>driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(50,50),<br> core::rect&lt;s32&gt;(0,0,342,224), 0, <br> video::SColor(255,255,255,255), true);</pre>
  112. <pre>// draw flying imp
  113. driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(164,125),
  114. (time/500 % 2) ? imp1 : imp2, 0,
  115. video::SColor(255,255,255,255), true);</pre>
  116. <pre>// draw second flying imp with colorcylce
  117. driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(270,105),
  118. (time/500 % 2) ? imp1 : imp2, 0,
  119. video::SColor(255,(time) % 255,255,255), true);</pre></td>
  120. </tr>
  121. </table>
  122. <p> Drawing text is really simple. The code should be self explanatory.</p>
  123. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  124. <tr>
  125. <td><pre>// draw some text<br>if (font)<br> font-&gt;draw(L&quot;This is some text.&quot;,<br> core::rect&lt;s32&gt;(130,10,300,50),<br> video::SColor(255,255,255,255));</pre>
  126. <pre>// draw some other text
  127. if (font2)
  128. font2-&gt;draw(L&quot;This is some other text.&quot;,
  129. core::rect&lt;s32&gt;(130,20,300,60),
  130. video::SColor(255,time % 255,time % 255,255));</pre></td>
  131. </tr>
  132. </table>
  133. <p>At last, we draw the Irrlicht Engine logo (without using a color or an
  134. alpha channel) and a transparent 2d Rectangle at the position of the mouse
  135. cursor.</p>
  136. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  137. <tr>
  138. <td> <pre> // draw logo<br> driver-&gt;draw2DImage(images, core::position2d&lt;s32&gt;(10,10),<br> core::rect&lt;s32&gt;(354,87,442,118));</pre>
  139. <pre> // draw transparent rect under cursor
  140. core::position2d&lt;s32&gt; m = device-&gt;getCursorControl()-&gt;getPosition();
  141. driver-&gt;draw2DRectangle(video::SColor(100,255,255,255),
  142. core::rect&lt;s32&gt;(m.X-20, m.Y-20, m.X+20, m.Y+20));</pre>
  143. <pre> driver-&gt;endScene();
  144. }
  145. }</pre></td>
  146. </tr>
  147. </table>
  148. <p>That's all, it was not really difficult, I hope.</p>
  149. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  150. <tr>
  151. <td> <pre> device-&gt;drop();
  152. return 0;
  153. }</pre>
  154. </td>
  155. </tr>
  156. </table>
  157. <p>&nbsp;</p></td>
  158. </tr>
  159. </table>
  160. <p>&nbsp;</p>
  161. </body>
  162. </html>