ref: e6a5e66f150b06da6a4f539831149f3174a2c67f
dir: /images.c/
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <ctype.h>
#include "dat.h"
#include "fns.h"
static int
isnumeric(char *name)
{
while (*name && isdigit(*name))
name++;
return *name == 0;
}
Image *images[4*15];
static Image*
loadfromfile(char *path, int num)
{
char *file;
int fd;
Image *img;
file = smprint("%s/%d", path, num);
if (!file)
sysfatal("smprint: %r");
fd = open(file, OREAD);
if (fd < 0)
sysfatal("open file %s: %r", file);
free(file);
img = readimage(display, fd, 0);
close(fd);
if (!img)
sysfatal("readimage: %r");
return img;
}
void
loadimages(char *path)
{
int i, n, a;
Dir *d;
a = open(path, OREAD);
if (a < 0)
sysfatal("open: %r");
n = dirreadall(a, &d);
close(a);
memset(images, 0, sizeof images);
for (i = 0; i < n; i++) {
if (!isnumeric(d[i].name))
continue;
a = atoi(d[i].name);
if (a >= nelem(images))
continue;
images[a] = loadfromfile(path, a);
}
free(d);
}
Image*
getimage(int num)
{
Image *img;
if (num >= nelem(images)) {
werrstr("missing image: %d\n", num);
return nil;
}
img = images[num];
if (img)
return img;
werrstr("missing image: %d\n", num);
return nil;
}