paperplane.c 6.8 KB

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