~baltix/+junk/irrlicht-test

« back to all changes in this revision

Viewing changes to examples/17.HelloWorld_Mobile/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 017 Helloworld mobile
 
2
        This example show Hello World for Windows mobile.
 
3
        It compiles on other platform too. The only differences between the original
 
4
        examples are. You need a GUI, because otherwise you can't quit the application.
 
5
        You need a Filesystem, which is relative based to your executable.
 
6
*/
 
7
 
 
8
#include <irrlicht.h>
 
9
 
 
10
#if defined ( _IRR_WINDOWS_ )
 
11
        #include <windows.h>
 
12
#endif
 
13
 
 
14
using namespace irr;
 
15
using namespace core;
 
16
using namespace scene;
 
17
using namespace video;
 
18
using namespace io;
 
19
using namespace gui;
 
20
 
 
21
#pragma comment(lib, "Irrlicht.lib")
 
22
 
 
23
class EventReceiver_basic : public IEventReceiver
 
24
{
 
25
private:
 
26
        IrrlichtDevice *Device;
 
27
public:
 
28
        EventReceiver_basic ( IrrlichtDevice *device ): Device ( device ) {}
 
29
 
 
30
        virtual bool OnEvent(const SEvent& event)
 
31
        {
 
32
                if (event.EventType == EET_GUI_EVENT)
 
33
                {
 
34
                        s32 id = event.GUIEvent.Caller->getID();
 
35
 
 
36
                        switch(event.GUIEvent.EventType)
 
37
                        {
 
38
                                case EGET_BUTTON_CLICKED:
 
39
                                if (id == 2)
 
40
                                {
 
41
                                        Device->closeDevice();
 
42
                                        return true;
 
43
                                } break;
 
44
                        }
 
45
                }
 
46
 
 
47
                return false;
 
48
        }
 
49
};
 
50
 
 
51
class CSampleSceneNode : public ISceneNode
 
52
{
 
53
        aabbox3d<f32> Box;
 
54
        S3DVertex Vertices[4];
 
55
        SMaterial Material;
 
56
public:
 
57
 
 
58
        CSampleSceneNode(ISceneNode* parent, ISceneManager* mgr, s32 id)
 
59
                : ISceneNode(parent, mgr, id)
 
60
        {
 
61
                Material.Wireframe = false;
 
62
                Material.Lighting = false;
 
63
 
 
64
                Vertices[0] = S3DVertex(0,0,10, 1,1,0, SColor(255,0,255,255), 0, 1);
 
65
                Vertices[1] = S3DVertex(10,0,-10, 1,0,0, SColor(255,255,0,255), 1, 1);
 
66
                Vertices[2] = S3DVertex(0,20,0, 0,1,1, SColor(255,255,255,0), 1, 0);
 
67
                Vertices[3] = S3DVertex(-10,0,-10, 0,0,1, SColor(255,0,255,0), 0, 0);
 
68
                Box.reset(Vertices[0].Pos);
 
69
                for (s32 i=1; i<4; ++i)
 
70
                        Box.addInternalPoint(Vertices[i].Pos);
 
71
        }
 
72
        virtual void OnRegisterSceneNode()
 
73
        {
 
74
                if (IsVisible)
 
75
                        SceneManager->registerNodeForRendering(this);
 
76
 
 
77
                ISceneNode::OnRegisterSceneNode();
 
78
        }
 
79
 
 
80
        virtual void render()
 
81
        {
 
82
                u16 indices[] = {       0,2,3, 2,1,3, 1,0,3, 2,0,1      };
 
83
                IVideoDriver* driver = SceneManager->getVideoDriver();
 
84
 
 
85
                driver->setMaterial(Material);
 
86
                driver->setTransform(ETS_WORLD, AbsoluteTransformation);
 
87
                driver->drawIndexedTriangleList(&Vertices[0], 4, &indices[0], 4);
 
88
        }
 
89
 
 
90
        virtual const aabbox3d<f32>& getBoundingBox() const
 
91
        {
 
92
                return Box;
 
93
        }
 
94
 
 
95
        virtual u32 getMaterialCount()
 
96
        {
 
97
                return 1;
 
98
        }
 
99
 
 
100
        virtual SMaterial& getMaterial(u32 i)
 
101
        {
 
102
                return Material;
 
103
        }       
 
104
};
 
105
 
 
106
/*!
 
107
        Startup a Windows Mobile Device
 
108
*/
 
109
IrrlichtDevice *startup()
 
110
{
 
111
        // both software and burnings video can be used
 
112
        E_DRIVER_TYPE driverType = EDT_SOFTWARE; // EDT_BURNINGSVIDEO;
 
113
 
 
114
        // create device
 
115
        IrrlichtDevice *device = 0;
 
116
 
 
117
#if defined (_IRR_USE_WINDOWS_CE_DEVICE_)
 
118
        // set to standard mobile fullscreen 240x320
 
119
        device = createDevice(driverType, dimension2d<u32>(240, 320), 16, true );
 
120
#else
 
121
        // on PC. use window mode
 
122
        device = createDevice(driverType, dimension2d<u32>(240, 320), 16, false );
 
123
#endif          
 
124
        if ( 0 == device )
 
125
                return 0;
 
126
 
 
127
        IVideoDriver* driver = device->getVideoDriver();
 
128
        ISceneManager* smgr = device->getSceneManager();
 
129
        IGUIEnvironment* guienv = device->getGUIEnvironment();
 
130
 
 
131
        // set the filesystem relative to the executable
 
132
#if defined (_IRR_WINDOWS_)
 
133
        {
 
134
                wchar_t buf[255];
 
135
                GetModuleFileNameW ( 0, buf, 255 );
 
136
 
 
137
                io::path base = buf;
 
138
                base = base.subString ( 0, base.findLast ( '\\' ) + 1 );
 
139
                device->getFileSystem()->addFileArchive ( base );
 
140
        }
 
141
#endif
 
142
 
 
143
        IGUIStaticText *text = guienv->addStaticText(L"FPS: 25",
 
144
                rect<s32>(140,15,200,30), false, false, 0, 100 );
 
145
 
 
146
        guienv->addButton(core::rect<int>(200,10,238,30), 0, 2, L"Quit");
 
147
 
 
148
        // add irrlicht logo
 
149
        guienv->addImage(driver->getTexture("../../media/irrlichtlogo3.png"),
 
150
                                        core::position2d<s32>(0,-2));
 
151
        return device;
 
152
}
 
153
 
 
154
/*!
 
155
*/
 
156
int run ( IrrlichtDevice *device )
 
157
{
 
158
        while(device->run())
 
159
        if (device->isWindowActive())
 
160
        {
 
161
                device->getVideoDriver()->beginScene(true, true, SColor(0,100,100,100));
 
162
                device->getSceneManager()->drawAll();
 
163
                device->getGUIEnvironment()->drawAll();
 
164
                device->getVideoDriver()->endScene ();
 
165
 
 
166
                IGUIElement *stat = device->getGUIEnvironment()->
 
167
                        getRootGUIElement()->getElementFromId ( 100 );
 
168
                if ( stat )
 
169
                {
 
170
                        stringw str = L"FPS: ";
 
171
                        str += (s32)device->getVideoDriver()->getFPS();
 
172
 
 
173
                        stat->setText ( str.c_str() );
 
174
                }
 
175
        }
 
176
 
 
177
        device->drop();
 
178
        return 0;
 
179
}
 
180
 
 
181
/*!
 
182
*/
 
183
int example_customscenenode()
 
184
{
 
185
        // create device
 
186
        IrrlichtDevice *device = startup();
 
187
        if (device == 0)
 
188
                return 1; // could not create selected driver.
 
189
 
 
190
        // create engine and camera
 
191
        EventReceiver_basic receiver(device);
 
192
        device->setEventReceiver(&receiver);
 
193
        
 
194
        IVideoDriver* driver = device->getVideoDriver();
 
195
        ISceneManager* smgr = device->getSceneManager();
 
196
        IGUIEnvironment* guienv = device->getGUIEnvironment();
 
197
 
 
198
 
 
199
        smgr->addCameraSceneNode(0, vector3df(0,-40,0), vector3df(0,0,0));
 
200
 
 
201
        CSampleSceneNode *myNode = 
 
202
                new CSampleSceneNode(smgr->getRootSceneNode(), smgr, 666);
 
203
 
 
204
        ISceneNodeAnimator* anim = 
 
205
                smgr->createRotationAnimator(vector3df(0.8f, 0, 0.8f));
 
206
 
 
207
        if(anim)
 
208
        {
 
209
                myNode->addAnimator(anim);
 
210
                anim->drop();
 
211
                anim = 0; // As I shouldn't refer to it again, ensure that I can't
 
212
        }
 
213
 
 
214
        myNode->drop();
 
215
        myNode = 0; // As I shouldn't refer to it again, ensure that I can't
 
216
 
 
217
        return run ( device );
 
218
}
 
219
 
 
220
class EventReceiver_terrain : public IEventReceiver
 
221
{
 
222
public:
 
223
 
 
224
        EventReceiver_terrain(IrrlichtDevice *device, scene::ISceneNode* terrain, scene::ISceneNode* skybox, scene::ISceneNode* skydome) :
 
225
                Device ( device ), Terrain(terrain), Skybox(skybox), Skydome(skydome), showBox(true)
 
226
        {
 
227
                Skybox->setVisible(true);
 
228
                Skydome->setVisible(false);
 
229
        }
 
230
 
 
231
        bool OnEvent(const SEvent& event)
 
232
        {
 
233
                if (event.EventType == EET_GUI_EVENT)
 
234
                {
 
235
                        s32 id = event.GUIEvent.Caller->getID();
 
236
 
 
237
                        switch(event.GUIEvent.EventType)
 
238
                        {
 
239
                                case EGET_BUTTON_CLICKED:
 
240
                                if (id == 2)
 
241
                                {
 
242
                                        Device->closeDevice();
 
243
                                        return true;
 
244
                                } break;
 
245
                        }
 
246
                }
 
247
 
 
248
                // check if user presses the key 'W' or 'D'
 
249
                if (event.EventType == irr::EET_KEY_INPUT_EVENT && !event.KeyInput.PressedDown)
 
250
                {
 
251
                        switch (event.KeyInput.Key)
 
252
                        {
 
253
                        case irr::KEY_KEY_W: // switch wire frame mode
 
254
                                Terrain->setMaterialFlag(video::EMF_WIREFRAME,
 
255
                                                !Terrain->getMaterial(0).Wireframe);
 
256
                                Terrain->setMaterialFlag(video::EMF_POINTCLOUD, false);
 
257
                                return true;
 
258
                        case irr::KEY_KEY_P: // switch wire frame mode
 
259
                                Terrain->setMaterialFlag(video::EMF_POINTCLOUD,
 
260
                                                !Terrain->getMaterial(0).PointCloud);
 
261
                                Terrain->setMaterialFlag(video::EMF_WIREFRAME, false);
 
262
                                return true;
 
263
                        case irr::KEY_KEY_D: // toggle detail map
 
264
                                Terrain->setMaterialType(
 
265
                                        Terrain->getMaterial(0).MaterialType == video::EMT_SOLID ?
 
266
                                        video::EMT_DETAIL_MAP : video::EMT_SOLID);
 
267
                                return true;
 
268
                        case irr::KEY_KEY_S: // toggle skies
 
269
                                showBox=!showBox;
 
270
                                Skybox->setVisible(showBox);
 
271
                                Skydome->setVisible(!showBox);
 
272
                                return true;
 
273
                        default:
 
274
                                break;
 
275
                        }
 
276
                }
 
277
 
 
278
                return false;
 
279
        }
 
280
 
 
281
private:
 
282
        IrrlichtDevice *Device;
 
283
        scene::ISceneNode* Terrain;
 
284
        scene::ISceneNode* Skybox;
 
285
        scene::ISceneNode* Skydome;
 
286
        bool showBox;
 
287
};
 
288
 
 
289
 
 
290
/*
 
291
The start of the main function starts like in most other example. We ask the user
 
292
for the desired renderer and start it up. This time with the advanced parameter handling.
 
293
*/
 
294
int example_terrain()
 
295
{
 
296
        // create device
 
297
        IrrlichtDevice *device = startup();
 
298
        if (device == 0)
 
299
                return 1; // could not create selected driver.
 
300
        
 
301
        /*
 
302
        First, we add standard stuff to the scene: A nice irrlicht engine
 
303
        logo, a small help text, a user controlled camera, and we disable
 
304
        the mouse cursor.
 
305
        */
 
306
 
 
307
        video::IVideoDriver* driver = device->getVideoDriver();
 
308
        scene::ISceneManager* smgr = device->getSceneManager();
 
309
        gui::IGUIEnvironment* env = device->getGUIEnvironment();
 
310
 
 
311
 
 
312
        //set other font
 
313
        //env->getSkin()->setFont(env->getFont("../../media/fontlucida.png"));
 
314
 
 
315
        // add some help text
 
316
        env->addStaticText(
 
317
                L"Press 'W' to change wireframe mode\nPress 'D' to toggle detail map\nPress 'S' to toggle skybox/skydome",
 
318
                core::rect<s32>(5,250,235,320), true, true, 0, -1, true);
 
319
 
 
320
        // add camera
 
321
        scene::ICameraSceneNode* camera =
 
322
                smgr->addCameraSceneNodeFPS(0,100.0f,1.2f);
 
323
 
 
324
        camera->setPosition(core::vector3df(2700*2,255*2,2600*2));
 
325
        camera->setTarget(core::vector3df(2397*2,343*2,2700*2));
 
326
        camera->setFarValue(42000.0f);
 
327
 
 
328
        // disable mouse cursor
 
329
        device->getCursorControl()->setVisible(false);
 
330
 
 
331
        /*
 
332
        Here comes the terrain renderer scene node: We add it just like any
 
333
        other scene node to the scene using
 
334
        ISceneManager::addTerrainSceneNode(). The only parameter we use is a
 
335
        file name to the heightmap we use. A heightmap is simply a gray scale
 
336
        texture. The terrain renderer loads it and creates the 3D terrain from
 
337
        it.
 
338
 
 
339
        To make the terrain look more big, we change the scale factor of
 
340
        it to (40, 4.4, 40). Because we don't have any dynamic lights in the
 
341
        scene, we switch off the lighting, and we set the file
 
342
        terrain-texture.jpg as texture for the terrain and detailmap3.jpg as
 
343
        second texture, called detail map. At last, we set the scale values for
 
344
        the texture: The first texture will be repeated only one time over the
 
345
        whole terrain, and the second one (detail map) 20 times.
 
346
        */
 
347
 
 
348
        // add terrain scene node
 
349
        scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode(
 
350
                "../../media/terrain-heightmap.bmp",
 
351
                0,                                      // parent node
 
352
                -1,                                     // node id
 
353
                core::vector3df(0.f, 0.f, 0.f),         // position
 
354
                core::vector3df(0.f, 0.f, 0.f),         // rotation
 
355
                core::vector3df(40.f, 4.4f, 40.f),      // scale
 
356
                video::SColor ( 255, 255, 255, 255 ),   // vertexColor
 
357
                5,                                      // maxLOD
 
358
                scene::ETPS_17,                         // patchSize
 
359
                4                                       // smoothFactor
 
360
                );
 
361
 
 
362
        if ( terrain )
 
363
        {
 
364
                terrain->setMaterialFlag(video::EMF_LIGHTING, false);
 
365
 
 
366
                terrain->setMaterialTexture(0,
 
367
                                driver->getTexture("../../media/terrain-texture.jpg"));
 
368
                terrain->setMaterialTexture(1,
 
369
                                driver->getTexture("../../media/detailmap3.jpg"));
 
370
                
 
371
                terrain->setMaterialType(video::EMT_DETAIL_MAP);
 
372
 
 
373
                terrain->scaleTexture(1.0f, 20.0f);
 
374
                //terrain->setDebugDataVisible ( true );
 
375
 
 
376
                /*
 
377
                To be able to do collision with the terrain, we create a triangle selector.
 
378
                If you want to know what triangle selectors do, just take a look into the
 
379
                collision tutorial. The terrain triangle selector works together with the
 
380
                terrain. To demonstrate this, we create a collision response animator
 
381
                and attach it to the camera, so that the camera will not be able to fly
 
382
                through the terrain.
 
383
                */
 
384
 
 
385
                // create triangle selector for the terrain     
 
386
                scene::ITriangleSelector* selector
 
387
                        = smgr->createTerrainTriangleSelector(terrain, 0);
 
388
                terrain->setTriangleSelector(selector);
 
389
 
 
390
                // create collision response animator and attach it to the camera
 
391
                scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
 
392
                        selector, camera, core::vector3df(60,100,60),
 
393
                        core::vector3df(0,0,0),
 
394
                        core::vector3df(0,50,0));
 
395
                selector->drop();
 
396
                camera->addAnimator(anim);
 
397
                anim->drop();
 
398
 
 
399
                /* If you need access to the terrain data you can also do this directly via the following code fragment.
 
400
                */
 
401
                scene::CDynamicMeshBuffer* buffer = new scene::CDynamicMeshBuffer(video::EVT_2TCOORDS, video::EIT_16BIT);
 
402
                terrain->getMeshBufferForLOD(*buffer, 0);
 
403
                video::S3DVertex2TCoords* data = (video::S3DVertex2TCoords*)buffer->getVertexBuffer().getData();
 
404
                // Work on data or get the IndexBuffer with a similar call.
 
405
                buffer->drop(); // When done drop the buffer again.
 
406
        }
 
407
 
 
408
        /*
 
409
        To make the user be able to switch between normal and wireframe mode,
 
410
        we create an instance of the event reciever from above and let Irrlicht
 
411
        know about it. In addition, we add the skybox which we already used in
 
412
        lots of Irrlicht examples and a skydome, which is shown mutually
 
413
        exclusive with the skybox by pressing 'S'.
 
414
        */
 
415
 
 
416
        // create skybox and skydome
 
417
        driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, false);
 
418
 
 
419
        scene::ISceneNode* skybox=smgr->addSkyBoxSceneNode(
 
420
                driver->getTexture("../../media/irrlicht2_up.jpg"),
 
421
                driver->getTexture("../../media/irrlicht2_dn.jpg"),
 
422
                driver->getTexture("../../media/irrlicht2_lf.jpg"),
 
423
                driver->getTexture("../../media/irrlicht2_rt.jpg"),
 
424
                driver->getTexture("../../media/irrlicht2_ft.jpg"),
 
425
                driver->getTexture("../../media/irrlicht2_bk.jpg"));
 
426
        scene::ISceneNode* skydome=smgr->addSkyDomeSceneNode(driver->getTexture("../../media/skydome.jpg"),16,8,0.95f,2.0f);
 
427
 
 
428
        driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, true);
 
429
 
 
430
        // create event receiver
 
431
        EventReceiver_terrain receiver( device, terrain, skybox, skydome);
 
432
        device->setEventReceiver(&receiver);
 
433
 
 
434
        return run ( device );
 
435
}
 
436
 
 
437
/*
 
438
*/
 
439
int example_helloworld()
 
440
{
 
441
        // create device
 
442
        IrrlichtDevice *device = startup();
 
443
        if (device == 0)
 
444
                return 1; // could not create selected driver.
 
445
 
 
446
        IVideoDriver* driver = device->getVideoDriver();
 
447
        ISceneManager* smgr = device->getSceneManager();
 
448
        IGUIEnvironment* guienv = device->getGUIEnvironment();
 
449
 
 
450
        IAnimatedMesh* mesh = smgr->getMesh("../../media/sydney.md2");
 
451
        if (!mesh)
 
452
        {
 
453
                device->drop();
 
454
                return 1;
 
455
        }
 
456
        IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode( mesh );
 
457
 
 
458
        /*
 
459
        To let the mesh look a little bit nicer, we change its material. We
 
460
        disable lighting because we do not have a dynamic light in here, and
 
461
        the mesh would be totally black otherwise. Then we set the frame loop,
 
462
        such that the predefined STAND animation is used. And last, we apply a
 
463
        texture to the mesh. Without it the mesh would be drawn using only a
 
464
        color.
 
465
        */
 
466
        if (node)
 
467
        {
 
468
                node->setMaterialFlag(EMF_LIGHTING, false);
 
469
                node->setMD2Animation(scene::EMAT_STAND);
 
470
                node->setMaterialTexture( 0, driver->getTexture("../../media/sydney.bmp") );
 
471
        }
 
472
 
 
473
        /*
 
474
        To look at the mesh, we place a camera into 3d space at the position
 
475
        (0, 30, -40). The camera looks from there to (0,5,0), which is
 
476
        approximately the place where our md2 model is.
 
477
        */
 
478
        smgr->addCameraSceneNode(0, vector3df(0,30,-40), vector3df(0,5,0));
 
479
 
 
480
        EventReceiver_basic receiver(device);
 
481
        device->setEventReceiver(&receiver);
 
482
 
 
483
        return run ( device );
 
484
 
 
485
}
 
486
 
 
487
#if defined (_IRR_USE_WINDOWS_CE_DEVICE_)
 
488
        #pragma comment(linker, "/subsystem:WINDOWSCE /ENTRY:main") 
 
489
#elif defined (_IRR_WINDOWS_)
 
490
        #pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
 
491
#endif
 
492
 
 
493
/*
 
494
*/
 
495
int main()
 
496
{
 
497
        example_helloworld ();
 
498
        example_customscenenode();
 
499
        //example_terrain();
 
500
}
 
501
 
 
502
/*
 
503
**/