app.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611
  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. #define GAME_START_LIVES 3
  17. #ifndef PI
  18. #define PI 3.14159265358979f
  19. #endif
  20. /* ============================ Data structures ============================= */
  21. typedef struct Ship {
  22. float x, /* Ship x position. */
  23. y, /* Ship y position. */
  24. vx, /* x velocity. */
  25. vy, /* y velocity. */
  26. rot; /* Current rotation. 2*PI full ortation. */
  27. } Ship;
  28. typedef struct Bullet {
  29. float x, y, vx, vy; /* Fields like in ship. */
  30. uint32_t ttl; /* Time to live, in ticks. */
  31. } Bullet;
  32. typedef struct Asteroid {
  33. float x, y, vx, vy, rot, /* Fields like ship. */
  34. rot_speed, /* Angular velocity (rot speed and sense). */
  35. size; /* Asteroid size. */
  36. uint8_t shape_seed; /* Seed to give random shape. */
  37. } Asteroid;
  38. #define MAXBUL 10 /* Max bullets on the screen. */
  39. #define MAXAST 32 /* Max asteroids on the screen. */
  40. #define SHIP_HIT_ANIMATION_LEN 15
  41. typedef struct AsteroidsApp {
  42. /* GUI */
  43. Gui *gui;
  44. ViewPort *view_port; /* We just use a raw viewport and we render
  45. everything into the low level canvas. */
  46. FuriMessageQueue *event_queue; /* Keypress events go here. */
  47. /* Game state. */
  48. int running; /* Once false exists the app. */
  49. int gameover; /* Gameover status. */
  50. uint32_t ticks; /* Game ticks. Increments at each refresh. */
  51. uint32_t score; /* Game score. */
  52. uint32_t lives; /* Number of lives in the current game. */
  53. uint32_t ship_hit; /* When non zero, the ship was hit by an asteroid
  54. and we need to show an animation as long as
  55. its value is non-zero (and decrease it's value
  56. at each tick of animation). */
  57. /* Ship state. */
  58. struct Ship ship;
  59. /* Bullets state. */
  60. struct Bullet bullets[MAXBUL]; /* Each bullet state. */
  61. int bullets_num; /* Active bullets. */
  62. uint32_t last_bullet_tick; /* Tick the last bullet was fired. */
  63. /* Asteroids state. */
  64. Asteroid asteroids[MAXAST]; /* Each asteroid state. */
  65. int asteroids_num; /* Active asteroids. */
  66. uint32_t pressed[InputKeyMAX]; /* pressed[id] is true if pressed.
  67. Each array item contains the time
  68. in milliseconds the key was pressed. */
  69. bool fire; /* Short press detected: fire a bullet. */
  70. } AsteroidsApp;
  71. /* ============================ 2D drawing ================================== */
  72. /* This structure represents a polygon of at most POLY_MAX points.
  73. * The function draw_poly() is able to render it on the screen, rotated
  74. * by the amount specified. */
  75. #define POLY_MAX 8
  76. typedef struct Poly {
  77. float x[POLY_MAX];
  78. float y[POLY_MAX];
  79. uint32_t points; /* Number of points actually populated. */
  80. } Poly;
  81. /* Define the polygons we use. */
  82. Poly ShipPoly = {
  83. {-3, 0, 3},
  84. {-3, 6, -3},
  85. 3
  86. };
  87. /* Rotate the point of the poligon 'poly' and store the new rotated
  88. * polygon in 'rot'. The polygon is rotated by an angle 'a', with
  89. * center at 0,0. */
  90. void rotate_poly(Poly *rot, Poly *poly, float a) {
  91. /* We want to compute sin(a) and cos(a) only one time
  92. * for every point to rotate. It's a slow operation. */
  93. float sin_a = (float)sin(a);
  94. float cos_a = (float)cos(a);
  95. for (uint32_t j = 0; j < poly->points; j++) {
  96. rot->x[j] = poly->x[j]*cos_a - poly->y[j]*sin_a;
  97. rot->y[j] = poly->y[j]*cos_a + poly->x[j]*sin_a;
  98. }
  99. rot->points = poly->points;
  100. }
  101. /* This is an 8 bit LFSR we use to generate a predictable and fast
  102. * pseudorandom sequence of numbers, to give a different shape to
  103. * each asteroid. */
  104. void lfsr_next(unsigned char *prev) {
  105. unsigned char lsb = *prev & 1;
  106. *prev = *prev >> 1;
  107. if (lsb == 1) *prev ^= 0b11000111;
  108. *prev ^= *prev<<7; /* Mix things a bit more. */
  109. }
  110. /* Render the polygon 'poly' at x,y, rotated by the specified angle. */
  111. void draw_poly(Canvas *const canvas, Poly *poly, uint8_t x, uint8_t y, float a)
  112. {
  113. Poly rot;
  114. rotate_poly(&rot,poly,a);
  115. canvas_set_color(canvas, ColorBlack);
  116. for (uint32_t j = 0; j < rot.points; j++) {
  117. uint32_t a = j;
  118. uint32_t b = j+1;
  119. if (b == rot.points) b = 0;
  120. canvas_draw_line(canvas,x+rot.x[a],y+rot.y[a],
  121. x+rot.x[b],y+rot.y[b]);
  122. }
  123. }
  124. /* A bullet is just a + pixels pattern. A single pixel is not
  125. * visible enough. */
  126. void draw_bullet(Canvas *const canvas, Bullet *b) {
  127. canvas_draw_dot(canvas,b->x-1,b->y);
  128. canvas_draw_dot(canvas,b->x+1,b->y);
  129. canvas_draw_dot(canvas,b->x,b->y);
  130. canvas_draw_dot(canvas,b->x,b->y-1);
  131. canvas_draw_dot(canvas,b->x,b->y+1);
  132. }
  133. /* Draw an asteroid. The asteroid shapes is computed on the fly and
  134. * is not stored in a permanent shape structure. In order to generate
  135. * the shape, we use an initial fixed shape that we resize according
  136. * to the asteroid size, perturbate according to the asteroid shape
  137. * seed, and finally draw it rotated of the right amount. */
  138. void draw_asteroid(Canvas *const canvas, Asteroid *ast) {
  139. Poly ap;
  140. /* Start with what is kinda of a circle. Note that this could be
  141. * stored into a template and copied here, to avoid computing
  142. * sin() / cos(). But the Flipper can handle it without problems. */
  143. uint8_t r = ast->shape_seed;
  144. for (int j = 0; j < 8; j++) {
  145. float a = (PI*2)/8*j;
  146. /* Before generating the point, to make the shape unique generate
  147. * a random factor between .7 and 1.3 to scale the distance from
  148. * the center. However this asteroid should have its unique shape
  149. * that remains always the same, so we use a predictable PRNG
  150. * implemented by an 8 bit shift register. */
  151. lfsr_next(&r);
  152. float scaling = .7+((float)r/255*.6);
  153. ap.x[j] = (float)sin(a) * ast->size * scaling;
  154. ap.y[j] = (float)cos(a) * ast->size * scaling;
  155. }
  156. ap.points = 8;
  157. draw_poly(canvas,&ap,ast->x,ast->y,ast->rot);
  158. }
  159. /* Draw small ships in the top-right part of the screen, one for
  160. * each left live. */
  161. void draw_left_lives(Canvas *const canvas, AsteroidsApp *app) {
  162. int lives = app->lives;
  163. int x = SCREEN_XRES-5;
  164. Poly mini_ship = {
  165. {-2, 0, 2},
  166. {-2, 4, -2},
  167. 3
  168. };
  169. while(lives--) {
  170. draw_poly(canvas,&mini_ship,x,6,PI);
  171. x -= 6;
  172. }
  173. }
  174. /* Given the current position, update it according to the velocity and
  175. * wrap it back to the other side if the object went over the screen. */
  176. void update_pos_by_velocity(float *x, float *y, float vx, float vy) {
  177. /* Return back from one side to the other of the screen. */
  178. *x += vx;
  179. *y += vy;
  180. if (*x >= SCREEN_XRES) *x = 0;
  181. else if (*x < 0) *x = SCREEN_XRES-1;
  182. if (*y >= SCREEN_YRES) *y = 0;
  183. else if (*y < 0) *y = SCREEN_YRES-1;
  184. }
  185. /* Render the current game screen. */
  186. void render_callback(Canvas *const canvas, void *ctx) {
  187. AsteroidsApp *app = ctx;
  188. /* Clear screen. */
  189. canvas_set_color(canvas, ColorWhite);
  190. canvas_draw_box(canvas, 0, 0, 127, 63);
  191. /* Draw score. */
  192. canvas_set_color(canvas, ColorBlack);
  193. canvas_set_font(canvas, FontSecondary);
  194. char score[32];
  195. snprintf(score,sizeof(score),"%lu",app->score);
  196. canvas_draw_str(canvas, 0, 8, score);
  197. /* Draw left ships. */
  198. draw_left_lives(canvas,app);
  199. /* Draw ship, asteroids, bullets. */
  200. draw_poly(canvas,&ShipPoly,app->ship.x,app->ship.y,app->ship.rot);
  201. for (int j = 0; j < app->bullets_num; j++)
  202. draw_bullet(canvas,&app->bullets[j]);
  203. for (int j = 0; j < app->asteroids_num; j++)
  204. draw_asteroid(canvas,&app->asteroids[j]);
  205. }
  206. /* ============================ Game logic ================================== */
  207. float distance(float x1, float y1, float x2, float y2) {
  208. float dx = x1-x2;
  209. float dy = y1-y2;
  210. return sqrt(dx*dx+dy*dy);
  211. }
  212. /* Detect a collision between the object at x1,y1 of radius r1 and
  213. * the object at x2, y2 of radius r2. A factor < 1 will make the
  214. * function detect the collision even if the objects are yet not
  215. * relly touching, while a factor > 1 will make it detect the collision
  216. * only after they are a bit overlapping. It basically is used to
  217. * rescale the distance.
  218. *
  219. * Note that in this simplified 2D world, objects are all considered
  220. * spheres (this is why this function only takes the radius). This
  221. * is, after all, kinda accurate for asteroids, for bullets, and
  222. * even for the ship "core" itself. */
  223. bool detect_collision(float x1, float y1, float r1,
  224. float x2, float y2, float r2,
  225. float factor)
  226. {
  227. /* The objects are colliding if the distance between object 1 and 2
  228. * is smaller than the sum of the two radiuses r1 and r2.
  229. * So it would be like: sqrt((x1-x2)^2+(y1-y2)^2) < r1+r2.
  230. * However we can avoid computing the sqrt (which is slow) by
  231. * squaring the second term and removing the square root, making
  232. * the comparison like this:
  233. *
  234. * (x1-x2)^2+(y1-y2)^2 < (r1+r2)^2. */
  235. float dx = (x1-x2)*factor;
  236. float dy = (y1-y2)*factor;
  237. float rsum = r1+r2;
  238. return dx*dx+dy*dy < rsum*rsum;
  239. }
  240. /* Create a new bullet headed in the same direction of the ship. */
  241. void ship_fire_bullet(AsteroidsApp *app) {
  242. if (app->bullets_num == MAXBUL) return;
  243. Bullet *b = &app->bullets[app->bullets_num];
  244. b->x = app->ship.x;
  245. b->y = app->ship.y;
  246. b->vx = -sin(app->ship.rot);
  247. b->vy = cos(app->ship.rot);
  248. /* Ship should fire from its head, not in the middle. */
  249. b->x += b->vx*5;
  250. b->y += b->vy*5;
  251. /* Give the bullet some velocity (for now the vector is just
  252. * normalized to 1). */
  253. b->vx *= 2;
  254. b->vy *= 2;
  255. /* It's more realistic if we add the velocity vector of the
  256. * ship, too. Otherwise if the ship is going fast the bullets
  257. * will be slower, which is not how the world works. */
  258. b->vx += app->ship.vx;
  259. b->vy += app->ship.vy;
  260. b->ttl = 50; /* The bullet will disappear after N ticks. */
  261. app->bullets_num++;
  262. }
  263. /* Remove the specified bullet by id (index in the array). */
  264. void remove_bullet(AsteroidsApp *app, int bid) {
  265. /* Replace the top bullet with the empty space left
  266. * by the removal of this bullet. This way we always take the
  267. * array dense, which is an advantage when looping. */
  268. int n = --app->bullets_num;
  269. if (n && bid != n) app->bullets[bid] = app->bullets[n];
  270. }
  271. /* Create a new asteroid, away from the ship. Return the
  272. * pointer to the asteroid object, so that the caller can change
  273. * certain things of the asteroid if needed. */
  274. Asteroid *add_asteroid(AsteroidsApp *app) {
  275. if (app->asteroids_num == MAXAST) return NULL;
  276. float size = 4+rand()%15;
  277. float min_distance = 20;
  278. float x,y;
  279. do {
  280. x = rand() % SCREEN_XRES;
  281. y = rand() % SCREEN_YRES;
  282. } while(distance(app->ship.x,app->ship.y,x,y) < min_distance+size);
  283. Asteroid *a = &app->asteroids[app->asteroids_num++];
  284. a->x = x;
  285. a->y = y;
  286. a->vx = 2*(-.5 + ((float)rand()/RAND_MAX));
  287. a->vy = 2*(-.5 + ((float)rand()/RAND_MAX));
  288. a->size = size;
  289. a->rot = 0;
  290. a->rot_speed = ((float)rand()/RAND_MAX)/10;
  291. if (app->ticks & 1) a->rot_speed = -(a->rot_speed);
  292. a->shape_seed = rand() & 255;
  293. return a;
  294. }
  295. /* Remove the specified asteroid by id (index in the array). */
  296. void remove_asteroid(AsteroidsApp *app, int id) {
  297. /* Replace the top asteroid with the empty space left
  298. * by the removal of this one. This way we always take the
  299. * array dense, which is an advantage when looping. */
  300. int n = --app->asteroids_num;
  301. if (n && id != n) app->asteroids[id] = app->asteroids[n];
  302. }
  303. /* Called when an asteroid was reached by a bullet. The asteroid
  304. * hit is the one with the specified 'id'. */
  305. void asteroid_was_hit(AsteroidsApp *app, int id) {
  306. float sizelimit = 6; // Smaller than that polverize in one shot.
  307. Asteroid *a = &app->asteroids[id];
  308. /* Asteroid is large enough to break into fragments. */
  309. float size = a->size;
  310. float x = a->x, y = a->y;
  311. remove_asteroid(app,id);
  312. if (size > sizelimit) {
  313. int max_fragments = size / sizelimit;
  314. int fragments = 2+rand()%max_fragments;
  315. float newsize = size/fragments;
  316. if (newsize < 2) newsize = 2;
  317. for (int j = 0; j < fragments; j++) {
  318. a = add_asteroid(app);
  319. if (a == NULL) break; // Too many asteroids on screen.
  320. a->x = x + -(size/2) + rand() % (int)newsize;
  321. a->y = y + -(size/2) + rand() % (int)newsize;
  322. a->size = newsize;
  323. }
  324. } else {
  325. app->score++;
  326. }
  327. }
  328. /* Function called when a collision between the asteroid and the
  329. * ship is detected. */
  330. void ship_was_hit(AsteroidsApp *app) {
  331. app->ship_hit = SHIP_HIT_ANIMATION_LEN;
  332. if (app->lives)
  333. app->lives--;
  334. else
  335. app->gameover = true;
  336. }
  337. /* Restart game after the ship is hit. Will reset the ship position, bullets
  338. * and asteroids to restart the game. */
  339. void restart_game(AsteroidsApp *app) {
  340. app->ship.x = SCREEN_XRES / 2;
  341. app->ship.y = SCREEN_YRES / 2;
  342. app->ship.rot = PI; /* Start headed towards top. */
  343. app->ship.vx = 0;
  344. app->ship.vy = 0;
  345. app->bullets_num = 0;
  346. app->last_bullet_tick = 0;
  347. app->asteroids_num = 0;
  348. }
  349. /* Called after gameover to restart the game. This function
  350. * also calls restart_game(). */
  351. void restart_game_after_gameover(AsteroidsApp *app) {
  352. app->gameover = 0;
  353. app->ticks = 0;
  354. app->score = 0;
  355. app->lives = GAME_START_LIVES;
  356. restart_game(app);
  357. }
  358. /* This is the main game execution function, called 10 times for
  359. * second (with the Flipper screen latency, an higher FPS does not
  360. * make sense). In this function we update the position of objects based
  361. * on velocity. Detect collisions. Update the score and so forth.
  362. *
  363. * Each time this function is called, app->tick is incremented. */
  364. void game_tick(void *ctx) {
  365. AsteroidsApp *app = ctx;
  366. /* This is a special condition: ship was hit, we frozen the game
  367. * as long as ship_hit isn't zero again, and show an animation of
  368. * a rotating ship. */
  369. if (app->ship_hit) {
  370. app->ship.rot += 0.5;
  371. app->ship_hit--;
  372. view_port_update(app->view_port);
  373. if (app->ship_hit == 0) {
  374. restart_game(app);
  375. }
  376. return;
  377. }
  378. /* Handle keypresses. */
  379. if (app->pressed[InputKeyLeft]) app->ship.rot -= .35;
  380. if (app->pressed[InputKeyRight]) app->ship.rot += .35;
  381. if (app->pressed[InputKeyOk]) {
  382. app->ship.vx -= 0.5*(float)sin(app->ship.rot);
  383. app->ship.vy += 0.5*(float)cos(app->ship.rot);
  384. }
  385. /* Fire a bullet if needed. app->fire is set in
  386. * asteroids_update_keypress_state() since depends on exact
  387. * pressure timing. */
  388. if (app->fire) {
  389. ship_fire_bullet(app);
  390. app->fire = false;
  391. }
  392. /* Update ship position according to its velocity. */
  393. update_pos_by_velocity(&app->ship.x,&app->ship.y,app->ship.vx,app->ship.vy);
  394. /* Update bullets position. */
  395. for (int j = 0; j < app->bullets_num; j++) {
  396. update_pos_by_velocity(&app->bullets[j].x,&app->bullets[j].y,
  397. app->bullets[j].vx,app->bullets[j].vy);
  398. if (--app->bullets[j].ttl == 0) {
  399. remove_bullet(app,j);
  400. j--; /* Process this bullet index again: the removal will
  401. fill it with the top bullet to take the array dense. */
  402. }
  403. }
  404. /* Update asteroids position. */
  405. for (int j = 0; j < app->asteroids_num; j++) {
  406. update_pos_by_velocity(&app->asteroids[j].x,&app->asteroids[j].y,
  407. app->asteroids[j].vx,app->asteroids[j].vy);
  408. app->asteroids[j].rot += app->asteroids[j].rot_speed;
  409. if (app->asteroids[j].rot < 0) app->asteroids[j].rot = 2*PI;
  410. else if (app->asteroids[j].rot > 2*PI) app->asteroids[j].rot = 0;
  411. }
  412. /* Detect collision between bullet and asteroid. */
  413. for (int j = 0; j < app->bullets_num; j++) {
  414. Bullet *b = &app->bullets[j];
  415. for (int i = 0; i < app->asteroids_num; i++) {
  416. Asteroid *a = &app->asteroids[i];
  417. if (detect_collision(a->x, a->y, a->size,
  418. b->x, b->y, 1, 1))
  419. {
  420. asteroid_was_hit(app,i);
  421. remove_bullet(app,j);
  422. /* The bullet no longer exist. Break the loop.
  423. * However we want to start processing from the
  424. * same bullet index, since now it is used by
  425. * another bullet (see remove_bullet()). */
  426. j--; /* Scan this j value again. */
  427. break;
  428. }
  429. }
  430. }
  431. /* Detect collision between ship and asteroid. */
  432. for (int j = 0; j < app->asteroids_num; j++) {
  433. Asteroid *a = &app->asteroids[j];
  434. if (detect_collision(a->x, a->y, a->size,
  435. app->ship.x, app->ship.y, 4, 1))
  436. {
  437. ship_was_hit(app);
  438. break;
  439. }
  440. }
  441. /* From time to time, create a new asteroid. The more asteroids
  442. * already on the screen, the smaller probability of creating
  443. * a new one. */
  444. if (app->asteroids_num == 0 ||
  445. (random() % 5000) < (30/(1+app->asteroids_num)))
  446. {
  447. add_asteroid(app);
  448. }
  449. app->ticks++;
  450. view_port_update(app->view_port);
  451. }
  452. /* ======================== Flipper specific code =========================== */
  453. /* Here all we do is putting the events into the queue that will be handled
  454. * in the while() loop of the app entry point function. */
  455. void input_callback(InputEvent* input_event, void* ctx)
  456. {
  457. AsteroidsApp *app = ctx;
  458. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  459. }
  460. /* Allocate the application state and initialize a number of stuff.
  461. * This is called in the entry point to create the application state. */
  462. AsteroidsApp* asteroids_app_alloc() {
  463. AsteroidsApp *app = malloc(sizeof(AsteroidsApp));
  464. app->gui = furi_record_open(RECORD_GUI);
  465. app->view_port = view_port_alloc();
  466. view_port_draw_callback_set(app->view_port, render_callback, app);
  467. view_port_input_callback_set(app->view_port, input_callback, app);
  468. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  469. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  470. app->running = 1; /* Turns 0 when back is pressed. */
  471. restart_game_after_gameover(app);
  472. memset(app->pressed,0,sizeof(app->pressed));
  473. return app;
  474. }
  475. /* Free what the application allocated. It is not clear to me if the
  476. * Flipper OS, once the application exits, will be able to reclaim space
  477. * even if we forget to free something here. */
  478. void asteroids_app_free(AsteroidsApp *app) {
  479. furi_assert(app);
  480. // View related.
  481. view_port_enabled_set(app->view_port, false);
  482. gui_remove_view_port(app->gui, app->view_port);
  483. view_port_free(app->view_port);
  484. furi_record_close(RECORD_GUI);
  485. furi_message_queue_free(app->event_queue);
  486. app->gui = NULL;
  487. free(app);
  488. }
  489. /* Handle keys interaction. */
  490. void asteroids_update_keypress_state(AsteroidsApp *app, InputEvent input) {
  491. if (input.type == InputTypePress) {
  492. app->pressed[input.key] = furi_get_tick();
  493. } else if (input.type == InputTypeRelease) {
  494. uint32_t dur = furi_get_tick() - app->pressed[input.key];
  495. app->pressed[input.key] = 0;
  496. if (dur < 200 && input.key == InputKeyOk) app->fire = true;
  497. }
  498. }
  499. int32_t asteroids_app_entry(void* p) {
  500. UNUSED(p);
  501. AsteroidsApp *app = asteroids_app_alloc();
  502. /* Create a timer. We do data analysis in the callback. */
  503. FuriTimer *timer = furi_timer_alloc(game_tick, FuriTimerTypePeriodic, app);
  504. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 10);
  505. /* This is the main event loop: here we get the events that are pushed
  506. * in the queue by input_callback(), and process them one after the
  507. * other. The timeout is 100 milliseconds, so if not input is received
  508. * before such time, we exit the queue_get() function and call
  509. * view_port_update() in order to refresh our screen content. */
  510. InputEvent input;
  511. while(app->running) {
  512. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  513. if (qstat == FuriStatusOk) {
  514. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  515. input.type, input.key);
  516. /* Handle navigation here. Then handle view-specific inputs
  517. * in the view specific handling function. */
  518. if (input.type == InputTypeShort &&
  519. input.key == InputKeyBack)
  520. {
  521. app->running = 0;
  522. } else {
  523. asteroids_update_keypress_state(app,input);
  524. }
  525. } else {
  526. /* Useful to understand if the app is still alive when it
  527. * does not respond because of bugs. */
  528. if (DEBUG_MSG) {
  529. static int c = 0; c++;
  530. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  531. }
  532. }
  533. }
  534. furi_timer_free(timer);
  535. asteroids_app_free(app);
  536. return 0;
  537. }