app.c 9.0 KB

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