comparison blastem.c @ 1541:f8ef74e7c800

Merged nuklear_ui into default
author Michael Pavone <pavone@retrodev.com>
date Sun, 25 Mar 2018 12:01:49 -0700
parents c59adc305e46
children b525491b4e5b
comparison
equal deleted inserted replaced
1533:78b7fc03c7c6 1541:f8ef74e7c800
23 #include "terminal.h" 23 #include "terminal.h"
24 #include "arena.h" 24 #include "arena.h"
25 #include "config.h" 25 #include "config.h"
26 #include "menu.h" 26 #include "menu.h"
27 #include "zip.h" 27 #include "zip.h"
28 #ifndef DISABLE_NUKLEAR
29 #include "nuklear_ui/blastem_nuklear.h"
30 #endif
28 31
29 #define BLASTEM_VERSION "0.5.2-pre" 32 #define BLASTEM_VERSION "0.5.2-pre"
30 33
31 #ifdef __ANDROID__ 34 #ifdef __ANDROID__
32 #define FULLSCREEN_DEFAULT 1 35 #define FULLSCREEN_DEFAULT 1
323 lock_on.name = basename_no_extension(lock_on_path); 326 lock_on.name = basename_no_extension(lock_on_path);
324 lock_on.extension = path_extension(lock_on_path); 327 lock_on.extension = path_extension(lock_on_path);
325 lock_on.size = load_rom(lock_on_path, &lock_on.buffer, NULL); 328 lock_on.size = load_rom(lock_on_path, &lock_on.buffer, NULL);
326 } 329 }
327 330
331 static uint32_t opts = 0;
332 static uint8_t force_region = 0;
333 void init_system_with_media(char *path, system_type force_stype)
334 {
335 if (game_system) {
336 game_system->persist_save(game_system);
337 //swap to game context arena and mark all allocated pages in it free
338 if (current_system == menu_system) {
339 current_system->arena = set_current_arena(game_system->arena);
340 }
341 mark_all_free();
342 game_system->free_context(game_system);
343 } else if(current_system) {
344 //start a new arena and save old one in suspended system context
345 current_system->arena = start_new_arena();
346 }
347 system_type stype = SYSTEM_UNKNOWN;
348 if (!(cart.size = load_rom(path, &cart.buffer, &stype))) {
349 fatal_error("Failed to open %s for reading\n", path);
350 }
351 free(cart.dir);
352 free(cart.name);
353 free(cart.extension);
354 cart.dir = path_dirname(path);
355 cart.name = basename_no_extension(path);
356 cart.extension = path_extension(path);
357 if (force_stype != SYSTEM_UNKNOWN) {
358 stype = force_stype;
359 }
360 if (stype == SYSTEM_UNKNOWN) {
361 stype = detect_system_type(&cart);
362 }
363 if (stype == SYSTEM_UNKNOWN) {
364 fatal_error("Failed to detect system type for %s\n", path);
365 }
366 rom_info info;
367 //allocate new system context
368 game_system = alloc_config_system(stype, &cart, opts, force_region, &info);
369 if (!game_system) {
370 fatal_error("Failed to configure emulated machine for %s\n", path);
371 }
372 if (menu_system) {
373 menu_system->next_context = game_system;
374 }
375 game_system->next_context = menu_system;
376 setup_saves(&cart, &info, game_system);
377 update_title(info.name);
378 }
379
380 static void save_config(void)
381 {
382 persist_config(config);
383 }
384
328 int main(int argc, char ** argv) 385 int main(int argc, char ** argv)
329 { 386 {
330 set_exe_str(argv[0]); 387 set_exe_str(argv[0]);
331 config = load_config(); 388 config = load_config();
332 int width = -1; 389 int width = -1;
333 int height = -1; 390 int height = -1;
334 int debug = 0; 391 int debug = 0;
335 uint32_t opts = 0;
336 int loaded = 0; 392 int loaded = 0;
337 system_type stype = SYSTEM_UNKNOWN, force_stype = SYSTEM_UNKNOWN; 393 system_type stype = SYSTEM_UNKNOWN, force_stype = SYSTEM_UNKNOWN;
338 uint8_t force_region = 0;
339 char * romfname = NULL; 394 char * romfname = NULL;
340 char * statefile = NULL; 395 char * statefile = NULL;
341 debugger_type dtype = DEBUGGER_NATIVE; 396 debugger_type dtype = DEBUGGER_NATIVE;
342 uint8_t start_in_debugger = 0; 397 uint8_t start_in_debugger = 0;
343 uint8_t fullscreen = FULLSCREEN_DEFAULT, use_gl = 1; 398 uint8_t fullscreen = FULLSCREEN_DEFAULT, use_gl = 1;
470 width = atoi(argv[i]); 525 width = atoi(argv[i]);
471 } else if (height < 0) { 526 } else if (height < 0) {
472 height = atoi(argv[i]); 527 height = atoi(argv[i]);
473 } 528 }
474 } 529 }
530
531 int def_width = 0, def_height = 0;
532 char *config_width = tern_find_path(config, "video\0width\0", TVAL_PTR).ptrval;
533 if (config_width) {
534 def_width = atoi(config_width);
535 }
536 if (!def_width) {
537 def_width = 640;
538 }
539 char *config_height = tern_find_path(config, "video\0height\0", TVAL_PTR).ptrval;
540 if (config_height) {
541 def_height = atoi(config_height);
542 }
543 if (!def_height) {
544 def_height = -1;
545 }
546 width = width < 1 ? def_width : width;
547 height = height < 1 ? def_height : height;
548
549 char *config_fullscreen = tern_find_path(config, "video\0fullscreen\0", TVAL_PTR).ptrval;
550 if (config_fullscreen && !strcmp("on", config_fullscreen)) {
551 fullscreen = !fullscreen;
552 }
553 if (!headless) {
554 render_init(width, height, "BlastEm", fullscreen);
555 render_set_drag_drop_handler(on_drag_drop);
556 }
557
475 uint8_t menu = !loaded; 558 uint8_t menu = !loaded;
476 if (!loaded) { 559 uint8_t use_nuklear = 0;
560 #ifndef DISABLE_NUKLEAR
561 use_nuklear = is_nuklear_available();
562 #endif
563 if (!loaded && !use_nuklear) {
477 //load menu 564 //load menu
478 romfname = tern_find_path(config, "ui\0rom\0", TVAL_PTR).ptrval; 565 romfname = tern_find_path(config, "ui\0rom\0", TVAL_PTR).ptrval;
479 if (!romfname) { 566 if (!romfname) {
480 romfname = "menu.bin"; 567 romfname = "menu.bin";
481 } 568 }
499 cart.dir = path_dirname(romfname); 586 cart.dir = path_dirname(romfname);
500 cart.name = basename_no_extension(romfname); 587 cart.name = basename_no_extension(romfname);
501 cart.extension = path_extension(romfname); 588 cart.extension = path_extension(romfname);
502 loaded = 1; 589 loaded = 1;
503 } 590 }
504
505 int def_width = 0, def_height = 0;
506 char *config_width = tern_find_path(config, "video\0width\0", TVAL_PTR).ptrval;
507 if (config_width) {
508 def_width = atoi(config_width);
509 }
510 if (!def_width) {
511 def_width = 640;
512 }
513 char *config_height = tern_find_path(config, "video\0height\0", TVAL_PTR).ptrval;
514 if (config_height) {
515 def_height = atoi(config_height);
516 }
517 if (!def_height) {
518 def_height = -1;
519 }
520 width = width < 1 ? def_width : width;
521 height = height < 1 ? def_height : height;
522
523 char *config_fullscreen = tern_find_path(config, "video\0fullscreen\0", TVAL_PTR).ptrval;
524 if (config_fullscreen && !strcmp("on", config_fullscreen)) {
525 fullscreen = !fullscreen;
526 }
527 if (!headless) {
528 render_init(width, height, "BlastEm", fullscreen);
529 render_set_drag_drop_handler(on_drag_drop);
530 }
531
532 if (stype == SYSTEM_UNKNOWN) {
533 stype = detect_system_type(&cart);
534 }
535 if (stype == SYSTEM_UNKNOWN) {
536 fatal_error("Failed to detect system type for %s\n", romfname);
537 }
538 rom_info info;
539 current_system = alloc_config_system(stype, &cart, menu ? 0 : opts, force_region, &info);
540 if (!current_system) {
541 fatal_error("Failed to configure emulated machine for %s\n", romfname);
542 }
543 char *state_format = tern_find_path(config, "ui\0state_format\0", TVAL_PTR).ptrval; 591 char *state_format = tern_find_path(config, "ui\0state_format\0", TVAL_PTR).ptrval;
544 if (state_format && !strcmp(state_format, "gst")) { 592 if (state_format && !strcmp(state_format, "gst")) {
545 use_native_states = 0; 593 use_native_states = 0;
546 } else if (state_format && strcmp(state_format, "native")) { 594 } else if (state_format && strcmp(state_format, "native")) {
547 warning("%s is not a valid value for the ui.state_format setting. Valid values are gst and native\n", state_format); 595 warning("%s is not a valid value for the ui.state_format setting. Valid values are gst and native\n", state_format);
548 } 596 }
549 setup_saves(&cart, &info, current_system); 597
550 update_title(info.name); 598 if (loaded) {
551 if (menu) { 599 if (stype == SYSTEM_UNKNOWN) {
552 menu_system = current_system; 600 stype = detect_system_type(&cart);
553 } else { 601 }
554 game_system = current_system; 602 if (stype == SYSTEM_UNKNOWN) {
555 } 603 fatal_error("Failed to detect system type for %s\n", romfname);
556 604 }
605 rom_info info;
606 current_system = alloc_config_system(stype, &cart, menu ? 0 : opts, force_region, &info);
607 if (!current_system) {
608 fatal_error("Failed to configure emulated machine for %s\n", romfname);
609 }
610
611 setup_saves(&cart, &info, current_system);
612 update_title(info.name);
613 if (menu) {
614 menu_system = current_system;
615 } else {
616 game_system = current_system;
617 }
618 }
619
620 atexit(save_config);
621
622 #ifndef DISABLE_NUKLEAR
623 if (use_nuklear) {
624 blastem_nuklear_init(!menu);
625 current_system = game_system;
626 menu = 0;
627 }
628 #endif
629
557 current_system->debugger_type = dtype; 630 current_system->debugger_type = dtype;
558 current_system->enter_debugger = start_in_debugger && menu == debug_target; 631 current_system->enter_debugger = start_in_debugger && menu == debug_target;
559 current_system->start_context(current_system, menu ? NULL : statefile); 632 current_system->start_context(current_system, menu ? NULL : statefile);
560 for(;;) 633 for(;;)
561 { 634 {
563 break; 636 break;
564 } 637 }
565 if (current_system->next_rom) { 638 if (current_system->next_rom) {
566 char *next_rom = current_system->next_rom; 639 char *next_rom = current_system->next_rom;
567 current_system->next_rom = NULL; 640 current_system->next_rom = NULL;
568 if (game_system) { 641 init_system_with_media(next_rom, force_stype);
569 game_system->persist_save(game_system);
570 //swap to game context arena and mark all allocated pages in it free
571 if (menu) {
572 current_system->arena = set_current_arena(game_system->arena);
573 }
574 mark_all_free();
575 game_system->free_context(game_system);
576 } else {
577 //start a new arena and save old one in suspended genesis context
578 current_system->arena = start_new_arena();
579 }
580 if (!(cart.size = load_rom(next_rom, &cart.buffer, &stype))) {
581 fatal_error("Failed to open %s for reading\n", next_rom);
582 }
583 free(cart.dir);
584 free(cart.name);
585 free(cart.extension);
586 cart.dir = path_dirname(next_rom);
587 cart.name = basename_no_extension(next_rom);
588 cart.extension = path_extension(next_rom);
589 stype = force_stype;
590 if (stype == SYSTEM_UNKNOWN) {
591 stype = detect_system_type(&cart);
592 }
593 if (stype == SYSTEM_UNKNOWN) {
594 fatal_error("Failed to detect system type for %s\n", next_rom);
595 }
596 //allocate new system context
597 game_system = alloc_config_system(stype, &cart, opts,force_region, &info);
598 if (!game_system) {
599 fatal_error("Failed to configure emulated machine for %s\n", next_rom);
600 }
601 if (menu_system) {
602 menu_system->next_context = game_system;
603 }
604 game_system->next_context = menu_system;
605 setup_saves(&cart, &info, game_system);
606 update_title(info.name);
607 free(next_rom); 642 free(next_rom);
608 menu = 0; 643 menu = 0;
609 current_system = game_system; 644 current_system = game_system;
610 current_system->debugger_type = dtype; 645 current_system->debugger_type = dtype;
611 current_system->enter_debugger = start_in_debugger && menu == debug_target; 646 current_system->enter_debugger = start_in_debugger && menu == debug_target;
613 } else if (menu && game_system) { 648 } else if (menu && game_system) {
614 current_system->arena = set_current_arena(game_system->arena); 649 current_system->arena = set_current_arena(game_system->arena);
615 current_system = game_system; 650 current_system = game_system;
616 menu = 0; 651 menu = 0;
617 current_system->resume_context(current_system); 652 current_system->resume_context(current_system);
618 } else if (!menu && menu_system) { 653 } else if (!menu && (menu_system || use_nuklear)) {
619 current_system->arena = set_current_arena(menu_system->arena); 654 if (use_nuklear) {
620 current_system = menu_system; 655 #ifndef DISABLE_NUKLEAR
621 menu = 1; 656 ui_idle_loop();
622 current_system->resume_context(current_system); 657 #endif
658 } else {
659 current_system->arena = set_current_arena(menu_system->arena);
660 current_system = menu_system;
661 menu = 1;
662 }
663 if (!current_system->next_rom) {
664 current_system->resume_context(current_system);
665 }
623 } else { 666 } else {
624 break; 667 break;
625 } 668 }
626 } 669 }
627 670