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