trexrunner.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 128
  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. int score;
  47. } GameState;
  48. static void timer_callback(void *ctx) {
  49. GameState *game_state = ctx;
  50. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  51. if (game_state == NULL) {
  52. return;
  53. }
  54. uint32_t ticks_elapsed = furi_get_tick() - game_state->last_tick;
  55. game_state->last_tick = furi_get_tick();
  56. int delta_time_ms = ticks_elapsed * 1000 / furi_kernel_get_tick_frequency();
  57. // dino update
  58. game_state->dino_frame_ms += delta_time_ms;
  59. // TODO: switch by dino state
  60. if (game_state->dino_frame_ms >= DINO_RUNNING_MS_PER_FRAME) {
  61. if (game_state->dino_icon == &I_DinoRun0) {
  62. game_state->dino_icon = &I_DinoRun1;
  63. } else {
  64. game_state->dino_icon = &I_DinoRun0;
  65. }
  66. game_state->dino_frame_ms = 0;
  67. }
  68. // Compute dino dynamics
  69. game_state->y_acceleration = game_state->y_acceleration - GRAVITY * delta_time_ms / 1000;
  70. game_state->y_speed = game_state->y_speed + game_state->y_acceleration * delta_time_ms / 1000;
  71. game_state->y_position = game_state->y_position - game_state->y_speed * delta_time_ms / 1000;
  72. // Touch ground
  73. if (game_state->y_position >= DINO_START_Y) {
  74. game_state->y_acceleration = 0;
  75. game_state->y_speed = 0;
  76. game_state->y_position = DINO_START_Y;
  77. }
  78. // Update Cactus state
  79. if (game_state->has_cactus){
  80. game_state->cactus_position = game_state->cactus_position - game_state->x_speed * delta_time_ms / 1000;
  81. if (game_state->cactus_position <= CACTUS_W + 1) {
  82. game_state->has_cactus = 0;
  83. game_state->score = game_state->score + 1;
  84. }
  85. }
  86. // Create cactus (not random)
  87. else {
  88. game_state->has_cactus = 1;
  89. game_state->cactus_position = 120;
  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. char score_string[12];
  112. if(!game_state->lost){
  113. // Show Ground
  114. canvas_draw_icon(canvas, game_state->background_position, 64 - BACKGROUND_H, &I_HorizonLine0);
  115. canvas_draw_icon(canvas, game_state->background_position + BACKGROUND_W, 64 - BACKGROUND_H, &I_HorizonLine0);
  116. // Show DINO
  117. canvas_draw_icon(canvas, DINO_START_X, game_state->y_position, game_state->dino_icon);
  118. // Show cactus
  119. if (game_state->has_cactus)
  120. canvas_draw_triangle(canvas, game_state->cactus_position, 64 - BACKGROUND_H + CACTUS_W, CACTUS_W, CACTUS_H, CanvasDirectionBottomToTop);
  121. // Show score
  122. if(game_state->score == 0)
  123. canvas_set_font(canvas, FontSecondary);
  124. snprintf(score_string, 12, "Score: %d", game_state->score);
  125. canvas_draw_str_aligned(canvas, 85, 5, AlignLeft, AlignTop, score_string);
  126. } else {
  127. canvas_set_font(canvas, FontPrimary);
  128. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "You lost :c");
  129. }
  130. furi_mutex_release(game_state->mutex);
  131. }
  132. static void game_state_init(GameState *const game_state) {
  133. game_state->last_tick = furi_get_tick();
  134. game_state->dino_frame_ms = 0;
  135. game_state->dino_icon = &I_Dino;
  136. game_state->y_acceleration = game_state->y_speed = 0;
  137. game_state->y_position = DINO_START_Y;
  138. game_state->has_cactus = 0;
  139. game_state->background_position = 0;
  140. game_state->lost = 0;
  141. game_state->x_speed = START_x_speed;
  142. game_state->score = 0;
  143. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  144. }
  145. int32_t trexrunner_app() {
  146. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  147. GameState *game_state = malloc(sizeof(GameState));
  148. game_state_init(game_state);
  149. if (!game_state->mutex) {
  150. FURI_LOG_E("T-rex runner", "cannot create mutex\r\n");
  151. free(game_state);
  152. return 255;
  153. }
  154. // BEGIN IMPLEMENTATION
  155. // Set system callbacks
  156. ViewPort *view_port = view_port_alloc();
  157. view_port_draw_callback_set(view_port, render_callback, game_state);
  158. view_port_input_callback_set(view_port, input_callback, event_queue);
  159. game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
  160. furi_timer_start(game_state->timer, (uint32_t) furi_kernel_get_tick_frequency() / FPS);
  161. // Open GUI and register view_port
  162. Gui *gui = furi_record_open("gui");
  163. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  164. PluginEvent event;
  165. for (bool processing = true; processing && !game_state->lost;) {
  166. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  167. if (event_status == FuriStatusOk) {
  168. // press events
  169. if (event.type == EventTypeKey) {
  170. if (event.input.type == InputTypeShort) {
  171. switch (event.input.key) {
  172. case InputKeyUp:
  173. // Test command
  174. game_state->y_position -= 10;
  175. break;
  176. case InputKeyDown:
  177. // Test command
  178. game_state->y_position += 10;
  179. break;
  180. case InputKeyLeft:
  181. break;
  182. case InputKeyRight:
  183. break;
  184. case InputKeyOk:
  185. if (game_state->y_position == DINO_START_Y)
  186. game_state->y_speed = JUMP_SPEED;
  187. break;
  188. case InputKeyMAX:
  189. break;
  190. case InputKeyBack:
  191. // Exit the app
  192. processing = false;
  193. break;
  194. }
  195. }
  196. }
  197. } else {
  198. // event timeout
  199. ;
  200. }
  201. if (game_state->lost){
  202. furi_message_queue_get(event_queue, &event, 1500); //Sleep to show the "you lost" message
  203. }
  204. view_port_update(view_port);
  205. furi_mutex_release(game_state->mutex);
  206. }
  207. view_port_enabled_set(view_port, false);
  208. gui_remove_view_port(gui, view_port);
  209. furi_record_close("gui");
  210. view_port_free(view_port);
  211. furi_message_queue_free(event_queue);
  212. furi_mutex_free(game_state->mutex);
  213. furi_timer_free(game_state->timer);
  214. free(game_state);
  215. return 0;
  216. }