2021-04-13 18:06:25 +00:00
|
|
|
#include <furi.h>
|
2021-04-15 16:39:15 +00:00
|
|
|
#include <api-hal.h>
|
2021-04-26 12:19:14 +00:00
|
|
|
#include "scene.h"
|
2021-04-13 18:06:25 +00:00
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
static SceneAppGui* scene_app_gui = NULL;
|
|
|
|
static ValueMutex* scene_state_mutex = NULL;
|
|
|
|
|
2021-04-26 12:19:14 +00:00
|
|
|
void dolphin_scene_redraw(Canvas* canvas, void* ctx) {
|
|
|
|
furi_assert(canvas);
|
|
|
|
furi_assert(ctx);
|
|
|
|
|
|
|
|
SceneState* state = (SceneState*)acquire_mutex((ValueMutex*)ctx, 25);
|
|
|
|
if(state == NULL) return; // redraw fail
|
|
|
|
uint32_t t = xTaskGetTickCount();
|
|
|
|
|
|
|
|
canvas_clear(canvas);
|
|
|
|
dolphin_scene_render(state, canvas, t);
|
|
|
|
dolphin_scene_render_state(state, canvas);
|
|
|
|
release_mutex((ValueMutex*)ctx, state);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dolphin_scene_handle_input(SceneState* state, InputEvent* input) {
|
|
|
|
// printf("[kb] event: %02x %s\n", input->key, input->state ? "pressed" : "released");
|
|
|
|
dolphin_scene_handle_user_input(state, input);
|
|
|
|
}
|
|
|
|
|
|
|
|
void dolphin_scene_tick_handler(SceneState* state, uint32_t t, uint32_t dt) {
|
|
|
|
// printf("t: %d, dt: %d\n", t, dt);
|
|
|
|
dolphin_scene_coordinates(state, dt);
|
|
|
|
dolphin_scene_update_state(state, t, dt);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void scene_engine_tick_callback(void* p) {
|
2021-04-13 18:06:25 +00:00
|
|
|
osMessageQueueId_t event_queue = p;
|
2021-04-15 16:39:15 +00:00
|
|
|
AppEvent event;
|
|
|
|
event.type = EventTypeTick;
|
|
|
|
osMessageQueuePut(event_queue, (void*)&event, 0, 0);
|
2021-04-13 18:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 12:19:14 +00:00
|
|
|
static void scene_engine_input_callback(InputEvent* input_event, void* ctx) {
|
2021-04-13 18:06:25 +00:00
|
|
|
osMessageQueueId_t event_queue = ctx;
|
|
|
|
AppEvent event;
|
|
|
|
event.type = EventTypeKey;
|
|
|
|
event.value.input = *input_event;
|
|
|
|
osMessageQueuePut(event_queue, (void*)&event, 0, osWaitForever);
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
void scene_alloc() {
|
|
|
|
printf("scene_alloc: start\r\n");
|
|
|
|
furi_assert(scene_app_gui == NULL);
|
|
|
|
furi_assert(scene_state_mutex == NULL);
|
|
|
|
|
|
|
|
// SceneAppGui
|
|
|
|
scene_app_gui = furi_alloc(sizeof(SceneAppGui));
|
|
|
|
scene_app_gui->mqueue = osMessageQueueNew(2, sizeof(AppEvent), NULL);
|
|
|
|
scene_app_gui->gui = furi_record_open("gui");
|
|
|
|
scene_app_gui->view_port = view_port_alloc();
|
|
|
|
scene_app_gui->timer =
|
2021-04-26 12:19:14 +00:00
|
|
|
osTimerNew(scene_engine_tick_callback, osTimerPeriodic, scene_app_gui->mqueue, NULL);
|
2021-04-15 16:39:15 +00:00
|
|
|
printf("scene_alloc: timer %p\r\n", scene_app_gui->timer);
|
|
|
|
// Scene State
|
2021-04-13 18:06:25 +00:00
|
|
|
SceneState* scene_state = furi_alloc(sizeof(SceneState));
|
|
|
|
scene_state->player.y = DOLPHIN_DEFAULT_Y;
|
|
|
|
scene_state->player.x = DOLPHIN_CENTER;
|
|
|
|
scene_state->player_global.x = random() % WORLD_WIDTH / 4;
|
|
|
|
scene_state->screen.x = scene_state->player.x;
|
|
|
|
scene_state->screen.y = scene_state->player.y;
|
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
scene_state_mutex = furi_alloc(sizeof(ValueMutex));
|
|
|
|
furi_check(init_mutex(scene_state_mutex, scene_state, sizeof(SceneState)));
|
2021-04-13 18:06:25 +00:00
|
|
|
|
|
|
|
// Open GUI and register fullscreen view_port
|
2021-04-15 16:39:15 +00:00
|
|
|
view_port_draw_callback_set(scene_app_gui->view_port, dolphin_scene_redraw, scene_state_mutex);
|
2021-04-13 18:06:25 +00:00
|
|
|
view_port_input_callback_set(
|
2021-04-26 12:19:14 +00:00
|
|
|
scene_app_gui->view_port, scene_engine_input_callback, scene_app_gui->mqueue);
|
2021-04-15 16:39:15 +00:00
|
|
|
gui_add_view_port(scene_app_gui->gui, scene_app_gui->view_port, GuiLayerMain);
|
|
|
|
view_port_enabled_set(scene_app_gui->view_port, true);
|
|
|
|
printf("scene_alloc: complete\r\n");
|
2021-04-13 18:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
void scene_free() {
|
|
|
|
printf("scene_free: start\r\n");
|
|
|
|
view_port_enabled_set(scene_app_gui->view_port, false);
|
|
|
|
gui_remove_view_port(scene_app_gui->gui, scene_app_gui->view_port);
|
2021-04-13 18:06:25 +00:00
|
|
|
|
|
|
|
SceneState* scene_state = (SceneState*)acquire_mutex_block(scene_state_mutex);
|
2021-04-15 16:39:15 +00:00
|
|
|
furi_assert(scene_state);
|
2021-04-13 18:06:25 +00:00
|
|
|
free(scene_state);
|
|
|
|
release_mutex(scene_state_mutex, scene_state);
|
|
|
|
delete_mutex(scene_state_mutex);
|
|
|
|
free(scene_state_mutex);
|
2021-04-15 16:39:15 +00:00
|
|
|
scene_state_mutex = NULL;
|
|
|
|
|
|
|
|
furi_check(osTimerDelete(scene_app_gui->timer) == osOK);
|
|
|
|
furi_record_close("gui");
|
|
|
|
view_port_free(scene_app_gui->view_port);
|
|
|
|
furi_check(osMessageQueueDelete(scene_app_gui->mqueue) == osOK);
|
|
|
|
free(scene_app_gui);
|
|
|
|
scene_app_gui = NULL;
|
|
|
|
printf("scene_free: complete\r\n");
|
2021-04-13 18:06:25 +00:00
|
|
|
}
|
|
|
|
|
2021-04-26 12:19:14 +00:00
|
|
|
int32_t scene_app(void* p) {
|
2021-04-15 16:39:15 +00:00
|
|
|
api_hal_power_insomnia_enter();
|
|
|
|
scene_alloc();
|
2021-04-13 18:06:25 +00:00
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
osTimerStart(scene_app_gui->timer, 40);
|
2021-04-13 18:06:25 +00:00
|
|
|
|
|
|
|
uint32_t t = xTaskGetTickCount();
|
|
|
|
uint32_t prev_t = 0;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
AppEvent event;
|
2021-04-15 16:39:15 +00:00
|
|
|
if(osMessageQueueGet(scene_app_gui->mqueue, &event, 0, osWaitForever) == osOK) {
|
|
|
|
SceneState* scene_state = (SceneState*)acquire_mutex_block(scene_state_mutex);
|
2021-04-13 18:06:25 +00:00
|
|
|
if(event.type == EventTypeTick) {
|
|
|
|
t = xTaskGetTickCount();
|
2021-04-15 16:39:15 +00:00
|
|
|
dolphin_scene_tick_handler(scene_state, t, (t - prev_t) % 1024);
|
2021-04-13 18:06:25 +00:00
|
|
|
prev_t = t;
|
|
|
|
} else if(event.type == EventTypeKey) {
|
|
|
|
if(event.value.input.key == InputKeyBack &&
|
|
|
|
event.value.input.type == InputTypeShort) {
|
2021-04-15 16:39:15 +00:00
|
|
|
release_mutex(scene_state_mutex, scene_state);
|
2021-04-13 18:06:25 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
2021-04-15 16:39:15 +00:00
|
|
|
dolphin_scene_handle_input(scene_state, &event.value.input);
|
2021-04-13 18:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-15 16:39:15 +00:00
|
|
|
release_mutex(scene_state_mutex, scene_state);
|
|
|
|
view_port_update(scene_app_gui->view_port);
|
2021-04-13 18:06:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
osTimerStop(scene_app_gui->timer);
|
2021-04-30 06:41:15 +00:00
|
|
|
|
|
|
|
// CMSIS + FreeRTOS = Enterprise
|
|
|
|
osDelay(15);
|
|
|
|
|
2021-04-15 16:39:15 +00:00
|
|
|
scene_free();
|
|
|
|
api_hal_power_insomnia_exit();
|
2021-04-13 18:06:25 +00:00
|
|
|
return 0;
|
2021-04-26 12:19:14 +00:00
|
|
|
}
|