diff options
author | David Walter Seikel | 2014-01-13 19:47:58 +1000 |
---|---|---|
committer | David Walter Seikel | 2014-01-13 19:47:58 +1000 |
commit | f9158592e1478b2013afc7041d9ed041cf2d2f4a (patch) | |
tree | b16e389d7988700e21b4c9741044cefa536dcbae /libraries/irrlicht-1.8.1/examples/04.Movement/main.cpp | |
parent | Libraries readme updated with change markers and more of the Irrlicht changes. (diff) | |
download | SledjHamr-f9158592e1478b2013afc7041d9ed041cf2d2f4a.zip SledjHamr-f9158592e1478b2013afc7041d9ed041cf2d2f4a.tar.gz SledjHamr-f9158592e1478b2013afc7041d9ed041cf2d2f4a.tar.bz2 SledjHamr-f9158592e1478b2013afc7041d9ed041cf2d2f4a.tar.xz |
Update Irrlicht to 1.8.1. Include actual change markers this time. lol
Diffstat (limited to '')
-rw-r--r-- | libraries/irrlicht-1.8.1/examples/04.Movement/main.cpp | 258 |
1 files changed, 258 insertions, 0 deletions
diff --git a/libraries/irrlicht-1.8.1/examples/04.Movement/main.cpp b/libraries/irrlicht-1.8.1/examples/04.Movement/main.cpp new file mode 100644 index 0000000..7bacda3 --- /dev/null +++ b/libraries/irrlicht-1.8.1/examples/04.Movement/main.cpp | |||
@@ -0,0 +1,258 @@ | |||
1 | /** Example 004 Movement | ||
2 | |||
3 | This Tutorial shows how to move and animate SceneNodes. The | ||
4 | basic concept of SceneNodeAnimators is shown as well as manual | ||
5 | movement of nodes using the keyboard. We'll demonstrate framerate | ||
6 | independent movement, which means moving by an amount dependent | ||
7 | on the duration of the last run of the Irrlicht loop. | ||
8 | |||
9 | Example 19.MouseAndJoystick shows how to handle those kinds of input. | ||
10 | |||
11 | As always, I include the header files, use the irr namespace, | ||
12 | and tell the linker to link with the .lib file. | ||
13 | */ | ||
14 | #ifdef _MSC_VER | ||
15 | // We'll also define this to stop MSVC complaining about sprintf(). | ||
16 | #define _CRT_SECURE_NO_WARNINGS | ||
17 | #pragma comment(lib, "Irrlicht.lib") | ||
18 | #endif | ||
19 | |||
20 | #include <irrlicht.h> | ||
21 | #include "driverChoice.h" | ||
22 | |||
23 | using namespace irr; | ||
24 | |||
25 | /* | ||
26 | To receive events like mouse and keyboard input, or GUI events like "the OK | ||
27 | button has been clicked", we need an object which is derived from the | ||
28 | irr::IEventReceiver object. There is only one method to override: | ||
29 | irr::IEventReceiver::OnEvent(). This method will be called by the engine once | ||
30 | when an event happens. What we really want to know is whether a key is being | ||
31 | held down, and so we will remember the current state of each key. | ||
32 | */ | ||
33 | class MyEventReceiver : public IEventReceiver | ||
34 | { | ||
35 | public: | ||
36 | // This is the one method that we have to implement | ||
37 | virtual bool OnEvent(const SEvent& event) | ||
38 | { | ||
39 | // Remember whether each key is down or up | ||
40 | if (event.EventType == irr::EET_KEY_INPUT_EVENT) | ||
41 | KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown; | ||
42 | |||
43 | return false; | ||
44 | } | ||
45 | |||
46 | // This is used to check whether a key is being held down | ||
47 | virtual bool IsKeyDown(EKEY_CODE keyCode) const | ||
48 | { | ||
49 | return KeyIsDown[keyCode]; | ||
50 | } | ||
51 | |||
52 | MyEventReceiver() | ||
53 | { | ||
54 | for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i) | ||
55 | KeyIsDown[i] = false; | ||
56 | } | ||
57 | |||
58 | private: | ||
59 | // We use this array to store the current state of each key | ||
60 | bool KeyIsDown[KEY_KEY_CODES_COUNT]; | ||
61 | }; | ||
62 | |||
63 | |||
64 | /* | ||
65 | The event receiver for keeping the pressed keys is ready, the actual responses | ||
66 | will be made inside the render loop, right before drawing the scene. So lets | ||
67 | just create an irr::IrrlichtDevice and the scene node we want to move. We also | ||
68 | create some other additional scene nodes, to show that there are also some | ||
69 | different possibilities to move and animate scene nodes. | ||
70 | */ | ||
71 | int main() | ||
72 | { | ||
73 | // ask user for driver | ||
74 | video::E_DRIVER_TYPE driverType=driverChoiceConsole(); | ||
75 | if (driverType==video::EDT_COUNT) | ||
76 | return 1; | ||
77 | |||
78 | // create device | ||
79 | MyEventReceiver receiver; | ||
80 | |||
81 | IrrlichtDevice* device = createDevice(driverType, | ||
82 | core::dimension2d<u32>(640, 480), 16, false, false, false, &receiver); | ||
83 | |||
84 | if (device == 0) | ||
85 | return 1; // could not create selected driver. | ||
86 | |||
87 | video::IVideoDriver* driver = device->getVideoDriver(); | ||
88 | scene::ISceneManager* smgr = device->getSceneManager(); | ||
89 | |||
90 | /* | ||
91 | Create the node which will be moved with the WSAD keys. We create a | ||
92 | sphere node, which is a built-in geometry primitive. We place the node | ||
93 | at (0,0,30) and assign a texture to it to let it look a little bit more | ||
94 | interesting. Because we have no dynamic lights in this scene we disable | ||
95 | lighting for each model (otherwise the models would be black). | ||
96 | */ | ||
97 | scene::ISceneNode * node = smgr->addSphereSceneNode(); | ||
98 | if (node) | ||
99 | { | ||
100 | node->setPosition(core::vector3df(0,0,30)); | ||
101 | node->setMaterialTexture(0, driver->getTexture("../../media/wall.bmp")); | ||
102 | node->setMaterialFlag(video::EMF_LIGHTING, false); | ||
103 | } | ||
104 | |||
105 | /* | ||
106 | Now we create another node, movable using a scene node animator. Scene | ||
107 | node animators modify scene nodes and can be attached to any scene node | ||
108 | like mesh scene nodes, billboards, lights and even camera scene nodes. | ||
109 | Scene node animators are not only able to modify the position of a | ||
110 | scene node, they can also animate the textures of an object for | ||
111 | example. We create a cube scene node and attach a 'fly circle' scene | ||
112 | node animator to it, letting this node fly around our sphere scene node. | ||
113 | */ | ||
114 | scene::ISceneNode* n = smgr->addCubeSceneNode(); | ||
115 | |||
116 | if (n) | ||
117 | { | ||
118 | n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg")); | ||
119 | n->setMaterialFlag(video::EMF_LIGHTING, false); | ||
120 | scene::ISceneNodeAnimator* anim = | ||
121 | smgr->createFlyCircleAnimator(core::vector3df(0,0,30), 20.0f); | ||
122 | if (anim) | ||
123 | { | ||
124 | n->addAnimator(anim); | ||
125 | anim->drop(); | ||
126 | } | ||
127 | } | ||
128 | |||
129 | /* | ||
130 | The last scene node we add to show possibilities of scene node animators is | ||
131 | a b3d model, which uses a 'fly straight' animator to run between to points. | ||
132 | */ | ||
133 | scene::IAnimatedMeshSceneNode* anms = | ||
134 | smgr->addAnimatedMeshSceneNode(smgr->getMesh("../../media/ninja.b3d")); | ||
135 | |||
136 | if (anms) | ||
137 | { | ||
138 | scene::ISceneNodeAnimator* anim = | ||
139 | smgr->createFlyStraightAnimator(core::vector3df(100,0,60), | ||
140 | core::vector3df(-100,0,60), 3500, true); | ||
141 | if (anim) | ||
142 | { | ||
143 | anms->addAnimator(anim); | ||
144 | anim->drop(); | ||
145 | } | ||
146 | |||
147 | /* | ||
148 | To make the model look right we disable lighting, set the | ||
149 | frames between which the animation should loop, rotate the | ||
150 | model around 180 degrees, and adjust the animation speed and | ||
151 | the texture. To set the right animation (frames and speed), we | ||
152 | would also be able to just call | ||
153 | "anms->setMD2Animation(scene::EMAT_RUN)" for the 'run' | ||
154 | animation instead of "setFrameLoop" and "setAnimationSpeed", | ||
155 | but this only works with MD2 animations, and so you know how to | ||
156 | start other animations. But a good advice is to not use | ||
157 | hardcoded frame-numbers... | ||
158 | */ | ||
159 | anms->setMaterialFlag(video::EMF_LIGHTING, false); | ||
160 | |||
161 | anms->setFrameLoop(0, 13); | ||
162 | anms->setAnimationSpeed(15); | ||
163 | // anms->setMD2Animation(scene::EMAT_RUN); | ||
164 | |||
165 | anms->setScale(core::vector3df(2.f,2.f,2.f)); | ||
166 | anms->setRotation(core::vector3df(0,-90,0)); | ||
167 | // anms->setMaterialTexture(0, driver->getTexture("../../media/sydney.bmp")); | ||
168 | |||
169 | } | ||
170 | |||
171 | |||
172 | /* | ||
173 | To be able to look at and move around in this scene, we create a first | ||
174 | person shooter style camera and make the mouse cursor invisible. | ||
175 | */ | ||
176 | smgr->addCameraSceneNodeFPS(); | ||
177 | device->getCursorControl()->setVisible(false); | ||
178 | |||
179 | /* | ||
180 | Add a colorful irrlicht logo | ||
181 | */ | ||
182 | device->getGUIEnvironment()->addImage( | ||
183 | driver->getTexture("../../media/irrlichtlogoalpha2.tga"), | ||
184 | core::position2d<s32>(10,20)); | ||
185 | |||
186 | gui::IGUIStaticText* diagnostics = device->getGUIEnvironment()->addStaticText( | ||
187 | L"", core::rect<s32>(10, 10, 400, 20)); | ||
188 | diagnostics->setOverrideColor(video::SColor(255, 255, 255, 0)); | ||
189 | |||
190 | /* | ||
191 | We have done everything, so lets draw it. We also write the current | ||
192 | frames per second and the name of the driver to the caption of the | ||
193 | window. | ||
194 | */ | ||
195 | int lastFPS = -1; | ||
196 | |||
197 | // In order to do framerate independent movement, we have to know | ||
198 | // how long it was since the last frame | ||
199 | u32 then = device->getTimer()->getTime(); | ||
200 | |||
201 | // This is the movemen speed in units per second. | ||
202 | const f32 MOVEMENT_SPEED = 5.f; | ||
203 | |||
204 | while(device->run()) | ||
205 | { | ||
206 | // Work out a frame delta time. | ||
207 | const u32 now = device->getTimer()->getTime(); | ||
208 | const f32 frameDeltaTime = (f32)(now - then) / 1000.f; // Time in seconds | ||
209 | then = now; | ||
210 | |||
211 | /* Check if keys W, S, A or D are being held down, and move the | ||
212 | sphere node around respectively. */ | ||
213 | core::vector3df nodePosition = node->getPosition(); | ||
214 | |||
215 | if(receiver.IsKeyDown(irr::KEY_KEY_W)) | ||
216 | nodePosition.Y += MOVEMENT_SPEED * frameDeltaTime; | ||
217 | else if(receiver.IsKeyDown(irr::KEY_KEY_S)) | ||
218 | nodePosition.Y -= MOVEMENT_SPEED * frameDeltaTime; | ||
219 | |||
220 | if(receiver.IsKeyDown(irr::KEY_KEY_A)) | ||
221 | nodePosition.X -= MOVEMENT_SPEED * frameDeltaTime; | ||
222 | else if(receiver.IsKeyDown(irr::KEY_KEY_D)) | ||
223 | nodePosition.X += MOVEMENT_SPEED * frameDeltaTime; | ||
224 | |||
225 | node->setPosition(nodePosition); | ||
226 | |||
227 | driver->beginScene(true, true, video::SColor(255,113,113,133)); | ||
228 | |||
229 | smgr->drawAll(); // draw the 3d scene | ||
230 | device->getGUIEnvironment()->drawAll(); // draw the gui environment (the logo) | ||
231 | |||
232 | driver->endScene(); | ||
233 | |||
234 | int fps = driver->getFPS(); | ||
235 | |||
236 | if (lastFPS != fps) | ||
237 | { | ||
238 | core::stringw tmp(L"Movement Example - Irrlicht Engine ["); | ||
239 | tmp += driver->getName(); | ||
240 | tmp += L"] fps: "; | ||
241 | tmp += fps; | ||
242 | |||
243 | device->setWindowCaption(tmp.c_str()); | ||
244 | lastFPS = fps; | ||
245 | } | ||
246 | } | ||
247 | |||
248 | /* | ||
249 | In the end, delete the Irrlicht device. | ||
250 | */ | ||
251 | device->drop(); | ||
252 | |||
253 | return 0; | ||
254 | } | ||
255 | |||
256 | /* | ||
257 | That's it. Compile and play around with the program. | ||
258 | **/ | ||