27#include "freertos/FreeRTOS.h"
28#include "freertos/task.h"
29#include "freertos/semphr.h"
36#pragma GCC optimize ("O2")
45 m_updateTimeMS(updateTimeMS),
46 m_collisionDetector(maxSpritesCount,
width,
height),
47 m_suspendedTask(nullptr),
50 m_mutex = xSemaphoreCreateMutex();
51 xSemaphoreTake(m_mutex, portMAX_DELAY);
52 xTaskCreate(updateTask,
"", FABGL_DEFAULT_SCENETASK_STACKSIZE,
this, 5, &m_updateTaskHandle);
59 xSemaphoreTake(m_mutex, portMAX_DELAY);
60 vTaskDelete(m_updateTaskHandle);
61 vSemaphoreDelete(m_mutex);
71 xSemaphoreGive(m_mutex);
73 m_suspendedTask = xTaskGetCurrentTaskHandle();
74 vTaskSuspend(m_suspendedTask);
76 m_suspendedTask =
nullptr;
85 if (xTaskGetCurrentTaskHandle() != m_updateTaskHandle)
86 xSemaphoreTake(m_mutex, portMAX_DELAY);
89 vTaskResume(m_suspendedTask);
94void Scene::updateTask(
void * pvParameters)
100 xSemaphoreTake(scene->m_mutex, portMAX_DELAY);
102 int64_t t0 = esp_timer_get_time();
104 if (scene->m_running) {
105 scene->m_updateCount += 1;
106 scene->
update(scene->m_updateCount);
109 xSemaphoreGive(scene->m_mutex);
111 int64_t t1 = esp_timer_get_time();
112 int delayMS = (scene->m_updateTimeMS - (t1 - t0) / 1000);
114 vTaskDelay(delayMS / portTICK_PERIOD_MS);
119void collisionDetectionCallback(
void * callbackObj, Sprite * spriteA, Sprite * spriteB, Point collisionPoint)
121 ((Scene*)callbackObj)->collisionDetected(spriteA, spriteB, collisionPoint);
Sprite * updateAndDetectCollision(Sprite *sprite, bool removeCollidingSprites=true)
Updates collision detector and detect collision with the specified sprite.
Scene(int maxSpritesCount, int updateTimeMS, int width, int height, int stackSize=2048)
The Scene constructor.
void updateSpriteAndDetectCollisions(Sprite *sprite)
Updates collision detector and generate collision events.
void stop()
Stops scene updates and resumes suspended task.
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.
virtual void update(int updateCount)=0
This is an abstract method called whenever the scene needs to be updated.
Scene is an abstract class useful to encapsulate functionalities of a scene (sprites,...
This file contains some utility classes and functions.
This file contains fabgl::Scene definition.