comparison tools/ppl.c @ 2565:d56a0e86067a

Improve error handling.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 28 Feb 2022 11:49:58 +0200
parents aacf3bd1cceb
children 9807ae37ad69
comparison
equal deleted inserted replaced
2564:2cf4e995b50c 2565:d56a0e86067a
548 548
549 int main(int argc, char *argv[]) 549 int main(int argc, char *argv[])
550 { 550 {
551 BOOL initSDL = FALSE, audioInit = FALSE; 551 BOOL initSDL = FALSE, audioInit = FALSE;
552 DMResource *file = NULL; 552 DMResource *file = NULL;
553 int res = -1; 553 int res = DMERR_OK;
554 BOOL muteState = FALSE; 554 BOOL muteState = FALSE;
555 555
556 memset(&eng, 0, sizeof(eng)); 556 memset(&eng, 0, sizeof(eng));
557 557
558 eng.optScrWidth = 640; 558 eng.optScrWidth = 640;
562 dmInitProg("CBP", "Cyrbe Basci Player", "0.2", NULL, NULL); 562 dmInitProg("CBP", "Cyrbe Basci Player", "0.2", NULL, NULL);
563 563
564 // Parse arguments 564 // Parse arguments
565 if (!dmArgsProcess(argc, argv, optList, optListN, 565 if (!dmArgsProcess(argc, argv, optList, optListN,
566 argHandleOpt, argHandleFile, OPTH_BAILOUT)) 566 argHandleOpt, argHandleFile, OPTH_BAILOUT))
567 exit(1); 567 goto out;
568 568
569 // Open the files 569 // Open the files
570 if (optFilename == NULL || argc < 2) 570 if (optFilename == NULL || argc < 2)
571 { 571 {
572 argShowHelp(); 572 argShowHelp();
573 return 1; 573 res = dmError(DMERR_INVALID_ARGS,
574 "No filename specified.\n");
575 goto out;
574 } 576 }
575 577
576 if ((res = dmf_open_stdio(optFilename, "rb", &file)) != DMERR_OK) 578 if ((res = dmf_open_stdio(optFilename, "rb", &file)) != DMERR_OK)
577 { 579 {
578 dmErrorMsg("Error opening file '%s': %s\n", 580 dmErrorMsg("Error opening file '%s': %s\n",
579 optFilename, dmErrorStr(res)); 581 optFilename, dmErrorStr(res));
580 return 1; 582 goto out;
581 } 583 }
582 584
583 // Initialize miniJSS 585 // Initialize miniJSS
584 jssInit(); 586 jssInit();
585 587
614 // Check for errors, we still might have some data tho 616 // Check for errors, we still might have some data tho
615 if (res != DMERR_OK) 617 if (res != DMERR_OK)
616 { 618 {
617 dmErrorMsg("Error loading module file: %s\n", 619 dmErrorMsg("Error loading module file: %s\n",
618 dmErrorStr(res)); 620 dmErrorStr(res));
619 goto exit; 621 goto out;
620 } 622 }
621 623
622 // Check if we have anything 624 // Check if we have anything
623 if (eng.mod == NULL) 625 if (eng.mod == NULL)
624 { 626 {
625 res = dmError(DMERR_INIT_FAIL, 627 res = dmError(DMERR_INIT_FAIL,
626 "Could not load module file.\n"); 628 "Could not load module file.\n");
627 goto exit; 629 goto out;
628 } 630 }
629 631
630 // Try to convert it 632 // Try to convert it
631 if ((res = jssConvertModuleForPlaying(eng.mod)) != DMERR_OK) 633 if ((res = jssConvertModuleForPlaying(eng.mod)) != DMERR_OK)
632 { 634 {
633 dmErrorMsg("Could not convert module for playing: %s\n", 635 dmErrorMsg("Could not convert module for playing: %s\n",
634 dmErrorStr(res)); 636 dmErrorStr(res));
635 goto exit; 637 goto out;
636 } 638 }
637 639
638 640
639 // Initialize SDL components 641 // Initialize SDL components
640 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0) 642 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_TIMER) != 0)
641 { 643 {
642 res = dmError(DMERR_INIT_FAIL, 644 res = dmError(DMERR_INIT_FAIL,
643 "Could not initialize SDL: %s\n", 645 "Could not initialize SDL: %s\n",
644 SDL_GetError()); 646 SDL_GetError());
645 goto exit; 647 goto out;
646 } 648 }
647 initSDL = TRUE; 649 initSDL = TRUE;
648 650
649 651
650 // Initialize mixing device 652 // Initialize mixing device
654 eng.dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO); 656 eng.dev = jvmInit(optOutFormat, optOutChannels, optOutFreq, JMIX_AUTO);
655 if (eng.dev == NULL) 657 if (eng.dev == NULL)
656 { 658 {
657 res = dmError(DMERR_INIT_FAIL, 659 res = dmError(DMERR_INIT_FAIL,
658 "jvmInit() returned NULL\n"); 660 "jvmInit() returned NULL\n");
659 goto exit; 661 goto out;
660 } 662 }
661 663
662 switch (optOutFormat) 664 switch (optOutFormat)
663 { 665 {
664 case JSS_AUDIO_S16: eng.afmt.format = AUDIO_S16SYS; break; 666 case JSS_AUDIO_S16: eng.afmt.format = AUDIO_S16SYS; break;
667 case JSS_AUDIO_U8: eng.afmt.format = AUDIO_U8; break; 669 case JSS_AUDIO_U8: eng.afmt.format = AUDIO_U8; break;
668 default: 670 default:
669 res = dmError(DMERR_NOT_SUPPORTED, 671 res = dmError(DMERR_NOT_SUPPORTED,
670 "Unsupported audio format %d (could not set matching SDL format)\n", 672 "Unsupported audio format %d (could not set matching SDL format)\n",
671 optOutFormat); 673 optOutFormat);
672 goto exit; 674 goto out;
673 } 675 }
674 676
675 eng.afmt.freq = optOutFreq; 677 eng.afmt.freq = optOutFreq;
676 eng.afmt.channels = optOutChannels; 678 eng.afmt.channels = optOutChannels;
677 eng.afmt.samples = optOutFreq / 16; 679 eng.afmt.samples = optOutFreq / 16;
682 if (SDL_OpenAudio(&eng.afmt, NULL) < 0) 684 if (SDL_OpenAudio(&eng.afmt, NULL) < 0)
683 { 685 {
684 res = dmError(DMERR_INIT_FAIL, 686 res = dmError(DMERR_INIT_FAIL,
685 "Couldn't open SDL audio: %s\n", 687 "Couldn't open SDL audio: %s\n",
686 SDL_GetError()); 688 SDL_GetError());
687 goto exit; 689 goto out;
688 } 690 }
689 audioInit = TRUE; 691 audioInit = TRUE;
690 692
691 // Initialize player 693 // Initialize player
692 if ((eng.plr = jmpInit(eng.dev)) == NULL) 694 if ((eng.plr = jmpInit(eng.dev)) == NULL)
693 { 695 {
694 res = dmError(DMERR_INIT_FAIL, 696 res = dmError(DMERR_INIT_FAIL,
695 "jmpInit() returned NULL\n"); 697 "jmpInit() returned NULL\n");
696 goto exit; 698 goto out;
697 } 699 }
698 700
699 jvmSetCallback(eng.dev, jmpExec, eng.plr); 701 jvmSetCallback(eng.dev, jmpExec, eng.plr);
700 jmpSetModule(eng.plr, eng.mod); 702 jmpSetModule(eng.plr, eng.mod);
701 jmpPlayOrder(eng.plr, optStartOrder); 703 jmpPlayOrder(eng.plr, optStartOrder);
716 res = dmf_open_memio(NULL, engineFontName, engineSetupFont, sizeof(engineSetupFont), &file); 718 res = dmf_open_memio(NULL, engineFontName, engineSetupFont, sizeof(engineSetupFont), &file);
717 if (res != DMERR_OK) 719 if (res != DMERR_OK)
718 { 720 {
719 dmErrorMsg("Error opening font file '%s': %s\n", 721 dmErrorMsg("Error opening font file '%s': %s\n",
720 engineFontName, dmErrorStr(res)); 722 engineFontName, dmErrorStr(res));
721 goto exit; 723 goto out;
722 } 724 }
723 res = dmLoadBitmapFont(file, &font); 725 res = dmLoadBitmapFont(file, &font);
724 dmf_close(file); 726 dmf_close(file);
725 if (res != DMERR_OK) 727 if (res != DMERR_OK)
726 { 728 {
727 dmErrorMsg("Could not load font data from '%s': %s\n", 729 dmErrorMsg("Could not load font data from '%s': %s\n",
728 engineFontName, dmErrorStr(res)); 730 engineFontName, dmErrorStr(res));
729 goto exit; 731 goto out;
730 } 732 }
731 733
732 SDL_Color pal[DMFONT_NPALETTE]; 734 SDL_Color pal[DMFONT_NPALETTE];
733 for (int n = 0; n < DMFONT_NPALETTE; n++) 735 for (int n = 0; n < DMFONT_NPALETTE; n++)
734 { 736 {
746 )) == NULL) 748 )) == NULL)
747 { 749 {
748 res = dmError(DMERR_INIT_FAIL, 750 res = dmError(DMERR_INIT_FAIL,
749 "Can't create an SDL window: %s\n", 751 "Can't create an SDL window: %s\n",
750 SDL_GetError()); 752 SDL_GetError());
751 goto exit; 753 goto out;
752 } 754 }
753 755
754 SDL_SetWindowTitle(eng.window, dmProgDesc); 756 SDL_SetWindowTitle(eng.window, dmProgDesc);
755 757
756 if ((eng.renderer = SDL_CreateRenderer(eng.window, -1, SDL_RENDERER_PRESENTVSYNC)) == NULL) 758 if ((eng.renderer = SDL_CreateRenderer(eng.window, -1, SDL_RENDERER_PRESENTVSYNC)) == NULL)
757 { 759 {
758 res = dmError(DMERR_INIT_FAIL, 760 res = dmError(DMERR_INIT_FAIL,
759 "Can't create an SDL renderer: %s\n", 761 "Can't create an SDL renderer: %s\n",
760 SDL_GetError()); 762 SDL_GetError());
761 goto exit; 763 goto out;
762 } 764 }
763 765
764 if (!dmInitializeVideo()) 766 if (!dmInitializeVideo())
765 { 767 {
766 res = DMERR_INIT_FAIL; 768 res = DMERR_INIT_FAIL;
767 goto exit; 769 goto out;
768 } 770 }
769 771
770 //SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best"); 772 //SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "best");
771 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL); 773 //SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);
772 } 774 }
852 break; 854 break;
853 855
854 case SDLK_f: 856 case SDLK_f:
855 eng.optVFlags ^= SDL_WINDOW_FULLSCREEN_DESKTOP; 857 eng.optVFlags ^= SDL_WINDOW_FULLSCREEN_DESKTOP;
856 if (SDL_SetWindowFullscreen(eng.window, eng.optVFlags) != 0) 858 if (SDL_SetWindowFullscreen(eng.window, eng.optVFlags) != 0)
857 goto exit; 859 goto out;
858 needUpdate = TRUE; 860 needUpdate = TRUE;
859 break; 861 break;
860 862
861 default: 863 default:
862 break; 864 break;
873 875
874 case SDL_WINDOWEVENT_RESIZED: 876 case SDL_WINDOWEVENT_RESIZED:
875 eng.optScrWidth = eng.event.window.data1; 877 eng.optScrWidth = eng.event.window.data1;
876 eng.optScrHeight = eng.event.window.data2; 878 eng.optScrHeight = eng.event.window.data2;
877 if (!dmInitializeVideo()) 879 if (!dmInitializeVideo())
878 goto exit; 880 goto out;
879 881
880 needUpdate = TRUE; 882 needUpdate = TRUE;
881 break; 883 break;
882 } 884 }
883 break; 885 break;
968 } // optUseGUI 970 } // optUseGUI
969 971
970 SDL_Delay(eng.pauseFlag ? 100 : 30); 972 SDL_Delay(eng.pauseFlag ? 100 : 30);
971 } 973 }
972 974
973 exit: 975 out:
974 // Cleanup 976 // Cleanup
975 if (optUseGUI) 977 if (optUseGUI)
976 { 978 {
977 dmMsg(1, "GUI shutdown.\n"); 979 dmMsg(1, "GUI shutdown.\n");
978 if (eng.texture != NULL) 980 if (eng.texture != NULL)