1
/** Example 006 2D Graphics
3
This Tutorial shows how to do 2d graphics with the Irrlicht Engine.
4
It shows how to draw images, keycolor based sprites,
5
transparent rectangles, and different fonts. You may consider
6
this useful if you want to make a 2d game with the engine, or if
7
you want to draw a cool interface or head up display for your 3d game.
9
As always, I include the header files, use the irr namespace,
10
and tell the linker to link with the .lib file.
13
#include "driverChoice.h"
18
#pragma comment(lib, "Irrlicht.lib")
22
At first, we let the user select the driver type, then start up the engine, set
23
a caption, and get a pointer to the video driver.
27
// ask user for driver
28
video::E_DRIVER_TYPE driverType=driverChoiceConsole();
29
if (driverType==video::EDT_COUNT)
34
IrrlichtDevice *device = createDevice(driverType,
35
core::dimension2d<u32>(512, 384));
38
return 1; // could not create selected driver.
40
device->setWindowCaption(L"Irrlicht Engine - 2D Graphics Demo");
42
video::IVideoDriver* driver = device->getVideoDriver();
45
All 2d graphics in this example are put together into one texture,
46
2ddemo.png. Because we want to draw colorkey based sprites, we need to
47
load this texture and tell the engine, which part of it should be
48
transparent based on a colorkey.
50
In this example, we don't tell it the color directly, we just say "Hey
51
Irrlicht Engine, you'll find the color I want at position (0,0) on the
52
texture.". Instead, it would be also possible to call
53
driver->makeColorKeyTexture(images, video::SColor(0,0,0,0)), to make
54
e.g. all black pixels transparent. Please note that
55
makeColorKeyTexture just creates an alpha channel based on the color.
57
video::ITexture* images = driver->getTexture("../../media/2ddemo.png");
58
driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));
61
To be able to draw some text with two different fonts, we first load
62
them. Ok, we load just one. As the first font we just use the default
63
font which is built into the engine. Also, we define two rectangles
64
which specify the position of the images of the red imps (little flying
65
creatures) in the texture.
67
gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
68
gui::IGUIFont* font2 =
69
device->getGUIEnvironment()->getFont("../../media/fonthaettenschweiler.bmp");
71
core::rect<s32> imp1(349,15,385,78);
72
core::rect<s32> imp2(387,15,423,78);
75
Prepare a nicely filtering 2d render mode for special cases.
77
driver->getMaterial2D().TextureLayer[0].BilinearFilter=true;
78
driver->getMaterial2D().AntiAliasing=video::EAAM_FULL_BASIC;
81
Everything is prepared, now we can draw everything in the draw loop,
82
between the begin scene and end scene calls. In this example, we are
83
just doing 2d graphics, but it would be no problem to mix them with 3d
84
graphics. Just try it out, and draw some 3d vertices or set up a scene
85
with the scene manager and draw it.
87
while(device->run() && driver)
89
if (device->isWindowActive())
91
u32 time = device->getTimer()->getTime();
93
driver->beginScene(true, true, video::SColor(255,120,102,136));
96
First, we draw 3 sprites, using the alpha channel we
97
created with makeColorKeyTexture. The last parameter
98
specifies that the drawing method should use this alpha
99
channel. The last-but-one parameter specifies a
100
color, with which the sprite should be colored.
101
(255,255,255,255) is full white, so the sprite will
102
look like the original. The third sprite is drawn
103
with the red channel modulated based on the time.
106
// draw fire & dragons background world
107
driver->draw2DImage(images, core::position2d<s32>(50,50),
108
core::rect<s32>(0,0,342,224), 0,
109
video::SColor(255,255,255,255), true);
112
driver->draw2DImage(images, core::position2d<s32>(164,125),
113
(time/500 % 2) ? imp1 : imp2, 0,
114
video::SColor(255,255,255,255), true);
116
// draw second flying imp with colorcylce
117
driver->draw2DImage(images, core::position2d<s32>(270,105),
118
(time/500 % 2) ? imp1 : imp2, 0,
119
video::SColor(255,(time) % 255,255,255), true);
122
Drawing text is really simple. The code should be self
128
font->draw(L"This demo shows that Irrlicht is also capable of drawing 2D graphics.",
129
core::rect<s32>(130,10,300,50),
130
video::SColor(255,255,255,255));
132
// draw some other text
134
font2->draw(L"Also mixing with 3d graphics is possible.",
135
core::rect<s32>(130,20,300,60),
136
video::SColor(255,time % 255,time % 255,255));
139
Next, we draw the Irrlicht Engine logo (without
140
using a color or an alpha channel). Since we slightly scale
141
the image we use the prepared filter mode.
143
driver->enableMaterial2D();
144
driver->draw2DImage(images, core::rect<s32>(10,10,108,48),
145
core::rect<s32>(354,87,442,118));
146
driver->enableMaterial2D(false);
149
Finally draw a half-transparent rect under the mouse cursor.
151
core::position2d<s32> m = device->getCursorControl()->getPosition();
152
driver->draw2DRectangle(video::SColor(100,255,255,255),
153
core::rect<s32>(m.X-20, m.Y-20, m.X+20, m.Y+20));
165
That's all. I hope it was not too difficult.