trexrunner.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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 =
  81. game_state->cactus_position - game_state->x_speed * delta_time_ms / 1000;
  82. if(game_state->cactus_position <= 0) {
  83. game_state->has_cactus = 0;
  84. game_state->score = game_state->score + 1;
  85. }
  86. }
  87. // Create cactus (not random)
  88. else {
  89. game_state->has_cactus = 1;
  90. game_state->cactus_position = 120;
  91. }
  92. // Move horizontal line
  93. if(game_state->background_position <= -BACKGROUND_W)
  94. game_state->background_position += BACKGROUND_W;
  95. game_state->background_position =
  96. game_state->background_position - game_state->x_speed * delta_time_ms / 1000;
  97. // Lose condition
  98. if((game_state->y_position + 22 >= (64 - CACTUS_H)) &&
  99. ((DINO_START_X + 20) >= game_state->cactus_position) &&
  100. (DINO_START_X <= (game_state->cactus_position + CACTUS_W)))
  101. game_state->lost = 1;
  102. furi_mutex_release(game_state->mutex);
  103. }
  104. static void input_callback(InputEvent* input_event, FuriMessageQueue* event_queue) {
  105. furi_assert(event_queue);
  106. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  107. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  108. }
  109. static void render_callback(Canvas* const canvas, void* ctx) {
  110. const GameState* game_state = ctx;
  111. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  112. if(game_state == NULL) {
  113. return;
  114. }
  115. char score_string[12];
  116. if(!game_state->lost) {
  117. // Show Ground
  118. canvas_draw_icon(
  119. canvas, game_state->background_position, 64 - BACKGROUND_H, &I_HorizonLine0);
  120. canvas_draw_icon(
  121. canvas,
  122. game_state->background_position + BACKGROUND_W,
  123. 64 - BACKGROUND_H,
  124. &I_HorizonLine0);
  125. // Show DINO
  126. canvas_draw_icon(canvas, DINO_START_X, game_state->y_position, game_state->dino_icon);
  127. // Show cactus
  128. if(game_state->has_cactus)
  129. //canvas_draw_triangle(canvas, game_state->cactus_position, 64 - BACKGROUND_H + CACTUS_W, CACTUS_W, CACTUS_H, CanvasDirectionBottomToTop);
  130. canvas_draw_icon(
  131. canvas,
  132. game_state->cactus_position,
  133. 64 - BACKGROUND_H / 2 - CACTUS_H - 2,
  134. &I_Cactus);
  135. // Show score
  136. if(game_state->score == 0) canvas_set_font(canvas, FontSecondary);
  137. snprintf(score_string, 12, "Score: %d", game_state->score);
  138. canvas_draw_str_aligned(canvas, 85, 5, AlignLeft, AlignTop, score_string);
  139. } else {
  140. canvas_set_font(canvas, FontPrimary);
  141. canvas_draw_str_aligned(canvas, 64, 32, AlignCenter, AlignBottom, "You lost :c");
  142. }
  143. furi_mutex_release(game_state->mutex);
  144. }
  145. static void game_state_init(GameState* const game_state) {
  146. game_state->last_tick = furi_get_tick();
  147. game_state->dino_frame_ms = 0;
  148. game_state->dino_icon = &I_Dino;
  149. game_state->y_acceleration = game_state->y_speed = 0;
  150. game_state->y_position = DINO_START_Y;
  151. game_state->has_cactus = 0;
  152. game_state->background_position = 0;
  153. game_state->lost = 0;
  154. game_state->x_speed = START_x_speed;
  155. game_state->score = 0;
  156. game_state->mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  157. }
  158. static void game_state_reinit(GameState* const game_state) {
  159. game_state->last_tick = furi_get_tick();
  160. game_state->y_acceleration = game_state->y_speed = 0;
  161. game_state->y_position = DINO_START_Y;
  162. game_state->has_cactus = 0;
  163. game_state->background_position = 0;
  164. game_state->lost = 0;
  165. game_state->x_speed = START_x_speed;
  166. game_state->score = 0;
  167. }
  168. int32_t trexrunner_app() {
  169. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  170. GameState* game_state = malloc(sizeof(GameState));
  171. game_state_init(game_state);
  172. if(!game_state->mutex) {
  173. FURI_LOG_E("T-rex runner", "cannot create mutex\r\n");
  174. free(game_state);
  175. return 255;
  176. }
  177. // BEGIN IMPLEMENTATION
  178. // Set system callbacks
  179. ViewPort* view_port = view_port_alloc();
  180. view_port_draw_callback_set(view_port, render_callback, game_state);
  181. view_port_input_callback_set(view_port, input_callback, event_queue);
  182. game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
  183. furi_timer_start(game_state->timer, (uint32_t)furi_kernel_get_tick_frequency() / FPS);
  184. // Open GUI and register view_port
  185. Gui* gui = furi_record_open("gui");
  186. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  187. PluginEvent event;
  188. for(bool processing = true; processing;) {
  189. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  190. if(event_status == FuriStatusOk) {
  191. // press events
  192. if(event.type == EventTypeKey) {
  193. if(event.input.type == InputTypeShort) {
  194. switch(event.input.key) {
  195. case InputKeyUp:
  196. break;
  197. case InputKeyDown:
  198. break;
  199. case InputKeyLeft:
  200. break;
  201. case InputKeyRight:
  202. break;
  203. case InputKeyOk:
  204. if(game_state->lost) {
  205. game_state_reinit(game_state);
  206. break;
  207. }
  208. if(game_state->y_position == DINO_START_Y)
  209. game_state->y_speed = JUMP_SPEED;
  210. break;
  211. case InputKeyMAX:
  212. break;
  213. case InputKeyBack:
  214. // Exit the app
  215. processing = false;
  216. break;
  217. }
  218. }
  219. }
  220. }
  221. view_port_update(view_port);
  222. furi_mutex_release(game_state->mutex);
  223. }
  224. view_port_enabled_set(view_port, false);
  225. gui_remove_view_port(gui, view_port);
  226. furi_record_close("gui");
  227. view_port_free(view_port);
  228. furi_message_queue_free(event_queue);
  229. furi_mutex_free(game_state->mutex);
  230. furi_timer_free(game_state->timer);
  231. free(game_state);
  232. return 0;
  233. }