The Game Engine  1
Game Class Reference

High-level controller for the game This class is tasked with the following: More...

#include <Game.h>

+ Collaboration diagram for Game:

Public Member Functions

LevelgetCurrentLevel ()
 Gets the current level being played. More...
 
void setCurrentLevel (string name)
 Sets the current level to play. More...
 
LevelloadLevel (string name)
 Loads content for the next level to play. More...
 
string getLevelKey ()
 Gets the index of the current level being played. More...
 
int getLevelCount ()
 Gets the total count of levels. More...
 
FontgetFont (FontType type, int size)
 Gets a font to be used to display text. More...
 
void removeFont (FontType type, int size)
 Removes the font if it has been loaded and nothing is using it. More...
 
void removeFont (Font *font)
 Removes the font if it has been loaded and nothing is using it. More...
 
TexturegetTexture (string filename, Texture *tex=NULL)
 Gets a Texture from the cache or generates it otherwise. More...
 
void removeTexture (string filename)
 Removes the Texture if it has been loaded and nothing is using it. More...
 
void removeTexture (Texture *Texture)
 Removes the Texture if it has been loaded and nothing is using it. More...
 
void addAnimation (Animation *anim)
 add a Actor to the list of sprites More...
 
AnimationgetAnimation (string name)
 get a Actor* to the list of sprites More...
 
void removeAnimation (Animation *anim)
 remove an Actor to the list of sprites More...
 
void addActor (string name, Actor actor)
 add a Actor to the list of sprites More...
 
void removeActor (string name)
 remove the Actor sp from the list of sprites More...
 
void addObject (WorldObject *sp, Level *l=NULL)
 add a Object to the list of sprites More...
 
void removeObject (WorldObject *sp, Level *l=NULL)
 remove the Object sp from the list of sprites More...
 
void removeAllSprites (string name, Level *l=NULL)
 remove the Sprite sp from the list of sprites More...
 
void removeAllParticleSystems (string name, Level *l=NULL)
 remove the ParticleSystem ps from the list of sprites More...
 
void run ()
 Makes the game loop begin and load objects. More...
 
void stop ()
 Makes the game loop stop. More...
 
Uint32 getFPS ()
 Returns the Current FPS of the game. More...
 
Point2D getScreenSize ()
 Gets the Screen's dimensions. More...
 

Static Public Member Functions

static Gamegame ()
 This function is called to create an instance of the class. More...
 

Public Attributes

bool ShowCollisions
 Decides whether or not to draw bounding boxes. More...
 
bool ShowFPS
 Decides whether or not to output FPS. More...
 
bool FollowMouse
 Decides whether or not the mouse should follow the cursor. More...
 
bool Paused
 State of if the game should be paused or not. More...
 
bool Focused
 Whether the game has focus or not. More...
 
Camera mCamera
 Camera for the game. More...
 
LevelCurrentLevel
 Stores the current level. More...
 
SpriteCurrentSprite
 Stores the current sprite while behaviors are being executed. More...
 
ParticleSystemCurrentParticleSystem
 Stores the current particle system while behaviors are being executed. More...
 
ParticleSystem::ParticleCurrentParticle
 Stores the current particle while initialization or update functions are being executed. More...
 
TextCurrentText
 Stores the current text object while behaviors are being executed. More...
 
LevelLoadingLevel
 The level that will be active next (for onloadFunctions) More...
 
FontType Language
 The game's language. More...
 

Static Public Attributes

static const int FRAMES_PER_SECOND = 60
 Number of times the Screen is drawn per second. More...
 
static const int TICKS_PER_SECOND = 30
 Number of times the update loop is to run per second. More...
 

Protected Member Functions

 Game ()
 
 ~Game ()
 
void LoadResources ()
 Loads the game's resources. More...
 
void SetStaticObjectValues ()
 Sets up nondefault static values. More...
 
void addLevel (Level *l)
 Adds a level safely. More...
 
void handleInput (SDL_Event event)
 Handles input. More...
 
void draw ()
 draws More...
 
void startDrawTimer ()
 Starts Drawing Timer to limit number of draws. More...
 
void stopDrawTimer ()
 Stops the Drawing timer. More...
 
void resize (int w, int h)
 Resizes the screen to wxh. More...
 
void CalculateFramerate ()
 Calculates our frame rate or FPS (may not mean anything since Updates are locked at 25 and draws are locked at 60 max) More...
 

Static Protected Member Functions

static Uint32 pushUserEvent (Uint32 interval, void *data)
 Pushes user events (used in timer) More...
 

Protected Attributes

bool Running
 Whether the game should keep running. More...
 
bool poll
 Tells us whether to wait or poll. More...
 
bool alwaysDraw
 Whether or not the game should draw as fast as possible. More...
 
SDL_TimerID timer
 The drawing timer. More...
 
map< string, Level * > mLevels
 Vector of pointers to all of the levels. More...
 
map< string, ActormActors
 This level's actors. More...
 
vector< Animation * > mAnims
 This level's Animations. More...
 
SDL_Surface * mScreen
 The surface to draw to. More...
 
map< string, Behavior * > mBehaviors
 This level's Behaviors. More...
 
map< pair< FontType, int >
, Font * > 
mFonts
 The fonts that exist (should be a small amount) More...
 
map< string, Texture * > mTextures
 Textures that have been loaded (since we really don't want to delete them unless they're unused). More...
 
Uint32 unfocusTime
 keeping all timing in sync More...
 
Uint32 nextSecondTick
 estimated ticks to next second More...
 
Uint32 numFrames
 current number of frames More...
 
Uint32 currentFPS
 index into FPSs More...
 
Uint32 avgFPS
 the fps we want to display More...
 
Uint32 FPSs [FPSRANGE]
 list of frame values More...
 
TexttxtFPS
 The fps text. More...
 
Uint32 nextDrawTick
 Tick at which the game should update next. More...
 
Uint32 nextGameTick
 Tick at which the game should update next. More...
 
int drawLoops
 Number of times drawing has happened this frame (for limiting draw counts) More...
 

Static Protected Attributes

static Gamem_instance = NULL
 Global static pointer used to ensure a single instance of the class. More...
 
static const int FPSRANGE = 1
 fps data calc More...
 
static const Uint32 drawInterval = 1000/FRAMES_PER_SECOND
 Draw loop Throttling. More...
 
static const int SKIP_TICKS = 1000 / TICKS_PER_SECOND
 Update loop Throttling. More...
 
static const int MAX_FRAMESKIP = 5
 Max number of frames we want to update in a tick. More...
 

Friends

class Camera
 so that we can use the screen's dimensions More...
 

Detailed Description

High-level controller for the game This class is tasked with the following:

  • game-wide operations
  • creating the SDL screen
  • loading levels from appropriate locations
  • switching levels as appropriate
  • storing game-wide settings
  • Handling Loading/Unloading of Font content from disk
  • Handling Loading/Unloading of Textures content from disk
  • Todo:
    Handling Loading/Unloading of Audio content from disk

Definition at line 24 of file Game.h.

Constructor & Destructor Documentation

Game::Game ( )
protected

Initialize SDL

Set the title of our application window handler

Seed random number generator

Set values for Static objects

Initialize Keyboard Input

Initialize Joystick

Load Resources

Definition at line 23 of file Game.cpp.

+ Here is the call graph for this function:

Game::~Game ( )
protected

Definition at line 112 of file Game.cpp.

Member Function Documentation

void Game::addActor ( string  name,
Actor  actor 
)

add a Actor to the list of sprites

Parameters
nameName of the actor
actorThe actor to add
Todo:
add find for each of these things.

Definition at line 322 of file Game.cpp.

void Game::addAnimation ( Animation anim)

add a Actor to the list of sprites

Parameters
animThe actor to add

Definition at line 290 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::addLevel ( Level l)
protected

Adds a level safely.

Definition at line 366 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Game::addObject ( WorldObject sp,
Level l = NULL 
)

add a Object to the list of sprites

Parameters
spObject to add
lthe level which to add the sprites to (defaults to CurrentLevel)

Definition at line 330 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Game::CalculateFramerate ( )
protected

Calculates our frame rate or FPS (may not mean anything since Updates are locked at 25 and draws are locked at 60 max)

Definition at line 590 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::draw ( )
protected

draws

All Drawing happens here.

Flip the working image buffer with the mScreen buffer

Definition at line 517 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Game * Game::game ( )
static

This function is called to create an instance of the class.

Calling the constructor publicly is not allowed. The constructor is private and is only called by this Instance function.

Definition at line 17 of file Game.cpp.

+ Here is the caller graph for this function:

Animation * Game::getAnimation ( string  name)

get a Actor* to the list of sprites

Parameters
nameName of the actor
Returns
Pointer to the actor we requested or null

Definition at line 294 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Level * Game::getCurrentLevel ( )

Gets the current level being played.

Returns
current level

Definition at line 120 of file Game.cpp.

+ Here is the caller graph for this function:

Font * Game::getFont ( FontType  type = Game::game()->Language,
int  size = 24 
)

Gets a font to be used to display text.

This uses a cache of loaded fonts in order to conserve rendering time.

Parameters
sizeSize (in pixels) of the desired font
typeType of text to be displayed
Returns
Pointer to the Font to be returned Null if not found

Definition at line 216 of file Game.cpp.

Uint32 Game::getFPS ( )
inline

Returns the Current FPS of the game.

Returns
Unsigned int representing the FPS

Definition at line 230 of file Game.h.

+ Here is the caller graph for this function:

int Game::getLevelCount ( )

Gets the total count of levels.

Returns
number of levels

Definition at line 152 of file Game.cpp.

string Game::getLevelKey ( )

Gets the index of the current level being played.

Returns
index of the current level

Definition at line 125 of file Game.cpp.

+ Here is the call graph for this function:

Point2D Game::getScreenSize ( )
inline

Gets the Screen's dimensions.

Definition at line 235 of file Game.h.

+ Here is the caller graph for this function:

Texture * Game::getTexture ( string  filename,
Texture tex = NULL 
)

Gets a Texture from the cache or generates it otherwise.

Parameters
filenamefilename for the texture
texPointer to the texture to use otherwise returns the Texture
Returns
Pointer to the Texture to be returned (always returns a value)

Definition at line 248 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::handleInput ( SDL_Event  event)
protected

Handles input.

If the event is a click on the close button in the top right corner of the window, we kill the application

Pause / unpause the game when window looses/gains focus

If our event reports a key being pressed down we process it

<

Todo:
get rid of most of these. Put the rest in a #define determining whether or not to use cheats

Definition at line 377 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Level * Game::loadLevel ( string  name)

Loads content for the next level to play.

Parameters
nameName of the level to load

Definition at line 140 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Game::LoadResources ( )
protected

Loads the game's resources.

Definition at line 7 of file LoadResources.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Uint32 Game::pushUserEvent ( Uint32  interval,
void *  data 
)
staticprotected

Pushes user events (used in timer)

Definition at line 575 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::removeActor ( string  name)

remove the Actor sp from the list of sprites

Parameters
nameName of the actor to remove

Definition at line 326 of file Game.cpp.

void Game::removeAllParticleSystems ( string  name,
Level l = NULL 
)

remove the ParticleSystem ps from the list of sprites

Parameters
nameParticleSystems to remove
lthe level which to add the sprites to (defaults to CurrentLevel)

Definition at line 357 of file Game.cpp.

+ Here is the call graph for this function:

void Game::removeAllSprites ( string  name,
Level l = NULL 
)

remove the Sprite sp from the list of sprites

Parameters
nameSprites to remove
lthe level which to add the sprites to (defaults to CurrentLevel)

Definition at line 348 of file Game.cpp.

+ Here is the call graph for this function:

void Game::removeAnimation ( Animation anim)

remove an Actor to the list of sprites

Parameters
animThe actor to add

Definition at line 312 of file Game.cpp.

void Game::removeFont ( FontType  type = English,
int  size = 24 
)

Removes the font if it has been loaded and nothing is using it.

Parameters
typeThis is the font's name to be removed
sizeSize (in pixels) of the font to be removed

Definition at line 232 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::removeFont ( Font font)

Removes the font if it has been loaded and nothing is using it.

Parameters
fontThe font to try to remove.

Definition at line 244 of file Game.cpp.

+ Here is the call graph for this function:

void Game::removeObject ( WorldObject sp,
Level l = NULL 
)

remove the Object sp from the list of sprites

Parameters
spObject to remove
lthe level which to add the sprites to (defaults to CurrentLevel)

Definition at line 339 of file Game.cpp.

+ Here is the call graph for this function:

void Game::removeTexture ( string  filename)

Removes the Texture if it has been loaded and nothing is using it.

Parameters
filenameFilename of the Texture to get/add

Definition at line 273 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::removeTexture ( Texture Texture)

Removes the Texture if it has been loaded and nothing is using it.

Parameters
TextureThe Texture to try to remove.

Definition at line 286 of file Game.cpp.

void Game::resize ( int  w = 640,
int  h = 480 
)
protected

Resizes the screen to wxh.

Parameters
wWidth of the screen
hHeight of the screen

Definition at line 549 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::run ( )

Makes the game loop begin and load objects.

Game Loop

This will let us track events

Wait for an event to occur

Todo:
remove this after testing done

Definition at line 157 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Game::setCurrentLevel ( string  name)

Sets the current level to play.

Parameters
nameName of the level to load

Definition at line 130 of file Game.cpp.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void Game::SetStaticObjectValues ( )
protected

Sets up nondefault static values.

Definition at line 13 of file LoadResources.cpp.

+ Here is the caller graph for this function:

void Game::startDrawTimer ( )
protected

Starts Drawing Timer to limit number of draws.

Definition at line 533 of file Game.cpp.

+ Here is the call graph for this function:

void Game::stop ( )

Makes the game loop stop.

Definition at line 212 of file Game.cpp.

+ Here is the caller graph for this function:

void Game::stopDrawTimer ( )
protected

Stops the Drawing timer.

Definition at line 543 of file Game.cpp.

+ Here is the caller graph for this function:

Friends And Related Function Documentation

friend class Camera
friend

so that we can use the screen's dimensions

Definition at line 58 of file Game.h.

Member Data Documentation

bool Game::alwaysDraw
protected

Whether or not the game should draw as fast as possible.

Definition at line 248 of file Game.h.

Uint32 Game::avgFPS
protected

the fps we want to display

Definition at line 304 of file Game.h.

Uint32 Game::currentFPS
protected

index into FPSs

Definition at line 302 of file Game.h.

Level* Game::CurrentLevel

Stores the current level.

Definition at line 61 of file Game.h.

ParticleSystem::Particle* Game::CurrentParticle

Stores the current particle while initialization or update functions are being executed.

Definition at line 70 of file Game.h.

ParticleSystem* Game::CurrentParticleSystem

Stores the current particle system while behaviors are being executed.

Definition at line 67 of file Game.h.

Sprite* Game::CurrentSprite

Stores the current sprite while behaviors are being executed.

Definition at line 64 of file Game.h.

Text* Game::CurrentText

Stores the current text object while behaviors are being executed.

Definition at line 73 of file Game.h.

const Uint32 Game::drawInterval = 1000/FRAMES_PER_SECOND
staticprotected

Draw loop Throttling.

Ticks to skip before drawing again.

Definition at line 312 of file Game.h.

int Game::drawLoops
protected

Number of times drawing has happened this frame (for limiting draw counts)

Definition at line 325 of file Game.h.

bool Game::Focused

Whether the game has focus or not.

Definition at line 52 of file Game.h.

bool Game::FollowMouse

Decides whether or not the mouse should follow the cursor.

Definition at line 46 of file Game.h.

const int Game::FPSRANGE = 1
staticprotected

fps data calc

Buffer size of fps datas to keep in calculating fps

Definition at line 296 of file Game.h.

Uint32 Game::FPSs[FPSRANGE]
protected

list of frame values

Definition at line 306 of file Game.h.

const int Game::FRAMES_PER_SECOND = 60
static

Number of times the Screen is drawn per second.

Definition at line 35 of file Game.h.

FontType Game::Language

The game's language.

Definition at line 79 of file Game.h.

Level* Game::LoadingLevel

The level that will be active next (for onloadFunctions)

Definition at line 76 of file Game.h.

Game * Game::m_instance = NULL
staticprotected

Global static pointer used to ensure a single instance of the class.

Definition at line 254 of file Game.h.

map<string,Actor> Game::mActors
protected

This level's actors.

Definition at line 275 of file Game.h.

vector<Animation*> Game::mAnims
protected

This level's Animations.

Todo:
make into map?

Definition at line 278 of file Game.h.

const int Game::MAX_FRAMESKIP = 5
staticprotected

Max number of frames we want to update in a tick.

Definition at line 321 of file Game.h.

map<string,Behavior*> Game::mBehaviors
protected

This level's Behaviors.

Definition at line 283 of file Game.h.

Camera Game::mCamera

Camera for the game.

Definition at line 55 of file Game.h.

map<pair<FontType, int>, Font*> Game::mFonts
protected

The fonts that exist (should be a small amount)

Definition at line 286 of file Game.h.

map<string, Level*> Game::mLevels
protected

Vector of pointers to all of the levels.

Definition at line 272 of file Game.h.

SDL_Surface* Game::mScreen
protected

The surface to draw to.

Definition at line 281 of file Game.h.

map<string, Texture* > Game::mTextures
protected

Textures that have been loaded (since we really don't want to delete them unless they're unused).

Definition at line 289 of file Game.h.

Uint32 Game::nextDrawTick
protected

Tick at which the game should update next.

Definition at line 314 of file Game.h.

Uint32 Game::nextGameTick
protected

Tick at which the game should update next.

Definition at line 323 of file Game.h.

Uint32 Game::nextSecondTick
protected

estimated ticks to next second

Definition at line 298 of file Game.h.

Uint32 Game::numFrames
protected

current number of frames

Definition at line 300 of file Game.h.

bool Game::Paused

State of if the game should be paused or not.

Definition at line 49 of file Game.h.

bool Game::poll
protected

Tells us whether to wait or poll.

Definition at line 245 of file Game.h.

bool Game::Running
protected

Whether the game should keep running.

Definition at line 242 of file Game.h.

bool Game::ShowCollisions

Decides whether or not to draw bounding boxes.

Definition at line 40 of file Game.h.

bool Game::ShowFPS

Decides whether or not to output FPS.

Definition at line 43 of file Game.h.

const int Game::SKIP_TICKS = 1000 / TICKS_PER_SECOND
staticprotected

Update loop Throttling.

Number of ticks to skip for next update loop

Definition at line 319 of file Game.h.

const int Game::TICKS_PER_SECOND = 30
static

Number of times the update loop is to run per second.

Definition at line 37 of file Game.h.

SDL_TimerID Game::timer
protected

The drawing timer.

Definition at line 251 of file Game.h.

Text* Game::txtFPS
protected

The fps text.

Definition at line 308 of file Game.h.

Uint32 Game::unfocusTime
protected

keeping all timing in sync

Definition at line 292 of file Game.h.


The documentation for this class was generated from the following files: