game.c 3.3 KB

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