-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseApplication.cpp
More file actions
executable file
·667 lines (546 loc) · 18 KB
/
Copy pathBaseApplication.cpp
File metadata and controls
executable file
·667 lines (546 loc) · 18 KB
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
#include "BaseApplication.h"
#include <boost/lexical_cast.hpp>
#include <string>
using std::cout;
/* D-BUGZ */
int counter = 0;
//-------------------------------------------------------------------------------------
BaseApplication::BaseApplication(void)
: mRoot(0),
mCamera(0),
mSceneMgr(0),
mWindow(0),
mResourcesCfg(Ogre::StringUtil::BLANK),
mPluginsCfg(Ogre::StringUtil::BLANK),
mCameraMan(0),
mDetailsPanel(0),
mCursorWasVisible(false),
mShutDown(false),
mInputManager(0),
mMouse(0),
mKeyboard(0),
isPaused(true),
mcapture(true)
//ns(GAME_SINGLE)
{
networkObject=new Network(0,this);
}
//-------------------------------------------------------------------------------------
BaseApplication::~BaseApplication(void)
{
if (mCameraMan) delete mCameraMan;
//Remove ourself as a Window listener
Ogre::WindowEventUtilities::removeWindowEventListener(mWindow, this);
windowClosed(mWindow);
delete mRoot;
}
void BaseApplication::createScene(void)
{
//init sound
sounds = new SoundEffects();
event = new SDL_Event();
sounds->init();
sounds->load_files();
sounds->playMusic();
//init sim
sim = new Simulator();
// create your scene
mSceneMgr->setAmbientLight(Ogre::ColourValue(.5f,0.45f,0.4f,.3f));
mSceneMgr->setShadowTechnique(Ogre::SHADOWTYPE_STENCIL_ADDITIVE);
//CEGUI
mRenderer = &CEGUI::OgreRenderer::bootstrapSystem();
gui = new BaseGui(this);
gui->init();
//create elements
Court * court = new Court(mSceneMgr);
ball = new Ball(mSceneMgr);
player = new Player(mSceneMgr);
ball->setSimulator(sim);
court->setSimulator(sim);
//create sim
/* */
sim->addObject(ball);
sim->addObject(player);
sim->addObject(court);
court->addToSimulator();
//mSceneMgr->getSceneNode("PlayerNode")->translate(player->getPos());
//lighting
Ogre::Light * light = mSceneMgr->createLight("light1");
light->setType(Ogre::Light::LT_POINT);
light->setPosition(Ogre::Vector3(0,110,0));
light->setDiffuseColour(1.0, 0.95, 0.6);
light->setSpecularColour(1.0, 1.0, 1.0);
light->setAttenuation(400,.1,.00001,.00004);
SoundEffects *sounds = new SoundEffects();
sounds->init();
sounds->load_files();
sounds->playMusic();
}
//kill music
void BaseApplication::musicOff()
{
sounds->playMusic();
}
//-------------------------------------------------------------------------------------
bool BaseApplication::configure(void)
{
// Show the configuration dialog and initialise the system
// You can skip this and use root.restoreConfig() to load configuration
// settings if you were sure there are valid ones saved in ogre.cfg
if(mRoot->showConfigDialog())
{
// If returned true, user clicked OK so initialise
// Here we choose to let the system create a default rendering window by passing 'true'
mWindow = mRoot->initialise(true, "Assignment 1 Render Window");
return true;
}
else
{
return false;
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::chooseSceneManager(void)
{
// Get the SceneManager, in this case a generic one
mSceneMgr = mRoot->createSceneManager(Ogre::ST_GENERIC);
mOverlaySystem = new Ogre::OverlaySystem();
mSceneMgr->addRenderQueueListener(mOverlaySystem);
}
//-------------------------------------------------------------------------------------
void BaseApplication::createCamera(void)
{
// Create the camera
mCamera = mSceneMgr->createCamera("PlayerCam");
// Position it
mCamera->setPosition(Ogre::Vector3(0,0,400));
// Look back along -Z
mCamera->lookAt(Ogre::Vector3(0,0,-200));
mCamera->setNearClipDistance(5);
mCameraMan = new OgreBites::SdkCameraMan(mCamera); // create a default camera controller
}
//-------------------------------------------------------------------------------------
void BaseApplication::createFrameListener(void)
{
Ogre::LogManager::getSingletonPtr()->logMessage("*** Initializing OIS ***");
OIS::ParamList pl;
size_t windowHnd = 0;
std::ostringstream windowHndStr;
mWindow->getCustomAttribute("WINDOW", &windowHnd);
windowHndStr << windowHnd;
pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
pl.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
pl.insert(std::make_pair(std::string("x11_keyboard_grab"), std::string("false")));
//mouse capture
mInputManager = OIS::InputManager::createInputSystem( pl );
mKeyboard = static_cast<OIS::Keyboard*>(mInputManager->createInputObject( OIS::OISKeyboard, true ));
mMouse = static_cast<OIS::Mouse*>(mInputManager->createInputObject( OIS::OISMouse, true ));
mMouse->setEventCallback(this);
mKeyboard->setEventCallback(this);
//Set initial mouse clipping size
windowResized(mWindow);
//Register as a Window listener
Ogre::WindowEventUtilities::addWindowEventListener(mWindow, this);
OgreBites::InputContext inputContext;
inputContext.mMouse = mMouse;
inputContext.mKeyboard = mKeyboard;
mRoot->addFrameListener(this);
}
//-------------------------------------------------------------------------------------
void BaseApplication::destroyScene(void)
{
}
//-------------------------------------------------------------------------------------
void BaseApplication::createViewports(void)
{
// Create one viewport, entire window
Ogre::Viewport* vp = mWindow->addViewport(mCamera);
vp->setBackgroundColour(Ogre::ColourValue(0,0,0));
// Alter the camera aspect ratio to match the viewport
mCamera->setAspectRatio(
Ogre::Real(vp->getActualWidth()) / Ogre::Real(vp->getActualHeight()));
}
//-------------------------------------------------------------------------------------
void BaseApplication::setupResources(void)
{
// Load resource paths from config file
Ogre::ConfigFile cf;
cf.load(mResourcesCfg);
// Go through all sections & settings in the file
Ogre::ConfigFile::SectionIterator seci = cf.getSectionIterator();
Ogre::String secName, typeName, archName;
while (seci.hasMoreElements())
{
secName = seci.peekNextKey();
Ogre::ConfigFile::SettingsMultiMap *settings = seci.getNext();
Ogre::ConfigFile::SettingsMultiMap::iterator i;
for (i = settings->begin(); i != settings->end(); ++i)
{
typeName = i->first;
archName = i->second;
Ogre::ResourceGroupManager::getSingleton().addResourceLocation(
archName, typeName, secName);
}
}
}
//-------------------------------------------------------------------------------------
void BaseApplication::createResourceListener(void)
{
}
//-------------------------------------------------------------------------------------
void BaseApplication::loadResources(void)
{
Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}
//-------------------------------------------------------------------------------------
void BaseApplication::go(void)
{
#ifdef _DEBUG
mResourcesCfg = "resources_d.cfg";
mPluginsCfg = "plugins_d.cfg";
#else
mResourcesCfg = "resources.cfg";
mPluginsCfg = "plugins.cfg";
#endif
if (!setup())
return;
mRoot->startRendering();
// clean up
destroyScene();
}
//-------------------------------------------------------------------------------------
bool BaseApplication::setup(void)
{
mRoot = new Ogre::Root(mPluginsCfg);
up=down=left=right=false;
setupResources();
bool carryOn = configure();
if (!carryOn) return false;
chooseSceneManager();
createCamera();
createViewports();
// Set default mipmap level (NB some APIs ignore this)
Ogre::TextureManager::getSingleton().setDefaultNumMipmaps(5);
// Create any resource listeners (for loading screens)
createResourceListener();
// Load resources
loadResources();
// Create the scene
createScene();
createFrameListener();
return true;
};
//-------------------------------------------------------------------------------------
CEGUI::MouseButton convertButton(OIS::MouseButtonID buttonID)
{
switch (buttonID)
{
case OIS::MB_Left:
return CEGUI::LeftButton;
case OIS::MB_Right:
return CEGUI::RightButton;
case OIS::MB_Middle:
return CEGUI::MiddleButton;
default:
return CEGUI::LeftButton;
}
}
bool BaseApplication::frameRenderingQueued(const Ogre::FrameEvent& evt)
{
if(mWindow->isClosed())
return false;
if(mShutDown)
return false;
//Need to capture/update each device
mKeyboard->capture();
mMouse->capture();
CEGUI::System::getSingleton().injectTimePulse(evt.timeSinceLastFrame);
processUnbufferedInput(evt);
static Ogre::Real mMove = 100;//0.8; // The movement constant, unit/sec
Ogre::Vector3 transVector = Ogre::Vector3::ZERO;
btVector3 playerLV=btVector3(transVector.x,transVector.y,transVector.z);
if(!isPaused){
if(up){
if(player->getZ() > -240 + (player->l)/2.0f) //up
transVector.z += -mMove;
}
if(left){
//cout << player->getX();
if (player->getX() > -120 + (player->w)/2.0f) // left
transVector.x += -mMove;
}
if(down){
if (player->getZ() < 240 - (player->l)/2.0f) // Down
transVector.z += mMove;
}
if(right){
if (player->getX() < 120 - (player->w)/2.0f) // right
transVector.x += mMove;
}
//player->setPos( player->getPos() + (transVector) );
//player->setTransform(player->getPos());
//player->getNode()->translate(transVector, Ogre::Node::TS_LOCAL);
playerLV=btVector3(transVector.x,transVector.y,transVector.z);
}
//place camera
Ogre::Vector3 behindplayer;
if(networkObject->curState==GAME_CLIENT){
behindplayer = player2->getPos() + Ogre::Vector3(0,60,200);
}
else{
behindplayer = player->getPos() + Ogre::Vector3(0,60,200);
}
mCamera->setPosition(behindplayer);
// Look back along -Z
Ogre::Vector3 target = behindplayer + Ogre::Vector3(0,-10,-10);
//mCamera->lookAt(Ogre::Vector3(0,0,-200));
//packetswap
if(networkObject->connected){
int packetsize;
if(networkObject->curState==GAME_CLIENT){
packetsize=sizeof(btVector3);
clientPacket(networkObject->toSendPacket,&playerLV);
}else{
packetsize=sizeof(btVector3)*3;
serverPacket(networkObject->toSendPacket);
}
//cout << "to send packet: ";
//networkObject->printpacket(networkObject->toSendPacket);
networkObject->sendPacket(packetsize);
networkObject->receivePacket();
}
if(!isPaused){
updateFromPacket();
player->getBody()->setLinearVelocity(playerLV);
//simulator step
if(networkObject->curState!=GAME_CLIENT){
//if(networkObject->curState!=GAME_SINGLE)
sim->stepSimulation(evt.timeSinceLastFrame,10,1./60.);
}
else{
player2->setTransform(playerLV);
}
//updateFromPacket();
//player->updateTransform();
if(player->getBody()->hasContactResponse()){}
gui->incrementTics();
}
counter++;
return true;
}
void BaseApplication::updateFromPacket(){
btVector3 * bp, *pp, *p2p;
if(networkObject->curState==GAME_SERVER){
p2p=(btVector3 *)(networkObject->toRecPacket);
player2->getBody()->setLinearVelocity(*p2p);
//player2->setPos(*p2p);
// player2->updateTransform();
if(counter%60==0){
//cout << "P2:" <<p2p->getX() <<","<< p2p->getY() <<","<<p2p->getZ() <<"\n";
}
}else if(networkObject->curState==GAME_CLIENT){
//networkObject->toRecPacket[0]
bp = (btVector3 *)(networkObject->toRecPacket);
pp = (btVector3 *)(networkObject->toRecPacket+sizeof(btVector3));
p2p = (btVector3 *)(networkObject->toRecPacket+2*sizeof(btVector3));
player->setPos(*pp);
player->getNode()->setPosition(Ogre::Vector3(pp->getX(),pp->getY(),pp->getZ()));
//cout << p2p->getX()<<" "<<p2p->getY()<<" "<<p2p->getZ();
player2->setPos(*p2p);
player2->getNode()->setPosition(Ogre::Vector3(p2p->getX(),p2p->getY(),p2p->getZ()));
// player->updateTransform();
ball->setPos(*bp);
ball->getNode()->setPosition(Ogre::Vector3(bp->getX(),bp->getY(),bp->getZ()));
// ball->updateTransform();
//btVector3 * btpp=(btVector3 *)bp;
/*if(counter%60==0){
cout << "BP:"<<bp->getX() <<","<< bp->getY() <<","<<bp->getZ() <<"\n";
cout << "PP:"<<pp->getX() <<","<< pp->getY() <<","<<pp->getZ() <<"\n";
cout << "P2P:"<<p2p->getX() <<","<< p2p->getY() <<","<<p2p->getZ() <<"\n";
}*/
}
}
void BaseApplication::clientPacket(char * b,btVector3* bv){
//btVector3 bp=btVector3(ball->getPos().x,ball->getPos().y,ball->getPos().z);
//btVector3 pp=btVector3(player2->getPos().x,player2->getPos().y,player2->getPos().z);
memcpy(b,bv,sizeof(btVector3));
//memcpy(b+sizeof(btVector3),&pp,sizeof(btVector3));
}
void BaseApplication::serverPacket(char * b){
btVector3 bp=btVector3(ball->getPos().x,ball->getPos().y,ball->getPos().z);
btVector3 pp=btVector3(player->getPos().x,player->getPos().y,player->getPos().z);
btVector3 p2p=btVector3(player2->getPos().x,player2->getPos().y,player2->getPos().z);
//cout << player2->getPos().x<<","<<player2->getPos().y<<","<<player2->getPos().z<<"\n";
memcpy(b,&bp,sizeof(btVector3));
memcpy(b+sizeof(btVector3),&pp,sizeof(btVector3));
memcpy(b+sizeof(btVector3)*2,&p2p,sizeof(btVector3));
}
void BaseApplication::togglemouse(){
mcapture=!mcapture;
}
//-------------------------------------------------------------------------------------
bool BaseApplication::keyPressed( const OIS::KeyEvent &arg )
{
if (arg.key == OIS::KC_ESCAPE)
{
mShutDown = true;
}
else if(arg.key == OIS::KC_9)
{
sounds->pauseMusic();
}
else if(arg.key == OIS::KC_M)
{
sounds->playEffect(1);
}
else if(arg.key == OIS::KC_1)
{
sounds->enableSound();
}
else if (arg.key == OIS::KC_P)
{
if(isPaused)
isPaused=false;
else
isPaused=true;
}
else if(arg.key == OIS::KC_W){
up=true;
}
else if(arg.key == OIS::KC_A){
left=true;
}
else if(arg.key == OIS::KC_S){
down=true;
}
else if(arg.key == OIS::KC_D){
right=true;
}
CEGUI::GUIContext& context = CEGUI::System::getSingleton().getDefaultGUIContext();
context.injectKeyDown((CEGUI::Key::Scan)arg.key);
context.injectChar((CEGUI::Key::Scan)arg.text);
return true;
}
bool BaseApplication::processUnbufferedInput(const Ogre::FrameEvent& evt)
{
}
bool BaseApplication::keyReleased( const OIS::KeyEvent &arg )
{
if(arg.key == OIS::KC_W){
up=false;
}
else if(arg.key == OIS::KC_A){
left=false;
}
else if(arg.key == OIS::KC_S){
down=false;
}
else if(arg.key == OIS::KC_D){
right=false;
}
CEGUI::System::getSingleton().getDefaultGUIContext().injectKeyUp((CEGUI::Key::Scan)arg.key);
return true;
}
bool BaseApplication::mouseMoved( const OIS::MouseEvent &arg )
{
CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseMove(arg.state.X.rel, arg.state.Y.rel);
// Scroll wheel.
if (arg.state.Z.rel)
CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseWheelChange(arg.state.Z.rel / 120.0f);
return true;
}
bool BaseApplication::mousePressed( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonDown(convertButton(id));
return true;
}
bool BaseApplication::mouseReleased( const OIS::MouseEvent &arg, OIS::MouseButtonID id )
{
CEGUI::System::getSingleton().getDefaultGUIContext().injectMouseButtonUp(convertButton(id));
return true;
}
//Adjust mouse clipping area
void BaseApplication::windowResized(Ogre::RenderWindow* rw)
{
unsigned int width, height, depth;
int left, top;
rw->getMetrics(width, height, depth, left, top);
const OIS::MouseState &ms = mMouse->getMouseState();
ms.width = width;
ms.height = height;
}
//Unattach OIS before window shutdown (very important under Linux)
void BaseApplication::windowClosed(Ogre::RenderWindow* rw)
{
//Only close for window that created OIS (the main window in these demos)
if( rw == mWindow )
{
if( mInputManager )
{
mInputManager->destroyInputObject( mMouse );
mInputManager->destroyInputObject( mKeyboard );
OIS::InputManager::destroyInputSystem(mInputManager);
mInputManager = 0;
}
}
}
/*
void BaseApplication::changeNetworkState(NetworkState in){
ns = in;
}
*/
void BaseApplication::togglePause(){
if(isPaused)
isPaused=false;
else
isPaused=true;
}
void BaseApplication::pause(){
isPaused=true;
}
void BaseApplication::unpause(){
isPaused=false;
}
void BaseApplication::insertP2(){
player2= new Player(mSceneMgr,"Player2");
player->setTransform(player->pos1);
player->setPos(player->pos1);
player2->setTransform(player->pos2);
player2->setPos(player->pos2);
sim->addObject(player2);
player->updateTransform();
player2->updateTransform();
}
//```````````````````````````
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
#else
int main(int argc, char *argv[])
#endif
{
//parse args
// Create application object
BaseApplication app;
try {
app.go();
} catch( Ogre::Exception& e ) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
MessageBox( NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
std::cerr << "An exception has occured: " <<
e.getFullDescription().c_str() << std::endl;
#endif
}
app.networkObject->closeConnections();
return 0;
}
#ifdef __cplusplus
}
#endif