changeset 2200:dcd26cdc395e

Replace dmC64InitializeFormats() with dmLib64GFXInit() and dmLib64GFXClose(). Add in sorting of the C64 formats list.
author Matti Hamalainen <ccr@tnsp.org>
date Fri, 14 Jun 2019 00:00:27 +0300
parents f331cc750b37
children 9f3fb4004c20
files tools/64vw.c tools/gfxconv.c tools/lib64gfx.c tools/lib64gfx.h
diffstat 4 files changed, 70 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/tools/64vw.c	Thu Jun 13 23:59:05 2019 +0300
+++ b/tools/64vw.c	Fri Jun 14 00:00:27 2019 +0300
@@ -61,7 +61,7 @@
     );
     for (int i = 0; i < ndmC64ImageFormats; i++)
     {
-        const DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
+        const DMC64ImageFormat *fmt = dmC64ImageFormatsSorted[i];
         char buf[64];
         printf("%-6s| %-15s | %s%s\n",
             fmt->fext,
@@ -269,7 +269,14 @@
     size_t currIndex, prevIndex;
     int res;
 
-    dmC64InitializeFormats();
+    // Initialize pre-requisites
+    if ((res = dmLib64GFXInit()) != DMERR_OK)
+    {
+        dmErrorMsg("Could not initialize lib64gfx: %s\n",
+            dmErrorStr(res));
+        goto exit;
+    }
+
     dmSetScaleFactor(2.0);
     memset(&spec, 0, sizeof(spec));
     memset(&setCharROM, 0, sizeof(setCharROM));
@@ -599,5 +606,7 @@
     if (initSDL)
         SDL_Quit();
 
+    dmLib64GFXClose();
+
     return 0;
 }
--- a/tools/gfxconv.c	Thu Jun 13 23:59:05 2019 +0300
+++ b/tools/gfxconv.c	Fri Jun 14 00:00:27 2019 +0300
@@ -204,7 +204,7 @@
 
     for (int i = 0; i < ndmC64ImageFormats; i++)
     {
-        const DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
+        const DMC64ImageFormat *fmt = dmC64ImageFormatsSorted[i];
         char buf[64];
         printf("%-6s| %c%c | %-15s | %s%s\n",
             fmt->fext,
@@ -1930,7 +1930,13 @@
     memset(&imageSpecC64, 0, sizeof(imageSpecC64));
 
     // Initialize list of additional conversion formats
-    dmC64InitializeFormats();
+    if ((res = dmLib64GFXInit()) != DMERR_OK)
+    {
+        dmErrorMsg("Could not initialize lib64gfx: %s\n",
+            dmErrorStr(res));
+        goto exit;
+    }
+
     nconvFormatList = ndmImageFormatList + nbaseFormatList;
     convFormatList = dmCalloc(nconvFormatList, sizeof(DMConvFormat));
 
@@ -2314,6 +2320,7 @@
     dmC64ImageFree(outC64Image);
     dmImageFree(inImage);
     dmImageFree(outImage);
+    dmLib64GFXClose();
 
     return 0;
 }
--- a/tools/lib64gfx.c	Thu Jun 13 23:59:05 2019 +0300
+++ b/tools/lib64gfx.c	Fri Jun 14 00:00:27 2019 +0300
@@ -1702,12 +1702,53 @@
 }
 
 
-void dmC64InitializeFormats(void)
+BOOL             dmLib64GFXInitialized = FALSE;
+DMC64ImageFormat **dmC64ImageFormatsSorted = NULL;
+
+
+int dmC64ImageFormatCompare(const void *va, const void *vb)
 {
+    const DMC64ImageFormat
+        *fmta = *(DMC64ImageFormat **) va,
+        *fmtb = *(DMC64ImageFormat **) vb;
+
+    int res = fmta->format->type - fmtb->format->type;
+    if (res == 0)
+        return strcmp(fmta->name, fmtb->name);
+    else
+        return res;
+}
+
+
+int dmLib64GFXInit(void)
+{
+    // Safety check
+    if (dmLib64GFXInitialized)
+        dmLib64GFXClose();
+
+    dmLib64GFXInitialized = TRUE;
+
+    if ((dmC64ImageFormatsSorted = dmCalloc(ndmC64ImageFormats, sizeof(dmC64ImageFormatsSorted[0]))) == NULL)
+        return DMERR_MALLOC;
+
     for (int i = 0; i < ndmC64ImageFormats; i++)
     {
         DMC64ImageFormat *fmt = &dmC64ImageFormats[i];
         if (fmt->format == NULL)
             fmt->format = &fmt->formatDef;
+
+        dmC64ImageFormatsSorted[i] = fmt;
     }
+
+    qsort(dmC64ImageFormatsSorted, ndmC64ImageFormats,
+        sizeof(DMC64ImageFormat *), dmC64ImageFormatCompare);
+
+    return DMERR_OK;
 }
+
+
+void dmLib64GFXClose(void)
+{
+    dmFreeR(&dmC64ImageFormatsSorted);
+    dmLib64GFXInitialized = FALSE;
+}
--- a/tools/lib64gfx.h	Thu Jun 13 23:59:05 2019 +0300
+++ b/tools/lib64gfx.h	Fri Jun 14 00:00:27 2019 +0300
@@ -339,12 +339,19 @@
 extern const int         ndmC64DefaultPalettes;
 extern DMC64ImageFormat  dmC64ImageFormats[];
 extern const int         ndmC64ImageFormats;
+extern DMC64ImageFormat  **dmC64ImageFormatsSorted;
+
+
+//
+// Library init/close
+//
+int       dmLib64GFXInit(void);
+void      dmLib64GFXClose(void);
 
 
 //
 // Miscellaneous functions
 //
-void      dmC64InitializeFormats(void);
 int       dmC64ProbeBMP(const DMGrowBuf *buf, const DMC64ImageFormat **fmt);
 
 char *    dmC64GetImageTypeString(char *buf, const size_t len, const int type, const BOOL lng);