00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00031 #include <game.h>
00032 #include <startmenu.h>
00033
00104
00105 coeur::coeur() : base("coeur")
00106 {
00107 py = new pyInt;
00108 in = new interface;
00109 co = new console;
00110 mycredit = new credit;
00111
00112 currentChap = new chap;
00113 currentLieu = new lieu;
00114 currentLocal = new local;
00115
00116 mylogger = new logger;
00117
00118 pathChap = new std::vector<stringc*>;
00119 pathLieu = new std::vector<stringc*>;
00120 pathLocal = new std::vector<stringc*>;
00121
00122 dr = EDT_NULL;
00123 dim = dimension2d<s32>(640, 480);
00124 bits = 8;
00125 fullscreen = false;
00126 stencilbuffer = false;
00127 vsync = false;
00128
00129 lang = 0;
00130 }
00131
00133 coeur::~coeur()
00134 {
00135 delete pathLocal;
00136 delete pathLieu;
00137 delete pathChap;
00138
00139 delete currentLocal;
00140 delete currentLieu;
00141 delete currentChap;
00142
00143 delete mylogger;
00144
00145 delete mycredit;
00146 delete co;
00147 delete in;
00148 delete py;
00149 }
00150
00154 int coeur::init(startmenu* cb)
00155 {
00156
00157 cb->progress();
00158
00159 bool classic;
00160
00161 stringc pathMain = "";
00162
00163 stringc pathConfig = "config.xml";
00164
00165 IXMLReader* xmlConfig = cb->getFileSystem()->createXMLReader(pathConfig.c_str());
00166
00167 cb->progress();
00168 while(xmlConfig && xmlConfig->read())
00169 {
00170 if (xmlConfig->getNodeType()==EXN_ELEMENT)
00171 {
00172 if (core::stringw("device") == xmlConfig->getNodeName())
00173 {
00174 dr = E_DRIVER_TYPE(xmlConfig->getAttributeValueAsInt(L"driver"));
00175 bits = xmlConfig->getAttributeValueAsInt(L"bits");
00176 fullscreen = xmlConfig->getAttributeValueAsInt(L"fs");
00177 stencilbuffer = xmlConfig->getAttributeValueAsInt(L"sb");
00178 vsync = xmlConfig->getAttributeValueAsInt(L"vs");
00179 }
00180 else if (core::stringw("dimension") == xmlConfig->getNodeName())
00181 {
00182 dim = dimension2d<s32>(xmlConfig->getAttributeValueAsInt(L"width"),xmlConfig->getAttributeValueAsInt(L"height"));
00183 }
00184 else if (core::stringw("local") == xmlConfig->getNodeName())
00185 {
00186 lang = xmlConfig->getAttributeValueAsInt(L"lang");
00187 }
00188 else if (core::stringw("main") == xmlConfig->getNodeName())
00189 {
00190 pathMain = xmlConfig->getAttributeValue(L"file");
00191 }
00192 else if (core::stringw("interface") == xmlConfig->getNodeName())
00193 {
00194 classic = xmlConfig->getAttributeValueAsInt(L"classic")==1;
00195 }
00196 }
00197 }
00198
00199 xmlConfig->drop();
00200
00201 device = createDevice(dr,dim,bits,fullscreen,stencilbuffer,vsync);
00202
00203 #ifdef AUDIO
00204 engine = createIrrKlangDevice();
00205 #endif
00206
00207 driver = device->getVideoDriver();
00208 smgr = device->getSceneManager();
00209 env = device->getGUIEnvironment();
00210
00211 mylogger->init(this);
00212 base::init(0,this);
00213
00214 log(stringc("Main xml : ") + pathMain + " loading ...");
00215
00216 pathGame = device->getFileSystem()->getAbsolutePath(device->getFileSystem()->getFileDir(pathMain)).c_str();
00217 device->getFileSystem()->addFolderFileArchive(pathGame.c_str());
00218
00219 IXMLReader* xmlMain = device->getFileSystem()->createXMLReader(pathMain.c_str());
00220
00221 stringc pathInterface = "";
00222 stringc pathCredit = "";
00223
00224 cb->progress();
00225 while(xmlMain && xmlMain->read())
00226 {
00227 if (xmlMain->getNodeType()==EXN_ELEMENT)
00228 {
00229 if (core::stringw("main") == xmlMain->getNodeName())
00230 {
00231 pathChap->reserve(xmlMain->getAttributeValueAsInt(L"nbrchap"));
00232 pathLieu->reserve(xmlMain->getAttributeValueAsInt(L"nbrlieu"));
00233 pathLocal->reserve(xmlMain->getAttributeValueAsInt(L"nbrlocal"));
00234 }
00235 else if (core::stringw("interface") == xmlMain->getNodeName())
00236 {
00237 pathInterface = xmlMain->getAttributeValue(L"file");
00238 log( stringc("pathInterface : ") + pathInterface.c_str());
00239 }
00240 else if (core::stringw("chap") == xmlMain->getNodeName())
00241 {
00242 pathChap->push_back(new stringc(xmlMain->getAttributeValue(L"file")));
00243 log( stringc("pathChap : ") + pathInterface.c_str());
00244 }
00245 else if (core::stringw("lieu") == xmlMain->getNodeName())
00246 {
00247 pathLieu->push_back(new stringc(xmlMain->getAttributeValue(L"file")));
00248 log( stringc("pathLieu : ") + (*pathLieu)[pathLieu->size()-1]->c_str());
00249 }
00250 else if (core::stringw("local") == xmlMain->getNodeName())
00251 {
00252 pathLocal->push_back(new stringc(xmlMain->getAttributeValue(L"file")));
00253 log( stringc("pathLocal : ") + (*pathLocal)[pathLocal->size()-1]->c_str());
00254 }
00255 else if (core::stringw("credit") == xmlMain->getNodeName())
00256 {
00257 pathCredit = xmlMain->getAttributeValue(L"file");
00258 log( stringc("pathCredit : ") + pathCredit.c_str() );
00259 }
00260 }
00261 }
00262
00263 xmlMain->drop();
00264
00265 cb->progress();
00266 metaSelector = smgr->createMetaTriangleSelector();
00267
00268 cam = smgr->addCameraSceneNodeFPS();
00269 cam->setPosition(vector3df(0,150,-200));
00270 device->getCursorControl()->setVisible(false);
00271 log("Camera ajoute");
00272
00273 q = false;
00274
00275 cb->progress();
00276 IXMLReader* xmlInterface = device->getFileSystem()->createXMLReader(pathInterface.c_str());
00277 if (xmlInterface)
00278 {
00279 in->init(this,env,xmlInterface,classic);
00280 xmlInterface->drop();
00281 log("Interface initialized");
00282 }
00283 else
00284 {
00285 log(stringc("Problem loading interface xml :") + pathInterface + ", exiting...");
00286 return 1;
00287 }
00288 cb->progress();
00289 IXMLReader* xmlCredit = device->getFileSystem()->createXMLReader(pathCredit.c_str());
00290 mycredit->init(this,env,xmlCredit);
00291 xmlCredit->drop();
00292 log("Credit initialized");
00293
00294 cb->progress();
00295 loadChap(0);
00296 log("Chapitre load");
00297
00298 cb->progress();
00299 loadLocal(lang);
00300 log("Local load");
00301
00302 setCaption(currentLocal->getCaption() + " - " + VERSION);
00303
00304 py->init(this);
00305 log("Python initialized");
00306
00307 cb->progress();
00308 co->init(this,env);
00309 log("Console initialized");
00310
00311 cb->progress();
00312 device->setEventReceiver(this);
00313 log("Event receiver on");
00314
00315 ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(metaSelector, cam, vector3df(10,40,8), vector3df(0,-980,0), vector3df(0,40,0));
00316 cam->addAnimator(anim);
00317 anim->drop();
00318
00319 selectedAction=0;
00320
00321 smgr->setShadowColor(video::SColor(150,0,0,0));
00322
00323 activeScene = true;
00324 activeIn = true;
00325
00326 return 0;
00327 }
00328
00329 ISceneNode* coeur::getSelectedNode()
00330 {
00331 line3df line = smgr->getSceneCollisionManager()->getRayFromScreenCoordinates(curPos);
00332
00333 ISceneNode* best = 0;
00334 f32 dist = 1000000;
00335
00336 pickNode(smgr->getRootSceneNode(), line, dist, best);
00337
00338 return best;
00339 }
00340
00341 void coeur::pickNode(ISceneNode* root,line3df ray,f32& outbestdistance,ISceneNode*& outbestnode)
00342 {
00343 const core::list<ISceneNode*>& children = root->getChildren();
00344 core::list<ISceneNode*>::ConstIterator it = children.begin();
00345 for (; it != children.end(); ++it)
00346 {
00347 ISceneNode* current = *it;
00348
00349 if (current->isVisible() && !current->isDebugObject() && current->getID() != -1)
00350 {
00351 vector3df p;
00352 triangle3df t;
00353 if (smgr->getSceneCollisionManager()->getCollisionPoint(ray,current->getTriangleSelector(),p,t))
00354 {
00355 f32 distance = p.getDistanceFrom(ray.start);
00356 if (distance < outbestdistance)
00357 {
00358 outbestnode = current;
00359 outbestdistance = distance;
00360 }
00361 }
00362 }
00363 pickNode(current, ray, outbestdistance, outbestnode);
00364 }
00365 }
00366
00369 int coeur::update()
00370 {
00371 int r = device->run();
00372 if (r && device->isWindowActive())
00373 {
00374 curPos=device->getCursorControl()->getPosition();
00375
00376 selectedAction = getActionMesh(getSelectedNode());
00377
00378 if (activeIn)
00379 in->update();
00380 co->update();
00381 mycredit->update();
00382 driver->beginScene(true, true, SColor(0,0,0,0));
00383
00384 if (activeScene)
00385 smgr->drawAll();
00386 env->drawAll();
00387
00388 driver->endScene();
00389 if (q)
00390 {
00391 log("Quitting");
00392 return 0;
00393 }
00394 }
00395 return r;
00396 }
00397
00398 int coeur::close()
00399 {
00400 co->close();
00401 currentLocal->close();
00402 currentLieu->close();
00403 currentChap->close();
00404 in->close();
00405 mycredit->close();
00406 log("All subclass closed");
00407
00408 while (! pathChap->empty())
00409 {
00410 log(stringc("Deleting last pathChap"));
00411 delete pathChap->back();
00412 pathChap->pop_back();
00413 }
00414 while (! pathLieu->empty())
00415 {
00416 log(stringc("Deleting last pathLieu"));
00417 delete pathLieu->back();
00418 pathLieu->pop_back();
00419 }
00420 while (! pathLocal->empty())
00421 {
00422 log(stringc("Deleting last pathChap"));
00423 delete pathLocal->back();
00424 pathLocal->pop_back();
00425 }
00426 log("All path have been deleted");
00427
00428 py->close();
00429
00430 log("coeur is going to close itself and the logger");
00431 log("Irrlicht device and irrklang engine will be drop too");
00432 base::close();
00433 mylogger->close();
00434
00435 device->drop();
00436 #ifdef AUDIO
00437 engine->drop();
00438 #endif
00439 return 1;
00440 }
00441
00442 int coeur::setCaption(stringw caption)
00443 {
00444 device->setWindowCaption(caption.c_str());
00445 log(stringc("Change window caption to ") + caption.c_str());
00446 return 1;
00447 }
00448
00449 IAnimatedMeshSceneNode* coeur::addMesh(stringc pathm)
00450 {
00451 IAnimatedMesh* mesh = smgr->getMesh(pathm.c_str());
00452 if (!mesh)
00453 return 0;
00454
00455 IAnimatedMeshSceneNode* node = smgr->addAnimatedMeshSceneNode(mesh);
00456 node->setFrameLoop(0, 0);
00457 node->setLoopMode(false);
00458
00459 ITriangleSelector* selector = smgr->createOctTreeTriangleSelector(mesh, node);
00460 node->setTriangleSelector(selector);
00461 metaSelector->addTriangleSelector(selector);
00462 selector->drop();
00463
00464 node->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);
00465 node->addShadowVolumeSceneNode();
00466
00467 if (!node)
00468 return 0;
00469 log(stringc("New mesh added : \"") + pathm + "\" number of frame :" + stringc(node->getEndFrame()));
00470 return node;
00471 }
00472
00473 ILightSceneNode* coeur::addLight(int radius)
00474 {
00475 ILightSceneNode* light = smgr->addLightSceneNode(0,vector3df(0,0,0),SColorf(0,0,0,0),radius);
00476
00477 return light;
00478 }
00479
00480 ITexture* coeur::getTexture(stringc path)
00481 {
00482 log(stringc("Get texture ") + path);
00483 return driver->getTexture(path.c_str());
00484 }
00485
00486 dimension2d<s32> coeur::getDim()
00487 {
00488 return dim;
00489 }
00490
00491 actionMesh *coeur::getSelectedAction()
00492 {
00493 return selectedAction;
00494 }
00495
00496 position2d<s32> coeur::getCurPos()
00497 {
00498 return curPos;
00499 }
00500
00501 u32 coeur::getTime()
00502 {
00503 return device->getTimer()->getRealTime();
00504 }
00505
00506
00507 int coeur::play(stringc path)
00508 {
00509 log(stringc("Play sound ") + path);
00510 #ifdef AUDIO
00511 engine->play2D((pathGame + "/" + path).c_str(), false);
00512 #endif
00513 return 1;
00514 }
00515
00516 bool coeur::OnEvent(const SEvent& event)
00517 {
00518 if (co->OnEvent(event))
00519 return false;
00520 if (mycredit->OnEvent(event))
00521 return false;
00522 if (event.EventType==EET_KEY_INPUT_EVENT)
00523 {
00524 if (event.KeyInput.PressedDown)
00525 {
00526 switch(event.KeyInput.Key)
00527 {
00528 case KEY_KEY_Q:
00529 q = true;
00530 return true;
00531 case KEY_KEY_G:
00532 startCredit();
00533 return true;
00534 case KEY_KEY_M:
00535 cam->setInputReceiverEnabled(false);
00536 return true;
00537 default:
00538 break;
00539 }
00540 }
00541 else if (event.KeyInput.Key == KEY_KEY_M)
00542 {
00543 cam->setInputReceiverEnabled(true);
00544 return true;
00545 }
00546 }
00547 if (in->OnEvent(event))
00548 return true;
00549 return false;
00550 }
00551
00552 int coeur::loadChap(int i)
00553 {
00554 if (currentChap->isInit())
00555 currentChap->close();
00556 IXMLReader* xmlChap = device->getFileSystem()->createXMLReader(pathChap->at(i)->c_str());
00557 int numLieu = currentChap->init(i,this,xmlChap);
00558 xmlChap->drop();
00559
00560 loadLieu(numLieu);
00561
00562 log(stringc("Chap ") + stringc(i) + " load");
00563
00564 return 1;
00565 }
00566
00567 int coeur::loadLieu(int i)
00568 {
00569 if (currentLieu->isInit())
00570 currentLieu->close();
00571 IXMLReader* xmlLieu = device->getFileSystem()->createXMLReader(pathLieu->at(i)->c_str());
00572 currentLieu->init(i,this,xmlLieu);
00573 xmlLieu->drop();
00574
00575 log(stringc("Lieu ") + stringc(i) + " load");
00576
00577 return 1;
00578 }
00579
00580 int coeur::loadLocal(int i)
00581 {
00582 if (currentLocal->isInit())
00583 currentLocal->close();
00584 IXMLReader* xmlLocal = device->getFileSystem()->createXMLReader(pathLocal->at(i)->c_str());
00585 currentLocal->init(i,this,xmlLocal);
00586 xmlLocal->drop();
00587
00588 log(stringc("Local ") + stringc(i) + " load");
00589
00590 return 1;
00591 }
00592
00593 int coeur::startCredit()
00594 {
00595 cam->setInputReceiverEnabled(false);
00596 activeScene = false;
00597 activeIn = false;
00598 mycredit->start();
00599 return 1;
00600 }
00601
00602 item coeur::getItem(int id)
00603 {
00604 return currentChap->getItemFromId(id);
00605 }
00606
00608 actionMesh* coeur::getActionMesh(int id)
00609 {
00610 return currentLieu->getActionMesh(id);
00611 }
00612
00613 actionMesh* coeur::getActionMesh(ISceneNode * node)
00614 {
00615 return currentLieu->getActionMesh(node);
00616 }
00617
00618 localItem* coeur::getObservationMesh(int id)
00619 {
00620 return currentLocal->getObservationMesh(id);
00621 }
00622
00623 localItem* coeur::getObservationItem(int id)
00624 {
00625 return currentLocal->getObservationItem(id);
00626 }
00627
00628 localItem* coeur::getCombinaison()
00629 {
00630 return currentLocal->getCombinaison();
00631 }
00632
00633 logger* coeur::getLogger()
00634 {
00635 return mylogger;
00636 }
00637
00638 stringc coeur::getVersion()
00639 {
00640 return stringc(VERSION);
00641 }
00642
00643 int coeur::anim(int id,int begin,int end)
00644 {
00645 IAnimatedMeshSceneNode* node = getActionMesh(id)->getNode();
00646
00647 if (!node)
00648 return -1;
00649
00650 node->setFrameLoop(begin,end);
00651
00652 metaSelector->removeTriangleSelector(node->getTriangleSelector());
00653
00654 ITriangleSelector* selector = smgr->createOctTreeTriangleSelector(node->getMesh()->getMesh(40), node);
00655 node->setTriangleSelector(selector);
00656 metaSelector->addTriangleSelector(selector);
00657 selector->drop();
00658
00659
00660 return 1;
00661 }
00662
00663 int coeur::setSkyBox(stringc top,stringc bottom,stringc left,stringc right,stringc front,stringc back)
00664 {
00665 smgr->addSkyBoxSceneNode (getTexture(top),getTexture(bottom),getTexture(left),getTexture(right),getTexture(front),getTexture(back));
00666 return 1;
00667 }
00668
00669
00670 IParticleSystemSceneNode* coeur::addParticleSystem()
00671 {
00672 return smgr->addParticleSystemSceneNode(false);
00673 }
00674
00675
00676
00677
00678