60 lines
1.4 KiB
C++
60 lines
1.4 KiB
C++
#include "images.h"
|
|
#include <map>
|
|
#include <stdio.h>
|
|
#include <stdint.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
namespace images
|
|
{
|
|
std::map<std::string, draw::surface*> images;
|
|
|
|
draw::surface *getImage(std::string filename)
|
|
{
|
|
auto item = images.find(filename);
|
|
if (item != images.end()) return item->second;
|
|
|
|
draw::surface *image = draw::loadSurface(filename.c_str());
|
|
images[filename] = image;
|
|
return image;
|
|
}
|
|
|
|
int loadPalette(std::string filename)
|
|
{
|
|
uint32_t palette[16];
|
|
|
|
FILE *file = fopen(filename.c_str(), "r");
|
|
if (!file) return -1;
|
|
|
|
char header[32];
|
|
int version, count;
|
|
|
|
// Read and validate header
|
|
if (!fgets(header, sizeof(header), file) || strncmp(header, "JASC-PAL", 8) != 0) {
|
|
fclose(file);
|
|
return -2;
|
|
}
|
|
|
|
if (fscanf(file, "%d\n%d\n", &version, &count) != 2 || count != 16) {
|
|
fclose(file);
|
|
return -3;
|
|
}
|
|
|
|
// Read 16 RGB triplets
|
|
for (int i = 0; i < 16; ++i) {
|
|
int r, g, b;
|
|
if (fscanf(file, "%d %d %d\n", &r, &g, &b) != 3) {
|
|
fclose(file);
|
|
return -4;
|
|
}
|
|
palette[i] = (r << 16) | (g << 8) | b;
|
|
}
|
|
|
|
fclose(file);
|
|
|
|
draw::setPalette(palette, 16);
|
|
|
|
return 0;
|
|
}
|
|
}
|