app.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 MAXBUL 10
  17. typedef struct AsteroidsApp {
  18. /* GUI */
  19. Gui *gui;
  20. ViewPort *view_port; /* We just use a raw viewport and we render
  21. everything into the low level canvas. */
  22. FuriMessageQueue *event_queue; /* Keypress events go here. */
  23. /* Game state. */
  24. int running; /* Once false exists the app. */
  25. uint32_t ticks; /* Game ticks. Increments at each refresh. */
  26. float shipx; /* Ship x position. */
  27. float shipy; /* Ship y position. */
  28. float shipa; /* Ship current angle, 2*PI is a full rotation. */
  29. float shipvx; /* x velocity. */
  30. float shipvy; /* y velocity. */
  31. float bulletsx[MAXBUL]; /* Bullets x position. */
  32. float bulletsy[MAXBUL]; /* Bullets y position. */
  33. int bullets; /* Active bullets. */
  34. uint32_t last_bullet_tick; /* Tick the last bullet was fired. */
  35. float asteroidsx[MAXBUL]; /* Asteroids x position. */
  36. float asteroidsy[MAXBUL]; /* Asteroids y position. */
  37. int asteroids; /* Active asteroids. */
  38. } AsteroidsApp;
  39. /* Rotate the point X,Y by an angle 'a', with center 0,0. */
  40. void rot2D(float x, float y, float *rx, float *ry, float a) {
  41. *rx = x*(float)cos(a)-y*(float)sin(a),
  42. *ry = y*(float)cos(a)+x*(float)sin(a);
  43. }
  44. /* Render the ship at the current position, and rotated by the current
  45. * angle. */
  46. void render_ship(Canvas *const canvas, float x, float y, float a) {
  47. struct { float x; float y; } shape[3] = {
  48. {-3,3}, {0,-6}, {3,3}
  49. };
  50. for (int j =0; j < 3; j++) {
  51. float nx, ny;
  52. rot2D(shape[j].x, shape[j].y, &nx, &ny, a);
  53. shape[j].x = nx;
  54. shape[j].y = ny;
  55. }
  56. canvas_set_color(canvas, ColorBlack);
  57. for (int j =0; j < 4; j++) {
  58. int a = j%3;
  59. int b = (j+1)%3;
  60. canvas_draw_line(canvas,x+shape[a].x,y+shape[a].y,
  61. x+shape[b].x,y+shape[b].y);
  62. }
  63. }
  64. /* Render the current game screen. */
  65. static void render_callback(Canvas *const canvas, void *ctx) {
  66. AsteroidsApp *app = ctx;
  67. /* Clear screen. */
  68. canvas_set_color(canvas, ColorWhite);
  69. canvas_draw_box(canvas, 0, 0, 127, 63);
  70. render_ship(canvas,app->shipx,app->shipy,app->shipa);
  71. }
  72. /* Here all we do is putting the events into the queue that will be handled
  73. * in the while() loop of the app entry point function. */
  74. static void input_callback(InputEvent* input_event, void* ctx)
  75. {
  76. AsteroidsApp *app = ctx;
  77. furi_message_queue_put(app->event_queue,input_event,FuriWaitForever);
  78. }
  79. /* Allocate the application state and initialize a number of stuff.
  80. * This is called in the entry point to create the application state. */
  81. AsteroidsApp* asteroids_app_alloc() {
  82. AsteroidsApp *app = malloc(sizeof(AsteroidsApp));
  83. app->gui = furi_record_open(RECORD_GUI);
  84. app->view_port = view_port_alloc();
  85. view_port_draw_callback_set(app->view_port, render_callback, app);
  86. view_port_input_callback_set(app->view_port, input_callback, app);
  87. gui_add_view_port(app->gui, app->view_port, GuiLayerFullscreen);
  88. app->event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  89. app->running = 1;
  90. app->ticks = 0;
  91. app->shipx = SCREEN_XRES / 2;
  92. app->shipy = SCREEN_YRES / 2;
  93. app->shipa = 0;
  94. app->shipvx = 0;
  95. app->shipvy = 0;
  96. app->bullets = 0;
  97. app->last_bullet_tick = 0;
  98. app->asteroids = 0;
  99. return app;
  100. }
  101. /* Free what the application allocated. It is not clear to me if the
  102. * Flipper OS, once the application exits, will be able to reclaim space
  103. * even if we forget to free something here. */
  104. void asteroids_app_free(AsteroidsApp *app) {
  105. furi_assert(app);
  106. // View related.
  107. view_port_enabled_set(app->view_port, false);
  108. gui_remove_view_port(app->gui, app->view_port);
  109. view_port_free(app->view_port);
  110. furi_record_close(RECORD_GUI);
  111. furi_message_queue_free(app->event_queue);
  112. app->gui = NULL;
  113. free(app);
  114. }
  115. /* Called periodically. Do signal processing here. Data we process here
  116. * will be later displayed by the render callback. The side effect of this
  117. * function is to scan for signals and set DetectedSamples. */
  118. static void timer_callback(void *ctx) {
  119. AsteroidsApp *app = ctx;
  120. UNUSED(app);
  121. }
  122. /* Handle keys interaction. */
  123. void asteroids_process_keypress(AsteroidsApp *app, InputEvent input) {
  124. UNUSED(app);
  125. UNUSED(input);
  126. }
  127. int32_t asteroids_app_entry(void* p) {
  128. UNUSED(p);
  129. AsteroidsApp *app = asteroids_app_alloc();
  130. /* Create a timer. We do data analysis in the callback. */
  131. FuriTimer *timer = furi_timer_alloc(timer_callback, FuriTimerTypePeriodic, app);
  132. furi_timer_start(timer, furi_kernel_get_tick_frequency() / 4);
  133. /* This is the main event loop: here we get the events that are pushed
  134. * in the queue by input_callback(), and process them one after the
  135. * other. The timeout is 100 milliseconds, so if not input is received
  136. * before such time, we exit the queue_get() function and call
  137. * view_port_update() in order to refresh our screen content. */
  138. InputEvent input;
  139. while(app->running) {
  140. FuriStatus qstat = furi_message_queue_get(app->event_queue, &input, 100);
  141. if (qstat == FuriStatusOk) {
  142. if (DEBUG_MSG) FURI_LOG_E(TAG, "Main Loop - Input: type %d key %u",
  143. input.type, input.key);
  144. /* Handle navigation here. Then handle view-specific inputs
  145. * in the view specific handling function. */
  146. if (input.type == InputTypeShort &&
  147. input.key == InputKeyBack)
  148. {
  149. app->running = 0;
  150. } else {
  151. asteroids_process_keypress(app,input);
  152. }
  153. } else {
  154. /* Useful to understand if the app is still alive when it
  155. * does not respond because of bugs. */
  156. if (DEBUG_MSG) {
  157. static int c = 0; c++;
  158. if (!(c % 20)) FURI_LOG_E(TAG, "Loop timeout");
  159. }
  160. }
  161. view_port_update(app->view_port);
  162. }
  163. furi_timer_free(timer);
  164. asteroids_app_free(app);
  165. return 0;
  166. }