Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/story_entities/simple_game_menu.cc @ 7131

Last change on this file since 7131 was 7131, checked in by bensch, 18 years ago

orxonox/trunk: framerate calculation is much more 'accurate' meaning, if it is falling from one value into another one, the rate will be averanged to the middle.

File size: 15.2 KB
RevLine 
[6501]1/*
2   orxonox - the future of 3D-vertical-scrollers
3
4   Copyright (C) 2004 orx
5
6   This program is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10
11### File Specific:
12   main-programmer: Patrick Boenzli
13
14*/
15
16#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WORLD
17
18
19#include "simple_game_menu.h"
20
[6502]21#include "state.h"
22#include "class_list.h"
23
24#include "load_param.h"
25#include "fast_factory.h"
26#include "factory.h"
27
[6521]28#include "p_node.h"
[6502]29#include "world_entity.h"
[7016]30#include "elements/image_entity.h"
[6502]31#include "terrain.h"
[6521]32#include "camera.h"
[6502]33
[6504]34#include "event_handler.h"
[6524]35#include "graphics_engine.h"
[6841]36#include "object_manager.h"
[6504]37
[6841]38
[6502]39#include "cd_engine.h"
40
41
[6501]42using namespace std;
43
44
[6502]45//! This creates a Factory to fabricate a SimpleGameMenu
46CREATE_FACTORY(SimpleGameMenu, CL_SIMPLE_GAME_MENU);
47
48
49
50SimpleGameMenu::SimpleGameMenu(const TiXmlElement* root)
[6989]51  : GameWorld()
[6502]52{
53  this->setClassID(CL_SIMPLE_GAME_MENU, "SimpleGameMenu");
54  this->setName("SimpleGameMenu uninitialized");
55
56  this->dataTank = new SimpleGameMenuData();
57
[6521]58  this->cameraVector = Vector(50.0, 0.0, 0.0);
[6991]59  this->menuLayers.push_back(MenuLayer());
60  this->menuLayers.push_back(MenuLayer());
[6862]61
[6837]62  this->layerIndex = 0;
[6862]63  this->menuSelectedIndex = 0;
[6521]64
[6502]65  this->loadParams(root);
[6878]66
[7032]67  State::setMenuID(this->getNextStoryID());
[6502]68}
69
70
[6501]71/**
[6502]72 *  remove the SimpleGameMenu from memory
73 *
74 *  delete everything explicitly, that isn't contained in the parenting tree!
75 *  things contained in the tree are deleted automaticaly
[6501]76 */
[6502]77SimpleGameMenu::~SimpleGameMenu ()
78{
79  PRINTF(3)("SimpleGameMenu::~SimpleGameMenu() - deleting current world\n");
[6501]80
[6502]81  if( this->dataTank)
82    delete this->dataTank;
83}
[6501]84
[6502]85
86/**
87 * loads the parameters of a SimpleGameMenu from an XML-element
88 * @param root the XML-element to load from
[6501]89 */
[6502]90void SimpleGameMenu::loadParams(const TiXmlElement* root)
91{
92  /* skip the GameWorld, since it does not define any useful loadParams for this class */
[6696]93  //static_cast<GameWorld*>(this)->loadParams(root);
94  GameWorld::loadParams(root);
[6501]95
[6502]96  PRINTF(4)("Loaded SimpleGameMenu specific stuff\n");
97}
[6501]98
[6502]99
[6501]100/**
[6504]101 * this is executed just before load
102 *
103 * since the load function sometimes needs data, that has been initialized
104 * before the load and after the proceeding storyentity has finished
105 */
106ErrorMessage SimpleGameMenu::init()
107{
108  /* call underlying init funciton */
109  GameWorld::init();
110
111  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_UP);
112  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_DOWN);
113  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_RETURN);
114  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_SPACE);
[6854]115  EventHandler::getInstance()->subscribe(this, ES_MENU, SDLK_ESCAPE);
[6521]116
117  this->dataTank->localCamera->setRelCoor(this->cameraVector);
[6524]118
119  GraphicsEngine::getInstance()->displayFPS(false);
[6862]120
121  this->layerIndex = 0;
122  this->menuSelectedIndex = 0;
[6504]123}
124
125
[6524]126/**
127 * load the data
128 */
[6521]129ErrorMessage SimpleGameMenu::loadData()
[6504]130{
[6521]131  GameWorld::loadData();
[6504]132
[6845]133  if (this->dataXML != NULL)
134  {
135    TiXmlElement* element = this->dataXML->FirstChildElement("Elements");
136
137    if( element == NULL)
138    {
139      PRINTF(1)("SimpleGameMenu is missing 'Elements'\n");
140    }
141    else
142    {
143      element = element->FirstChildElement();
144    // load Players/Objects/Whatever
145      PRINTF(4)("Loading Elements\n");
146      while( element != NULL)
147      {
148        BaseObject* created = Factory::fabricate(element);
149        if( created != NULL )
[6852]150        {
151          PRINTF(4)("Created a %s::%s\n", created->getClassName(), created->getName());
152          if (!created->isA(CL_ELEMENT_2D))
153            PRINTF(2)("Error the Created Entity is not an Element2D but an %s::%s\n", created->getClassName(), created->getName());
154        }
[6851]155        element = element->NextSiblingElement();
[6845]156      }
157      PRINTF(4)("Done loading Elements\n");
158    }
159  }
160
[6520]161  /* get the menu list */
162  const std::list<BaseObject*>* imageEntityList = ClassList::getList(CL_IMAGE_ENTITY);
163  std::list<BaseObject*>::const_iterator entity;
164  for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++)
165  {
166
167    if( !strcmp("Selector_Menu", (*entity)->getName()))
168    {
169      this->menuSelector = dynamic_cast<ImageEntity*>(*entity);
[6878]170      this->menuSelector->setBindNode((const PNode*)NULL);
[6520]171    }
[7019]172  }
173
174  imageEntityList = ClassList::getList(CL_TEXT_ELEMENT);
175  for (entity = imageEntityList->begin(); entity != imageEntityList->end(); entity++)
176  {
177    if( !strcmp( "StartGame_Menu", (*entity)->getName()))
[6520]178    {
[7019]179      this->menuStartGame = dynamic_cast<TextElement*>(*entity);
[6878]180      this->menuStartGame->setBindNode((const PNode*)NULL);
[6883]181      this->menuStartGame->setRelCoor2D(State::getResX() / 2.0f,
182                                        State::getResY() / 2.0f - 60.0f,
183                                        0.0f);
[7019]184      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
[6835]185
[6520]186    }
187    else if( !strcmp( "Multiplayer_Menu", (*entity)->getName()))
188    {
[7019]189      this->menuStartMultiplayerGame = dynamic_cast<TextElement*>(*entity);
[6878]190      this->menuStartMultiplayerGame->setBindNode((const PNode*)NULL);
[6883]191      this->menuStartMultiplayerGame->setRelCoor2D(State::getResX() / 2.0f,
[6991]192                                                   State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 ) * 60.0f),
[6883]193                                                   0.0f);
[7019]194      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
[6520]195    }
196    else if( !strcmp( "Quit_Menu", (*entity)->getName()))
197    {
[7019]198      this->menuQuitGame = dynamic_cast<TextElement*>(*entity);
[6878]199      this->menuQuitGame->setBindNode((const PNode*)NULL);
[6883]200      this->menuQuitGame->setRelCoor2D(State::getResX() / 2.0f,
[6991]201                                       State::getResY() / 2.0f + ((this->menuLayers[0].menuList.size() -1 )* 60.0f),
[6883]202                                       0.0f);
[7019]203      this->menuLayers[0].menuList.push_back(dynamic_cast<TextElement*>(*entity));
[6520]204    }
205  }
[6980]206  this->menuSelected->getNullElement()->update2D(0.1f);
[6520]207  this->menuSelectedIndex = 0;
[6991]208  this->menuSelected = this->menuLayers[0].menuList[this->menuSelectedIndex];
[7063]209  this->sliderTo(this->menuSelected, 0.0f);
[6839]210
211
212  // loading the storyentities submenu (singleplayer)
213  const std::list<BaseObject*>* storyEntities = ClassList::getList(CL_STORY_ENTITY);
214  std::list<BaseObject*>::const_iterator it;
215  for( it = storyEntities->begin(); it != storyEntities->end(); it++)
216  {
217    StoryEntity* se = dynamic_cast<StoryEntity*>(*it);
218    if( se->isContainedInMenu())
219    {
[6991]220      this->menuLayers[1].storyList.push_back(se);
[6874]221
222      // generating menu item
[7019]223      TextElement* te = new TextElement();
224      te->setVisibility(false);
225      te->setText(se->getName());
226      te->setRelCoor2D(State::getResX() / 2.0f - 200.0f, State::getResY() / 2.0f + ((this->menuLayers[1].menuList.size() - 2.0f) * 60.0f), 0.0f);
227      this->menuLayers[1].menuList.push_back(te);
228
229      // generating screenshoot item
[6841]230      ImageEntity* ie = new ImageEntity();
[6848]231      ie->setVisibility(false);
[6878]232      ie->setBindNode((const PNode*)NULL);
233      ie->setTexture(se->getMenuScreenshoot());
[6883]234      ie->setRelCoor2D(State::getResX() / 2.0f + 250.0f, State::getResY() / 2.0f, 0.0f);
[6884]235      ie->setSize2D(140.0f, 105.0f);
[6991]236      this->menuLayers[1].screenshootList.push_back(ie);
[6839]237    }
238  }
[6521]239}
[6520]240
[6521]241
[6862]242ErrorMessage SimpleGameMenu::unloadData()
243{
[6521]244
[6862]245  EventHandler::getInstance()->unsubscribe(this, ES_MENU);
246
[6991]247  std::vector<MenuLayer>::iterator mit;
248  for(mit = this->menuLayers.begin(); mit != this->menuLayers.end(); mit++)
[6862]249  {
[7019]250    while(!(*mit).menuList.empty())
251    {
252      delete (*mit).menuList.back();
253      (*mit).menuList.pop_back();
254    }
255
256    (*mit).menuList.clear();
257    (*mit).storyList.clear();
[6991]258    (*mit).screenshootList.clear();
[6862]259  }
[7029]260
261
262  GameWorld::unloadData();
[6862]263}
264
265
[6524]266/**
267 * start the menu
268 */
[6521]269bool SimpleGameMenu::start()
270{
271  EventHandler::getInstance()->pushState(ES_MENU);
272
[6504]273  /* now call the underlying*/
274  GameWorld::start();
275}
276
277
278
[6524]279/**
280 * stop the menu
281 */
[6504]282bool SimpleGameMenu::stop()
283{
284  EventHandler::getInstance()->popState();
285
286  /* now call the underlying*/
287  GameWorld::stop();
288}
289
290
[6521]291/**
292 *  override the standard tick for more functionality
293 */
294void SimpleGameMenu::tick()
295{
296  GameWorld::tick();
[6504]297
[7131]298  this->animateScene(this->dtS);
[6521]299}
300
301
[6504]302/**
[6521]303 *  no collision detection in the menu
[6501]304 */
[6502]305void SimpleGameMenu::collide()
[6521]306{
307//   this->dataTank->localCamera->
308}
[6501]309
310
[6504]311/**
[6521]312 *  animate the scene
313 */
314void SimpleGameMenu::animateScene(float dt)
315{
[7131]316  Quaternion q(/*0.00005*/ dt * .1, Vector(0.0, 1.0, 0.0));
[6521]317  this->cameraVector = q.apply(this->cameraVector);
318  this->dataTank->localCamera->setRelCoor(this->cameraVector);
319  this->dataTank->localCamera->getTarget()->setRelCoorSoft(0,0,0);
320}
321
322
323/**
[6504]324 * event dispatcher funciton
325 * @param event the incoming event
326 */
327void SimpleGameMenu::process(const Event &event)
328{
[6839]329  /* ----------------- LAYER 1 ---------------*/
[6837]330  if( this->layerIndex == 0)
[6504]331  {
[6837]332    if( event.type == SDLK_RETURN && event.bPressed == true)
[6520]333    {
[6837]334      if( this->menuSelected == this->menuQuitGame)
335      {
336        this->setNextStoryID(WORLD_ID_GAMEEND);
337        this->stop();
338      }
339      if( this->menuSelected == this->menuStartGame)
340      {
341        // switch to first submenu
[6991]342        if( this->menuLayers[1].menuList.size() == 0)
[6837]343        {
[6839]344          PRINTF(1)("Haven't got any StoryEntities to play!\n");
[6837]345          return;
346        }
347
348        this->switchMenuLayer(this->layerIndex, 1);
349      }
[6520]350    }
[6854]351    if( event.type == SDLK_ESCAPE && event.bPressed == true)
352    {
353      this->setNextStoryID(WORLD_ID_GAMEEND);
354      this->stop();
355    }
[6839]356  }  /* ----------------- LAYER 2 ---------------*/
[6837]357  else if( this->layerIndex == 1)
[6520]358  {
[6848]359    if( event.type == SDLK_RETURN && event.bPressed == true)
360    {
[6991]361      this->setNextStoryID( this->menuLayers[1].storyList[this->menuSelectedIndex]->getStoryID());
[6848]362      this->stop();
363    }
[6854]364    if( event.type == SDLK_ESCAPE && event.bPressed == true)
365    {
366      this->switchMenuLayer(this->layerIndex, 0);
367    }
[6520]368  }
[6853]369
370
371
372  // The menu selction cursor
373  if( event.type == SDLK_DOWN && event.bPressed == true)
374  {
[6991]375    if(this->menuSelectedIndex < (this->menuLayers[this->layerIndex].menuList.size() - 1))
[6853]376    {
[6991]377      this->menuSelected = this->menuLayers[this->layerIndex].menuList[++this->menuSelectedIndex];
[7063]378      this->sliderTo(this->menuSelected, 5.0f);
[6883]379
380      if( this->layerIndex == 1)
381      {
[6991]382        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
383        this->menuLayers[1].screenshootList[this->menuSelectedIndex-1]->setVisibility(false);
[6883]384      }
[6853]385    }
386  }
387  else if( event.type == SDLK_UP && event.bPressed == true)
388  {
389    if(this->menuSelectedIndex > 0)
390    {
[6991]391      this->menuSelected = this->menuLayers[this->layerIndex].menuList[--this->menuSelectedIndex];
[7063]392      this->sliderTo(this->menuSelected, 5.0f);
[6883]393
394      if( this->layerIndex == 1)
395      {
[6991]396        this->menuLayers[1].screenshootList[this->menuSelectedIndex]->setVisibility(true);
397        this->menuLayers[1].screenshootList[this->menuSelectedIndex+1]->setVisibility(false);
[6883]398      }
[6853]399    }
400  }
[6504]401}
[6502]402
403
[6837]404/**
405 *  switches to from one meny layer to an other
406 * @param layer1 from layer
407 * @param layer2 to layer
408 */
409void SimpleGameMenu::switchMenuLayer(int layer1, int layer2)
410{
411  // wrong sizes
[6991]412  if(layer1 >= this->menuLayers.size() || layer2 >= this->menuLayers.size())
[6837]413    return;
[6502]414
415
[6837]416  PRINTF(0)("Removing layer %i\n", layer1);
[7019]417  std::vector<TextElement*>::iterator te;
[6837]418  // fade old menu
[7019]419  for( te = this->menuLayers[layer1].menuList.begin(); te != this->menuLayers[layer1].menuList.end(); te++)
[6837]420  {
[7019]421    (*te)->setVisibility(false);
[6837]422  }
[7019]423
424  std::vector<ImageEntity*>::iterator it;
425
[6878]426  //also fade the screenshots if in level choosement mode
[6991]427  for( it = this->menuLayers[layer1].screenshootList.begin(); it != this->menuLayers[layer1].screenshootList.end(); it++)
[6878]428  {
429    (*it)->setVisibility(false);
430  }
[6502]431
432
[6837]433  PRINTF(0)("Showing layer %i\n", layer1);
434  // beam here the new menu
[7019]435  for( te = this->menuLayers[layer2].menuList.begin(); te != this->menuLayers[layer2].menuList.end(); te++ )
[6848]436  {
[7019]437    (*te)->setVisibility(true);
[6848]438  }
[6837]439
[6878]440
[6837]441  this->layerIndex = layer2;
[6991]442  this->menuSelected = this->menuLayers[layer2].menuList[0];
[6878]443  this->menuSelector->setAbsCoor2D(this->menuSelected->getAbsCoor2D());
[6855]444  this->menuSelectedIndex = 0;
[6883]445
446  if( layer2 == 1)
[6991]447    this->menuLayers[layer2].screenshootList[0]->setVisibility(true);
[6837]448}
449
[7063]450void SimpleGameMenu::sliderTo(const Element2D* element, float bias)
451{
452  if (bias > 0.0)
453  {
454    this->menuSelector->setAbsCoorSoft2D(element->getAbsCoor2D() + Vector(0, element->getSizeY2D() *.5,0), bias);
455    this->menuSelector->setSizeSoft2D(element->getSizeX2D(), element->getSizeY2D(), bias);
456  }
457  else
458  {
459    this->menuSelector->setAbsCoor2D(element->getAbsCoor2D() + Vector(0, element->getSizeY2D() *.5,0));
460    this->menuSelector->setSize2D(element->getSizeX2D(), element->getSizeY2D());
461  }
462}
[6837]463
464
465
[6502]466/**********************************************************************************************
467    SimpleGameMenuData
468 **********************************************************************************************/
469
470
[6501]471/**
[6502]472 * SimpleGameMenuData constructor
[6501]473 */
[6502]474SimpleGameMenuData::SimpleGameMenuData()
[6501]475{}
476
477/**
[6502]478 * SimpleGameMenuData decontructor
[6501]479 */
[6502]480SimpleGameMenuData::~SimpleGameMenuData()
[6501]481{}
482
483
484/**
[6502]485 *  initialize the GameWorldDataData
[6501]486 */
[6502]487ErrorMessage SimpleGameMenuData::init()
488{
489  /* call underlying function */
490  GameWorldData::init();
491}
[6501]492
493
494/**
[6502]495 *  loads the GUI data
496 * @param root reference to the xml root element
[6501]497 */
[6502]498ErrorMessage SimpleGameMenuData::loadGUI(TiXmlElement* root)
499{
500  /* call underlying function */
501  GameWorldData::loadGUI(root);
502}
[6501]503
504
505/**
[6502]506 *  unloads the GUI data
[6501]507 */
[6502]508ErrorMessage SimpleGameMenuData::unloadGUI()
509{
510  /* call underlying function */
511  GameWorldData::unloadGUI();
512}
513
514
515/**
516 *  overloads the GameWorld::loadWorldEntities(...) class since the menu WorldEntity loading is different (less loading stuff)
517 * @param root reference to the xml root parameter
518 */
519ErrorMessage SimpleGameMenuData::loadWorldEntities(TiXmlElement* root)
520{
521  TiXmlElement* element = root->FirstChildElement("WorldEntities");
522
523  if( element != NULL)
524  {
525    element = element->FirstChildElement();
526    PRINTF(4)("Loading WorldEntities\n");
527    while( element != NULL)
528    {
529      BaseObject* created = Factory::fabricate(element);
530      if( created != NULL )
531        printf("Created a %s: %s\n", created->getClassName(), created->getName());
532
533      if( element->Value() != NULL && !strcmp( element->Value(), "SkyBox"))
534        this->sky = dynamic_cast<WorldEntity*>(created);
535      if( element->Value() != NULL && !strcmp( element->Value(), "Terrain"))
536        this->terrain = dynamic_cast<Terrain*>(created);
537      element = element->NextSiblingElement();
538    }
539    PRINTF(4)("Done loading WorldEntities\n");
540  }
541
542  /* init the pnode tree */
543  PNode::getNullParent()->init();
544}
545
546
547/**
548 *  unloads the world entities from the xml file
549 */
550ErrorMessage SimpleGameMenuData::unloadWorldEntities()
551{
552  /* call underlying function */
553  GameWorldData::unloadWorldEntities();
554}
555
556
557/**
558 *  loads the scene data
559 * @param root reference to the xml root element
560 */
561ErrorMessage SimpleGameMenuData::loadScene(TiXmlElement* root)
562{
563  /* call underlying function */
564  GameWorldData::loadScene(root);
565}
566
567
568/**
569 *  unloads the scene data
570 */
571ErrorMessage SimpleGameMenuData::unloadScene()
572{
573  /* call underlying function */
574  GameWorldData::unloadScene();
575}
576
577
578
Note: See TracBrowser for help on using the repository browser.