pinball0.cxx 25 KB

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