tutorial.html 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 5.User Interface</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 use the built in User Interface of the Irrlicht
  22. Engine. It will give a brief overview and show how to create and use
  23. windows, buttons, scroll bars, static texts and list boxes. </p>
  24. <p>The program which is described here will look like this:</p>
  25. <p align="center"><img src="../../media/005shot.jpg" width="259" height="204"><br>
  26. </p>
  27. </div>
  28. </td>
  29. </tr>
  30. </table>
  31. <br>
  32. <table width="95%" border="0" cellspacing="0" cellpadding="2" align="center">
  33. <tr>
  34. <td bgcolor="#666699"> <div align="center"><b><font color="#FFFFFF"></font></b></div>
  35. <b><font color="#FFFFFF">Lets start!</font></b></td>
  36. </tr>
  37. <tr>
  38. <td height="90" bgcolor="#eeeeff" valign="top"> <div align="left">
  39. <p>As always, we include the header files (conio and curses for getting
  40. user input from the console), and use the irrlicht namespaces. We also
  41. store a pointer to the Irrlicht device, a counter variable for changing
  42. the creation position of a window, and a pointer to a listbox.</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;
  46. #include &lt;iostream&gt;<br>
  47. using namespace irr;</pre>
  48. <pre>using namespace core;
  49. using namespace scene;
  50. using namespace video;
  51. using namespace io;
  52. using namespace gui;</pre>
  53. <pre>#pragma comment(lib, &quot;Irrlicht.lib&quot;)</pre>
  54. <pre>IrrlichtDevice *device = 0;
  55. s32 cnt = 0;
  56. IGUIListBox* listbox = 0;
  57. </pre></td>
  58. </tr>
  59. </table>
  60. <p>The Event Receiver is not only capable of getting keyboard and mouse
  61. input events, but also events of the graphical user interface (gui).
  62. There are events for almost everything: Button click, Listbox selection
  63. change, events that say that a element was hovered and so on. To be
  64. able to react to some of these events, we create <br>
  65. an event receiver. We only react to gui events, and if it's such an
  66. event, we get the id of the caller (the gui element which caused the
  67. event) and get the pointer to the gui environment. </p>
  68. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  69. <tr>
  70. <td> <pre>class MyEventReceiver : public IEventReceiver<br>{<br>public:<br> virtual bool OnEvent(const SEvent&amp; event)<br> {<br> if (event.EventType == EET_GUI_EVENT)<br> {<br> s32 id = event.GUIEvent.Caller-&gt;getID();<br> IGUIEnvironment* env = device-&gt;getGUIEnvironment();</pre>
  71. <pre> switch(event.GUIEvent.EventType)
  72. {</pre>
  73. </td>
  74. </tr>
  75. </table>
  76. <p> If a scrollbar changed its scroll position, and it is 'our' scrollbar
  77. (the one with id 104), then we change the <br>
  78. transparency of all gui elements. This is a very easy task: There is
  79. a skin object, in which all color settings are stored. We simply go
  80. through all colors stored in the skin and change their alpha value.
  81. </p>
  82. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  83. <tr>
  84. <td height="80"> <pre>case EGET_SCROLL_BAR_CHANGED:<br> if (id == 104)<br> {<br> s32 pos = ((IGUIScrollBar*)event.GUIEvent.Caller)-&gt;getPos();<br> <br> for (s32 i=0; i&lt;EGDC_COUNT ; ++i)<br> {<br> SColor col = env-&gt;getSkin()-&gt;getColor((EGUI_DEFAULT_COLOR)i);<br> col.setAlpha(pos);<br> env-&gt;getSkin()-&gt;setColor((EGUI_DEFAULT_COLOR)i, col);<br> }<br> }<br>break;</pre></td>
  85. </tr>
  86. </table>
  87. <p>If a button was clicked, it could be one of 'our' three buttons. If
  88. it is the first, we shut down the engine.<br>
  89. If it is the second, we create a little window with some text on it.
  90. We also add a string to the list box to log<br>
  91. what happened. And if it is the third button, we create a file open
  92. dialog, and add also this as string to the list box.<br>
  93. That's all for the event receiver.</p>
  94. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  95. <tr>
  96. <td>
  97. <pre> case EGET_BUTTON_CLICKED:
  98. if (id == 101)
  99. {
  100. device-&gt;closeDevice();
  101. return true;
  102. }</pre>
  103. <pre> if (id == 102)
  104. {
  105. listbox-&gt;addItem(L&quot;Window created&quot;);
  106. cnt += 30;
  107. if (cnt &gt; 200)
  108. cnt = 0;</pre>
  109. <pre> IGUIWindow* window = env-&gt;addWindow(
  110. rect&lt;s32&gt;(100 + cnt, 100 + cnt, 300 + cnt, 200 + cnt), <br> false, // modal?
  111. L&quot;Test window&quot;);</pre>
  112. <pre> env-&gt;addStaticText(L&quot;Please close me&quot;,
  113. rect&lt;s32&gt;(35,35,140,50),
  114. true, // border?,
  115. false, // wordwrap?
  116. window);
  117. return true;
  118. }</pre>
  119. <pre> if (id == 103)
  120. {
  121. listbox-&gt;addItem(L&quot;File open&quot;);
  122. env-&gt;addFileOpenDialog(L&quot;Please choose a file.&quot;);
  123. return true;
  124. }</pre>
  125. <pre> break;
  126. }
  127. }
  128. return false;
  129. }
  130. };</pre>
  131. </td>
  132. </tr>
  133. </table>
  134. <p>Ok, now for the more interesting part. First, create the Irrlicht device.
  135. As in some examples before, we ask the user which driver he wants to
  136. use for this example:</p>
  137. </div>
  138. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  139. <tr>
  140. <td> <pre>int main()
  141. {
  142. // ask user for driver
  143. video::E_DRIVER_TYPE driverType;
  144. 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>
  145. 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 1;<br> }
  146. // create device and exit if creation failed
  147. device = createDevice(driverType, core::dimension2d&lt;s32&gt;(640, 480));<br>
  148. if (device == 0)
  149. return 1;
  150. </pre>
  151. </td>
  152. </tr>
  153. </table>
  154. <p>The creation was successful, now we set the event receiver and store
  155. pointers to the driver and to the gui environment. </p>
  156. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  157. <tr>
  158. <td><pre>MyEventReceiver receiver;
  159. device-&gt;setEventReceiver(&amp;receiver);
  160. device-&gt;setWindowCaption(L&quot;Irrlicht Engine - User Inferface Demo&quot;);</pre>
  161. <pre>video::IVideoDriver* driver = device-&gt;getVideoDriver();
  162. IGUIEnvironment* env = device-&gt;getGUIEnvironment();
  163. </pre>
  164. </td>
  165. </tr>
  166. </table>
  167. <p>We add three buttons. The first one closes the engine. The second creates
  168. a window and the third opens a file open dialog. The third parameter is
  169. the id of the button, with which we can easily identify the button in
  170. the event receiver.</p>
  171. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  172. <tr>
  173. <td><pre>env-&gt;addButton(rect&lt;s32&gt;(10,240,100,270), 0, 101, L&quot;Quit&quot;);<br>env-&gt;addButton(rect&lt;s32&gt;(10,280,100,320), 0, 102, L&quot;New Window&quot;);<br>env-&gt;addButton(rect&lt;s32&gt;(10,330,100,370), 0, 103, L&quot;File Open&quot;);</pre></td>
  174. </tr>
  175. </table>
  176. <p> Now, we add a static text and a scrollbar, which modifies the transparency
  177. of all gui elements. We set the maximum value of the scrollbar to 255,
  178. because that's the maximal value for a color value.<br>
  179. Then we create an other static text and a list box.</p>
  180. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  181. <tr>
  182. <td><pre>env-&gt;addStaticText(L&quot;Transparent Control:&quot;, rect&lt;s32&gt;(150,20,350,40), true);<br>IGUIScrollBar* scrollbar = env-&gt;addScrollBar(true,
  183. rect&lt;s32&gt;(150, 45, 350, 60), 0, 104);<br>scrollbar-&gt;setMax(255);</pre>
  184. <pre>env-&gt;addStaticText(L&quot;Logging ListBox:&quot;, rect&lt;s32&gt;(50,110,250,130), true);
  185. listbox = env-&gt;addListBox(rect&lt;s32&gt;(50, 140, 250, 210));</pre></td>
  186. </tr>
  187. </table>
  188. <br>
  189. To make the font a little bit nicer, we load an external font and set it
  190. as new font in the skin. An at last, we create a nice Irrlicht Engine logo
  191. in the top left corner. <br>
  192. <br>
  193. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  194. <tr>
  195. <td> <pre>IGUISkin* skin = env-&gt;getSkin();<br>IGUIFont* font = env-&gt;getFont(&quot;../../media/fonthaettenschweiler.bmp&quot;);<br>if (font)<br> skin-&gt;setFont(font);</pre>
  196. <pre>IGUIImage* img = env-&gt;addImage(<br> driver-&gt;getTexture(&quot;../../media/irrlichtlogoalpha.tga&quot;),<br> position2d&lt;int&gt;(10,10));</pre></td>
  197. </tr>
  198. </table>
  199. <p>That's all, we only have to draw everything.</p>
  200. <table width="95%" border="0" cellspacing="2" cellpadding="0" bgcolor="#CCCCCC" align="center">
  201. <tr>
  202. <td>
  203. <pre> while(device-&gt;run() &amp;&amp; driver)<br> if (device-&gt;isWindowActive()) <br> {<br> driver-&gt;beginScene(true, true, SColor(0,122,65,171));
  204. env-&gt;drawAll();
  205. driver-&gt;endScene();
  206. }
  207. device-&gt;drop();</pre>
  208. <pre> return 0;
  209. }</pre>
  210. </td>
  211. </tr>
  212. </table>
  213. </td>
  214. </tr>
  215. </table>
  216. <p>&nbsp;</p>
  217. </body>
  218. </html>