pinball0.cxx 25 KB

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