23 #include "freertos/FreeRTOS.h" 24 #include "freertos/task.h" 25 #include "freertos/semphr.h" 41 m_updateTimeMS(updateTimeMS),
42 m_collisionDetector(maxSpritesCount,
width,
height),
43 m_suspendedTask(nullptr),
46 m_mutex = xSemaphoreCreateMutex();
47 xSemaphoreTake(m_mutex, portMAX_DELAY);
48 xTaskCreate(updateTask,
"", FABGL_DEFAULT_SCENETASK_STACKSIZE,
this, 5, &m_updateTaskHandle);
55 xSemaphoreTake(m_mutex, portMAX_DELAY);
56 vTaskDelete(m_updateTaskHandle);
57 vSemaphoreDelete(m_mutex);
67 xSemaphoreGive(m_mutex);
69 m_suspendedTask = xTaskGetCurrentTaskHandle();
70 vTaskSuspend(m_suspendedTask);
72 m_suspendedTask =
nullptr;
81 if (xTaskGetCurrentTaskHandle() != m_updateTaskHandle)
82 xSemaphoreTake(m_mutex, portMAX_DELAY);
85 vTaskResume(m_suspendedTask);
90 void Scene::updateTask(
void * pvParameters)
96 xSemaphoreTake(scene->m_mutex, portMAX_DELAY);
98 int64_t t0 = esp_timer_get_time();
100 if (scene->m_running) {
101 scene->m_updateCount += 1;
102 scene->
update(scene->m_updateCount);
105 xSemaphoreGive(scene->m_mutex);
107 int64_t t1 = esp_timer_get_time();
108 int delayMS = (scene->m_updateTimeMS - (t1 - t0) / 1000);
110 vTaskDelay(delayMS / portTICK_PERIOD_MS);
115 void collisionDetectionCallback(
void * callbackObj, Sprite * spriteA, Sprite * spriteB, Point collisionPoint)
117 ((Scene*)callbackObj)->collisionDetected(spriteA, spriteB, collisionPoint);
This file contains fabgl::Scene definition.
Scene is an abstract class useful to encapsulate functionalities of a scene (sprites, collision detector and updates).
Scene(int maxSpritesCount, int updateTimeMS, int width, int height, int stackSize=FABGL_DEFAULT_SCENETASK_STACKSIZE)
The Scene constructor.
This file contains some utility classes and functions.
virtual void init()=0
This is an abstract method called when the scene needs to be initialized.
Sprite * updateAndDetectCollision(Sprite *sprite, bool removeCollidingSprites=true)
Updates collision detector and detect collision with the specified sprite.
void updateSpriteAndDetectCollisions(Sprite *sprite)
Updates collision detector and generate collision events.
void start(bool suspendTask=true)
Starts scene updates and suspends current task.
void stop()
Stops scene updates and resumes suspended task.
virtual void update(int updateCount)=0
This is an abstract method called whenever the scene needs to be updated.