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-2020 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 #define FABGL_DEFAULT_SCENETASK_STACKSIZE 2048
42 
43 
44 namespace fabgl {
45 
46 
47 
51 class Scene {
52 
53 public:
54 
64  Scene(int maxSpritesCount, int updateTimeMS, int width, int height, int stackSize = FABGL_DEFAULT_SCENETASK_STACKSIZE);
65 
66  virtual ~Scene();
67 
73  int getWidth() { return m_width; }
74 
80  int getHeight() { return m_height; }
81 
95  void start(bool suspendTask = true);
96 
100  void stop();
101 
105  virtual void init() = 0;
106 
112  virtual void update(int updateCount) = 0;
113 
124  virtual void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint) = 0;
125 
134  void addSprite(Sprite * sprite) { m_collisionDetector.addSprite(sprite); }
135 
141  void removeSprite(Sprite * sprite) { m_collisionDetector.removeSprite(sprite); }
142 
152  void updateSprite(Sprite * sprite) { m_collisionDetector.update(sprite); }
153 
163 
164 private:
165 
166  static void updateTask(void * pvParameters);
167 
168 
169  int m_width;
170  int m_height;
171 
172  TaskHandle_t m_updateTaskHandle;
173  int m_updateCount;
174  int m_updateTimeMS;
175  SemaphoreHandle_t m_mutex;
176 
177  CollisionDetector m_collisionDetector;
178 
179  TaskHandle_t m_suspendedTask;
180 
181  volatile bool m_running;
182 
183 };
184 
185 
186 
187 
188 
189 } // end of namespace
190 
191 
192 
193 
194 
195 
This file contains fabgl::CollisionDetector class definition.
Represents a sprite.
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
int getHeight()
Determines scene height.
Definition: scene.h:80
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: scene.h:134
A class to detect sprites collisions.
Scene is an abstract class useful to encapsulate functionalities of a scene (sprites, collision detector and updates).
Definition: scene.h:51
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Scene(int maxSpritesCount, int updateTimeMS, int width, int height, int stackSize=FABGL_DEFAULT_SCENETASK_STACKSIZE)
The Scene constructor.
Definition: scene.cpp:38
Represents the coordinate of a point.
Definition: fabutils.h:158
Definition: canvas.cpp:31
virtual void init()=0
This is an abstract method called when the scene needs to be initialized.
This file contains FabGL library configuration settings, like number of supported colors...
void updateSprite(Sprite *sprite)
Updates collision detector.
Definition: scene.h:152
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: scene.h:141
void updateSpriteAndDetectCollisions(Sprite *sprite)
Updates collision detector and generate collision events.
Definition: scene.cpp:121
void update(Sprite *sprite)
Updates collision detector.
uint8_t height
void start(bool suspendTask=true)
Starts scene updates and suspends current task.
Definition: scene.cpp:61
int getWidth()
Determines scene width.
Definition: scene.h:73
void stop()
Stops scene updates and resumes suspended task.
Definition: scene.cpp:77
uint8_t width
virtual void update(int updateCount)=0
This is an abstract method called whenever the scene needs to be updated.