jblanked 1 год назад
Родитель
Сommit
771738f42f
2 измененных файлов с 28 добавлено и 4 удалено
  1. 23 0
      callback/callback.c
  2. 5 4
      game.c

+ 23 - 0
callback/callback.c

@@ -287,6 +287,8 @@ static void free_variable_item_list(void *context)
         app->variable_item_pass = NULL;
     }
 }
+static FuriThreadId thread_id;
+static bool game_thread_running = false;
 void free_all_views(void *context, bool should_free_variable_item_list)
 {
     FlipWorldApp *app = (FlipWorldApp *)context;
@@ -308,6 +310,13 @@ void free_all_views(void *context, bool should_free_variable_item_list)
         view_dispatcher_remove_view(app->view_dispatcher, FlipWorldViewMain);
         app->view_main = NULL;
     }
+    // free game thread
+    if (game_thread_running)
+    {
+        game_thread_running = false;
+        furi_thread_flags_set(thread_id, WorkerEvtStop);
+        furi_thread_free(thread_id);
+    }
 }
 
 void callback_submenu_choices(void *context, uint32_t index)
@@ -321,6 +330,13 @@ void callback_submenu_choices(void *context, uint32_t index)
     switch (index)
     {
     case FlipWorldSubmenuIndexRun:
+        // free game thread
+        if (game_thread_running)
+        {
+            game_thread_running = false;
+            furi_thread_flags_set(thread_id, WorkerEvtStop);
+            furi_thread_free(thread_id);
+        }
         free_all_views(app, true);
         if (!app->view_main)
         {
@@ -335,7 +351,14 @@ void callback_submenu_choices(void *context, uint32_t index)
         }
         view_dispatcher_switch_to_view(app->view_dispatcher, FlipWorldViewMain);
         FuriThread *thread = furi_thread_alloc_ex("game", 4096, game_app, app);
+        if (!thread)
+        {
+            FURI_LOG_E(TAG, "Failed to allocate game thread");
+            return;
+        }
         furi_thread_start(thread);
+        thread_id = furi_thread_get_id(thread);
+        game_thread_running = true;
         break;
     case FlipWorldSubmenuIndexAbout:
         free_all_views(app, true);

+ 5 - 4
game.c

@@ -384,10 +384,11 @@ static void game_start(GameManager *game_manager, void *ctx)
 */
 static void game_stop(void *ctx)
 {
-    GameContext *game_context = ctx;
-    // Do some deinitialization here, for example you can save score to storage.
-    // For simplicity, we will just print it.
-    FURI_LOG_I("Game", "Your score: %lu", game_context->score);
+    UNUSED(ctx);
+    // GameContext *game_context = ctx;
+    //  Do some deinitialization here, for example you can save score to storage.
+    //  For simplicity, we will just print it.
+    // FURI_LOG_I("Game", "Your score: %lu", game_context->score);
 }
 
 /*