jetpack.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. #include <stdlib.h>
  2. #include <jetpack_joyride_icons.h>
  3. #include <furi.h>
  4. #include <gui/gui.h>
  5. #include <gui/icon_animation.h>
  6. #include <input/input.h>
  7. #define TAG "Jetpack Joyride"
  8. #define GRAVITY_BOOST -0.3
  9. #define GRAVITY_TICK 0.15
  10. #define SCIENTISTS_MAX 6
  11. #define COINS_MAX 25
  12. typedef struct {
  13. int x;
  14. int y;
  15. } POINT;
  16. typedef struct {
  17. float gravity;
  18. POINT point;
  19. IconAnimation* sprite;
  20. bool isBoosting;
  21. } BARRY;
  22. typedef struct {
  23. float gravity;
  24. POINT point;
  25. IconAnimation* sprite;
  26. } COIN;
  27. typedef struct {
  28. float gravity;
  29. POINT point;
  30. IconAnimation* sprite;
  31. } SCIENTIST;
  32. typedef enum {
  33. GameStateLife,
  34. GameStateGameOver,
  35. } State;
  36. typedef struct {
  37. int points;
  38. int distance;
  39. BARRY barry;
  40. SCIENTIST scientists[SCIENTISTS_MAX];
  41. COIN coins[COINS_MAX];
  42. State state;
  43. FuriMutex* mutex;
  44. } GameState;
  45. typedef enum {
  46. EventTypeTick,
  47. EventTypeKey,
  48. } EventType;
  49. typedef struct {
  50. EventType type;
  51. InputEvent input;
  52. } GameEvent;
  53. static void jetpack_game_random_coins(GameState* const game_state) {
  54. UNUSED(game_state);
  55. }
  56. static void jetpack_game_state_init(GameState* const game_state) {
  57. UNUSED(game_state);
  58. BARRY barry;
  59. barry.gravity = 0;
  60. barry.point.x = 64;
  61. barry.point.y = 32;
  62. barry.sprite = icon_animation_alloc(&A_barry);
  63. barry.isBoosting = false;
  64. icon_animation_start(barry.sprite);
  65. game_state->barry = barry;
  66. game_state->points = 0;
  67. game_state->distance = 0;
  68. game_state->state = GameStateLife;
  69. memset(game_state->scientists, 0, sizeof(game_state->scientists));
  70. memset(game_state->coins, 0, sizeof(game_state->coins));
  71. }
  72. static void jetpack_game_state_free(GameState* const game_state) {
  73. icon_animation_free(game_state->barry.sprite);
  74. free(game_state);
  75. }
  76. static void jetpack_game_tick(GameState* const game_state) {
  77. if(game_state->state == GameStateLife) {
  78. // Do jetpack things
  79. game_state->barry.gravity += GRAVITY_TICK;
  80. game_state->barry.point.y += game_state->barry.gravity;
  81. // spawn scientists and coins...
  82. jetpack_game_random_coins(game_state);
  83. if(game_state->barry.isBoosting) {
  84. game_state->barry.gravity += GRAVITY_BOOST;
  85. }
  86. }
  87. }
  88. static void jetpack_game_render_callback(Canvas* const canvas, void* ctx) {
  89. furi_assert(ctx);
  90. const GameState* game_state = ctx;
  91. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  92. canvas_draw_frame(canvas, 0, 0, 128, 64);
  93. if(game_state->state == GameStateLife) {
  94. // Draw scene
  95. // Draw coins + scientists
  96. // Draw barry
  97. canvas_draw_icon_animation(
  98. canvas, game_state->barry.point.x, game_state->barry.point.y, game_state->barry.sprite);
  99. canvas_set_font(canvas, FontSecondary);
  100. char buffer[12];
  101. snprintf(buffer, sizeof(buffer), "Dist: %u", game_state->distance);
  102. canvas_draw_str_aligned(canvas, 100, 12, AlignCenter, AlignBottom, buffer);
  103. }
  104. if(game_state->state == GameStateGameOver) {
  105. // Show highscore
  106. }
  107. furi_mutex_release(game_state->mutex);
  108. }
  109. static void jetpack_game_input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  110. furi_assert(event_queue);
  111. GameEvent event = {.type = EventTypeKey, .input = *input_event};
  112. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  113. }
  114. static void jetpack_game_update_timer_callback(FuriMessageQueue* event_queue) {
  115. furi_assert(event_queue);
  116. GameEvent event = {.type = EventTypeTick};
  117. furi_message_queue_put(event_queue, &event, 0);
  118. }
  119. int32_t jetpack_game_app(void* p) {
  120. UNUSED(p);
  121. int32_t return_code = 0;
  122. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(GameEvent));
  123. GameState* game_state = malloc(sizeof(GameState));
  124. jetpack_game_state_init(game_state);
  125. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  126. if(!game_state->mutex) {
  127. FURI_LOG_E(TAG, "cannot create mutex\r\n");
  128. return_code = 255;
  129. goto free_and_exit;
  130. }
  131. // Set system callbacks
  132. ViewPort* view_port = view_port_alloc();
  133. view_port_draw_callback_set(view_port, jetpack_game_render_callback, game_state);
  134. view_port_input_callback_set(view_port, jetpack_game_input_callback, event_queue);
  135. FuriTimer* timer =
  136. furi_timer_alloc(jetpack_game_update_timer_callback, FuriTimerTypePeriodic, event_queue);
  137. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 25);
  138. // Open GUI and register view_port
  139. Gui* gui = furi_record_open(RECORD_GUI);
  140. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  141. GameEvent event;
  142. for(bool processing = true; processing;) {
  143. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  144. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  145. if(event_status == FuriStatusOk) {
  146. // press events
  147. if(event.type == EventTypeKey) {
  148. if(event.input.type == InputTypeRelease && event.input.key == InputKeyOk) {
  149. game_state->barry.isBoosting = false;
  150. }
  151. if(event.input.type == InputTypePress) {
  152. switch(event.input.key) {
  153. case InputKeyUp:
  154. break;
  155. case InputKeyDown:
  156. break;
  157. case InputKeyRight:
  158. break;
  159. case InputKeyLeft:
  160. break;
  161. case InputKeyOk:
  162. if(game_state->state == GameStateGameOver) {
  163. jetpack_game_state_init(game_state);
  164. }
  165. if(game_state->state == GameStateLife) {
  166. // Do something
  167. game_state->barry.isBoosting = true;
  168. }
  169. break;
  170. case InputKeyBack:
  171. processing = false;
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. } else if(event.type == EventTypeTick) {
  178. jetpack_game_tick(game_state);
  179. }
  180. }
  181. view_port_update(view_port);
  182. furi_mutex_release(game_state->mutex);
  183. }
  184. furi_timer_free(timer);
  185. view_port_enabled_set(view_port, false);
  186. gui_remove_view_port(gui, view_port);
  187. furi_record_close(RECORD_GUI);
  188. view_port_free(view_port);
  189. furi_mutex_free(game_state->mutex);
  190. free_and_exit:
  191. jetpack_game_state_free(game_state);
  192. furi_message_queue_free(event_queue);
  193. return return_code;
  194. }