From 1bc4c29c89785dbf6af11615bbeaed19ba2729b8 Mon Sep 17 00:00:00 2001 From: Trent Huber Date: Fri, 25 Jul 2025 05:24:05 -0400 Subject: [PATCH] Minor refactoring and tweaks --- external/build.c | 28 +++++++++----------- external/cbs | 2 +- src/build.c | 24 +++++++---------- src/main.c | 66 +++++++++++++++++++++++----------------------- src/options.c | 15 ++++++----- src/options.h | 5 +++- src/xpm.c | 68 ++++++++++++++++++++++-------------------------- src/xpm.h | 4 ++- 8 files changed, 101 insertions(+), 111 deletions(-) diff --git a/external/build.c b/external/build.c index 1c21172..2633ded 100644 --- a/external/build.c +++ b/external/build.c @@ -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) { diff --git a/external/cbs b/external/cbs index 7b11a95..47569fb 160000 --- a/external/cbs +++ b/external/cbs @@ -1 +1 @@ -Subproject commit 7b11a95b82052299c2844578034b6336c051593a +Subproject commit 47569fbe806a5138763e7ef3447dca254357dff8 diff --git a/src/build.c b/src/build.c index 7046058..9d00289 100644 --- a/src/build.c +++ b/src/build.c @@ -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; } diff --git a/src/main.c b/src/main.c index 265fc30..d1d4ba7 100644 --- a/src/main.c +++ b/src/main.c @@ -10,30 +10,32 @@ #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; diff --git a/src/options.c b/src/options.c index e985db6..f8516cc 100644 --- a/src/options.c +++ b/src/options.c @@ -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; diff --git a/src/options.h b/src/options.h index 02491d5..cf9711c 100644 --- a/src/options.h +++ b/src/options.h @@ -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); diff --git a/src/xpm.c b/src/xpm.c index d3ca721..fe0bf0b 100644 --- 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; } diff --git a/src/xpm.h b/src/xpm.h index 3b1ffae..991a631 100644 --- 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); -- 2.51.0