trexrunner.c 8.9 KB

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