~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to examples/06.2DGraphics/main.cpp

  • Committer: Mantas Kriaučiūnas
  • Date: 2011-07-18 13:06:25 UTC
  • Revision ID: mantas@akl.lt-20110718130625-c5pvifp61e7kj1ol
Included whole irrlicht SVN libraries to work around launchpad recipe issue with quilt, see https://answers.launchpad.net/launchpad/+question/165193

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/** Example 006 2D Graphics
 
2
 
 
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.
 
8
 
 
9
As always, I include the header files, use the irr namespace,
 
10
and tell the linker to link with the .lib file.
 
11
*/
 
12
#include <irrlicht.h>
 
13
#include "driverChoice.h"
 
14
 
 
15
using namespace irr;
 
16
 
 
17
#ifdef _MSC_VER
 
18
#pragma comment(lib, "Irrlicht.lib")
 
19
#endif
 
20
 
 
21
/*
 
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.
 
24
*/
 
25
int main()
 
26
{
 
27
        // ask user for driver
 
28
        video::E_DRIVER_TYPE driverType=driverChoiceConsole();
 
29
        if (driverType==video::EDT_COUNT)
 
30
                return 1;
 
31
 
 
32
        // create device
 
33
 
 
34
        IrrlichtDevice *device = createDevice(driverType,
 
35
                core::dimension2d<u32>(512, 384));
 
36
 
 
37
        if (device == 0)
 
38
                return 1; // could not create selected driver.
 
39
 
 
40
        device->setWindowCaption(L"Irrlicht Engine - 2D Graphics Demo");
 
41
 
 
42
        video::IVideoDriver* driver = device->getVideoDriver();
 
43
 
 
44
        /*
 
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.
 
49
 
 
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.
 
56
        */
 
57
        video::ITexture* images = driver->getTexture("../../media/2ddemo.png");
 
58
        driver->makeColorKeyTexture(images, core::position2d<s32>(0,0));
 
59
 
 
60
        /*
 
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.
 
66
        */
 
67
        gui::IGUIFont* font = device->getGUIEnvironment()->getBuiltInFont();
 
68
        gui::IGUIFont* font2 =
 
69
                device->getGUIEnvironment()->getFont("../../media/fonthaettenschweiler.bmp");
 
70
 
 
71
        core::rect<s32> imp1(349,15,385,78);
 
72
        core::rect<s32> imp2(387,15,423,78);
 
73
 
 
74
        /*
 
75
        Prepare a nicely filtering 2d render mode for special cases.
 
76
        */
 
77
        driver->getMaterial2D().TextureLayer[0].BilinearFilter=true;
 
78
        driver->getMaterial2D().AntiAliasing=video::EAAM_FULL_BASIC;
 
79
 
 
80
        /*
 
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.
 
86
        */
 
87
        while(device->run() && driver)
 
88
        {
 
89
                if (device->isWindowActive())
 
90
                {
 
91
                        u32 time = device->getTimer()->getTime();
 
92
 
 
93
                        driver->beginScene(true, true, video::SColor(255,120,102,136));
 
94
 
 
95
                        /*
 
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.
 
104
                        */
 
105
 
 
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);
 
110
 
 
111
                        // draw flying imp
 
112
                        driver->draw2DImage(images, core::position2d<s32>(164,125),
 
113
                                (time/500 % 2) ? imp1 : imp2, 0,
 
114
                                video::SColor(255,255,255,255), true);
 
115
 
 
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);
 
120
 
 
121
                        /*
 
122
                        Drawing text is really simple. The code should be self
 
123
                        explanatory.
 
124
                        */
 
125
 
 
126
                        // draw some text
 
127
                        if (font)
 
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));
 
131
 
 
132
                        // draw some other text
 
133
                        if (font2)
 
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));
 
137
 
 
138
                        /*
 
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.
 
142
                        */
 
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);
 
147
 
 
148
                        /*
 
149
                        Finally draw a half-transparent rect under the mouse cursor.
 
150
                        */
 
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));
 
154
 
 
155
                        driver->endScene();
 
156
                }
 
157
        }
 
158
 
 
159
        device->drop();
 
160
 
 
161
        return 0;
 
162
}
 
163
 
 
164
/*
 
165
That's all. I hope it was not too difficult.
 
166
**/