app.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /* Copyright (C) 2023 Salvatore Sanfilippo -- All Rights Reserved
  2. * See the LICENSE file for information about the license. */
  3. #include <furi.h>
  4. #include <furi_hal.h>
  5. #include <input/input.h>
  6. #include <gui/gui.h>
  7. #include <stdlib.h>
  8. #include <gui/gui.h>
  9. #include <gui/view_dispatcher.h>
  10. #include <gui/scene_manager.h>
  11. #include <math.h>
  12. #define TAG "Asteroids" // Used for logging
  13. #define DEBUG_MSG 1
  14. #define SCREEN_XRES 128
  15. #define SCREEN_YRES 64
  16. #ifndef PI
  17. #define PI 3.14159265358979f
  18. #endif
  19. #define MAXBUL 10
  20. typedef struct AsteroidsApp {
  21. /* GUI */
  22. Gui *gui;
  23. ViewPort *view_port; /* We just use a raw viewport and we render
  24. everything into the low level canvas. */
  25. FuriMessageQueue *event_queue; /* Keypress events go here. */
  26. /* Game state. */
  27. int running; /* Once false exists the app. */
  28. uint32_t ticks; /* Game ticks. Increments at each refresh. */
  29. float shipx; /* Ship x position. */
  30. float shipy; /* Ship y position. */
  31. float shipa; /* Ship current angle, 2*PI is a full rotation. */
  32. float shipvx; /* x velocity. */
  33. float shipvy; /* y velocity. */
  34. float bulletsx[MAXBUL]; /* Bullets x position. */
  35. float bulletsy[MAXBUL]; /* Bullets y position. */
  36. int bullets; /* Active bullets. */
  37. uint32_t last_bullet_tick; /* Tick the last bullet was fired. */
  38. float asteroidsx[MAXBUL]; /* Asteroids x position. */
  39. float asteroidsy[MAXBUL]; /* Asteroids y position. */
  40. int asteroids; /* Active asteroids. */
  41. uint32_t pressed[InputKeyMAX]; /* pressed[id] is true if pressed. */
  42. bool fire; /* Short press detected: fire a bullet. */
  43. } AsteroidsApp;
  44. /* This structure represents a polygon of at most POLY_MAX points.
  45. * The function draw_poly() is able to render it on the screen, rotated
  46. * by the amount specified. */
  47. #define POLY_MAX 8
  48. typedef struct Poly {
  49. float x[POLY_MAX];
  50. float y[POLY_MAX];
  51. uint32_t points; /* Number of points actually populated. */
  52. } Poly;
  53. /* Define the polygons we use. */
  54. Poly ShipPoly = {
  55. {-3, 0, 3},
  56. {-3, 6, -3},
  57. 3
  58. };
  59. /* Rotate the point of the poligon 'poly' and store the new rotated
  60. * polygon in 'rot'. The polygon is rotated by an angle 'a', with
  61. * center at 0,0. */
  62. void rotate_poly(Poly *rot, Poly *poly, float a) {
  63. float sin_a = (float)sin(a);
  64. float cos_a = (float)cos(a);
  65. for (uint32_t j = 0; j < poly->points; j++) {
  66. rot->x[j] = poly->x[j]*cos_a - poly->y[j]*sin_a;
  67. rot->y[j] = poly->y[j]*cos_a + poly->x[j]*sin_a;
  68. }
  69. rot->points = poly->points;
  70. }
  71. #if 0
  72. /* This is an 8 bit LFSR we use to generate a predictable and fast
  73. * pseudorandom sequence of numbers, to give a different shape to
  74. * each asteroid. */
  75. static void lfsr_next(unsigned char *prev) {
  76. unsigned char lsb = *prev & 1;
  77. *prev = *prev >> 1;
  78. if (lsb == 1) *prev ^= 0b11000111;
  79. }
  80. #endif
  81. /* Render the polygon 'poly' at x,y, rotated by the specified angle. */
  82. void draw_poly(Canvas *const canvas, Poly *poly, uint8_t x, uint8_t y, float a)
  83. {
  84. Poly rot;
  85. rotate_poly(&rot,poly,a);
  86. canvas_set_color(canvas, ColorBlack);
  87. for (uint32_t j = 0; j < rot.points; j++) {
  88. uint32_t a = j;
  89. uint32_t b = j+1;
  90. if (b == rot.points) b = 0;
  91. canvas_draw_line(canvas,x+rot.x[a],y+rot.y[a],
  92. x+rot.x[b],y+rot.y[b]);
  93. }
  94. }
  95. /* Render the current game screen. */
  96. static void render_callback(Canvas *const canvas, void *ctx) {
  97. AsteroidsApp *app = ctx;
  98. /* Clear screen. */
  99. canvas_set_color(canvas, ColorWhite);
  100. canvas_draw_box(canvas, 0, 0, 127, 63);
  101. /* Draw ship and asteroids. */
  102. draw_poly(canvas,&ShipPoly,app->shipx,app->shipy,app->shipa);
  103. }
  104. /* Here all we do is putting the events into the queue that will be handled
  105. * in the while() loop of the app entry point function. */
  106. static void input_callback(InputEvent* input_event, void* ctx)
  107. {
  108. AsteroidsApp *app = ctx;
  109. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  110. }
  111. /* Allocate the application state and initialize a number of stuff.
  112. * This is called in the entry point to create the application state. */
  113. AsteroidsApp* asteroids_app_alloc() {
  114. AsteroidsApp *app = malloc(sizeof(AsteroidsApp));
  115. app->gui = furi_record_open(RECORD_GUI);
  116. app->view_port = view_port_alloc();
  117. view_port_draw_callback_set(app->view_port, render_callback, app);
  118. view_port_input_callback_set(app->view_port, input_callback, app);
  119. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  120. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  121. app->running = 1;
  122. app->ticks = 0;
  123. app->shipx = SCREEN_XRES / 2;
  124. app->shipy = SCREEN_YRES / 2;
  125. app->shipa = PI; /* Start headed towards top. */
  126. app->shipvx = 0;
  127. app->shipvy = 0;
  128. app->bullets = 0;
  129. app->last_bullet_tick = 0;
  130. app->asteroids = 0;
  131. memset(app->pressed,0,sizeof(app->pressed));
  132. return app;
  133. }
  134. /* Free what the application allocated. It is not clear to me if the
  135. * Flipper OS, once the application exits, will be able to reclaim space
  136. * even if we forget to free something here. */
  137. void asteroids_app_free(AsteroidsApp *app) {
  138. furi_assert(app);
  139. // View related.
  140. view_port_enabled_set(app->view_port, false);
  141. gui_remove_view_port(app->gui, app->view_port);
  142. view_port_free(app->view_port);
  143. furi_record_close(RECORD_GUI);
  144. furi_message_queue_free(app->event_queue);
  145. app->gui = NULL;
  146. free(app);
  147. }
  148. /* Thi is the main game execution function, called 10 times for
  149. * second (with the Flipper screen latency, an higher FPS does not
  150. * make sense). In this function we update the position of objects based
  151. * on velocity. Detect collisions. Update the score and so forth.
  152. *
  153. * Each time this function is called, app->tick is incremented. */
  154. static void game_tick(void *ctx) {
  155. AsteroidsApp *app = ctx;
  156. if (app->pressed[InputKeyLeft]) app->shipa -= .2;
  157. if (app->pressed[InputKeyRight]) app->shipa += .2;
  158. if (app->pressed[InputKeyOk]) {
  159. app->shipvx -= 0.15*(float)sin(app->shipa);
  160. app->shipvy += 0.15*(float)cos(app->shipa);
  161. }
  162. /* Update ship position according to its velocity. */
  163. app->shipx += app->shipvx;
  164. app->shipy += app->shipvy;
  165. /* Return back from one side to the other of the screen. */
  166. if (app->shipx >= SCREEN_XRES) app->shipx = 0;
  167. else if (app->shipx < 0) app->shipx = SCREEN_XRES-1;
  168. if (app->shipy >= SCREEN_YRES) app->shipy = 0;
  169. else if (app->shipy < 0) app->shipy = SCREEN_YRES-1;
  170. app->ticks++;
  171. view_port_update(app->view_port);
  172. }
  173. /* Handle keys interaction. */
  174. void asteroids_update_keypress_state(AsteroidsApp *app, InputEvent input) {
  175. if (input.type == InputTypePress) {
  176. app->pressed[input.key] = furi_get_tick();
  177. } else if (input.type == InputTypeRelease) {
  178. uint32_t dur = furi_get_tick() - app->pressed[input.key];
  179. app->pressed[input.key] = 0;
  180. if (dur < 100 && input.key == InputKeyOk) app->fire = true;
  181. }
  182. }
  183. int32_t asteroids_app_entry(void* p) {
  184. UNUSED(p);
  185. AsteroidsApp *app = asteroids_app_alloc();
  186. /* Create a timer. We do data analysis in the callback. */
  187. FuriTimer *timer = furi_timer_alloc(game_tick, FuriTimerTypePeriodic, app);
  188. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 10);
  189. /* This is the main event loop: here we get the events that are pushed
  190. * in the queue by input_callback(), and process them one after the
  191. * other. The timeout is 100 milliseconds, so if not input is received
  192. * before such time, we exit the queue_get() function and call
  193. * view_port_update() in order to refresh our screen content. */
  194. InputEvent input;
  195. while(app->running) {
  196. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  197. if (qstat == FuriStatusOk) {
  198. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  199. input.type, input.key);
  200. /* Handle navigation here. Then handle view-specific inputs
  201. * in the view specific handling function. */
  202. if (input.type == InputTypeShort &&
  203. input.key == InputKeyBack)
  204. {
  205. app->running = 0;
  206. } else {
  207. asteroids_update_keypress_state(app,input);
  208. }
  209. } else {
  210. /* Useful to understand if the app is still alive when it
  211. * does not respond because of bugs. */
  212. if (DEBUG_MSG) {
  213. static int c = 0; c++;
  214. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  215. }
  216. }
  217. }
  218. furi_timer_free(timer);
  219. asteroids_app_free(app);
  220. return 0;
  221. }