app.c 9.3 KB

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