game.c 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #include <game/game.h>
  2. #include <game/storage.h>
  3. /****** Game ******/
  4. /*
  5. Write here the start code for your game, for example: creating a level and so on.
  6. Game context is allocated (game.context_size) and passed to this function, you can use it to store your game data.
  7. */
  8. static void game_start(GameManager *game_manager, void *ctx)
  9. {
  10. // Do some initialization here, for example you can load score from storage.
  11. // For simplicity, we will just set it to 0.
  12. GameContext *game_context = ctx;
  13. game_context->fps = game_fps_choices_2[game_fps_index];
  14. game_context->player_context = NULL;
  15. game_context->current_level = 0;
  16. allocate_level(game_manager, 0);
  17. // Notifications - for LED light access
  18. game_context->notifications = furi_record_open(RECORD_NOTIFICATION);
  19. // imu
  20. game_context->imu = imu_alloc();
  21. game_context->imu_present = imu_present(game_context->imu);
  22. }
  23. /*
  24. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  25. You don't need to free level, sprites or entities, it will be done automatically.
  26. Also, you don't need to free game_context, it will be done automatically, after this function.
  27. */
  28. static void game_stop(void *ctx)
  29. {
  30. if (!ctx)
  31. {
  32. FURI_LOG_E("Game", "Invalid game context");
  33. return;
  34. }
  35. GameContext *game_context = ctx;
  36. if (!game_context)
  37. {
  38. FURI_LOG_E("Game", "Game context is NULL");
  39. return;
  40. }
  41. if (game_context->imu)
  42. {
  43. imu_free(game_context->imu);
  44. game_context->imu = NULL;
  45. }
  46. // close the notifications
  47. furi_record_close(RECORD_NOTIFICATION);
  48. if (game_context->player_context)
  49. {
  50. FURI_LOG_I("Game", "Game ending");
  51. if (!game_context->ended_early)
  52. {
  53. easy_flipper_dialog("Game Over", "Thanks for playing Flip World!\nHit BACK then wait for\nthe game to save.");
  54. }
  55. else
  56. {
  57. easy_flipper_dialog("Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
  58. }
  59. FURI_LOG_I("Game", "Saving player context");
  60. save_player_context(game_context->player_context);
  61. FURI_LOG_I("Game", "Player context saved");
  62. easy_flipper_dialog("Game Saved", "Hit BACK to exit.");
  63. }
  64. }
  65. /*
  66. Your game configuration, do not rename this variable, but you can change its content here.
  67. */
  68. const Game game = {
  69. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  70. .show_fps = false, // show fps counter on the screen
  71. .always_backlight = true, // keep display backlight always on
  72. .start = game_start, // will be called once, when game starts
  73. .stop = game_stop, // will be called once, when game stops
  74. .context_size = sizeof(GameContext), // size of game context
  75. };