diff tests/plrtest.c @ 2530:aacf3bd1cceb

Cleanups.
author Matti Hamalainen <ccr@tnsp.org>
date Sat, 16 May 2020 06:38:52 +0300
parents 186cf6a7d634
children 9807ae37ad69
line wrap: on
line diff
--- a/tests/plrtest.c	Sat May 16 06:11:53 2020 +0300
+++ b/tests/plrtest.c	Sat May 16 06:38:52 2020 +0300
@@ -106,7 +106,7 @@
     SDL_AudioSpec afmt;
     DMResource *file = NULL;
     char *inFilename = NULL;
-    int result = -1;
+    int res = -1;
     JSSModule *mod = NULL;
     JSSMixer *dev = NULL;
     JSSPlayer *plr = NULL;
@@ -116,63 +116,71 @@
 
     // Open the files
     if (inFilename == NULL)
-        result = dmf_open_stdio_stream(stdin, &file);
+        res = dmf_open_stdio_stream(stdin, &file);
     else
-        result = dmf_open_stdio(inFilename, "rb", &file);
+        res = dmf_open_stdio(inFilename, "rb", &file);
 
-    if (result != DMERR_OK)
+    if (res != DMERR_OK)
     {
-        fprintf(stderr, "Error opening input file '%s', #%d: %s\n",
-            inFilename, result, dmErrorStr(result));
-        return 1;
+        dmErrorMsg("Error opening input file '%s': %s\n",
+            inFilename, dmErrorStr(res));
+        goto exit;
     }
 
     // Initialize miniJSS
-    fprintf(stderr, "Initializing miniJSS\n");
+    printf("Initializing miniJSS\n");
     jssInit();
 
-
     // Read module file
-    dmMsg(1, "Reading file: %s\n", inFilename);
+    printf("Reading file: %s\n", inFilename);
 #ifdef JSS_SUP_XM
-    result = jssLoadXM(file, &mod, TRUE);
+    if (mod == NULL)
+    {
+        printf("* Trying XM...\n");
+        dmfreset(file);
+        if ((res = jssLoadXM(file, &mod, TRUE)) == DMERR_OK)
+        {
+            dmfreset(file);
+            res = jssLoadXM(file, &mod, FALSE);
+        }
+    }
 #endif
 #ifdef JSS_SUP_JSSMOD
-    dmfreset(file);
-    if (result != DMERR_OK)
+    if (mod == NULL)
     {
-        dmMsg(1, "* Trying JSSMOD ...\n");
-        result = jssLoadJSSMOD(file, &mod, TRUE);
+        printf("* Trying JSSMOD ...\n");
         dmfreset(file);
-        if (result == DMERR_OK)
-            result = jssLoadJSSMOD(file, &mod, FALSE);
-    }
-    else
-    {
-        dmMsg(2, "* Trying XM...\n");
-        result = jssLoadXM(file, &mod, FALSE);
+        if ((res = jssLoadJSSMOD(file, &mod, TRUE)) == DMERR_OK)
+        {
+            dmfreset(file);
+            res = jssLoadJSSMOD(file, &mod, FALSE);
+        }
     }
 #endif
     dmf_close(file);
 
     // Check for errors, we still might have some data tho
-    if (result != DMERR_OK)
+    if (res != DMERR_OK)
     {
-        dmErrorMsg("Error loading module file, %d: %s\n",
-            result, dmErrorStr(result));
+        dmErrorMsg(
+            "Error loading module file: %s\n",
+            dmErrorStr(res));
     }
 
     // Check if we have anything
     if (mod == NULL)
-        return 3;
-
+    {
+        res = dmError(DMERR_INIT_FAIL,
+            "Could not load module file.\n");
+        goto exit;
+    }
 
     // Try to convert it
-    if ((result = jssConvertModuleForPlaying(mod)) != DMERR_OK)
+    if ((res = jssConvertModuleForPlaying(mod)) != DMERR_OK)
     {
-        fprintf(stderr, "Could not convert module for playing, %d: %s\n",
-            result, dmErrorStr(result));
-        return 3;
+        dmErrorMsg("Could not convert module for playing: %s\n",
+            dmErrorStr(res));
+        goto exit;
     }
 
     // Initialize SDL audio
@@ -181,14 +189,15 @@
     afmt.channels = 2;
 
     // Initialize mixing device
-    fprintf(stderr, "Initializing miniJSS mixer with: %d, %d, %d\n",
+    printf("Initializing miniJSS mixer with: %d, %d, %d\n",
         JSS_AUDIO_S16, afmt.channels, afmt.freq);
 
     dev = jvmInit(JSS_AUDIO_S16, afmt.channels, afmt.freq, JMIX_AUTO);
     if (dev == NULL)
     {
-        fprintf(stderr, "jvmInit() returned NULL\n");
-        return 3;
+        res = dmError(DMERR_INIT_FAIL,
+            "jvmInit() returned NULL\n");
+        goto exit;
     }
 
     afmt.samples  = afmt.freq / 4;
@@ -196,20 +205,25 @@
     afmt.userdata = (void *) dev;
 
     // Open the audio device
-    fprintf(stderr, "Trying to init SDL with: %d, %d, %d\n",
+    
+    fprintf(stderr,
+        "Trying to init SDL with: %d, %d, %d\n",
         afmt.format, afmt.channels, afmt.freq);
 
     if (SDL_OpenAudio(&afmt, NULL) < 0)
     {
-        fprintf(stderr, "Couldn't open audio: %s\n", SDL_GetError());
-        return 4;
+        res = dmError(DMERR_INIT_FAIL,
+            "Couldn't open audio: %s\n",
+            SDL_GetError());
+        goto exit;
     }
 
     // Initialize player
     if ((plr = jmpInit(dev)) == NULL)
     {
-        fprintf(stderr, "jmpInit() returned NULL\n");
-        return 4;
+        res = dmError(DMERR_INIT_FAIL,
+            "jmpInit() returned NULL\n");
+        goto exit;
     }
 
     // Initialize playing
@@ -252,6 +266,7 @@
 
     printf("----------------------------------------------------\n");
 
+exit:
     SDL_LockAudio();
     SDL_PauseAudio(1);
     jmpClose(plr);
@@ -263,5 +278,5 @@
 
     jssClose();
 
-    return 0;
+    return res;
 }