table.cxx 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. #include <toolbox/dir_walk.h>
  2. #include <toolbox/path.h>
  3. #include <toolbox/stream/stream.h>
  4. #include <toolbox/stream/file_stream.h>
  5. #include <toolbox/args.h>
  6. #include "nxjson/nxjson.h"
  7. #include "pinball0.h"
  8. #include "table.h"
  9. // Table defaults
  10. #define LIVES 3
  11. #define LIVES_POS Vec2(20, 20)
  12. Table::~Table() {
  13. for(size_t i = 0; i < objects.size(); i++) {
  14. delete objects[i];
  15. }
  16. if(plunger != nullptr) {
  17. delete plunger;
  18. }
  19. }
  20. void Table::draw(Canvas* canvas) {
  21. // da balls
  22. for(auto& b : balls) {
  23. b.draw(canvas);
  24. }
  25. // loop through objects on the table and draw them
  26. for(auto& o : objects) {
  27. o->draw(canvas);
  28. }
  29. for(auto& f : flippers) {
  30. f.draw(canvas);
  31. }
  32. if(plunger) {
  33. plunger->draw(canvas);
  34. }
  35. // we don't draw the last one, as it's in play!
  36. if(num_lives > 0) {
  37. float x = num_lives_pos.x;
  38. float y = num_lives_pos.y;
  39. for(size_t l = 0; l < num_lives - 1; x += (2 * 20) + 20, l++) {
  40. canvas_draw_disc(canvas, x / 10, y / 10, 2);
  41. }
  42. }
  43. // score
  44. // char buf[32];
  45. // snprintf(buf, 32, "%8u", score);
  46. // canvas_draw_str_aligned(canvas, LCD_WIDTH - 30, 1, AlignLeft, AlignTop, buf);
  47. }
  48. void table_table_list_init(void* ctx) {
  49. PinballApp* pb = (PinballApp*)ctx;
  50. // using the asset file path, read the table files, and for each one, extract their
  51. // display name (oof). let's just use their filenames for now (stripping any XX_ prefix)
  52. // sort tables by original filename
  53. const char* paths[] = {APP_ASSETS_PATH("tables"), APP_DATA_PATH("tables")};
  54. for(size_t p = 0; p < 2; p++) {
  55. const char* path = paths[p];
  56. // const char* asset_path = APP_ASSETS_PATH("tables");
  57. FURI_LOG_I(TAG, "Loading table list from: %s", path);
  58. FuriString* table_path = furi_string_alloc();
  59. DirWalk* dir_walk = dir_walk_alloc(pb->storage);
  60. dir_walk_set_recursive(dir_walk, false);
  61. if(dir_walk_open(dir_walk, path)) {
  62. while(dir_walk_read(dir_walk, table_path, NULL) == DirWalkOK) {
  63. FURI_LOG_I(TAG, furi_string_get_cstr(table_path));
  64. // set display 'name' and 'filename'
  65. TableMenuItem tmi;
  66. const char* cpath = furi_string_get_cstr(table_path);
  67. tmi.filename = furi_string_alloc_set_str(cpath);
  68. tmi.name = furi_string_alloc();
  69. path_extract_filename_no_ext(cpath, tmi.name);
  70. // If filename starts with XX_ (for custom sorting) strip the prefix
  71. char c = furi_string_get_char(tmi.name, 2);
  72. if(c == '_') {
  73. char a = furi_string_get_char(tmi.name, 0);
  74. char b = furi_string_get_char(tmi.name, 1);
  75. if(a >= '0' && a <= '9' && b >= '0' && b <= '9') {
  76. furi_string_right(tmi.name, 3);
  77. }
  78. }
  79. // Insert in sorted order
  80. size_t i = 0;
  81. auto it = pb->table_list.menu_items.begin();
  82. for(; it != pb->table_list.menu_items.end(); it++, i++) {
  83. if(strcmp(
  84. furi_string_get_cstr(tmi.filename),
  85. furi_string_get_cstr(it->filename)) > 0) {
  86. continue;
  87. }
  88. pb->table_list.menu_items.insert(it, tmi);
  89. break;
  90. }
  91. if(pb->table_list.menu_items.size() == i) {
  92. pb->table_list.menu_items.push_back(tmi);
  93. }
  94. }
  95. }
  96. furi_string_free(table_path);
  97. dir_walk_free(dir_walk);
  98. }
  99. // Add 'Settings' as last element
  100. TableMenuItem settings;
  101. settings.filename = furi_string_alloc_set_str("99_Settings");
  102. settings.name = furi_string_alloc_set_str("SETTINGS");
  103. pb->table_list.menu_items.push_back(settings);
  104. FURI_LOG_I(TAG, "Found %d tables", pb->table_list.menu_items.size());
  105. for(auto& tmi : pb->table_list.menu_items) {
  106. FURI_LOG_I(TAG, "%s", furi_string_get_cstr(tmi.name));
  107. }
  108. pb->table_list.display_size = 5; // how many tables to display at once
  109. pb->table_list.selected = 0;
  110. }
  111. // json parse helper function
  112. bool table_file_parse_vec2(const nx_json* json, const char* key, Vec2* v) {
  113. furi_assert(v);
  114. const nx_json* item = nx_json_get(json, key);
  115. if(!item || item->children.length != 2) {
  116. return false;
  117. }
  118. v->x = nx_json_item(item, 0)->num.dbl_value;
  119. v->y = nx_json_item(item, 1)->num.dbl_value;
  120. return true;
  121. }
  122. bool table_file_parse_int(const nx_json* json, const char* key, int* v) {
  123. furi_assert(v);
  124. const nx_json* item = nx_json_get(json, key);
  125. if(!item) return false;
  126. *v = item->num.u_value;
  127. return true;
  128. }
  129. bool table_file_parse_float(const nx_json* json, const char* key, float* v) {
  130. furi_assert(v);
  131. const nx_json* item = nx_json_get(json, key);
  132. if(!item) return false;
  133. *v = item->num.dbl_value;
  134. return true;
  135. }
  136. Table* table_load_table_from_file(PinballApp* pb, size_t index) {
  137. auto& tmi = pb->table_list.menu_items[index];
  138. FURI_LOG_I(TAG, "Reading file: %s", furi_string_get_cstr(tmi.filename));
  139. File* file = storage_file_alloc(pb->storage);
  140. FileInfo fileinfo;
  141. FS_Error error =
  142. storage_common_stat(pb->storage, furi_string_get_cstr(tmi.filename), &fileinfo);
  143. if(error != FSE_OK) {
  144. FURI_LOG_E(TAG, "Could not find file");
  145. storage_file_free(file);
  146. return NULL;
  147. }
  148. // TODO: determine an appropriate max file size and make configurable
  149. FURI_LOG_I(TAG, "Found file ok!");
  150. if(fileinfo.size >= 8192) {
  151. FURI_LOG_E(TAG, "Table file size too big");
  152. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  153. storage_file_free(file);
  154. return NULL;
  155. }
  156. FURI_LOG_I(TAG, "File size is ok!");
  157. bool ok =
  158. storage_file_open(file, furi_string_get_cstr(tmi.filename), FSAM_READ, FSOM_OPEN_EXISTING);
  159. FURI_LOG_I(TAG, "File opened? %s", ok ? "YES" : "NO");
  160. // read the file as a string
  161. uint8_t* buffer;
  162. uint64_t file_size = storage_file_size(file);
  163. if(file_size > 8192) { // TODO - what's the right size?
  164. FURI_LOG_E(TAG, "Table file is too large! (> 8192 bytes)");
  165. snprintf(pb->text, 256, "Table file\nis too big!\n> 8192 bytes");
  166. storage_file_free(file);
  167. return NULL;
  168. }
  169. buffer = (uint8_t*)malloc(file_size);
  170. size_t read_count = storage_file_read(file, buffer, file_size);
  171. // if(storage_file_get_error(file) != FSE_OK) {
  172. // FURI_LOG_E(TAG, "Um, couldn't read file");
  173. // storage_file_free(file);
  174. // return NULL;
  175. // }
  176. storage_file_free(file);
  177. if(read_count != file_size) {
  178. FURI_LOG_E(TAG, "Error reading file. expected %lld, got %d", file_size, read_count);
  179. free(buffer);
  180. return NULL;
  181. }
  182. FURI_LOG_I(TAG, "Read file into buffer! %d bytes", read_count);
  183. // let's parse this shit
  184. char* json_buffer = (char*)malloc(read_count * sizeof(char) + 1);
  185. for(uint16_t i = 0; i < read_count; i++) {
  186. json_buffer[i] = buffer[i];
  187. }
  188. json_buffer[read_count] = 0;
  189. free(buffer);
  190. const nx_json* json = nx_json_parse(json_buffer, 0);
  191. if(!json) {
  192. FURI_LOG_E(TAG, "Failed to parse table json!");
  193. snprintf(pb->text, 256, "Failed to\nparse table\njson!!");
  194. free(json_buffer);
  195. return NULL;
  196. }
  197. Table* table = new Table();
  198. int lives = LIVES;
  199. table_file_parse_int(json, "lives", &lives);
  200. table->num_lives = lives;
  201. table->num_lives_pos = LIVES_POS;
  202. table_file_parse_vec2(json, "lives_position", &table->num_lives_pos);
  203. // TODO: this should be an attribute of balls?
  204. table->balls_released = false;
  205. const nx_json* released = nx_json_get(json, "released");
  206. if(released) {
  207. table->balls_released = released->num.u_value > 0;
  208. }
  209. do {
  210. const nx_json* balls = nx_json_get(json, "balls");
  211. if(balls) {
  212. for(int i = 0; i < balls->children.length; i++) {
  213. const nx_json* ball = nx_json_item(balls, i);
  214. Vec2 p;
  215. if(!table_file_parse_vec2(ball, "position", &p)) {
  216. FURI_LOG_E(TAG, "Ball missing \"position\", skipping");
  217. continue;
  218. }
  219. int r = DEF_BALL_RADIUS;
  220. table_file_parse_int(ball, "radius", &r);
  221. Vec2 v = (Vec2){0, 0};
  222. table_file_parse_vec2(ball, "velocity", &v);
  223. Ball new_ball(p, r);
  224. new_ball.accelerate(v);
  225. table->balls_initial.push_back(new_ball);
  226. table->balls.push_back(new_ball);
  227. }
  228. }
  229. if(table->balls.size() == 0) {
  230. FURI_LOG_E(TAG, "Table has NO BALLS");
  231. snprintf(pb->text, 256, "No balls\nfound in\ntable file!");
  232. delete table;
  233. table = NULL;
  234. break;
  235. }
  236. // TODO: plungers need work
  237. const nx_json* plunger = nx_json_get(json, "plunger");
  238. if(plunger) {
  239. Vec2 p;
  240. table_file_parse_vec2(plunger, "position", &p);
  241. int s = 100;
  242. table_file_parse_int(plunger, "size", &s);
  243. table->plunger = new Plunger(p);
  244. } else {
  245. FURI_LOG_E(TAG, "Table has NO PLUNGER");
  246. }
  247. const nx_json* flippers = nx_json_get(json, "flippers");
  248. if(flippers) {
  249. for(int i = 0; i < flippers->children.length; i++) {
  250. const nx_json* flipper = nx_json_item(flippers, i);
  251. Vec2 p;
  252. if(!table_file_parse_vec2(flipper, "position", &p)) {
  253. FURI_LOG_E(TAG, "Flipper missing \"position\", skipping");
  254. continue;
  255. }
  256. const nx_json* side = nx_json_get(flipper, "side");
  257. Flipper::Side sd = Flipper::LEFT;
  258. if(side && !strcmp(side->text_value, "RIGHT")) {
  259. sd = Flipper::RIGHT;
  260. }
  261. int sz = DEF_FLIPPER_SIZE;
  262. table_file_parse_int(flipper, "size", &sz);
  263. Flipper flip(p, sd, sz);
  264. table->flippers.push_back(flip);
  265. }
  266. }
  267. const nx_json* bumpers = nx_json_get(json, "bumpers");
  268. if(bumpers) {
  269. for(int i = 0; i < bumpers->children.length; i++) {
  270. const nx_json* bumper = nx_json_item(bumpers, i);
  271. Vec2 p;
  272. if(!table_file_parse_vec2(bumper, "position", &p)) {
  273. FURI_LOG_E(TAG, "Bumper missing \"position\", skipping");
  274. continue;
  275. }
  276. int r = DEF_BUMPER_RADIUS;
  277. table_file_parse_int(bumper, "radius", &r);
  278. float bnc = DEF_BUMPER_BOUNCE;
  279. table_file_parse_float(bumper, "bounce", &bnc);
  280. Bumper* new_bumper = new Bumper(p, r);
  281. FURI_LOG_I(TAG, "new bumper: %.3f,%.3f", (double)p.x, (double)p.y);
  282. new_bumper->bounce = bnc;
  283. table->objects.push_back(new_bumper);
  284. }
  285. }
  286. constexpr float pi_180 = M_PI / 180;
  287. const nx_json* arcs = nx_json_get(json, "arcs");
  288. if(arcs) {
  289. for(int i = 0; i < arcs->children.length; i++) {
  290. const nx_json* arc = nx_json_item(arcs, i);
  291. Vec2 p;
  292. if(!table_file_parse_vec2(arc, "position", &p)) {
  293. FURI_LOG_E(TAG, "Arc missing \"position\"");
  294. continue;
  295. }
  296. int r = DEF_BUMPER_RADIUS;
  297. table_file_parse_int(arc, "radius", &r);
  298. float bnc = 0.95f; // DEF_BUMPER_BOUNCE?
  299. table_file_parse_float(arc, "bounce", &bnc);
  300. float start_angle = 0.0;
  301. table_file_parse_float(arc, "start_angle", &start_angle);
  302. start_angle *= pi_180;
  303. float end_angle = 0.0;
  304. table_file_parse_float(arc, "end_angle", &end_angle);
  305. end_angle *= pi_180;
  306. Arc::Surface surface = Arc::OUTSIDE;
  307. const nx_json* stype = nx_json_get(arc, "surface");
  308. if(stype && !strcmp(stype->text_value, "INSIDE")) {
  309. surface = Arc::INSIDE;
  310. }
  311. Arc* new_bumper = new Arc(p, r, start_angle, end_angle, surface);
  312. new_bumper->bounce = bnc;
  313. table->objects.push_back(new_bumper);
  314. }
  315. }
  316. const nx_json* rails = nx_json_get(json, "rails");
  317. if(rails) {
  318. for(int i = 0; i < rails->children.length; i++) {
  319. const nx_json* rail = nx_json_item(rails, i);
  320. Vec2 s;
  321. if(!table_file_parse_vec2(rail, "start", &s)) {
  322. FURI_LOG_E(TAG, "Rail missing \"start\", skipping");
  323. continue;
  324. }
  325. Vec2 e;
  326. if(!table_file_parse_vec2(rail, "end", &e)) {
  327. FURI_LOG_E(TAG, "Rail missing \"end\", skipping");
  328. continue;
  329. }
  330. Polygon* new_rail = new Polygon();
  331. new_rail->add_point(s);
  332. new_rail->add_point(e);
  333. float bnc = DEF_RAIL_BOUNCE;
  334. table_file_parse_float(rail, "bounce", &bnc);
  335. new_rail->bounce = bnc;
  336. int double_sided = 0;
  337. table_file_parse_int(rail, "double_sided", &double_sided);
  338. new_rail->finalize();
  339. table->objects.push_back(new_rail);
  340. if(double_sided) {
  341. new_rail = new Polygon();
  342. new_rail->add_point(e);
  343. new_rail->add_point(s);
  344. new_rail->bounce = bnc;
  345. new_rail->finalize();
  346. table->objects.push_back(new_rail);
  347. }
  348. }
  349. }
  350. const nx_json* portals = nx_json_get(json, "portals");
  351. if(portals) {
  352. for(int i = 0; i < portals->children.length; i++) {
  353. const nx_json* portal = nx_json_item(portals, i);
  354. Vec2 a1;
  355. if(!table_file_parse_vec2(portal, "a_start", &a1)) {
  356. FURI_LOG_E(TAG, "Portal missing \"a_start\", skipping");
  357. continue;
  358. }
  359. Vec2 a2;
  360. if(!table_file_parse_vec2(portal, "a_end", &a2)) {
  361. FURI_LOG_E(TAG, "Portal missing \"a_end\", skipping");
  362. continue;
  363. }
  364. Vec2 b1;
  365. if(!table_file_parse_vec2(portal, "b_start", &b1)) {
  366. FURI_LOG_E(TAG, "Portal missing \"b_start\", skipping");
  367. continue;
  368. }
  369. Vec2 b2;
  370. if(!table_file_parse_vec2(portal, "b_end", &b2)) {
  371. FURI_LOG_E(TAG, "Portal missing \"b_end\", skipping");
  372. continue;
  373. }
  374. Portal* new_portal = new Portal(a1, a2, b1, b2);
  375. new_portal->finalize();
  376. table->objects.push_back(new_portal);
  377. }
  378. }
  379. const nx_json* rollovers = nx_json_get(json, "rollovers");
  380. if(rollovers) {
  381. for(int i = 0; i < rollovers->children.length; i++) {
  382. const nx_json* rollover = nx_json_item(rollovers, i);
  383. Vec2 p;
  384. if(!table_file_parse_vec2(rollover, "position", &p)) {
  385. FURI_LOG_E(TAG, "Rollover missing \"position\", skipping");
  386. continue;
  387. }
  388. char sym = '*';
  389. const nx_json* symbol = nx_json_get(rollover, "symbol");
  390. if(symbol) {
  391. sym = symbol->text_value[0];
  392. }
  393. Rollover* new_rollover = new Rollover(p, sym);
  394. table->objects.push_back(new_rollover);
  395. }
  396. }
  397. const nx_json* turbos = nx_json_get(json, "turbos");
  398. if(turbos) {
  399. for(int i = 0; i < turbos->children.length; i++) {
  400. const nx_json* turbo = nx_json_item(turbos, i);
  401. Vec2 p;
  402. if(!table_file_parse_vec2(turbo, "position", &p)) {
  403. FURI_LOG_E(TAG, "Turbo missing \"position\"");
  404. continue;
  405. }
  406. float angle = 0;
  407. table_file_parse_float(turbo, "angle", &angle);
  408. angle *= pi_180;
  409. float boost = 10;
  410. table_file_parse_float(turbo, "boost", &boost);
  411. Turbo* new_turbo = new Turbo(p, angle, boost);
  412. table->objects.push_back(new_turbo);
  413. }
  414. }
  415. break;
  416. } while(false);
  417. nx_json_free(json);
  418. free(json_buffer);
  419. return table;
  420. }
  421. TableList::~TableList() {
  422. for(auto& mi : menu_items) {
  423. furi_string_free(mi.name);
  424. furi_string_free(mi.filename);
  425. }
  426. }
  427. Table* table_init_table_select(void* ctx) {
  428. UNUSED(ctx);
  429. Table* table = new Table();
  430. table->balls.push_back(Ball(Vec2(20, 880), 35));
  431. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  432. table->balls.push_back(Ball(Vec2(610, 920), 30));
  433. table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  434. table->balls.push_back(Ball(Vec2(250, 980), 20));
  435. table->balls.back().add_velocity(Vec2(10, 0), .10f);
  436. table->balls_released = true;
  437. Polygon* new_rail = new Polygon();
  438. new_rail->add_point({-1, 840});
  439. new_rail->add_point({-1, 1280});
  440. new_rail->finalize();
  441. new_rail->hidden = true;
  442. table->objects.push_back(new_rail);
  443. new_rail = new Polygon();
  444. new_rail->add_point({-1, 1280});
  445. new_rail->add_point({640, 1280});
  446. new_rail->finalize();
  447. new_rail->hidden = true;
  448. table->objects.push_back(new_rail);
  449. new_rail = new Polygon();
  450. new_rail->add_point({640, 1280});
  451. new_rail->add_point({640, 840});
  452. new_rail->finalize();
  453. new_rail->hidden = true;
  454. table->objects.push_back(new_rail);
  455. int gap = 8;
  456. int speed = 3;
  457. float top = 20;
  458. // right side
  459. table->objects.push_back(new Chaser(Vec2(32, top), Vec2(62, top), gap, speed));
  460. table->objects.push_back(new Chaser(Vec2(62, top), Vec2(62, 84), gap, speed));
  461. table->objects.push_back(new Chaser(Vec2(62, 84), Vec2(32, 84), gap, speed));
  462. // left side
  463. table->objects.push_back(new Chaser(Vec2(32, top), Vec2(1, top), gap, speed));
  464. table->objects.push_back(new Chaser(Vec2(1, top), Vec2(1, 84), gap, speed));
  465. table->objects.push_back(new Chaser(Vec2(1, 84), Vec2(32, 84), gap, speed));
  466. return table;
  467. }
  468. Table* table_init_table_error(void* ctx) {
  469. UNUSED(ctx);
  470. // PinballApp* pb = (PinballApp*)ctx;
  471. Table* table = new Table();
  472. table->balls.push_back(Ball(Vec2(20, 880), 30));
  473. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  474. // table->balls.push_back(Ball(Vec2(610, 920), 30));
  475. // table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  476. // table->balls.push_back(Ball(Vec2(250, 980), 20));
  477. // table->balls.back().add_velocity(Vec2(10, 0), .10f);
  478. table->balls_released = true;
  479. Polygon* new_rail = new Polygon();
  480. new_rail->add_point({-1, 840});
  481. new_rail->add_point({-1, 1280});
  482. new_rail->finalize();
  483. new_rail->hidden = true;
  484. table->objects.push_back(new_rail);
  485. new_rail = new Polygon();
  486. new_rail->add_point({-1, 1280});
  487. new_rail->add_point({640, 1280});
  488. new_rail->finalize();
  489. new_rail->hidden = true;
  490. table->objects.push_back(new_rail);
  491. new_rail = new Polygon();
  492. new_rail->add_point({640, 1280});
  493. new_rail->add_point({640, 840});
  494. new_rail->finalize();
  495. new_rail->hidden = true;
  496. table->objects.push_back(new_rail);
  497. int gap = 8;
  498. int speed = 3;
  499. float top = 20;
  500. table->objects.push_back(new Chaser(Vec2(2, top), Vec2(61, top), gap, speed, Chaser::SLASH));
  501. table->objects.push_back(new Chaser(Vec2(2, top), Vec2(2, 84), gap, speed, Chaser::SLASH));
  502. table->objects.push_back(new Chaser(Vec2(2, 84), Vec2(61, 84), gap, speed, Chaser::SLASH));
  503. table->objects.push_back(new Chaser(Vec2(61, top), Vec2(61, 84), gap, speed, Chaser::SLASH));
  504. return table;
  505. }
  506. Table* table_init_table_settings(void* ctx) {
  507. UNUSED(ctx);
  508. Table* table = new Table();
  509. table->balls.push_back(Ball(Vec2(20, 880), 10));
  510. table->balls.back().add_velocity(Vec2(7, 0), .10f);
  511. table->balls.push_back(Ball(Vec2(610, 920), 10));
  512. table->balls.back().add_velocity(Vec2(-8, 0), .10f);
  513. table->balls.push_back(Ball(Vec2(250, 980), 10));
  514. table->balls.back().add_velocity(Vec2(10, 0), .10f);
  515. table->balls_released = true;
  516. Polygon* new_rail = new Polygon();
  517. new_rail->add_point({-1, 840});
  518. new_rail->add_point({-1, 1280});
  519. new_rail->finalize();
  520. new_rail->hidden = true;
  521. table->objects.push_back(new_rail);
  522. new_rail = new Polygon();
  523. new_rail->add_point({-1, 1280});
  524. new_rail->add_point({640, 1280});
  525. new_rail->finalize();
  526. new_rail->hidden = true;
  527. table->objects.push_back(new_rail);
  528. new_rail = new Polygon();
  529. new_rail->add_point({640, 1280});
  530. new_rail->add_point({640, 840});
  531. new_rail->finalize();
  532. new_rail->hidden = true;
  533. table->objects.push_back(new_rail);
  534. // int gap = 8;
  535. // int speed = 3;
  536. // float top = 20;
  537. // table->objects.push_back(new Chaser(Vec2(2, top), Vec2(61, top), gap, speed, Chaser::SLASH));
  538. // table->objects.push_back(new Chaser(Vec2(2, top), Vec2(2, 84), gap, speed, Chaser::SLASH));
  539. // table->objects.push_back(new Chaser(Vec2(2, 84), Vec2(61, 84), gap, speed, Chaser::SLASH));
  540. // table->objects.push_back(new Chaser(Vec2(61, top), Vec2(61, 84), gap, speed, Chaser::SLASH));
  541. return table;
  542. }
  543. bool table_load_table(void* ctx, size_t index) {
  544. PinballApp* pb = (PinballApp*)ctx;
  545. // read the index'th file in pb->table_list and allocate
  546. FURI_LOG_I(TAG, "Loading table %u", index);
  547. // if there's already a table loaded, free it
  548. if(pb->table) {
  549. delete pb->table;
  550. pb->table = nullptr;
  551. }
  552. pb->gameStarted = false;
  553. switch(index) {
  554. case TABLE_SELECT:
  555. pb->table = table_init_table_select(ctx);
  556. break;
  557. case TABLE_ERROR:
  558. pb->table = table_init_table_error(ctx);
  559. break;
  560. case TABLE_SETTINGS:
  561. pb->table = table_init_table_settings(ctx);
  562. break;
  563. default:
  564. pb->table = table_load_table_from_file(pb, index - TABLE_INDEX_OFFSET);
  565. break;
  566. }
  567. return pb->table != NULL;
  568. }