Angel
A 2D Game Prototyping Engine
Classes | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Protected Attributes
World Class Reference

The central class that manages all aspects of the simulation. More...

#include <World.h>

+ Inheritance diagram for World:

List of all members.

Classes

struct  RenderableLayerPair

Public Member Functions

bool Initialize (unsigned int windowWidth=1024, unsigned int windowHeight=768, String windowName="Angel Engine", bool antiAliasing=false, bool fullScreen=false, bool resizable=false)
std::vector< Vec3uiGetVideoModes ()
void AdjustWindow (int windowWidth, int windowHeight, const String &windowName)
void MoveWindow (int xPosition, int yPosition)
bool SetupPhysics (const Vector2 &gravity=Vector2(0,-10), const Vector2 &maxVertex=Vector2(100.0f, 100.0f), const Vector2 &minVertex=Vector2(-100.0f,-100.0f))
void Destroy ()
void ResetWorld ()
void StartGame ()
void StopGame ()
void ScriptExec (const String &code)
void LoadLevel (const String &levelName)
const float GetDT ()
const bool StopSimulation ()
const bool StartSimulation ()
void SetBackgroundColor (const Color &bgColor)
void Add (Renderable *newElement, int layer=0)
void Add (Renderable *newElement, const String &layer)
void Remove (Renderable *oldElement)
void UpdateLayer (Renderable *element, int newLayer)
void UpdateLayer (Renderable *element, const String &newLayerName)
void NameLayer (const String &name, int number)
const int GetLayerByName (const String &name)
RenderLayers & GetLayers ()
void RegisterConsole (Console *console)
ConsoleGetConsole ()
b2World & GetPhysicsWorld ()
const bool IsPhysicsSetUp ()
void WakeAllPhysics ()
virtual void BeginContact (b2Contact *contact)
virtual void EndContact (b2Contact *contact)
void SetSideBlockers (bool turnOn, float restitution=-1.0f)
float GetCurrentTimeSeconds ()
float GetTimeSinceSeconds (float lastTime)
void DrawDebugLine (const Vector2 &a, const Vector2 &b, float time=5.f, const Color &color=Color(1.f, 0.f, 0.f))
void PurgeDebugDrawing ()
const bool IsSimulationOn ()
void SetGameManager (GameManager *gameManager)
GameManagerGetGameManager ()
RenderableIterator GetFirstRenderable ()
RenderableIterator GetLastRenderable ()
void UnloadAll ()
virtual void ReceiveMessage (Message *m)
const bool IsHighResScreen ()
void SetHighResolutionScreen (bool highRes)
void TickAndRender ()

Static Public Member Functions

static WorldGetInstance ()

Protected Member Functions

float CalculateNewDT ()
void UpdateRenderables (float frame_dt)
void CleanupRenderables ()
void DrawRenderables ()
void Simulate (bool simRunning)
void ProcessDeferredAdds ()
void ProcessDeferredLayerChanges ()
void ProcessDeferredRemoves ()
void RunPhysics (float frame_dt)
void UpdateDebugItems (float frame_dt)
void DrawDebugItems ()

Static Protected Attributes

static Worlds_World = NULL

Detailed Description

The World is the class that keeps track of all Renderables, handles the Update/Render loop, processes events, etc.

Like the Camera, it uses the singleton pattern; you can't actually declare a new instance of a TagCollection. To access the World, use "theWorld" to retrieve the singleton object. "theWorld" is defined in both C++ and Lua.

If you're not familiar with the singleton pattern, this paper is a good starting point. (Don't be afraid that it's written by Microsoft.)

http://msdn.microsoft.com/en-us/library/ms954629.aspx

Definition at line 66 of file World.h.


Member Function Documentation

World & World::GetInstance ( ) [static]

Used to access the singleton instance of this class. As a shortcut, you can just use "theWorld".

Returns:
The singleton

Definition at line 75 of file World.cpp.

bool World::Initialize ( unsigned int  windowWidth = 1024,
unsigned int  windowHeight = 768,
String  windowName = "Angel Engine",
bool  antiAliasing = false,
bool  fullScreen = false,
bool  resizable = false 
)

The initial call to get Angel up and running. Opens the window, sets up our display, and begins the Update/Render loop.

The values passed in here will be overridden by any values set in the Preferences, with the WindowSettings table. (See the Preferences documentation and the example preferences file in IntroGame for more information.)

Parameters:
windowWidthThe desired width (in pixels) of the window
windowHeightThe desired height (in pixels) of the window
windowNameThe string that should appear in the window's title bar
antiAliasingWhether or not the window should be initialized with anti-aliasing
fullScreenWhether the game should be started in fullscreen mode. Note that the resolution is determined from windowWidth and windowHeight, and that Angel will attempt to change the system's video mode accordingly. We don't do any detection of what modes are valid, so make sure you're feeding this a legitimate resolution.
resizableWhether the user will be allowed to resize the game window 8 using the operating system's normal resize widgets.
Returns:
Returns true if initialization worked (i.e. if world was not already initialized)

Definition at line 103 of file World.cpp.

std::vector< Vec3ui > World::GetVideoModes ( )

Queries the video drivers to get a list of supported video modes for fullscreen gameplay. You'll likely want to get a list of valid modes to give the player a choice of resolutions. (You can then save the selected mode as part of the preferences.)

Returns:
A list of video modes bundled, each bundled as a Vec3ui. The X value is the width; the Y value is the height, and the Z-value represents the color depth.

Definition at line 258 of file World.cpp.

void World::AdjustWindow ( int  windowWidth,
int  windowHeight,
const String &  windowName 
)

Changes the dimension of the window while the game is running. Note that the behavior is undefined if this method is called while in fullscreen mode.

Parameters:
windowWidthThe new desired width
windowHeightThe new desired height
windowNameThe new string to go in the window's title bar

Definition at line 281 of file World.cpp.

void World::MoveWindow ( int  xPosition,
int  yPosition 
)

Moves the window around on screen, relative to the system origin. Note that behavior is undefined if this method is called while in fullscreen mode.

Parameters:
xPositionThe new x position offset from the system origin
yPositionThe new y position offset from the system origin

Definition at line 295 of file World.cpp.

bool World::SetupPhysics ( const Vector2 gravity = Vector2(0, -10),
const Vector2 maxVertex = Vector2(100.0f, 100.0f),
const Vector2 minVertex = Vector2(-100.0f, -100.0f) 
)

Intialize physics. If you're not using our built-in physics, you don't have to call this function, but no PhysicsActors will do anything until you do.

Parameters:
gravityThe gravity vector that will be applied to all PhysicsActors
maxVertexThe maximum vertex at which PhysicsActors will simulate. Any PhysicsActors that go beyond this point will get "stuck" in the bounding box.
minVertexThe minimum vertex at which PhysicsActors will simulate. Any PhysicsActors that go beyond this point will get "stuck" in the bounding box.
Returns:
True is successfully setup, false if physics were already initialized

Definition at line 303 of file World.cpp.

void World::Destroy ( )

Called when the game shuts down, does all cleanup.

Definition at line 323 of file World.cpp.

void World::ResetWorld ( )

Removes all Actors from the world (pending a check to the GameManager::IsProtectedFromUnloadAll function) and resets the camera to its default positioning.

Definition at line 608 of file World.cpp.

void World::StartGame ( )

Called once, after your world setup is done and you're ready to kick things into motion.

Definition at line 334 of file World.cpp.

void World::StopGame ( )

Ends the game, has everything prepare for shutdown. Should only be called once, when you're ready to finish.

Definition at line 365 of file World.cpp.

void World::ScriptExec ( const String &  code)

Execute a string of Lua code.

Parameters:
codeThe string to execute

Definition at line 370 of file World.cpp.

void World::LoadLevel ( const String &  levelName)

Loads a level from Config/Level/[levelName].lua.

Each section of a level file specifies an Actor to be added to the world. The name of the section will be the name of the Actor, and the values in each section will be applied in addition to (or overriding) the ones specified in the archetype. Check the Actor::Create function for more information on how archetypes are specified.

See also:
Actor::Create
Parameters:
levelName

Definition at line 375 of file World.cpp.

const float World::GetDT ( )

Get the amount of time that elapsed between the start of the last frame and the start of the current frame. Useful for rate controlling any changes on your actors.

Returns:
The amount of time elapsed in seconds

Definition at line 588 of file World.cpp.

const bool World::StopSimulation ( )

Toggles the simulation off. Several classes and objects will still receive updates (GameManager, Console, Camera, etc.), but all standard Actors will not receive Update calls until you call World::StartSimulation.

Returns:
Whether the simulation was successfully stopped

Definition at line 598 of file World.cpp.

const bool World::StartSimulation ( )

Toggles the simulation back on.

See also:
World::StopSimulation
Returns:
Whether the simulation was successfully restarted

Definition at line 593 of file World.cpp.

void World::SetBackgroundColor ( const Color bgColor)

Sets the world's background color. White by default.

Parameters:
bgColorThe new background color

Definition at line 614 of file World.cpp.

void World::Add ( Renderable newElement,
int  layer = 0 
)

Add a Renderable to the World so it will start receiving Update and Render calls every frame.

Parameters:
*newElementThe new object to insert into the world
layerThe layer at which to insert it

Definition at line 619 of file World.cpp.

void World::Add ( Renderable newElement,
const String &  layer 
)

Add a Renderable to the World with a named layer.

Parameters:
*newElementThe new object to insert into the world
layerThe name of the layer at which to insert it

Definition at line 652 of file World.cpp.

void World::Remove ( Renderable oldElement)

Remove a Renderable from the World. Does not deallocate any memory; you're responsible for doing that yourself.

Parameters:
*oldElementThe element to remove

Definition at line 657 of file World.cpp.

void World::UpdateLayer ( Renderable element,
int  newLayer 
)

Move a Renderable to a new layer

Parameters:
elementThe renderable to move
newLayerIts new home

Definition at line 711 of file World.cpp.

void World::UpdateLayer ( Renderable element,
const String &  newLayerName 
)

Move a Renderable to a new layer by name

Parameters:
elementThe renderable to move
newLayerNameThe name of its new home

Definition at line 722 of file World.cpp.

void World::NameLayer ( const String &  name,
int  number 
)

Lets you name layers for easier reference later. This name can be used by the World::Add and World::UpdateLayer functions.

Parameters:
nameThe string to assign as a layer name
numberThe number to which this name should refer

Definition at line 727 of file World.cpp.

const int World::GetLayerByName ( const String &  name)

Retrieve the layer number associated with a name.

Parameters:
nameThe name to look up
Returns:
The layer number. Will return 0 if the name has not been registered; note that 0 is still a valid layer.

Definition at line 732 of file World.cpp.

RenderLayers& World::GetLayers ( ) [inline]

Get the set of layers and their associated Renderables

Returns:
The world's layers

Definition at line 292 of file World.h.

void World::RegisterConsole ( Console console)

Register a new Console with the World. Only one Console can be activated at at time.

Parameters:
consoleThe new Console that should accept input

Definition at line 992 of file World.cpp.

Console * World::GetConsole ( )

Get a pointer to the current registered console

Returns:
The current console

Definition at line 997 of file World.cpp.

b2World & World::GetPhysicsWorld ( )

Get a reference to the Box2D world that's handling all the Physics

Returns:

Definition at line 766 of file World.cpp.

const bool World::IsPhysicsSetUp ( ) [inline]

Lets you know if physics have been appropriately initialized with the World::SetupPhysics function.

Returns:
True if physics has been initialized

Definition at line 322 of file World.h.

void World::WakeAllPhysics ( )

Wakes up all physics bodies in the world. See the Box2D documentation for more information on what this means.

Definition at line 758 of file World.cpp.

void World::BeginContact ( b2Contact *  contact) [virtual]

Implementation of the b2ContactListener::BeginContact function. We use it to manage collision notifications.

Definition at line 516 of file World.cpp.

void World::EndContact ( b2Contact *  contact) [virtual]

Implementation of the b2ContactListener::EndContact function. We use it to manage collision notifications.

Definition at line 521 of file World.cpp.

void World::SetSideBlockers ( bool  turnOn,
float  restitution = -1.0f 
)

When working with physics, oftentimes you want to keep objects from going beyond the edge of the visible screen. This function sets up objects to block the edges so they can't.

Parameters:
turnOnIf true, side blockers are enabled, if false, they're disabled
restitutionThe restitution of the blockers (how bouncy they are)

Definition at line 771 of file World.cpp.

float World::GetCurrentTimeSeconds ( ) [inline]

Find out how much time has elapsed since the game started

Returns:
Total elapsed time in seconds

Definition at line 358 of file World.h.

float World::GetTimeSinceSeconds ( float  lastTime) [inline]

Find out how much time has elapsed since a specific timestamp.

Parameters:
lastTimeThe time index you want to calculate the difference from
Returns:
How much time has elapsed since lastTime

Definition at line 366 of file World.h.

void World::DrawDebugLine ( const Vector2 a,
const Vector2 b,
float  time = 5.f,
const Color color = Color(1.f, 0.f, 0.f) 
)

Draw a line for a specified length of time.

Parameters:
aThe starting point of the line
bThe ending point of the line
timeThe length of time the line will be drawn (less than 0 will draw it permanently)
colorThe color of the line

Definition at line 745 of file World.cpp.

void World::PurgeDebugDrawing ( )

Purge all debug drawing.

Definition at line 934 of file World.cpp.

const bool World::IsSimulationOn ( )

Check whether the simulation is running. See also StartSimulation() and StopSimulation().

Returns:
Whether the simulation is running

Definition at line 603 of file World.cpp.

void World::SetGameManager ( GameManager gameManager)

Set a GameManager object to be your high-level coordinator.

Parameters:
gameManagerThe new manager

Definition at line 957 of file World.cpp.

GameManager* World::GetGameManager ( ) [inline]

Get the current registered manager

Returns:
The GameManager currently overseeing the game

Definition at line 403 of file World.h.

RenderableIterator World::GetFirstRenderable ( ) [inline]

Get an iterator to start cycling through all the Renderables that have been added to the World.

Returns:
An iterator pointing to the first Renderable

Definition at line 411 of file World.h.

RenderableIterator World::GetLastRenderable ( ) [inline]

Get an iterator pointing to the last Renderable in the World.

Returns:
An iterator at the end of the list

Definition at line 422 of file World.h.

void World::UnloadAll ( )

Removes all Actors from the world (pending a check to the GameManager::IsProtectedFromUnloadAll function).

Definition at line 968 of file World.cpp.

void World::ReceiveMessage ( Message m) [virtual]

Implementation of the MessageListener::ReceiveMessage function. Used to get notifications of camera changes so the side blockers can be updated if they're enabled.

Parameters:
mThe message being delivered.

Implements MessageListener.

Definition at line 863 of file World.cpp.

const bool World::IsHighResScreen ( ) [inline]

Lets you know whether the current rendering device is high resolution (like the iPhone 4's "Retina Display").

Returns:
Whether or not the screen is high resolution.

Definition at line 449 of file World.h.

void World::SetHighResolutionScreen ( bool  highRes) [inline]

INTERNAL: This function is called by the OS setup functions to let Angel know about the screen resolution. If you call in manually, expect weirdness.

Parameters:
highResWhether or not we should be set up for high resolution rendering.

Definition at line 458 of file World.h.

void World::TickAndRender ( )

INTERNAL: This function is used by various OS systems to run the Angel update loop. If you call it manually, expect weirdness.

Definition at line 526 of file World.cpp.


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