Guest

GameManager.cpp

By: a guest on Feb 27th, 2010  |  syntax: C++  |  size: 3.22 KB  |  hits: 282  |  expires: Never
download  |  raw  |  embed  |  report abuse
This paste has a previous version, view the difference. Copied
  1. //------------------------------------------------------------------------
  2. //
  3. //  Name:   GameManager.cpp
  4. //
  5. //  Desc:   Manages the game states.
  6. //
  7. //  Author: Nikko Bertoa 2010 (nicobertoa@gmail.com)
  8. //
  9. //------------------------------------------------------------------------
  10.  
  11.  
  12. //
  13. // Headers
  14. //
  15. #include "GameManager.h"
  16. #include <cassert>
  17. #include <sstream>
  18. #include "auxiliarFunctions.h"
  19. #include "MainMenuState.h"
  20.  
  21.  
  22. // Constructor
  23. GameManager::GameManager() :  m_pCurrentState(0), m_pPreviousState(0), m_pGlobalState(0),
  24.                               m_screen(/*sf::VideoMode::GetDesktopMode()*/sf::VideoMode(1024, 768, 32),
  25.                               "CaperucitaPlusPlus", sf::Style::Fullscreen | sf::Style::Close)
  26. {
  27.  
  28.     m_screen.ShowMouseCursor(false);
  29.  
  30.     // Resources
  31.     LoadImages(m_imgManager);
  32.  
  33.     // FPS
  34.     m_font = new sf::Font();
  35.     bool correctLoading = m_font->LoadFromFile("resources/fonts/arial.ttf");
  36.     assert(correctLoading);
  37.     m_fpsText = new sf::String("Test Text", *m_font);
  38.     m_fpsText->SetSize(30.0f);
  39.     float xCoord = 10.0f;
  40.     float yCoord = 10.0f;
  41.     m_fpsText->SetPosition(xCoord, yCoord);
  42.     m_fpsText->SetStyle(sf::String::Regular);
  43.     m_fpsText->SetColor(sf::Color::White);
  44.  
  45.     m_close = false;
  46.  
  47.     //Initial state
  48.     MainMenuState::Instance()->Init(m_screen, m_imgManager);
  49.     SetCurrentState(MainMenuState::Instance());
  50.  
  51. }
  52.  
  53.  
  54. // Destructor
  55. GameManager::~GameManager()
  56. {
  57.  
  58.     DestroyImages(m_imgManager);
  59.  
  60.     if(m_font != 0)
  61.     {
  62.        
  63.         delete m_font;
  64.         m_font = 0;
  65.  
  66.     }
  67.  
  68.     if(m_fpsText != 0)
  69.     {
  70.  
  71.         delete m_fpsText;
  72.         m_fpsText = 0;
  73.  
  74.     }
  75.                
  76. }
  77.  
  78.  
  79. void GameManager::Run()
  80. {
  81.  
  82.     // Start game loop
  83.     while (m_screen.IsOpened())
  84.     {
  85.         // Process events
  86.         sf::Event Event;
  87.         while (m_screen.GetEvent(Event))
  88.         {
  89.  
  90.             // Close window : exit
  91.             if (Event.Type == sf::Event::Closed)
  92.                 m_screen.Close();
  93.  
  94.         }
  95.  
  96.         // Clear the screen (fill it with black color)
  97.         m_screen.Clear();
  98.  
  99.         Update();
  100.  
  101.         DrawFPS();
  102.  
  103.         // Display window contents on screen
  104.         m_screen.Display();
  105.  
  106.         m_fpsManager.Update();
  107.  
  108.         //Close aplication?
  109.         if(m_close)
  110.             m_screen.Close();
  111.        
  112.     }
  113.  
  114. }
  115.  
  116.  
  117. void GameManager::Exit()
  118. {
  119.  
  120.     if(m_pCurrentState != 0)
  121.         m_pCurrentState->Clear();
  122.  
  123.     m_close = true;
  124.  
  125. }
  126.  
  127.  
  128. void GameManager::ChangeState(State* pNewState)
  129. {
  130.        
  131.     assert(pNewState && "<StateMachine::ChangeState>: trying to change to NULL state");
  132.  
  133.     //keep a record of the previous state
  134.     m_pPreviousState = m_pCurrentState;
  135.  
  136.     //call the exit method of the existing state
  137.     m_pCurrentState->Clear();
  138.  
  139.     //change state to the new state
  140.     m_pCurrentState = pNewState;
  141.  
  142.     //call the entry method of the new state
  143.     m_pCurrentState->Init(m_screen, m_imgManager);
  144.  
  145. }
  146.  
  147.  
  148. void GameManager::Update()
  149. {
  150.  
  151.     //if a global state exists, call its execute method, else do nothing
  152.     if(m_pGlobalState)  
  153.         m_pGlobalState->Execute(this);
  154.  
  155.     //same for the current state
  156.     if (m_pCurrentState)
  157.         m_pCurrentState->Execute(this);
  158.  
  159. }
  160.  
  161.  
  162. void GameManager::DrawFPS()
  163. {
  164.        
  165.     std::ostringstream oss;
  166.     oss << "FPS" << " " << m_fpsManager.GetFPS();
  167.     m_fpsText->SetText(oss.str());
  168.     m_screen.Draw(*m_fpsText);         
  169.  
  170. }