trexrunner.c 8.8 KB

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