minesweeper.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. #include "minesweeper.h"
  2. //#include <args.h>
  3. static bool minesweeper_custom_event_callback(void* context, uint32_t custom_event) {
  4. furi_assert(context);
  5. MineSweeperApp* app = (MineSweeperApp*)context;
  6. return scene_manager_handle_custom_event(app->scene_manager, custom_event);
  7. }
  8. static bool minesweeper_navigation_event_callback(void* context) {
  9. furi_assert(context);
  10. MineSweeperApp* app = (MineSweeperApp*)context;
  11. return scene_manager_handle_back_event(app->scene_manager);
  12. }
  13. static void minesweeper_tick_event_callback(void* context) {
  14. furi_assert(context);
  15. MineSweeperApp* app = (MineSweeperApp*)context;
  16. return scene_manager_handle_tick_event(app->scene_manager);
  17. }
  18. static MineSweeperApp* app_alloc() {
  19. MineSweeperApp* app = (MineSweeperApp*)malloc(sizeof(MineSweeperApp));
  20. // NotificationApp Service
  21. NotificationApp* notification_app = furi_record_open(RECORD_NOTIFICATION);
  22. // Turn backlight on when app starts
  23. notification_message(notification_app, &sequence_display_backlight_on);
  24. furi_record_close(RECORD_NOTIFICATION);
  25. // Alloc Scene Manager and set handlers for on_enter, on_event, on_exit
  26. app->scene_manager = scene_manager_alloc(&minesweeper_scene_handlers, app);
  27. // Alloc View Dispatcher and enable queue
  28. app->view_dispatcher = view_dispatcher_alloc();
  29. view_dispatcher_enable_queue(app->view_dispatcher);
  30. // Set View Dispatcher event callback context and callbacks
  31. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  32. view_dispatcher_set_custom_event_callback(
  33. app->view_dispatcher, minesweeper_custom_event_callback);
  34. view_dispatcher_set_navigation_event_callback(
  35. app->view_dispatcher, minesweeper_navigation_event_callback);
  36. view_dispatcher_set_tick_event_callback(
  37. app->view_dispatcher, minesweeper_tick_event_callback, 500);
  38. // Set setting info to default
  39. app->settings_info.width_str = furi_string_alloc();
  40. app->settings_info.height_str = furi_string_alloc();
  41. memset(&app->t_settings_info, 0, sizeof(app->t_settings_info));
  42. app->is_settings_changed = false;
  43. // If we cannot read the save file set to default values
  44. if(!(mine_sweeper_read_settings(app))) {
  45. FURI_LOG_I(TAG, "Cannot read save file, loading defaults");
  46. app->settings_info.board_width = 16;
  47. app->settings_info.board_height = 7;
  48. app->settings_info.difficulty = 0;
  49. app->haptic = 1;
  50. app->speaker = 1;
  51. app->led = 1;
  52. mine_sweeper_save_settings(app);
  53. } else {
  54. FURI_LOG_I(TAG, "Save file loaded sucessfully");
  55. }
  56. // Alloc views and add to view dispatcher
  57. app->start_screen = start_screen_alloc();
  58. view_dispatcher_add_view(
  59. app->view_dispatcher,
  60. MineSweeperStartScreenView,
  61. start_screen_get_view(app->start_screen));
  62. app->loading = loading_alloc();
  63. view_dispatcher_add_view(
  64. app->view_dispatcher, MineSweeperLoadingView, loading_get_view(app->loading));
  65. app->game_screen = mine_sweeper_game_screen_alloc(
  66. app->settings_info.board_width,
  67. app->settings_info.board_height,
  68. app->settings_info.difficulty,
  69. false);
  70. view_dispatcher_add_view(
  71. app->view_dispatcher,
  72. MineSweeperGameScreenView,
  73. mine_sweeper_game_screen_get_view(app->game_screen));
  74. app->menu_screen = dialog_ex_alloc();
  75. view_dispatcher_add_view(
  76. app->view_dispatcher, MineSweeperMenuView, dialog_ex_get_view(app->menu_screen));
  77. app->settings_screen = variable_item_list_alloc();
  78. view_dispatcher_add_view(
  79. app->view_dispatcher,
  80. MineSweeperSettingsView,
  81. variable_item_list_get_view(app->settings_screen));
  82. app->confirmation_screen = dialog_ex_alloc();
  83. view_dispatcher_add_view(
  84. app->view_dispatcher,
  85. MineSweeperConfirmationView,
  86. dialog_ex_get_view(app->confirmation_screen));
  87. app->info_screen = text_box_alloc();
  88. view_dispatcher_add_view(
  89. app->view_dispatcher, MineSweeperInfoView, text_box_get_view(app->info_screen));
  90. Gui* gui = furi_record_open(RECORD_GUI);
  91. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  92. furi_record_close(RECORD_GUI);
  93. return app;
  94. }
  95. static void app_free(MineSweeperApp* app) {
  96. furi_assert(app);
  97. // Remove each view from View Dispatcher
  98. for(MineSweeperView minesweeper_view = (MineSweeperView)0;
  99. minesweeper_view < MineSweeperViewCount;
  100. minesweeper_view++) {
  101. view_dispatcher_remove_view(app->view_dispatcher, minesweeper_view);
  102. }
  103. // Free View Dispatcher and Scene Manager
  104. scene_manager_free(app->scene_manager);
  105. view_dispatcher_free(app->view_dispatcher);
  106. // Free views
  107. loading_free(app->loading);
  108. start_screen_free(app->start_screen);
  109. mine_sweeper_game_screen_free(app->game_screen);
  110. dialog_ex_free(app->menu_screen);
  111. variable_item_list_free(app->settings_screen);
  112. dialog_ex_free(app->confirmation_screen);
  113. text_box_free(app->info_screen);
  114. furi_string_free(app->settings_info.width_str);
  115. furi_string_free(app->settings_info.height_str);
  116. // Free app structure
  117. free(app);
  118. }
  119. int32_t minesweeper_app(void* p) {
  120. UNUSED(p);
  121. MineSweeperApp* app = app_alloc();
  122. FURI_LOG_D(TAG, "Mine Sweeper app allocated with size : %d", sizeof(*app));
  123. // This will be the initial scene on app startup
  124. scene_manager_next_scene(app->scene_manager, MineSweeperSceneStartScreen);
  125. view_dispatcher_run(app->view_dispatcher);
  126. app_free(app);
  127. FURI_LOG_D(TAG, "Mine Sweeper app freed");
  128. return 0;
  129. }