paperplane.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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 "game_state.h"
  11. #include "paper_plane_icons.h"
  12. #define FPS 20
  13. #define SPRITE_SIZE 8
  14. typedef enum {
  15. EventTypeTick,
  16. EventTypeKey,
  17. } EventType;
  18. typedef struct {
  19. EventType type;
  20. InputEvent input;
  21. } PluginEvent;
  22. static void timer_callback(void* ctx) {
  23. GameState* game_state = ctx;
  24. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  25. if(game_state == NULL) {
  26. return;
  27. }
  28. uint32_t ticks_elapsed = furi_get_tick() - game_state->last_tick;
  29. game_state->last_tick = furi_get_tick();
  30. int delta_time_ms = ticks_elapsed * 1000 / furi_kernel_get_tick_frequency();
  31. if(!game_state->crash_flag) {
  32. update_position(game_state->paper, delta_time_ms);
  33. check_collision(game_state);
  34. }
  35. furi_mutex_release(game_state->mutex);
  36. }
  37. static void input_callback(InputEvent* input_event, void* ctx) {
  38. furi_assert(ctx);
  39. FuriMessageQueue* event_queue = ctx;
  40. PluginEvent event = {.type = EventTypeKey, .input = *input_event};
  41. furi_message_queue_put(event_queue, &event, FuriWaitForever);
  42. }
  43. static void render_callback(Canvas* const canvas, void* ctx) {
  44. const GameState* game_state = ctx;
  45. furi_mutex_acquire(game_state->mutex, FuriWaitForever);
  46. if(game_state == NULL) {
  47. return;
  48. }
  49. // draw map (this should probably be it's own function)
  50. float background_position = game_state->paper->y;
  51. for(int i = background_position; i < background_position + 10; i++) {
  52. /*
  53. using a uint32_t here so that bits
  54. that have been shifted out can still
  55. be read.
  56. */
  57. uint32_t currentRow = game_state->map[i];
  58. for(unsigned int j = 0; j < sizeof(uint16_t) * 8; j++) {
  59. /*
  60. 0x8000 is 1 with 15 zeros
  61. 00000000000000001000000000000000 - 0x8000
  62. 00000000000000001111111001111111 - map data (currentRow)
  63. using & will result in:
  64. 00000000000000001000000000000000
  65. the above number will evaluate to true
  66. OR
  67. 00000000000000001000000000000000 - 0x8000
  68. 00000000000000000111111001111111 - map data (currentRow)
  69. using & will result in:
  70. 00000000000000000000000000000000
  71. the above number will result in false
  72. */
  73. if(currentRow & 0x8000) {
  74. const Icon* ground_to_draw = &I_Ground;
  75. // if the bit to the left is 0, use the right facing ground sprite
  76. if(!(currentRow & 0x4000)) {
  77. ground_to_draw = &I_GroundRight;
  78. }
  79. // if the bit to the right is 0, use the left facing ground tile
  80. if(!(currentRow & 0x10000)) {
  81. ground_to_draw = &I_GroundLeft;
  82. }
  83. canvas_draw_icon(
  84. canvas,
  85. j * SPRITE_SIZE,
  86. i * SPRITE_SIZE - background_position * SPRITE_SIZE,
  87. ground_to_draw);
  88. }
  89. // bit shift currentRow to the left, so the bit to the right will be drawn
  90. currentRow <<= 1;
  91. }
  92. }
  93. // draw plane
  94. canvas_draw_icon(
  95. canvas, game_state->paper->x * SPRITE_SIZE, PAPER_START_Y, game_state->paper->icon);
  96. // Show score
  97. char score_string[11]; // length is 11 b/c: Score: xxx\0
  98. canvas_draw_icon(canvas, 77, 2, &I_Score);
  99. snprintf(
  100. score_string, 11, "Score: %d", (int)game_state->paper->y); // copy score into score_string
  101. canvas_draw_str_aligned(canvas, 80, 5, AlignLeft, AlignTop, score_string);
  102. furi_mutex_release(game_state->mutex);
  103. }
  104. int32_t paperplane_app() {
  105. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(PluginEvent));
  106. GameState* game_state = malloc(sizeof(GameState));
  107. game_state_init(game_state);
  108. if(!game_state->mutex) {
  109. FURI_LOG_E("Paper Plane", "cannot create mutex\r\n");
  110. // game crash, all initialized items must be freed.
  111. furi_message_queue_free(event_queue);
  112. //furi_timer_free(game_state->timer); this causes a null pointer dereference
  113. free(game_state->paper);
  114. free(game_state->map);
  115. free(game_state);
  116. return 255;
  117. }
  118. // Set system callbacks
  119. ViewPort* view_port = view_port_alloc();
  120. view_port_draw_callback_set(view_port, render_callback, game_state);
  121. view_port_input_callback_set(view_port, input_callback, event_queue);
  122. game_state->timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, game_state);
  123. furi_timer_start(game_state->timer, (uint32_t)furi_kernel_get_tick_frequency() / FPS);
  124. // Open GUI and register view_port
  125. Gui* gui = furi_record_open("gui");
  126. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  127. PluginEvent event;
  128. for(bool processing = true; processing;) {
  129. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 100);
  130. if(event_status == FuriStatusOk) {
  131. // press events
  132. if(event.type == EventTypeKey) {
  133. if(event.input.type == InputTypePress || event.input.type == InputTypeLong ||
  134. event.input.type == InputTypeRepeat) {
  135. switch(event.input.key) {
  136. case InputKeyUp:
  137. break;
  138. case InputKeyDown:
  139. break;
  140. case InputKeyLeft:
  141. rotate_left(game_state->paper);
  142. break;
  143. case InputKeyRight:
  144. rotate_right(game_state->paper);
  145. break;
  146. case InputKeyOk:
  147. if(game_state->crash_flag) {
  148. game_state_reinit(game_state);
  149. break;
  150. }
  151. break;
  152. case InputKeyMAX:
  153. break;
  154. case InputKeyBack:
  155. // Exit the app
  156. processing = false;
  157. break;
  158. }
  159. }
  160. }
  161. }
  162. furi_mutex_release(game_state->mutex);
  163. view_port_update(view_port);
  164. }
  165. view_port_enabled_set(view_port, false);
  166. gui_remove_view_port(gui, view_port);
  167. furi_record_close("gui");
  168. view_port_free(view_port);
  169. furi_message_queue_free(event_queue);
  170. furi_mutex_free(game_state->mutex);
  171. furi_timer_free(game_state->timer);
  172. free(game_state->paper);
  173. free(game_state->map);
  174. free(game_state);
  175. return 0;
  176. }