diff tools/lib64gfx.c @ 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 28871f500e84
children 9f3fb4004c20
line wrap: on
line diff
--- 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;
+}