This repository has been archived by the owner on Feb 12, 2018. It is now read-only.
/
main.cpp
531 lines (435 loc) · 15.5 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
#include <Ogre.h>
using namespace Ogre;
#include <OIS/OIS.h>
#include <deque>
//using namespace std;
class ExitListener : public FrameListener
{
public:
ExitListener(OIS::Keyboard *keyboard)
: mKeyboard(keyboard)
{
}
bool frameStarted(const FrameEvent& evt)
{
mKeyboard->capture();
return !mKeyboard->isKeyDown(OIS::KC_ESCAPE);
}
private:
OIS::Keyboard *mKeyboard;
};
class Application;
class ApplicationListener : public FrameListener, public OIS::KeyListener, public OIS::MouseListener, public OIS::JoyStickListener
{
public:
ApplicationListener(Root *root, OIS::InputManager *inputManager, Application *app, Camera *camera, SceneManager *sceneMgr, SceneNode *sn,
Entity *ent, std::deque<Vector3> &walk)
: mRoot(root), mInputManager(inputManager), mApp(app), mCamera(camera), mSceneMgr(sceneMgr), mNode(sn), mEntity(ent), mWalkList(walk)
{
/*try
{*/
mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject(OIS::OISKeyboard, true));
mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject(OIS::OISMouse, true));
//mJoy = static_cast<OIS::JoyStick*>(mInputManager->createInputObject(OIS::OISJoyStick, false));
/*}
catch (const OIS::Exception &e)
{
throw new Exception(42, e.eText, "Application::setupInputSystem");
}*/
mKeyboard->setEventCallback(this);
mMouse->setEventCallback(this);
//mJoy->setEventCallback(this);
// Populate the camera container
mCamNode = mCamera->getParentSceneNode();
// set the rotation and move speed
mRotate = 0.13;
mMove = 250;
// continue rendering
mContinue = true;
// not moving at first
mCharDirection = Vector3::ZERO;
mCowDirection = Vector3::ZERO;
// Set default values for variables
mWalkSpeed = 50.0f;
mDirection = Vector3::ZERO;
}
~ApplicationListener()
{
mInputManager->destroyInputObject(mKeyboard);
mInputManager->destroyInputObject(mMouse);
//mInputManager->destroyInputObject(mJoy);
}
// This function is called to start the object moving to the next position
// in mWalkList.
bool nextLocation()
{
if (mWalkList.empty())
return false;
mDestination = mWalkList.front(); // this gets the front of the deque
mWalkList.pop_front(); // this removes the front of the deque
mDirection = mDestination - mNode->getPosition();
mDistance = mDirection.normalise();
return true;
}
// FrameListener
bool frameStarted(const FrameEvent& evt)
{
mKeyboard->capture();
mMouse->capture();
//mJoy->capture();
mCamNode->translate(mCharDirection * evt.timeSinceLastFrame, Node::TS_LOCAL);
mNode->translate(mCowDirection * evt.timeSinceLastFrame, Node::TS_LOCAL);
if (mDirection == Vector3::ZERO)
{
if (nextLocation())
{
// Set walking animation
mAnimationState = mEntity->getAnimationState("Walk");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
}
else
{
Real move = mWalkSpeed * evt.timeSinceLastFrame;
mDistance -= move;
if (mDistance <= 0.0f)
{
mNode->setPosition(mDestination);
mDirection = Vector3::ZERO;
// Set animation based on if the robot has another point to walk to.
if (! nextLocation())
{
// Set Idle animation
mAnimationState = mEntity->getAnimationState("Idle");
mAnimationState->setLoop(true);
mAnimationState->setEnabled(true);
}
else
{
Vector3 src = mNode->getOrientation() * Vector3::UNIT_X;
if ((1.0f + src.dotProduct(mDirection)) < 0.0001f)
{
mNode->yaw(Degree(180));
}
else
{
Ogre::Quaternion quat = src.getRotationTo(mDirection);
mNode->rotate(quat);
} // else
}
}
else
{
mNode->translate(mDirection * move);
} // else
} // if
mAnimationState->addTime(evt.timeSinceLastFrame);
return mContinue;
}
// KeyListener
bool keyPressed(const OIS::KeyEvent &arg)
{
switch (arg.key)
{
case OIS::KC_ESCAPE:
mContinue = false;
break;
case OIS::KC_UP:
mCowDirection.z = mMove;
break;
case OIS::KC_DOWN:
mCowDirection.z = -mMove;
break;
case OIS::KC_LEFT:
mCowDirection.x = mMove;
break;
case OIS::KC_RIGHT:
mCowDirection.x = -mMove;
break;
case OIS::KC_PGDOWN:
mCowDirection.y = -mMove;
break;
case OIS::KC_PGUP:
mCowDirection.y = mMove;
break;
case OIS::KC_W:
mCharDirection.z = -mMove;
break;
case OIS::KC_S:
mCharDirection.z = mMove;
break;
case OIS::KC_A:
mCharDirection.x = -mMove;
break;
case OIS::KC_D:
mCharDirection.x = mMove;
break;
case OIS::KC_E:
mCharDirection.y = -mMove;
break;
case OIS::KC_Q:
mCharDirection.y = mMove;
break;
default:
break;
}
return mContinue;
}
bool keyReleased(const OIS::KeyEvent &arg)
{
switch (arg.key)
{
case OIS::KC_UP:
case OIS::KC_DOWN:
mCowDirection.z = 0;
break;
case OIS::KC_LEFT:
case OIS::KC_RIGHT:
mCowDirection.x = 0;
break;
case OIS::KC_PGDOWN:
case OIS::KC_PGUP:
mCowDirection.y = 0;
break;
case OIS::KC_W:
case OIS::KC_S:
mCharDirection.z = 0;
break;
case OIS::KC_A:
case OIS::KC_D:
mCharDirection.x = 0;
break;
case OIS::KC_E:
case OIS::KC_Q:
mCharDirection.y = 0;
break;
default:
break;
} // switch
return true;
}
// MouseListener
bool mouseMoved(const OIS::MouseEvent &arg)
{
if (arg.state.buttonDown(OIS::MB_Left))
{
mNode->yaw(Degree(-mRotate * arg.state.X.rel), Node::TS_WORLD);
mNode->pitch(Degree(-mRotate * arg.state.Y.rel), Node::TS_LOCAL);
}
if (arg.state.buttonDown(OIS::MB_Right))
{
mCamNode->yaw(Degree(-mRotate * arg.state.X.rel), Node::TS_WORLD);
mCamNode->pitch(Degree(-mRotate * arg.state.Y.rel), Node::TS_LOCAL);
}
return true;
}
bool mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
return true;
}
bool mouseReleased(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
return true;
}
// JoystickListener
bool buttonPressed(const OIS::JoyStickEvent &arg, int button) { return true; }
bool buttonReleased(const OIS::JoyStickEvent &arg, int button) { return true; }
bool axisMoved(const OIS::JoyStickEvent &arg, int axis) { return true; }
private:
Application *mApp;
Root *mRoot;
OIS::InputManager *mInputManager;
OIS::Keyboard *mKeyboard;
OIS::Mouse *mMouse;
OIS::JoyStick *mJoy;
Real mRotate; // The rotate constant
Real mMove; // The movement constant
SceneManager *mSceneMgr; // The current SceneManager
SceneNode *mCamNode; // The SceneNode the camera is currently attached to
Camera *mCamera; // The camera
bool mContinue; // Whether to continue rendering or not
Vector3 mCharDirection; // Value to move in the correct direction
Vector3 mCowDirection; // Value to move in the correct direction
Real mDistance; // The distance the object has left to travel
Vector3 mDirection; // The direction the object is moving
Vector3 mDestination; // The destination the object is moving towards
AnimationState *mAnimationState; // The current animation state of the object
Entity *mEntity; // The Entity we are animating
SceneNode *mNode; // The SceneNode that the Entity is attached to
std::deque<Vector3> mWalkList; // The list of points we are walking to
Real mWalkSpeed; // The speed at which the object is moving
};
class Application
{
public:
void go()
{
createRoot();
defineResources();
setupRenderSystem();
createRenderWindow();
initializeResourceGroups();
setupScene();
setupInputSystem();
createFrameListener();
startRenderLoop();
}
~Application()
{
delete mListener;
OIS::InputManager::destroyInputSystem(mInputManager);
delete mRoot;
}
private:
Root *mRoot;
OIS::InputManager *mInputManager;
ApplicationListener *mListener;
SceneManager *mSceneMgr;
Camera *mCamera;
Viewport *mViewport;
Entity *mEntity; // The entity of the object we are animating
SceneNode *mNode; // The SceneNode of the object we are moving
std::deque<Vector3> mWalkList; // A deque containing the waypoints
void createRoot()
{
mRoot = new Root();
}
void defineResources()
{
String secName, typeName, archName;
ConfigFile cf;
cf.load("resources.cfg");
ConfigFile::SectionIterator seci = cf.getSectionIterator();
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
ConfigFile::SettingsMultiMap *settings = seci.getNext();
ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
ResourceGroupManager::getSingleton().addResourceLocation(archName, typeName, secName);
}
}
}
void setupRenderSystem()
{
if (!mRoot->restoreConfig() && !mRoot->showConfigDialog())
throw Exception(52, "User canceled the config dialog!", "Application::setupRenderSystem()");
//// Do not add this to the application
//RenderSystem *rs = mRoot->getRenderSystemByName("Direct3D9 Rendering Subsystem");
// // or use "OpenGL Rendering Subsystem"
//mRoot->setRenderSystem(rs);
//rs->setConfigOption("Full Screen", "No");
//rs->setConfigOption("Video Mode", "800 x 600 @ 32-bit colour");
}
void createRenderWindow()
{
mRoot->initialise(true, "Duckinator's FPS Game of Failure");
//// Do not add this to the application
//mRoot->initialise(false);
//HWND hWnd = 0; // Get the hWnd of the application!
//NameValuePairList misc;
//misc["externalWindowHandle"] = StringConverter::toString((int)hWnd);
//RenderWindow *win = mRoot->createRenderWindow("Main RenderWindow", 800, 600, false, &misc);
}
void initializeResourceGroups()
{
TextureManager::getSingleton().setDefaultNumMipmaps(5);
ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
void setupScene()
{
mSceneMgr = mRoot->createSceneManager(ST_GENERIC, "Default SceneManager");
mCamera = mSceneMgr->createCamera("Camera");
mViewport = mRoot->getAutoCreatedWindow()->addViewport(mCamera);
SceneNode *node = mSceneMgr->getRootSceneNode()->createChildSceneNode("CamNode", Vector3(0, 200, 400));
node->attachObject(mCamera);
mSceneMgr->setAmbientLight(ColourValue(0.5, 0.5, 0.5));
mSceneMgr->setShadowTechnique(SHADOWTYPE_STENCIL_ADDITIVE);
// create the light
Light *light = mSceneMgr->createLight("Light1");
light->setType(Light::LT_POINT);
light->setPosition(Vector3(250, 150, 250));
light->setDiffuseColour(ColourValue::Red);
light->setSpecularColour(ColourValue::Red);
light = mSceneMgr->createLight("Light2");
light->setType(Light::LT_POINT);
light->setPosition(Vector3(-250, 150, 250));
light->setDiffuseColour(ColourValue::Blue);
light->setSpecularColour(ColourValue::Blue);
light = mSceneMgr->createLight("Light3");
light->setType(Light::LT_POINT);
light->setPosition(Vector3(0, 150, -250));
light->setDiffuseColour(ColourValue::Green);
light->setSpecularColour(ColourValue::Green);
Plane plane(Vector3::UNIT_Y, 0);
MeshManager::getSingleton().createPlane("ground",
ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, plane,
1500,1500,20,20,true,1,5,5,Vector3::UNIT_Z);
Entity *ent = mSceneMgr->createEntity("GroundEntity", "ground");
mSceneMgr->getRootSceneNode()->createChildSceneNode()->attachObject(ent);
ent->setMaterialName("Rockwall");
ent->setCastShadows(false);
mEntity = mSceneMgr->createEntity("Cow", "cow.mesh");
mEntity->setCastShadows(true);
mNode = mSceneMgr->getRootSceneNode()->createChildSceneNode("CowNode", Vector3(0.0f, 0.0f, 25.0f));
mNode->attachObject(mEntity);
// Create the walking list
mWalkList.push_back(Vector3(550.0f, 0.0f, 50.0f ));
mWalkList.push_back(Vector3(-100.0f, 0.0f, -200.0f));
}
void setupInputSystem()
{
size_t windowHnd = 0;
std::ostringstream windowHndStr;
OIS::ParamList pl;
RenderWindow *win = mRoot->getAutoCreatedWindow();
win->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
mInputManager = OIS::InputManager::createInputSystem(pl);
}
void createFrameListener()
{
mListener = new ApplicationListener(mRoot, mInputManager, this, mCamera, mSceneMgr, mNode, mEntity, mWalkList);
mRoot->addFrameListener(mListener);
}
void startRenderLoop()
{
mRoot->startRendering();
//// Do not add this to the application
//while (mRoot->renderOneFrame())
//{
// // Do some things here, like sleep for x milliseconds or perform other actions.
// // However, make sure you call the display update function:
// WindowEventUtilities::messagePump();
//}
}
};
#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
try
{
Application app;
app.go();
}
catch(Exception& e)
{
#if OGRE_PLATFORM == PLATFORM_WIN32 || OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occurred!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
fprintf(stderr, "An exception has occurred: %s\n",
e.getFullDescription().c_str());
#endif
}
return 0;
}