<!-- Wanted to avoid copying .css to each folder, so copied default .css from doxyen in here, kicked out most stuff we don't need for examples and modified some a little bit.
Target was having a single html in each example folder which is created from the main.cpp files and needs no files besides some images below media folder.
<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><divclass="fragment"><divclass="line"><spanclass="preprocessor">#include <irrlicht.h></span></div><divclass="line"><spanclass="preprocessor">#include "driverChoice.h"</span></div><divclass="line"><spanclass="preprocessor">#include "exampleHelper.h"</span></div><divclass="line"></div><divclass="line"><spanclass="keyword">using namespace </span>irr;</div><divclass="line"></div><divclass="line"><spanclass="preprocessor">#ifdef _MSC_VER</span></div><divclass="line"><spanclass="preprocessor">#pragma comment(lib, "Irrlicht.lib")</span></div><divclass="line"><spanclass="preprocessor">#endif</span></div></div><!-- fragment --><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><divclass="fragment"><divclass="line"><spanclass="keywordtype">int</span> main()</div><divclass="line">{</div><divclass="line"><spanclass="comment">// ask user for driver</span></div><divclass="line"> video::E_DRIVER_TYPE driverType=driverChoiceConsole();</div><divclass="line"><spanclass="keywordflow">if</span> (driverType==video::EDT_COUNT)</div><divclass="line"><spanclass="keywordflow">return</span> 1;</div><divclass="line"></div><divclass="line"><spanclass="comment">// create device</span></div><divclass="line"> IrrlichtDevice *device = createDevice(driverType,</div><divclass="line"> core::dimension2d<u32>(512, 384));</div><divclass="line"></div><divclass="line"><spanclass="keywordflow">if</span> (device == 0)</div><divclass="line"><spanclass="keywordflow">return</span> 1; <spanclass="comment">// could not create selected driver.</span></div><divclass="line"></div><divclass="line"> device->setWindowCaption(L<spanclass="stringliteral">"Irrlicht Engine - 2D Graphics Demo"</span>);</div><divclass="line"></div><divclass="line"> video::IVideoDriver* driver = device->getVideoDriver();</div><divclass="line"></div><divclass="line"><spanclass="keyword">const</span> io::path mediaPath = getExampleMediaPath();</div></div><!-- fragment --><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->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><divclass="fragment"><divclass="line">video::ITexture* images = driver->getTexture(mediaPath + <spanclass="stringliteral">"2ddemo.png"</span>);</div><divclass="line">driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));</div></div><!-- fragment --><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><divclass="fragment"><divclass="line">gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();</div><divclass="line">gui::IGUIFont* font2 =</div><divclass="line"> device->getGUIEnvironment()->getFont(mediaPath + <spanclass="stringliteral">"fonthaettenschweiler.bmp"</span>);</div><divclass="line"></div><divclass="line">core::rect<s32> imp1(349,15,385,78);</div><divclass="line">core::rect<s32> imp2(387,15,423,78);</div></div><!-- fragment --><p> Prepare a nicely filtering 2d render mode for special cases. </p><divclass="fragment"><divclass="line">driver->getMaterial2D().TextureLayer[0].BilinearFilter=<spanclass="keyword">true</span>;</div><divclass="line">driver->getMaterial2D().AntiAliasing=video::EAAM_FULL_BASIC;</div></div><!-- fragment --><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><divclass="fragment"><divclass="line"><spanclass="keywordflow">while</span>(device->run() && driver)</div><divclass="line">{</div><divclass="line"><spanclass="keywordflow">if</span> (device->isWindowActive())</div><divclass="line"> {</div><divclass="line"> u32 time = device->getTimer()->getTime();</div><divclass="line"></div><divclass="line"> driver->beginScene(video::ECBF_COLOR | video::ECBF_DEPTH, video::SColor(255,120,102,136));</div></div><!-- fragment --><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><divclass="fragment"><divclass="line"><spanclass="comment">// draw fire & dragons background world</span></div><divclass="line">driver->draw2DImage(images, core::position2d<s32>(50,50),</div><divclass="line"> core::rect<s32>(0,0,342,224), 0,</div><divclass="line"> video::SColor(255,255,255,255), <spanclass="keyword">true</span>);</div><divclass="line"></div><divclass="line"><spanclass="comment">// draw flying imp</span></div><divclass="line">driver->draw2DImage(images, core::position2d<s32>(164,125),</div><divclass="line"> (time/500 % 2) ? imp1 : imp2, 0,</div><divclass="line"> video::SColor(255,255,255,255), <spanclass="keyword">true</span>);</div><divclass="line"></div><divclass="line"><spanclass="comment">// draw second flying imp with color cycle</span></div><divclass="line">driver->draw2DImage(images, core::position2d<s32>(270,105),</div><divclass="line"> (time/500 % 2) ? imp1 : imp2, 0,</div><divclass="line"> video::SColor(255