FabGL
ESP32 VGA Controller and Graphics Library
scene.h
Go to the documentation of this file.
1 /*
2  Created by Fabrizio Di Vittorio (fdivitto2013@gmail.com) - <http://www.fabgl.com>
3  Copyright (c) 2019 Fabrizio Di Vittorio.
4  All rights reserved.
5 
6  This file is part of FabGL Library.
7 
8  FabGL is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  FabGL is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with FabGL. If not, see <http://www.gnu.org/licenses/>.
20  */
21 
22 
23 #pragma once
24 
25 
26 
34 #include "freertos/FreeRTOS.h"
35 
36 #include "fabglconf.h"
37 #include "canvas.h"
38 #include "collisiondetector.h"
39 
40 
41 
42 namespace fabgl {
43 
44 
45 
49 class Scene {
50 
51 public:
52 
61  Scene(int maxSpritesCount, int updateTimeMS = 20, int width = Canvas.getWidth(), int height = Canvas.getHeight());
62 
63  virtual ~Scene();
64 
70  int getWidth() { return m_width; }
71 
77  int getHeight() { return m_height; }
78 
92  void start(bool suspendTask = true);
93 
97  void stop();
98 
102  virtual void init() = 0;
103 
109  virtual void update(int updateCount) = 0;
110 
121  virtual void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint) = 0;
122 
131  void addSprite(Sprite * sprite) { m_collisionDetector.addSprite(sprite); }
132 
138  void removeSprite(Sprite * sprite) { m_collisionDetector.removeSprite(sprite); }
139 
149  void updateSprite(Sprite * sprite) { m_collisionDetector.update(sprite); }
150 
160 
161 private:
162 
163  static void updateTimerFunc(TimerHandle_t xTimer);
164 
165 
166  int m_width;
167  int m_height;
168 
169  TimerHandle_t m_updateTimer;
170  int m_updateCount;
171 
172  CollisionDetector m_collisionDetector;
173 
174  TaskHandle_t m_suspendedTask;
175 
176 };
177 
178 
179 
180 
181 
182 } // end of namespace
183 
184 
185 
186 
187 
188 
This file contains fabgl::CollisionDetector class definition.
Scene(int maxSpritesCount, int updateTimeMS=20, int width=Canvas.getWidth(), int height=Canvas.getHeight())
The Scene constructor.
Definition: scene.cpp:37
Represents a sprite.
Definition: vgacontroller.h:436
void stop()
Stops scene updates and resumes suspended task.
Definition: scene.cpp:63
virtual void init()=0
This is an abstract method called when the scene needs to be initialized.
int getHeight()
Determines scene height.
Definition: scene.h:77
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
Definition: scene.h:131
This file contains fabgl::CanvasClass definition and the related Canvas instance. ...
virtual void update(int updateCount)=0
This is an abstract method called whenever the scene needs to be updated.
A class to detect sprites collisions.
Definition: collisiondetector.h:129
int getWidth()
Determines the canvas width in pixels.
Definition: canvas.h:105
Scene is an abstract class useful to encapsulate functionalities of a scene (sprites, collision detector and updates).
Definition: scene.h:49
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: scene.h:138
Represents the coordinate of a point.
Definition: fabutils.h:125
void update(Sprite *sprite)
Updates collision detector.
Definition: collisiondetector.cpp:422
Definition: canvas.cpp:47
int getHeight()
Determines the canvas height in pixels.
Definition: canvas.h:114
This file contains FabGL library configuration settings, like number of supported colors...
virtual void collisionDetected(Sprite *spriteA, Sprite *spriteB, Point collisionPoint)=0
This is an abstract method called whenever a collision has been detected.
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
Definition: collisiondetector.cpp:373
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: collisiondetector.cpp:388
int getWidth()
Determines scene width.
Definition: scene.h:70
void updateSpriteAndDetectCollisions(Sprite *sprite)
Updates collision detector and generate collision events.
Definition: scene.cpp:86
void updateSprite(Sprite *sprite)
Updates collision detector.
Definition: scene.h:149
void start(bool suspendTask=true)
Starts scene updates and suspends current task.
Definition: scene.cpp:50