}
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) {
-Subproject commit 7b11a95b82052299c2844578034b6336c051593a
+Subproject commit 47569fbe806a5138763e7ef3447dca254357dff8
}
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;
}
#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) {
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");
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();
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);
}
if (xpm) free(xpm);
- UnloadTexture(texture);
+ UnloadTexture(*texture);
CloseWindow();
return EXIT_SUCCESS;
#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"
"\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);
}
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;
-int options(int argc, char **argv, int *debug, char **pathp);
+extern int debug;
+extern char *xpm;
+
+int options(int argc, char **argv);
#define TMPSRC TMP ".c"
#define TMPLIB "/tmp/libxpm" DYEXT
+Image image;
+
static char *strnsub(char *str, char *sub, size_t l) {
size_t subl;
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);
cpp = strtol(p, &p, 10);
if (1 + ncolors + height > *sizep) {
warnx("Actual image height too short");
- return result;
+ return;
}
// Colors
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;
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);
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;
}
DEFAULT = MODEC,
};
-Texture gettexture(char *path, Image *image, int mode);
+extern Image image;
+
+Texture2D *reloadtexture(char *xpm, int mode);