FabGL
ESP32 Display 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 "collisiondetector.h"
38 
39 
40 
41 namespace fabgl {
42 
43 
44 
48 class Scene {
49 
50 public:
51 
60  Scene(int maxSpritesCount, int updateTimeMS, int width, int height);
61 
62  virtual ~Scene();
63 
69  int getWidth() { return m_width; }
70 
76  int getHeight() { return m_height; }
77 
91  void start(bool suspendTask = true);
92 
96  void stop();
97 
101  virtual void init() = 0;
102 
108  virtual void update(int updateCount) = 0;
109 
120  virtual void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint) = 0;
121 
130  void addSprite(Sprite * sprite) { m_collisionDetector.addSprite(sprite); }
131 
137  void removeSprite(Sprite * sprite) { m_collisionDetector.removeSprite(sprite); }
138 
148  void updateSprite(Sprite * sprite) { m_collisionDetector.update(sprite); }
149 
159 
160 private:
161 
162  static void updateTimerFunc(TimerHandle_t xTimer);
163 
164 
165  int m_width;
166  int m_height;
167 
168  TimerHandle_t m_updateTimer;
169  int m_updateCount;
170 
171  CollisionDetector m_collisionDetector;
172 
173  TaskHandle_t m_suspendedTask;
174 
175 };
176 
177 
178 
179 
180 
181 } // end of namespace
182 
183 
184 
185 
186 
187 
This file contains fabgl::CollisionDetector class definition.
Represents a sprite.
Definition: displaycontroller.h:495
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:76
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
Definition: scene.h:130
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
Scene is an abstract class useful to encapsulate functionalities of a scene (sprites, collision detector and updates).
Definition: scene.h:48
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: scene.h:137
Represents the coordinate of a point.
Definition: fabutils.h:125
void update(Sprite *sprite)
Updates collision detector.
Definition: collisiondetector.cpp:439
Definition: canvas.cpp:31
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:390
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: collisiondetector.cpp:405
int getWidth()
Determines scene width.
Definition: scene.h:69
void updateSpriteAndDetectCollisions(Sprite *sprite)
Updates collision detector and generate collision events.
Definition: scene.cpp:85
Scene(int maxSpritesCount, int updateTimeMS, int width, int height)
The Scene constructor.
Definition: scene.cpp:37
void updateSprite(Sprite *sprite)
Updates collision detector.
Definition: scene.h:148
void start(bool suspendTask=true)
Starts scene updates and suspends current task.
Definition: scene.cpp:50