//------------------------------------------------------------------------
//
// Name: GameManager.cpp
//
// Desc: Manages the game states.
//
// Author: Nikko Bertoa 2010 (nicobertoa@gmail.com)
//
//------------------------------------------------------------------------
//
// Headers
//
#include "GameManager.h"
#include <cassert>
#include <sstream>
#include "auxiliarFunctions.h"
#include "MainMenuState.h"
// Constructor
GameManager::GameManager() : m_pCurrentState(0), m_pPreviousState(0), m_pGlobalState(0),
m_screen(/*sf::VideoMode::GetDesktopMode()*/sf::VideoMode(1024, 768, 32),
"CaperucitaPlusPlus", sf::Style::Fullscreen | sf::Style::Close)
{
m_screen.ShowMouseCursor(false);
// Resources
LoadImages(m_imgManager);
// FPS
m_font = new sf::Font();
bool correctLoading = m_font->LoadFromFile("resources/fonts/arial.ttf");
assert(correctLoading);
m_fpsText = new sf::String("Test Text", *m_font);
m_fpsText->SetSize(30.0f);
float xCoord = 10.0f;
float yCoord = 10.0f;
m_fpsText->SetPosition(xCoord, yCoord);
m_fpsText->SetStyle(sf::String::Regular);
m_fpsText->SetColor(sf::Color::White);
m_close = false;
//Initial state
MainMenuState::Instance()->Init(m_screen, m_imgManager);
SetCurrentState(MainMenuState::Instance());
}
// Destructor
GameManager::~GameManager()
{
DestroyImages(m_imgManager);
if(m_font != 0)
{
delete m_font;
m_font = 0;
}
if(m_fpsText != 0)
{
delete m_fpsText;
m_fpsText = 0;
}
}
void GameManager::Run()
{
// Start game loop
while (m_screen.IsOpened())
{
// Process events
sf::Event Event;
while (m_screen.GetEvent(Event))
{
// Close window : exit
if (Event.Type == sf::Event::Closed)
m_screen.Close();
}
// Clear the screen (fill it with black color)
m_screen.Clear();
Update();
DrawFPS();
// Display window contents on screen
m_screen.Display();
m_fpsManager.Update();
//Close aplication?
if(m_close)
m_screen.Close();
}
}
void GameManager::Exit()
{
if(m_pCurrentState != 0)
m_pCurrentState->Clear();
m_close = true;
}
void GameManager::ChangeState(State* pNewState)
{
assert(pNewState && "<StateMachine::ChangeState>: trying to change to NULL state");
//keep a record of the previous state
m_pPreviousState = m_pCurrentState;
//call the exit method of the existing state
m_pCurrentState->Clear();
//change state to the new state
m_pCurrentState = pNewState;
//call the entry method of the new state
m_pCurrentState->Init(m_screen, m_imgManager);
}
void GameManager::Update()
{
//if a global state exists, call its execute method, else do nothing
if(m_pGlobalState)
m_pGlobalState->Execute(this);
//same for the current state
if (m_pCurrentState)
m_pCurrentState->Execute(this);
}
void GameManager::DrawFPS()
{
std::ostringstream oss;
oss << "FPS" << " " << m_fpsManager.GetFPS();
m_fpsText->SetText(oss.str());
m_screen.Draw(*m_fpsText);
}