FabGL
ESP32 VGA Controller and Graphics Library
fabgl::Scene Class Referenceabstract

Scene is an abstract class useful to encapsulate functionalities of a scene (sprites, collision detector and updates). More...

#include <scene.h>

Public Member Functions

 Scene (int maxSpritesCount, int updateTimeMS=20, int width=Canvas.getWidth(), int height=Canvas.getHeight())
 The Scene constructor. More...
 
void addSprite (Sprite *sprite)
 Add the specified sprite to collision detector. More...
 
virtual void collisionDetected (Sprite *spriteA, Sprite *spriteB, Point collisionPoint)=0
 This is an abstract method called whenever a collision has been detected. More...
 
int getHeight ()
 Return scene height. More...
 
int getWidth ()
 Return scene width. More...
 
virtual void init ()=0
 This is an abstract method called when the scene needs to be initialized. More...
 
void removeSprite (Sprite *sprite)
 Remove the specified sprite from collision detector. More...
 
void start (bool suspendTask=true)
 Start scene updates and suspends current task. More...
 
void stop ()
 Stop scene updates and resumes suspended task. More...
 
virtual void update (int updateCount)=0
 This is an abstract method called whenever the scene needs to be updated. More...
 
void updateSprite (Sprite *sprite)
 Update collision detector. More...
 
void updateSpriteAndDetectCollisions (Sprite *sprite)
 Update collision detector and generate collision events. More...
 

Detailed Description

Scene is an abstract class useful to encapsulate functionalities of a scene (sprites, collision detector and updates).

Constructor & Destructor Documentation

◆ Scene()

fabgl::Scene::Scene ( int  maxSpritesCount,
int  updateTimeMS = 20,
int  width = Canvas.getWidth(),
int  height = Canvas.getHeight() 
)

The Scene constructor.

Parameters
maxSpritesCountSpecifies maximum number of sprites. This is required to size the collision detector object.
updateTimeMSNumber of milliseconds between updates. Scene.update() is called whenever an update occurs.
widthThe scene width in pixels.
heightThe scene height in pixels.

Member Function Documentation

◆ addSprite()

void fabgl::Scene::addSprite ( Sprite sprite)
inline

Add the specified sprite to collision detector.

The collision detector is updated calling Scene.updateSprite() or updateSpriteAndDetectCollisions().
The number of sprites cannot exceed the value specified in Scene constructor.

Parameters
spriteThe sprite to add.

◆ collisionDetected()

virtual void fabgl::Scene::collisionDetected ( Sprite spriteA,
Sprite spriteB,
Point  collisionPoint 
)
pure virtual

This is an abstract method called whenever a collision has been detected.

This method is called one o more times as a result of calling Scene.updateSpriteAndDetectCollisions() method when one o more collisions has been detected.

Parameters
spriteAOne of the two sprites collided. This is the same sprite specified in Scene.updateSpriteAndDetectCollisions() call.
spriteBOne of the two sprites collided.
collisionPointCoordinates of a collision point.

◆ getHeight()

int fabgl::Scene::getHeight ( )
inline

Return scene height.

Returns
Scene height in pixels.

◆ getWidth()

int fabgl::Scene::getWidth ( )
inline

Return scene width.

Returns
Scene width in pixels.

◆ init()

virtual void fabgl::Scene::init ( )
pure virtual

This is an abstract method called when the scene needs to be initialized.

◆ removeSprite()

void fabgl::Scene::removeSprite ( Sprite sprite)
inline

Remove the specified sprite from collision detector.

Parameters
spriteThe sprite to remove.

◆ start()

void fabgl::Scene::start ( bool  suspendTask = true)

Start scene updates and suspends current task.

Parameters
suspendTaskIf true (default) current calling task is suspended immeditaly.

Example:

void loop()
{
  GameScene gameScene;
  gameScene.start();
}

◆ stop()

void fabgl::Scene::stop ( )

Stop scene updates and resumes suspended task.

◆ update()

virtual void fabgl::Scene::update ( int  updateCount)
pure virtual

This is an abstract method called whenever the scene needs to be updated.

Parameters
updateCountIndicates number of times Scane.update() has been called.

◆ updateSprite()

void fabgl::Scene::updateSprite ( Sprite sprite)
inline

Update collision detector.

When a sprite changes its position or size it is necessary to update the collision detector.
This method just updates the detector without generate collision events.
To generate collision events call Scene.updateSpriteAndDetectCollisions instead.

Parameters
spriteThe sprite to update.

◆ updateSpriteAndDetectCollisions()

void fabgl::Scene::updateSpriteAndDetectCollisions ( Sprite sprite)

Update collision detector and generate collision events.

When a sprite changes its position or size it is necessary to update the collision detector.
This method updates the detector and generates collision events accordly.

Parameters
spriteThe sprite to update and to check for collisions.

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