trexrunner.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/icon_i.h>
  5. #include <gui/elements.h>
  6. #include <input/input.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "t-rex-runner_icons.h"
  10. #define DINO_START_X 10
  11. #define DINO_START_Y 35 // 64 - 22 - BACKGROUND_H / 2 - 1
  12. #define FPS 20
  13. #define DINO_RUNNING_MS_PER_FRAME 500
  14. #define GRAVITY 60
  15. #define JUMP_SPEED 30
  16. #define CACTUS_W 8
  17. #define CACTUS_H 10
  18. #define START_x_speed 25
  19. #define BACKGROUND_W 176
  20. #define BACKGROUND_H 12
  21. typedef enum {
  22. EventTypeTick,
  23. EventTypeKey,
  24. } EventType;
  25. typedef struct {
  26. EventType type;
  27. InputEvent input;
  28. } PluginEvent;
  29. typedef struct {
  30. FuriTimer *timer;
  31. uint32_t last_tick;
  32. const Icon* dino_icon;
  33. int dino_frame_ms;
  34. FuriMutex* mutex;
  35. // Dino info
  36. float y_position;
  37. float y_speed;
  38. int y_acceleration;
  39. float x_speed;
  40. // Cactus info
  41. int cactus_position;
  42. int has_cactus;
  43. // Horizontal line
  44. int background_position;
  45. int lost;
  46. } GameState;
  47. static void timer_callback(void *ctx) {
  48. GameState *game_state = ctx;
  49. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  50. if (game_state == NULL) {
  51. return;
  52. }
  53. uint32_t ticks_elapsed = furi_get_tick() - game_state->last_tick;
  54. game_state->last_tick = furi_get_tick();
  55. int delta_time_ms = ticks_elapsed * 1000 / furi_kernel_get_tick_frequency();
  56. // dino update
  57. game_state->dino_frame_ms += delta_time_ms;
  58. // TODO: switch by dino state
  59. if (game_state->dino_frame_ms >= DINO_RUNNING_MS_PER_FRAME) {
  60. if (game_state->dino_icon == &I_DinoRun0) {
  61. game_state->dino_icon = &I_DinoRun1;
  62. } else {
  63. game_state->dino_icon = &I_DinoRun0;
  64. }
  65. game_state->dino_frame_ms = 0;
  66. }
  67. // Compute dino dynamics
  68. game_state->y_acceleration = game_state->y_acceleration - GRAVITY * delta_time_ms / 1000;
  69. game_state->y_speed = game_state->y_speed + game_state->y_acceleration * delta_time_ms / 1000;
  70. game_state->y_position = game_state->y_position - game_state->y_speed * delta_time_ms / 1000;
  71. // Touch ground
  72. if (game_state->y_position >= DINO_START_Y) {
  73. game_state->y_acceleration = 0;
  74. game_state->y_speed = 0;
  75. game_state->y_position = DINO_START_Y;
  76. }
  77. // Update Cactus state
  78. if (game_state->has_cactus){
  79. game_state->cactus_position = game_state->cactus_position - game_state->x_speed * delta_time_ms / 1000;
  80. if (game_state->cactus_position <= CACTUS_W + 1) {
  81. game_state->has_cactus = 0;
  82. game_state->cactus_position = 120;
  83. }
  84. }
  85. // Create cactus (not random)
  86. else {
  87. game_state->has_cactus = 1;
  88. game_state->cactus_position = 120;
  89. game_state->x_speed = START_x_speed;
  90. }
  91. // Move horizontal line
  92. if (game_state->background_position <= - BACKGROUND_W)
  93. game_state->background_position += BACKGROUND_W;
  94. game_state->background_position = game_state->background_position - game_state->x_speed * delta_time_ms / 1000;
  95. // Lose condition
  96. if ((game_state->y_position + 22 >= (64 - CACTUS_H)) && ((DINO_START_X+20) >= game_state->cactus_position) && (DINO_START_X <= (game_state->cactus_position + CACTUS_W)))
  97. game_state->lost = 1;
  98. furi_mutex_release(game_state->mutex);
  99. }
  100. static void input_callback(InputEvent *input_event, FuriMessageQueue *event_queue) {
  101. furi_assert(event_queue);
  102. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  103. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  104. }
  105. static void render_callback(Canvas *const canvas, void *ctx) {
  106. const GameState *game_state = ctx;
  107. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  108. if (game_state == NULL) {
  109. return;
  110. }
  111. if(!game_state->lost){
  112. // Show Ground
  113. canvas_draw_icon(canvas, game_state->background_position, 64 - BACKGROUND_H, &I_HorizonLine0);
  114. canvas_draw_icon(canvas, game_state->background_position + BACKGROUND_W, 64 - BACKGROUND_H, &I_HorizonLine0);
  115. // Show DINO
  116. canvas_draw_icon(canvas, DINO_START_X, game_state->y_position, game_state->dino_icon);
  117. // Show cactus
  118. if (game_state->has_cactus)
  119. canvas_draw_triangle(canvas, game_state->cactus_position, 64 - BACKGROUND_H + CACTUS_W, CACTUS_W, CACTUS_H, CanvasDirectionBottomToTop);
  120. } else {
  121. canvas_set_font(canvas, FontPrimary);
  122. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "You lost :c");
  123. }
  124. furi_mutex_release(game_state->mutex);
  125. }
  126. static void game_state_init(GameState *const game_state) {
  127. game_state->last_tick = furi_get_tick();
  128. game_state->dino_frame_ms = 0;
  129. game_state->dino_icon = &I_Dino;
  130. game_state->y_acceleration = game_state->y_speed = 0;
  131. game_state->y_position = DINO_START_Y;
  132. game_state->has_cactus = 0;
  133. game_state->background_position = 0;
  134. game_state->lost = 0;
  135. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  136. }
  137. int32_t trexrunner_app() {
  138. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  139. GameState *game_state = malloc(sizeof(GameState));
  140. game_state_init(game_state);
  141. if (!game_state->mutex) {
  142. FURI_LOG_E("T-rex runner", "cannot create mutex\r\n");
  143. free(game_state);
  144. return 255;
  145. }
  146. // BEGIN IMPLEMENTATION
  147. // Set system callbacks
  148. ViewPort *view_port = view_port_alloc();
  149. view_port_draw_callback_set(view_port, render_callback, game_state);
  150. view_port_input_callback_set(view_port, input_callback, event_queue);
  151. game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
  152. furi_timer_start(game_state->timer, (uint32_t) furi_kernel_get_tick_frequency() / FPS);
  153. // Open GUI and register view_port
  154. Gui *gui = furi_record_open("gui");
  155. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  156. PluginEvent event;
  157. for (bool processing = true; processing && !game_state->lost;) {
  158. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  159. if (event_status == FuriStatusOk) {
  160. // press events
  161. if (event.type == EventTypeKey) {
  162. if (event.input.type == InputTypeShort) {
  163. switch (event.input.key) {
  164. case InputKeyUp:
  165. // Test command
  166. game_state->y_position -= 10;
  167. break;
  168. case InputKeyDown:
  169. // Test command
  170. game_state->y_position += 10;
  171. break;
  172. case InputKeyLeft:
  173. break;
  174. case InputKeyRight:
  175. break;
  176. case InputKeyOk:
  177. if (game_state->y_position == DINO_START_Y)
  178. game_state->y_speed = JUMP_SPEED;
  179. break;
  180. case InputKeyMAX:
  181. break;
  182. case InputKeyBack:
  183. // Exit the app
  184. processing = false;
  185. break;
  186. }
  187. }
  188. }
  189. } else {
  190. // event timeout
  191. ;
  192. }
  193. if (game_state->lost){
  194. furi_message_queue_get(event_queue, &event, 1500); //Sleep to show the "you lost" message
  195. }
  196. view_port_update(view_port);
  197. furi_mutex_release(game_state->mutex);
  198. }
  199. view_port_enabled_set(view_port, false);
  200. gui_remove_view_port(gui, view_port);
  201. furi_record_close("gui");
  202. view_port_free(view_port);
  203. furi_message_queue_free(event_queue);
  204. furi_mutex_free(game_state->mutex);
  205. furi_timer_free(game_state->timer);
  206. free(game_state);
  207. return 0;
  208. }