Earlier i moved some data to a singleton object, GamePropertiesManager. In this class i define a variable called _gameSpeed and in the class constructor i use an intialisation list to set it to 1 as default.
In another class i have an update loop that, (outside of my control), will always update as frequently as possible. I intended to, and successfully used a ‘speed’ variable to slow down the updating. This worked by, say, setting it to 2 the game would only update every other call to onUpdate.. I might make sense if you look at the contents of PlayState::onUpdate() below (I posted the whole cpp under the advice of a commenter):
#include "PlayState.hpp"
#include "PlayerController.hpp"
#include "AIController.hpp"
#include <stdlib.h>
#include <time.h>
//testin
#include <iostream>
/// Implement PlayState
PlayState::PlayState()
{}
PlayState::~PlayState()
{}
bool PlayState::onCreate()
{
_gameProperties = GamePropertiesManager::GetInstance();
_frameCounter = 0;
application.addKeyListener( *this );
_collisionFlag = false;
//snakes created, intialising a pointer to fruit managaer, controller and opposing snake
_player1 = new Snake();
_player2 = new Snake();
_player1->Initialise(&_fruitManager, new PlayerController(_player1), _player2);
_player2->Initialise(&_fruitManager, new AIController(_player2), _player1);
return true;
}
bool PlayState::onDestroy()
{
return true;
}
void PlayState::onEntry()
{
//seed to randomise food positions later
srand ( time(0) );
}
void PlayState::onExit()
{
}
void PlayState::onUpdate()
{
//TODO: rename _snake speed and maybe make it global gamespeed
++_frameCounter; //FrameCounter can be used to slow the snake down
if(_frameCounter >= _gameProperties->GetGameSpeed())
{
//updating
_fruitManager.UpdateFruits();
_player1->Update(&_collisionFlag);
_player2->Update(&_collisionFlag);
if (_collisionFlag)
application.setState("gameover");
_frameCounter = 0;
}
}
void PlayState::onRender( Canvas& c )
{
_player1->Draw(c);
_player2->Draw(c);
_fruitManager.DrawFruits(c);
}
bool PlayState::onKey (const KeyEvent& key)
{
if( key.key_state == KeyEvent::KB_DOWN ){
switch (key.key){
case KeyEvent::KB_ESC_KEY:
application.exit();
break;
case 'p':
application.setState("pause");
break;
}
}
return true;
}
_gameProperties is a pointer to a/the GamePropertiesManager which i assign in PlayState::onCreate(), when my class PlayState is first created..
The actual GamePropertiesManager class looks like this:
#include "GamePropertiesManager.hpp"
GamePropertiesManager* GamePropertiesManager::_instance = NULL;
GamePropertiesManager* GamePropertiesManager::GetInstance()
{
if (!_instance) //instance not yet created
_instance = new GamePropertiesManager();
return _instance;
}
void GamePropertiesManager::Destroy()
{
delete _instance;
_instance = 0;
}
GamePropertiesManager::GamePropertiesManager() :
_gameMode(PLAYERVSCOM), _player1Name("Player 1"), _player2Name("Player 2"),
_player1Score(0), _player2Score(0), _matchSurvivor("NONE"), _gameSpeed(1)
{
}
unsigned int GamePropertiesManager::GetGameSpeed()
{
return _gameSpeed;
}
My problem is that, when i change the value of _gameSpeed in the initialisation list to any value aside from (1) my program crashes at runtime. I find this particularly odd as i was previously comparing _frameCounter to both hard coded values other than 1 and a speed variable set to values other than 1 and the program ran fine as expected. Any ideas about the problem?
Hope i explained myself well enough, appreciate your help!
The reason is almost certainly because for any value other than 1, in the first call to
PlayState::onUpdatetheifcheck won’t run, most likely leaving something uninitialized.