game.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. game_context->ended_early = false;
  17. game_context->level_count = 0;
  18. // set all levels to NULL
  19. for (int i = 0; i < MAX_LEVELS; i++)
  20. {
  21. game_context->levels[i] = NULL;
  22. }
  23. // attempt to allocate all levels
  24. for (int i = 0; i < MAX_LEVELS; i++)
  25. {
  26. if (!allocate_level(game_manager, i))
  27. {
  28. if (i == 0)
  29. {
  30. FURI_LOG_E("Game", "Failed to allocate level %d, loading default level", i);
  31. game_context->levels[0] = game_manager_add_level(game_manager, generic_level("town_world_v2", 0));
  32. game_context->level_count = 1;
  33. break;
  34. }
  35. FURI_LOG_E("Game", "No more levels to load");
  36. break;
  37. }
  38. game_context->level_count++;
  39. }
  40. // imu
  41. game_context->imu = imu_alloc();
  42. game_context->imu_present = imu_present(game_context->imu);
  43. }
  44. /*
  45. Write here the stop code for your game, for example, freeing memory, if it was allocated.
  46. You don't need to free level, sprites or entities, it will be done automatically.
  47. Also, you don't need to free game_context, it will be done automatically, after this function.
  48. */
  49. static void game_stop(void *ctx)
  50. {
  51. if (!ctx)
  52. {
  53. FURI_LOG_E("Game", "Invalid game context");
  54. return;
  55. }
  56. GameContext *game_context = ctx;
  57. if (!game_context)
  58. {
  59. FURI_LOG_E("Game", "Game context is NULL");
  60. return;
  61. }
  62. imu_free(game_context->imu);
  63. game_context->imu = NULL;
  64. if (game_context->player_context)
  65. {
  66. FURI_LOG_I("Game", "Game ending");
  67. if (!game_context->ended_early)
  68. {
  69. easy_flipper_dialog("Game Over", "Thanks for playing Flip World!\nHit BACK then wait for\nthe game to save.");
  70. }
  71. else
  72. {
  73. easy_flipper_dialog("Game Over", "Ran out of memory so the\ngame ended early.\nHit BACK to exit.");
  74. }
  75. FURI_LOG_I("Game", "Saving player context");
  76. save_player_context_api(game_context->player_context);
  77. FURI_LOG_I("Game", "Player context saved");
  78. easy_flipper_dialog("Game Saved", "Hit BACK to exit.");
  79. }
  80. }
  81. /*
  82. Your game configuration, do not rename this variable, but you can change its content here.
  83. */
  84. const Game game = {
  85. .target_fps = 0, // set to 0 because we set this in game_app (callback.c line 22)
  86. .show_fps = false, // show fps counter on the screen
  87. .always_backlight = true, // keep display backlight always on
  88. .start = game_start, // will be called once, when game starts
  89. .stop = game_stop, // will be called once, when game stops
  90. .context_size = sizeof(GameContext), // size of game context
  91. };