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-2022 Fabrizio Di Vittorio.
4 All rights reserved.
5
6
7* Please contact fdivitto2013@gmail.com if you need a commercial license.
8
9
10* This library and related software is available under GPL v3.
11
12 FabGL is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
16
17 FabGL is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License
23 along with FabGL. If not, see <http://www.gnu.org/licenses/>.
24 */
25
26
27#pragma once
28
29
30
38#include "freertos/FreeRTOS.h"
39
40#include "fabglconf.h"
41#include "collisiondetector.h"
42
43
44
45#define FABGL_DEFAULT_SCENETASK_STACKSIZE 2048
46
47
48namespace fabgl {
49
50
51
55class Scene {
56
57public:
58
68 Scene(int maxSpritesCount, int updateTimeMS, int width, int height, int stackSize = FABGL_DEFAULT_SCENETASK_STACKSIZE);
69
70 virtual ~Scene();
71
77 int getWidth() { return m_width; }
78
84 int getHeight() { return m_height; }
85
99 void start(bool suspendTask = true);
100
104 void stop();
105
109 virtual void init() = 0;
110
116 virtual void update(int updateCount) = 0;
117
128 virtual void collisionDetected(Sprite * spriteA, Sprite * spriteB, Point collisionPoint) = 0;
129
138 void addSprite(Sprite * sprite) { m_collisionDetector.addSprite(sprite); }
139
145 void removeSprite(Sprite * sprite) { m_collisionDetector.removeSprite(sprite); }
146
156 void updateSprite(Sprite * sprite) { m_collisionDetector.update(sprite); }
157
167
168private:
169
170 static void updateTask(void * pvParameters);
171
172
173 int m_width;
174 int m_height;
175
176 TaskHandle_t m_updateTaskHandle;
177 int m_updateCount;
178 int m_updateTimeMS;
179 SemaphoreHandle_t m_mutex;
180
181 CollisionDetector m_collisionDetector;
182
183 TaskHandle_t m_suspendedTask;
184
185 volatile bool m_running;
186
187};
188
189
190
191
192
193} // end of namespace
194
195
196
197
198
199
void addSprite(Sprite *sprite)
Adds the specified sprite to collision detector.
void update(Sprite *sprite)
Updates collision detector.
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
A class to detect sprites collisions.
int getHeight()
Determines scene height.
Definition: scene.h:84
Scene(int maxSpritesCount, int updateTimeMS, int width, int height, int stackSize=2048)
The Scene constructor.
Definition: scene.cpp:42
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:138
void updateSpriteAndDetectCollisions(Sprite *sprite)
Updates collision detector and generate collision events.
Definition: scene.cpp:125
int getWidth()
Determines scene width.
Definition: scene.h:77
void updateSprite(Sprite *sprite)
Updates collision detector.
Definition: scene.h:156
void stop()
Stops scene updates and resumes suspended task.
Definition: scene.cpp:81
virtual void init()=0
This is an abstract method called when the scene needs to be initialized.
void start(bool suspendTask=true)
Starts scene updates and suspends current task.
Definition: scene.cpp:65
virtual void update(int updateCount)=0
This is an abstract method called whenever the scene needs to be updated.
void removeSprite(Sprite *sprite)
Removes the specified sprite from collision detector.
Definition: scene.h:145
Scene is an abstract class useful to encapsulate functionalities of a scene (sprites,...
Definition: scene.h:55
This file contains fabgl::CollisionDetector class definition.
uint8_t width
uint8_t height
This file contains FabGL library configuration settings, like number of supported colors,...
Represents the coordinate of a point.
Definition: fabutils.h:213
Represents a sprite.