pinball0.cxx 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  1. #include <furi.h>
  2. #include <notification/notification.h>
  3. #include <cstring>
  4. #include "pinball0.h"
  5. #include "table.h"
  6. #include "notifications.h"
  7. #include "settings.h"
  8. /* generated by fbt from .png files in images folder */
  9. #include <pinball0_icons.h>
  10. // Gravity should be lower than 9.8 m/s^2 since the ball is on
  11. // an angled table. We could calc this and derive the actual
  12. // vertical vector based on the angle of the table yadda yadda yadda
  13. #define GRAVITY 3.0f // 9.8f
  14. #define PHYSICS_SUB_STEPS 5
  15. #define GAME_FPS 30
  16. #define MANUAL_ADJUSTMENT 20
  17. #define IDLE_TIMEOUT 120 * 1000 // 120 seconds * 1000 ticks/sec
  18. #define BUMP_DELAY 2 * 1000 // 2 seconds
  19. #define BUMP_MAX 3
  20. void solve(PinballApp* pb, float dt) {
  21. Table* table = pb->table;
  22. float sub_dt = dt / PHYSICS_SUB_STEPS;
  23. for(int ss = 0; ss < PHYSICS_SUB_STEPS; ss++) {
  24. // apply gravity (and any other forces?)
  25. // FURI_LOG_I(TAG, "Applying gravity");
  26. if(table->balls_released) {
  27. float bump_amt = 1.0f;
  28. if(pb->keys[InputKeyUp]) {
  29. bump_amt = -1.04f;
  30. }
  31. for(auto& b : table->balls) {
  32. // We multiply GRAVITY by dt since gravity is based on seconds
  33. b.accelerate(Vec2(0, GRAVITY * bump_amt * sub_dt));
  34. }
  35. }
  36. // apply collisions (among moving objects)
  37. // only needed for multi-ball! - is this true? what about flippers...
  38. for(size_t b1 = 0; b1 < table->balls.size(); b1++) {
  39. for(size_t b2 = b1 + 1; b2 < table->balls.size(); b2++) {
  40. if(b1 != b2) {
  41. auto& ball1 = table->balls[b1];
  42. auto& ball2 = table->balls[b2];
  43. Vec2 axis = ball1.p - ball2.p;
  44. float dist2 = axis.mag2();
  45. float dist = sqrtf(dist2);
  46. float rr = ball1.r + ball2.r;
  47. if(dist < rr) {
  48. Vec2 v1 = ball1.p - ball1.prev_p;
  49. Vec2 v2 = ball2.p - ball2.prev_p;
  50. float factor = (dist - rr) / dist;
  51. ball1.p -= axis * factor * 0.5f;
  52. ball2.p -= axis * factor * 0.5f;
  53. float damping = 1.01f;
  54. float f1 = (damping * (axis.x * v1.x + axis.y * v1.y)) / dist2;
  55. float f2 = (damping * (axis.x * v2.x + axis.y * v2.y)) / dist2;
  56. v1.x += f2 * axis.x - f1 * axis.x;
  57. v2.x += f1 * axis.x - f2 * axis.x;
  58. v1.y += f2 * axis.y - f1 * axis.y;
  59. v2.y += f1 * axis.y - f2 * axis.y;
  60. ball1.prev_p = ball1.p - v1;
  61. ball2.prev_p = ball2.p - v2;
  62. }
  63. }
  64. }
  65. }
  66. // collisions with static objects and flippers
  67. for(auto& b : table->balls) {
  68. for(auto& o : table->objects) {
  69. if(o->physical && o->collide(b)) {
  70. if(pb->game_mode == GM_Tilted) {
  71. continue;
  72. }
  73. if(o->notification) {
  74. (*o->notification)(pb);
  75. }
  76. table->score.value += o->score;
  77. o->reset_animation();
  78. continue;
  79. }
  80. }
  81. for(auto& f : table->flippers) {
  82. if(f.collide(b)) {
  83. if(pb->game_mode == GM_Tilted) {
  84. continue;
  85. }
  86. if(f.notification) {
  87. (*f.notification)(pb);
  88. }
  89. table->score.value += f.score;
  90. continue;
  91. }
  92. }
  93. }
  94. // update positions - of balls AND flippers
  95. if(table->balls_released) {
  96. for(auto& b : table->balls) {
  97. b.update(sub_dt);
  98. }
  99. }
  100. for(auto& f : table->flippers) {
  101. f.update(sub_dt);
  102. }
  103. }
  104. // Did any balls fall off the table?
  105. if(table->balls.size()) {
  106. auto num_in_play = table->balls.size();
  107. auto i = table->balls.begin();
  108. while(i != table->balls.end()) {
  109. if(i->p.y > 1280 + 100) {
  110. FURI_LOG_I(TAG, "ball off table!");
  111. i = table->balls.erase(i);
  112. num_in_play--;
  113. notify_lost_life(pb);
  114. } else {
  115. ++i;
  116. }
  117. }
  118. if(num_in_play == 0) {
  119. table->balls_released = false;
  120. table->lives.value--;
  121. if(table->lives.value > 0) {
  122. // Reset our ball to it's starting position
  123. table->balls = table->balls_initial;
  124. if(pb->game_mode == GM_Tilted) {
  125. pb->game_mode = GM_Playing;
  126. }
  127. } else {
  128. table->game_over = true;
  129. }
  130. }
  131. }
  132. }
  133. static void pinball_draw_callback(Canvas* const canvas, void* ctx) {
  134. furi_assert(ctx);
  135. PinballApp* pb = (PinballApp*)ctx;
  136. furi_mutex_acquire(pb->mutex, FuriWaitForever);
  137. // What are we drawing? table select / menu or the actual game?
  138. switch(pb->game_mode) {
  139. case GM_TableSelect: {
  140. canvas_draw_icon(canvas, 0, 0, &I_pinball0_logo); // our sweet logo
  141. // draw the list of table names: display it as a carousel - where the list repeats
  142. // and the currently selected item is always in the middle, surrounded by pinballs
  143. const TableList& list = pb->table_list;
  144. int32_t y = 25;
  145. auto half_way = list.display_size / 2;
  146. for(auto i = 0; i < list.display_size; i++) {
  147. int index =
  148. (list.selected - half_way + i + list.menu_items.size()) % list.menu_items.size();
  149. const auto& menu_item = list.menu_items[index];
  150. canvas_draw_str_aligned(
  151. canvas,
  152. LCD_WIDTH / 2,
  153. y,
  154. AlignCenter,
  155. AlignTop,
  156. furi_string_get_cstr(menu_item.name));
  157. if(i == half_way) {
  158. canvas_draw_disc(canvas, 8, y + 3, 2);
  159. canvas_draw_disc(canvas, 56, y + 3, 2);
  160. }
  161. y += 12;
  162. }
  163. pb->table->draw(canvas);
  164. } break;
  165. case GM_Playing:
  166. pb->table->draw(canvas);
  167. break;
  168. case GM_GameOver: {
  169. pb->table->draw(canvas);
  170. const int32_t y = 56;
  171. const size_t interval = 40;
  172. const float theta = (float)((pb->tick % interval) / (interval * 1.0f)) * (float)(M_PI * 2);
  173. const float sin_theta_4 = sinf(theta) * 4;
  174. const int border = 3;
  175. canvas_set_color(canvas, ColorWhite);
  176. canvas_draw_box(
  177. canvas, 16 - border, y + sin_theta_4 - border, 32 + border * 2, 16 + border * 2);
  178. canvas_set_color(canvas, ColorBlack);
  179. canvas_draw_icon(canvas, 16, y + sin_theta_4, &I_Arcade_G);
  180. canvas_draw_icon(canvas, 24, y + sin_theta_4, &I_Arcade_A);
  181. canvas_draw_icon(canvas, 32, y + sin_theta_4, &I_Arcade_M);
  182. canvas_draw_icon(canvas, 40, y + sin_theta_4, &I_Arcade_E);
  183. canvas_draw_icon(canvas, 16, y + sin_theta_4 + 8, &I_Arcade_O);
  184. canvas_draw_icon(canvas, 24, y + sin_theta_4 + 8, &I_Arcade_V);
  185. canvas_draw_icon(canvas, 32, y + sin_theta_4 + 8, &I_Arcade_E);
  186. canvas_draw_icon(canvas, 40, y + sin_theta_4 + 8, &I_Arcade_R);
  187. } break;
  188. case GM_Error: {
  189. // pb->text contains error message
  190. canvas_draw_icon(canvas, 0, 10, &I_Arcade_E);
  191. canvas_draw_icon(canvas, 8, 10, &I_Arcade_R);
  192. canvas_draw_icon(canvas, 16, 10, &I_Arcade_R);
  193. canvas_draw_icon(canvas, 24, 10, &I_Arcade_O);
  194. canvas_draw_icon(canvas, 32, 10, &I_Arcade_R);
  195. int x = 10;
  196. int y = 30;
  197. // split the string on \n and display each line
  198. // strtok is disabled - whyyy
  199. char buf[256];
  200. strncpy(buf, pb->text, 256);
  201. char* str = buf;
  202. char* p = buf;
  203. bool at_end = false;
  204. while(str != NULL) {
  205. while(p && *p != '\n' && *p != '\0')
  206. p++;
  207. if(p && *p == '\0') at_end = true;
  208. *p = '\0';
  209. canvas_draw_str_aligned(canvas, x, y, AlignLeft, AlignTop, str);
  210. if(at_end) {
  211. str = NULL;
  212. break;
  213. }
  214. str = p + 1;
  215. p = str;
  216. y += 12;
  217. }
  218. pb->table->draw(canvas);
  219. } break;
  220. case GM_Settings: {
  221. // TODO: like... do better here. maybe vector of settings strings, etc
  222. canvas_draw_str_aligned(canvas, 2, 10, AlignLeft, AlignTop, "SETTINGS");
  223. int x = 55;
  224. int y = 30;
  225. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignTop, "Sound");
  226. canvas_draw_circle(canvas, x, y + 3, 4);
  227. if(pb->settings.sound_enabled) {
  228. canvas_draw_disc(canvas, x, y + 3, 2);
  229. }
  230. if(pb->settings.selected_setting == 0) {
  231. canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
  232. }
  233. y += 12;
  234. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignTop, "LED");
  235. canvas_draw_circle(canvas, x, y + 3, 4);
  236. if(pb->settings.led_enabled) {
  237. canvas_draw_disc(canvas, x, y + 3, 2);
  238. }
  239. if(pb->settings.selected_setting == 1) {
  240. canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
  241. }
  242. y += 12;
  243. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignTop, "Vibrate");
  244. canvas_draw_circle(canvas, x, y + 3, 4);
  245. if(pb->settings.vibrate_enabled) {
  246. canvas_draw_disc(canvas, x, y + 3, 2);
  247. }
  248. if(pb->settings.selected_setting == 2) {
  249. canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
  250. }
  251. y += 12;
  252. canvas_draw_str_aligned(canvas, 10, y, AlignLeft, AlignTop, "Debug");
  253. canvas_draw_circle(canvas, x, y + 3, 4);
  254. if(pb->settings.debug_mode) {
  255. canvas_draw_disc(canvas, x, y + 3, 2);
  256. }
  257. if(pb->settings.selected_setting == 3) {
  258. canvas_draw_triangle(canvas, 2, y + 3, 8, 5, CanvasDirectionLeftToRight);
  259. }
  260. // About information
  261. canvas_draw_str_aligned(canvas, 2, 88, AlignLeft, AlignTop, "Pinball0 " VERSION);
  262. canvas_draw_str_aligned(canvas, 2, 98, AlignLeft, AlignTop, "github.com/");
  263. canvas_draw_str_aligned(canvas, 2, 108, AlignLeft, AlignTop, " rdefeo/");
  264. canvas_draw_str_aligned(canvas, 2, 118, AlignLeft, AlignTop, " pinball0");
  265. pb->table->draw(canvas);
  266. } break;
  267. case GM_Tilted: {
  268. pb->table->draw(canvas);
  269. const int32_t y = 56;
  270. const int border = 8;
  271. canvas_set_color(canvas, ColorWhite);
  272. canvas_draw_box(canvas, 16 - border, y - border, 32 + border * 2, 8 + border * 2);
  273. canvas_set_color(canvas, ColorBlack);
  274. bool display = furi_get_tick() % 1000 < 500;
  275. if(display) {
  276. canvas_draw_icon(canvas, 17, y, &I_Arcade_T);
  277. canvas_draw_icon(canvas, 25, y, &I_Arcade_I);
  278. canvas_draw_icon(canvas, 33, y, &I_Arcade_L);
  279. canvas_draw_icon(canvas, 40, y, &I_Arcade_T);
  280. }
  281. int dots = 5;
  282. int x_start = 16;
  283. int x_gap = (48 - 16) / (dots - 1);
  284. for(int x = 0; x < 5; x++, x_start += x_gap) {
  285. if(x % 2 != display) {
  286. canvas_draw_disc(canvas, x_start, 50, 2);
  287. canvas_draw_disc(canvas, x_start, 70, 2);
  288. } else {
  289. canvas_draw_dot(canvas, x_start, 50);
  290. canvas_draw_dot(canvas, x_start, 70);
  291. }
  292. }
  293. } break;
  294. default:
  295. FURI_LOG_E(TAG, "Unknown Game Mode");
  296. break;
  297. }
  298. furi_mutex_release(pb->mutex);
  299. }
  300. static void pinball_input_callback(InputEvent* input_event, void* ctx) {
  301. furi_assert(ctx);
  302. FuriMessageQueue* event_queue = (FuriMessageQueue*)ctx;
  303. // PinballEvent event = {.type = EventTypeKey, .input = *input_event};
  304. furi_message_queue_put(event_queue, input_event, FuriWaitForever);
  305. }
  306. PinballApp::PinballApp() {
  307. initialized = false;
  308. mutex = furi_mutex_alloc(FuriMutexTypeNormal);
  309. if(!mutex) {
  310. FURI_LOG_E(TAG, "Cannot create mutex!");
  311. return;
  312. }
  313. storage = (Storage*)furi_record_open(RECORD_STORAGE);
  314. notify = (NotificationApp*)furi_record_open(RECORD_NOTIFICATION);
  315. // notify_init();
  316. notification_message(notify, &sequence_display_backlight_enforce_on);
  317. table = NULL;
  318. tick = 0;
  319. game_mode = GM_TableSelect;
  320. keys[InputKeyUp] = false;
  321. keys[InputKeyDown] = false;
  322. keys[InputKeyRight] = false;
  323. keys[InputKeyLeft] = false;
  324. initialized = true;
  325. }
  326. PinballApp::~PinballApp() {
  327. furi_mutex_free(mutex);
  328. delete table;
  329. // notify_free();
  330. notification_message(notify, &sequence_display_backlight_enforce_auto);
  331. notification_message(notify, &sequence_reset_rgb);
  332. furi_record_close(RECORD_STORAGE);
  333. furi_record_close(RECORD_NOTIFICATION);
  334. }
  335. extern "C" int32_t pinball0_app(void* p) {
  336. UNUSED(p);
  337. PinballApp app;
  338. if(!app.initialized) {
  339. FURI_LOG_E(TAG, "Failed to initialize Pinball0! Exiting.");
  340. return 0;
  341. }
  342. pinball_load_settings(app);
  343. // read the list of tables from storage
  344. table_table_list_init(&app);
  345. table_load_table(&app, TABLE_SELECT);
  346. FuriMessageQueue* event_queue = furi_message_queue_alloc(8, sizeof(InputEvent));
  347. furi_timer_set_thread_priority(FuriTimerThreadPriorityElevated);
  348. ViewPort* view_port = view_port_alloc();
  349. view_port_set_orientation(view_port, ViewPortOrientationVertical);
  350. view_port_draw_callback_set(view_port, pinball_draw_callback, &app);
  351. view_port_input_callback_set(view_port, pinball_input_callback, event_queue);
  352. // Open the GUI and register view_port
  353. Gui* gui = (Gui*)furi_record_open(RECORD_GUI);
  354. gui_add_view_port(gui, view_port, GuiLayerFullscreen);
  355. // TODO: Dolphin deed actions
  356. // dolphin_deed(DolphinDeedPluginGameStart);
  357. app.processing = true;
  358. float dt = 0.0f;
  359. uint32_t last_frame_time = furi_get_tick();
  360. app.idle_start = last_frame_time;
  361. // I'm not thrilled with this event loop - kinda messy but it'll do for now
  362. InputEvent event;
  363. while(app.processing) {
  364. FuriStatus event_status = furi_message_queue_get(event_queue, &event, 10);
  365. furi_mutex_acquire(app.mutex, FuriWaitForever);
  366. if(event_status == FuriStatusOk) {
  367. if(event.type == InputTypePress || event.type == InputTypeLong ||
  368. event.type == InputTypeRepeat) {
  369. switch(event.key) {
  370. case InputKeyBack: // navigate to previous screen or exit
  371. switch(app.game_mode) {
  372. case GM_TableSelect:
  373. app.processing = false;
  374. break;
  375. case GM_Settings:
  376. pinball_save_settings(app);
  377. // fall through
  378. default:
  379. app.game_mode = GM_TableSelect;
  380. table_load_table(&app, TABLE_SELECT);
  381. break;
  382. }
  383. break;
  384. case InputKeyRight: {
  385. if(app.game_mode == GM_Tilted) {
  386. break;
  387. }
  388. app.keys[InputKeyRight] = true;
  389. if(app.settings.debug_mode && app.table->balls_released == false) {
  390. app.table->balls[0].p.x += MANUAL_ADJUSTMENT;
  391. app.table->balls[0].prev_p.x += MANUAL_ADJUSTMENT;
  392. }
  393. bool flipper_pressed = false;
  394. for(auto& f : app.table->flippers) {
  395. if(f.side == Flipper::RIGHT) {
  396. f.powered = true;
  397. if(f.rotation != f.max_rotation) {
  398. flipper_pressed = true;
  399. }
  400. }
  401. }
  402. if(flipper_pressed) {
  403. notify_flipper(&app);
  404. }
  405. } break;
  406. case InputKeyLeft: {
  407. if(app.game_mode == GM_Tilted) {
  408. break;
  409. }
  410. app.keys[InputKeyLeft] = true;
  411. if(app.settings.debug_mode && app.table->balls_released == false) {
  412. app.table->balls[0].p.x -= MANUAL_ADJUSTMENT;
  413. app.table->balls[0].prev_p.x -= MANUAL_ADJUSTMENT;
  414. }
  415. bool flipper_pressed = false;
  416. for(auto& f : app.table->flippers) {
  417. if(f.side == Flipper::LEFT) {
  418. f.powered = true;
  419. if(f.rotation != f.max_rotation) {
  420. flipper_pressed = true;
  421. }
  422. }
  423. }
  424. if(flipper_pressed) {
  425. notify_flipper(&app);
  426. }
  427. } break;
  428. case InputKeyUp:
  429. switch(app.game_mode) {
  430. case GM_Playing:
  431. if(event.type == InputTypePress) {
  432. // Table bump and Tilt tracking
  433. uint32_t current_tick = furi_get_tick();
  434. if(current_tick - app.table->last_bump >= BUMP_DELAY) {
  435. app.table->bump_count++;
  436. app.table->last_bump = current_tick;
  437. if(!app.table->tilt_detect_enabled ||
  438. app.table->bump_count < BUMP_MAX) {
  439. app.keys[InputKeyUp] = true;
  440. notify_table_bump(&app);
  441. } else {
  442. FURI_LOG_W(TAG, "TABLE TILTED!");
  443. app.game_mode = GM_Tilted;
  444. app.table->bump_count = 0;
  445. notify_table_tilted(&app);
  446. }
  447. }
  448. }
  449. if(app.settings.debug_mode && app.table->balls_released == false) {
  450. app.table->balls[0].p.y -= MANUAL_ADJUSTMENT;
  451. app.table->balls[0].prev_p.y -= MANUAL_ADJUSTMENT;
  452. }
  453. break;
  454. case GM_TableSelect:
  455. app.table_list.selected =
  456. (app.table_list.selected - 1 + app.table_list.menu_items.size()) %
  457. app.table_list.menu_items.size();
  458. break;
  459. case GM_Settings:
  460. if(app.settings.selected_setting > 0) {
  461. app.settings.selected_setting--;
  462. }
  463. break;
  464. default:
  465. FURI_LOG_W(TAG, "Table tilted, UP does nothing!");
  466. break;
  467. }
  468. break;
  469. case InputKeyDown:
  470. switch(app.game_mode) {
  471. case GM_Playing:
  472. app.keys[InputKeyDown] = true;
  473. if(app.settings.debug_mode && app.table->balls_released == false) {
  474. app.table->balls[0].p.y += MANUAL_ADJUSTMENT;
  475. app.table->balls[0].prev_p.y += MANUAL_ADJUSTMENT;
  476. }
  477. break;
  478. case GM_TableSelect:
  479. app.table_list.selected =
  480. (app.table_list.selected + 1 + app.table_list.menu_items.size()) %
  481. app.table_list.menu_items.size();
  482. break;
  483. case GM_Settings:
  484. if(app.settings.selected_setting < app.settings.max_settings - 1) {
  485. app.settings.selected_setting++;
  486. }
  487. break;
  488. default:
  489. break;
  490. }
  491. break;
  492. case InputKeyOk:
  493. switch(app.game_mode) {
  494. case GM_Playing:
  495. if(!app.table->balls_released) {
  496. app.table->balls_released = true;
  497. notify_ball_released(&app);
  498. }
  499. break;
  500. case GM_TableSelect: {
  501. size_t sel = app.table_list.selected;
  502. if(sel == app.table_list.menu_items.size() - 1) {
  503. app.game_mode = GM_Settings;
  504. table_load_table(&app, TABLE_SETTINGS);
  505. } else if(!table_load_table(&app, sel + TABLE_INDEX_OFFSET)) {
  506. app.game_mode = GM_Error;
  507. table_load_table(&app, TABLE_ERROR);
  508. notify_error_message(&app);
  509. } else {
  510. app.game_mode = GM_Playing;
  511. }
  512. } break;
  513. case GM_Settings:
  514. switch(app.settings.selected_setting) {
  515. case 0:
  516. app.settings.sound_enabled = !app.settings.sound_enabled;
  517. break;
  518. case 1:
  519. app.settings.led_enabled = !app.settings.led_enabled;
  520. break;
  521. case 2:
  522. app.settings.vibrate_enabled = !app.settings.vibrate_enabled;
  523. break;
  524. case 3:
  525. app.settings.debug_mode = !app.settings.debug_mode;
  526. break;
  527. default:
  528. break;
  529. }
  530. break;
  531. default:
  532. break;
  533. }
  534. break;
  535. default:
  536. break;
  537. }
  538. } else if(event.type == InputTypeRelease) {
  539. if(event.key != InputKeyOk && event.key != InputKeyBack) {
  540. app.keys[event.key] = false;
  541. for(auto& f : app.table->flippers) {
  542. if(event.key == InputKeyLeft && f.side == Flipper::LEFT) {
  543. f.powered = false;
  544. } else if(event.key == InputKeyRight && f.side == Flipper::RIGHT) {
  545. f.powered = false;
  546. }
  547. }
  548. }
  549. }
  550. // a key was pressed, reset idle counter
  551. app.idle_start = furi_get_tick();
  552. }
  553. // update physics / motion
  554. solve(&app, dt);
  555. for(auto& o : app.table->objects) {
  556. o->step_animation();
  557. }
  558. // check game state
  559. if(app.game_mode != GM_GameOver && app.table->game_over) {
  560. FURI_LOG_I(TAG, "GAME OVER!");
  561. app.game_mode = GM_GameOver;
  562. notify_game_over(&app);
  563. }
  564. // render
  565. view_port_update(view_port);
  566. furi_mutex_release(app.mutex);
  567. // game timing + idle check
  568. uint32_t current_tick = furi_get_tick();
  569. if(current_tick - app.idle_start >= IDLE_TIMEOUT) {
  570. FURI_LOG_W(TAG, "Idle timeout! Exiting Pinball0...");
  571. app.processing = false;
  572. break;
  573. }
  574. uint32_t time_lapsed = current_tick - last_frame_time;
  575. dt = time_lapsed / 1000.0f;
  576. while(dt < 1.0f / GAME_FPS) {
  577. time_lapsed = furi_get_tick() - last_frame_time;
  578. dt = time_lapsed / 1000.0f;
  579. }
  580. app.tick++;
  581. last_frame_time = furi_get_tick();
  582. }
  583. // general cleanup
  584. view_port_enabled_set(view_port, false);
  585. gui_remove_view_port(gui, view_port);
  586. furi_record_close(RECORD_GUI);
  587. view_port_free(view_port);
  588. furi_message_queue_free(event_queue);
  589. furi_timer_set_thread_priority(FuriTimerThreadPriorityNormal);
  590. return 0;
  591. }