trexrunner.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 34 // 64 - 22 - BACKGROUND_H / 2 - 2
  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 10
  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 <= 0 ) {
  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. canvas_draw_icon(canvas, game_state->cactus_position, 64 - BACKGROUND_H/2 - CACTUS_H - 2, &I_Cactus);
  122. // Show score
  123. if(game_state->score == 0)
  124. canvas_set_font(canvas, FontSecondary);
  125. snprintf(score_string, 12, "Score: %d", game_state->score);
  126. canvas_draw_str_aligned(canvas, 85, 5, AlignLeft, AlignTop, score_string);
  127. } else {
  128. canvas_set_font(canvas, FontPrimary);
  129. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "You lost :c");
  130. }
  131. furi_mutex_release(game_state->mutex);
  132. }
  133. static void game_state_init(GameState *const game_state) {
  134. game_state->last_tick = furi_get_tick();
  135. game_state->dino_frame_ms = 0;
  136. game_state->dino_icon = &I_Dino;
  137. game_state->y_acceleration = game_state->y_speed = 0;
  138. game_state->y_position = DINO_START_Y;
  139. game_state->has_cactus = 0;
  140. game_state->background_position = 0;
  141. game_state->lost = 0;
  142. game_state->x_speed = START_x_speed;
  143. game_state->score = 0;
  144. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  145. }
  146. int32_t trexrunner_app() {
  147. FuriMessageQueue *event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  148. GameState *game_state = malloc(sizeof(GameState));
  149. game_state_init(game_state);
  150. if (!game_state->mutex) {
  151. FURI_LOG_E("T-rex runner", "cannot create mutex\r\n");
  152. free(game_state);
  153. return 255;
  154. }
  155. // BEGIN IMPLEMENTATION
  156. // Set system callbacks
  157. ViewPort *view_port = view_port_alloc();
  158. view_port_draw_callback_set(view_port, render_callback, game_state);
  159. view_port_input_callback_set(view_port, input_callback, event_queue);
  160. game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
  161. furi_timer_start(game_state->timer, (uint32_t) furi_kernel_get_tick_frequency() / FPS);
  162. // Open GUI and register view_port
  163. Gui *gui = furi_record_open("gui");
  164. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  165. PluginEvent event;
  166. for (bool processing = true; processing && !game_state->lost;) {
  167. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  168. if (event_status == FuriStatusOk) {
  169. // press events
  170. if (event.type == EventTypeKey) {
  171. if (event.input.type == InputTypeShort) {
  172. switch (event.input.key) {
  173. case InputKeyUp:
  174. break;
  175. case InputKeyDown:
  176. break;
  177. case InputKeyLeft:
  178. break;
  179. case InputKeyRight:
  180. break;
  181. case InputKeyOk:
  182. if (game_state->y_position == DINO_START_Y)
  183. game_state->y_speed = JUMP_SPEED;
  184. break;
  185. case InputKeyMAX:
  186. break;
  187. case InputKeyBack:
  188. // Exit the app
  189. processing = false;
  190. break;
  191. }
  192. }
  193. }
  194. } else {
  195. // event timeout
  196. ;
  197. }
  198. if (game_state->lost){
  199. furi_message_queue_get(event_queue, &event, 1500); //Sleep to show the "you lost" message
  200. }
  201. view_port_update(view_port);
  202. furi_mutex_release(game_state->mutex);
  203. }
  204. view_port_enabled_set(view_port, false);
  205. gui_remove_view_port(gui, view_port);
  206. furi_record_close("gui");
  207. view_port_free(view_port);
  208. furi_message_queue_free(event_queue);
  209. furi_mutex_free(game_state->mutex);
  210. furi_timer_free(game_state->timer);
  211. free(game_state);
  212. return 0;
  213. }