]> Trent Huber's Code - xpmview.git/commitdiff
Minor refactoring and tweaks
authorTrent Huber <trentmhuber@gmail.com>
Fri, 25 Jul 2025 09:24:05 +0000 (05:24 -0400)
committerTrent Huber <trentmhuber@gmail.com>
Fri, 25 Jul 2025 09:24:05 +0000 (05:24 -0400)
external/build.c
external/cbs
src/build.c
src/main.c
src/options.c
src/options.h
src/xpm.c
src/xpm.h

index 1c211723e05d047b3f9db14a7c8a7ad468936ce6..2633ded83bf881ce22b7235f076e5e3f08407d92 100644 (file)
@@ -40,24 +40,20 @@ void buildcbs(void) {
 }
 
 void buildraylib(void) {
-   struct cbsfile *files;
+   buildfiles((struct cbsfile []){
+               {RLLIB, LLRAYLIB, LIBTYPE},
 
-   files = (struct cbsfile []){
-       {RLLIB, LLRAYLIB, LIBTYPE},
+               {RLSRC "raudio", LIST(CFGENERAL)},
+               {RLSRC "rcore", LIST(CFGLFW, CFGENERAL)},
+               {RLSRC "rglfw", LIST(CFGLFW, CFGRAPHICS, CFGENERAL)},
+               {RLSRC "rmodels", LIST(CFGENERAL)},
+               {RLSRC "rshapes", LIST(CFGENERAL)},
+               {RLSRC "rtext", LIST(CFGENERAL)},
+               {RLSRC "rtextures", LIST(CFGENERAL)},
+               {RLSRC "utils", LIST(CFGENERAL)},
 
-       {RLSRC "raudio", LIST(CFGENERAL)},
-       {RLSRC "rcore", LIST(CFGLFW, CFGENERAL)},
-       {RLSRC "rglfw", LIST(CFGLFW, CFGRAPHICS, CFGENERAL)},
-       {RLSRC "rmodels", LIST(CFGENERAL)},
-       {RLSRC "rshapes", LIST(CFGENERAL)},
-       {RLSRC "rtext", LIST(CFGENERAL)},
-       {RLSRC "rtextures", LIST(CFGENERAL)},
-       {RLSRC "utils", LIST(CFGENERAL)},
-
-       {NULL}
-   };
-
-   buildfiles(files);
+               {NULL}
+              });
 }
 
 int main(void) {
index 7b11a95b82052299c2844578034b6336c051593a..47569fbe806a5138763e7ef3447dca254357dff8 160000 (submodule)
@@ -1 +1 @@
-Subproject commit 7b11a95b82052299c2844578034b6336c051593a
+Subproject commit 47569fbe806a5138763e7ef3447dca254357dff8
index 7046058d3fe236829f99fe24aa24477775d25fc0..9d002898d2951796d99d19a873b759f82454034e 100644 (file)
@@ -109,28 +109,24 @@ static void buildfont(void) {
 }
 
 int main(void) {
-   struct cbsfile *files;
-
    build("./");
 
    buildcolors();
    buildfont();
 
-   files = (struct cbsfile []){
-       {"../bin/simplexpm", LLRAYLIB, 'x'},
-
-       {"colors", NONE},
-       {"main", CLRAYLIB},
-       {"options", NONE},
-       {"xpm", CLRAYLIB},
+   buildfiles((struct cbsfile []){
+               {"../bin/simplexpm", LLRAYLIB, 'x'},
 
-       {CBSLIB},
-       {RLLIB},
+               {"colors", NONE},
+               {"main", CLRAYLIB},
+               {"options", NONE},
+               {"xpm", CLRAYLIB},
 
-       {NULL}
-   };
+               {CBSLIB},
+               {RLLIB},
 
-   buildfiles(files);
+               {NULL}
+              });
 
    return EXIT_SUCCESS;
 }
index 265fc30935d3873901903e3a11caba67dafc4fe9..d1d4ba7a5c4ba888c0f38e0445580779ce88506b 100644 (file)
 
 #include "font.c"
 
-static void getinput(char **xp, Texture2D *tp, Image *ip) {
+static Texture2D *texture;
+
+static void handleinput(void) {
    FilePathList files;
-   static int mode = DEFAULT;
    KeyboardKey key;
    size_t len;
+   static int mode = DEFAULT;
 
    if (IsFileDropped()) {
        files = LoadDroppedFiles();
-       if (*xp) RL_FREE(*xp);
-       *xp = allocate(FILENAME_MAX);
-       strcpy(*xp, files.paths[0]);
+       if (xpm) RL_FREE(xpm);
+       xpm = allocate(FILENAME_MAX);
+       strcpy(xpm, files.paths[0]);
        UnloadDroppedFiles(files);
-       *tp = gettexture(*xp, ip, mode);
+       texture = reloadtexture(xpm, mode);
    } else switch ((key = GetKeyPressed())) {
    case KEY_R:
-       if (*xp) *tp = gettexture(*xp, ip, mode);
+       if (xpm) texture = reloadtexture(xpm, mode);
    case KEY_NULL:
        break;
    case KEY_S:
-       if (tp->id == 0) break;
-       len = strlen(*xp);
-       strncpy(*xp + len - 4, ".png", 4);
-       ExportImage(*ip, *xp);
-       strncpy(*xp + len - 4, ".xpm", 4);
+       if (!texture) break;
+       len = strlen(xpm);
+       strncpy(xpm + len - 4, ".png", 4);
+       ExportImage(image, xpm);
+       strncpy(xpm + len - 4, ".xpm", 4);
        break;
    default:
        switch (key) {
@@ -52,21 +54,18 @@ static void getinput(char **xp, Texture2D *tp, Image *ip) {
        default:
            return;
        }
-       if (*xp) *tp = gettexture(NULL, ip, mode);
+       if (xpm) texture = reloadtexture(NULL, mode);
    }
 }
 
 int main(int argc, char **argv) {
-   int debug;
-   char *xpm, *welcome, *error;
-   Image image;
-   Texture2D texture;
    Font font;
+   char *welcome, *error;
    size_t width, height;
    float scale;
    Vector2 pos, dim;
 
-   if (!options(argc, argv, &debug, &xpm)) return EXIT_FAILURE;
+   if (!options(argc, argv)) return EXIT_FAILURE;
 
    if (!debug) SetTraceLogLevel(LOG_ERROR);
    InitWindow(800, 600, "simplexpm");
@@ -74,14 +73,13 @@ int main(int argc, char **argv) {
    SetTargetFPS(30);
    SetExitKey(KEY_Q);
 
-   image = (Image){0};
-   texture = gettexture(xpm, &image, DEFAULT);
-   if (xpm && texture.id == 0) return EXIT_FAILURE;
+   texture = reloadtexture(xpm, DEFAULT);
+   if (xpm && !texture) return EXIT_FAILURE;
    font = LoadFont_Font();
    welcome = "Drag and drop an XPM file here";
    error = "Unable to parse XPM file:\n see console for details";
    while (!WindowShouldClose()) {
-       getinput(&xpm, &texture, &image);
+       handleinput();
 
        BeginDrawing();
 
@@ -89,21 +87,21 @@ int main(int argc, char **argv) {
 
        width = GetScreenWidth();
        height = GetScreenHeight();
-       if (texture.id) {
-           scale = width * texture.height > height * texture.width
-                   ? (float)height / texture.height
-                   : (float)width / texture.width;
+       if (texture) {
+           scale = width * texture->height > height * texture->width
+                   ? (float)height / texture->height
+                   : (float)width / texture->width;
            pos = CLITERAL(Vector2){
-               (width - texture.width * scale) / 2,
-               (height - texture.height * scale) / 2,
-           };
-           DrawTextureEx(texture, pos, 0, scale, WHITE);
+                   (width - texture->width * scale) / 2,
+                   (height - texture->height * scale) / 2,
+                 };
+           DrawTextureEx(*texture, pos, 0, scale, WHITE);
        } else {
            dim = MeasureTextEx(font, xpm ? error : welcome, font.baseSize, 0);
            pos = (Vector2){
-               .x = (width - dim.x) / 2,
-               .y = (height - dim.y) / 2,
-           };
+                   .x = (width - dim.x) / 2,
+                   .y = (height - dim.y) / 2,
+                 };
            DrawTextEx(font, xpm ? error : welcome, pos, font.baseSize, 0, BLACK);
        }
 
@@ -112,7 +110,7 @@ int main(int argc, char **argv) {
 
    if (xpm) free(xpm);
 
-   UnloadTexture(texture);
+   UnloadTexture(*texture);
    CloseWindow();
 
    return EXIT_SUCCESS;
index e985db6ecfe5e1dbfe99b560955a3ac0514bcf8f..f8516ccf9f53bcc11458c041c9c3d1f1d52a3c98 100644 (file)
@@ -7,6 +7,9 @@
 
 #include "cbs.h"
 
+int debug;
+char *xpm;
+
 static void usage(char *prog, int fd) {
    dprintf(fd, "Usage: %s [-dh] [-f file]\n"
            "\t-d        Show debug messages\n"
@@ -22,18 +25,16 @@ static void usage(char *prog, int fd) {
            "\t c        Color visual mode\n", prog);
 }
 
-int options(int argc, char **argv, int *debug, char **xp) {
+int options(int argc, char **argv) {
    int opt, result, dnfd;
 
-   *debug = 0;
-   *xp = NULL;
    while ((opt = getopt(argc, argv, "df:h")) != -1) switch (opt) {
    case 'd':
-       *debug = 1;
+       debug = 1;
        break;
    case 'f':
-       *xp = allocate(FILENAME_MAX);
-       strcpy(*xp, optarg);
+       xpm = allocate(FILENAME_MAX);
+       strcpy(xpm, optarg);
        break;
    case 'h':
        usage(argv[0], STDOUT_FILENO);
@@ -45,7 +46,7 @@ int options(int argc, char **argv, int *debug, char **xp) {
    }
 
    result = 1;
-   if (!*debug) {
+   if (!debug) {
        if ((dnfd = open("/dev/null", O_WRONLY)) == -1) {
            warn("Unable to open `/dev/null'; showing debug messages");
            return result;
index 02491d5dc0c7d649b58c14773334e2ae32e18946..cf9711c39d8fc7a3246de7b722a818980b1c44d8 100644 (file)
@@ -1 +1,4 @@
-int options(int argc, char **argv, int *debug, char **pathp);
+extern int debug;
+extern char *xpm;
+
+int options(int argc, char **argv);
index d3ca7219837bb378ed1118ebd0dbff453afca72f..fe0bf0b1cf8264a46904cf749db3e09e30a6d281 100644 (file)
--- a/src/xpm.c
+++ b/src/xpm.c
@@ -18,6 +18,8 @@
 #define TMPSRC TMP ".c"
 #define TMPLIB "/tmp/libxpm" DYEXT
 
+Image image;
+
 static char *strnsub(char *str, char *sub, size_t l) {
    size_t subl;
 
@@ -138,15 +140,13 @@ static unsigned int str2color(char **strp) {
    return r;
 }
 
-static Image parse(char **data, long *sizep) {
-   Image result;
+static void parse(char **data, long *sizep) {
    char *p, *chars, **pp;
    long width, height, ncolors, cpp, l;
    unsigned int *colors, color, *pixels;
    int i, mode, j, k, m;
 
    // Values
-   result = (Image){0};
    p = data[0];
    width = strtol(p, &p, 10);
    height = strtol(p, &p, 10);
@@ -154,7 +154,7 @@ static Image parse(char **data, long *sizep) {
    cpp = strtol(p, &p, 10);
    if (1 + ncolors + height > *sizep) {
        warnx("Actual image height too short");
-       return result;
+       return;
    }
 
    // Colors
@@ -192,28 +192,21 @@ static Image parse(char **data, long *sizep) {
                if (strncmp(p, chars + k * cpp, cpp) == 0)
                    for (m = 0; m < NUMMODES; ++m)
                        pixels[m * width * height + i * width + j] = colors[m * ncolors + k];
-   if (j != width || l != 0) {
-       warnx("Actual image width too narrow");
-       goto free;
-   }
-
-   result = (Image){
-       .data = pixels,
-       .width = width,
-       .height = height,
-       .mipmaps = 1,
-       .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
-   };
+   if (j != width || l != 0) warnx("Actual image width too narrow");
+   else image = (Image){
+                   .data = pixels,
+                   .width = width,
+                   .height = height,
+                   .mipmaps = 1,
+                   .format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8,
+                };
 
 free:
    free(chars);
    free(colors);
-
-   return result;
 }
 
-static Image process(char *xpm) {
-   Image result;
+static void reloadimage(char *xpm) {
    int xpmfd, srcfd, e, cpid, status;
    struct stat xstat;
    size_t l, offset;
@@ -221,10 +214,10 @@ static Image process(char *xpm) {
    void *d;
    long *sizep;
 
-   result = (Image){0};
+   image = (Image){0};
    if ((xpmfd = open(xpm, O_RDONLY)) == -1) {
        warn("Unable to open `%s'", xpm);
-       return result;
+       return;
    }
    if (stat(xpm, &xstat) == -1) {
        warn("Unable to stat `%s'", xpm);
@@ -298,47 +291,48 @@ static Image process(char *xpm) {
        goto dlclose;
    }
 
-   result = parse(data, sizep);
+   parse(data, sizep);
 
 dlclose:
    if (dlclose(d)) {
        warnx("Unable to unload `%s': %s", lib, dlerror());
-       result.mipmaps = 0;
+       image.mipmaps = 0;
    }
 
 munmap:
    if (munmap(map, l) == -1) {
        warn("Unable to unmap `%s' from memory", xpm);
-       result.mipmaps = 0;
+       image.mipmaps = 0;
    }
 
 close:
    if (close(xpmfd) == -1) {
        warn("Unable to close `%s'", xpm);
-       result.mipmaps = 0;
+       image.mipmaps = 0;
    }
 
-   if (result.data && !result.mipmaps) {
-       free(result.data);
-       result = (Image){0};
+   if (image.data && !image.mipmaps) {
+       free(image.data);
+       image = (Image){0};
    }
-
-   return result;
 }
 
-Texture gettexture(char *xpm, Image *image, int mode) {
+Texture2D *reloadtexture(char *xpm, int mode) {
    static unsigned int *base;
    static Texture2D texture;
 
    if (xpm) {
-       *image = process(xpm);
+       reloadimage(xpm);
        if (base) free(base);
-       base = image->data;
+       base = image.data;
    }
 
-   if (!base) return (Texture2D){0};
+   if (!base) return NULL;
    
-   image->data = base + mode * image->width * image->height;
+   image.data = base + mode * image.width * image.height;
+
    UnloadTexture(texture);
-   return texture = LoadTextureFromImage(*image);
+   texture = LoadTextureFromImage(image);
+
+   return &texture;
 }
index 3b1ffaec5561b4e7f49996a17c104adf05a158a8..991a631105c1b16970682570d8986e77a421d95f 100644 (file)
--- a/src/xpm.h
+++ b/src/xpm.h
@@ -8,4 +8,6 @@ enum {
    DEFAULT = MODEC,
 };
 
-Texture gettexture(char *path, Image *image, int mode);
+extern Image image;
+
+Texture2D *reloadtexture(char *xpm, int mode);