Part 8: Tile System
This commit is contained in:
16 files changed
+425
-105
No files matched your search
@@ -4,5 +4,7 @@ project(tools)
|
||||
|
||||
set(CMAKE_C_FLAGS "-Wall -Werror")
|
||||
|
||||
add_executable(font_generator src/font_generator.c)
|
||||
add_executable(font_processor src/font_processor.c)
|
||||
add_executable(palette_processor src/palette_processor.c)
|
||||
add_executable(tile_processor src/tile_processor.c)
|
||||
|
||||
File renamed without changes.
@@ -0,0 +1,105 @@
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <input palette> <output header>\n", argv[0]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* inFilename = argv[1];
|
||||
const char* outFilename = argv[2];
|
||||
|
||||
// Read the file
|
||||
uint16_t* palette;
|
||||
int paletteSize;
|
||||
{
|
||||
FILE* inFile = fopen(inFilename, "r");
|
||||
assert(inFile);
|
||||
|
||||
char* line;
|
||||
size_t len = 0;
|
||||
|
||||
getline(&line, &len, inFile);
|
||||
assert(strncmp(line, "JASC-PAL", 8) == 0);
|
||||
|
||||
getline(&line, &len, inFile);
|
||||
assert(strncmp(line, "0100", 4) == 0);
|
||||
|
||||
getline(&line, &len, inFile);
|
||||
paletteSize = atoi(line);
|
||||
|
||||
palette = malloc(sizeof(*palette) * paletteSize);
|
||||
assert(palette);
|
||||
|
||||
// Each line is of form R G B
|
||||
for (int i = 0; i < paletteSize; ++i)
|
||||
{
|
||||
getline(&line, &len, inFile);
|
||||
|
||||
char* tok = strtok(line, " ");
|
||||
int red = atoi(tok);
|
||||
|
||||
tok = strtok(NULL, " ");
|
||||
int green = atoi(tok);
|
||||
|
||||
tok = strtok(NULL, " ");
|
||||
int blue = atoi(tok);
|
||||
|
||||
uint16_t rgb565 =
|
||||
((red >> 3u) << 11u)
|
||||
| ((green >> 2u) << 5u)
|
||||
| (blue >> 3u);
|
||||
|
||||
uint16_t endianSwap = ((rgb565 & 0xFFu) << 8u) | (rgb565 >> 8u);
|
||||
|
||||
palette[i] = endianSwap;
|
||||
}
|
||||
|
||||
fclose(inFile);
|
||||
}
|
||||
|
||||
|
||||
printf("Input: %s\n", inFilename);
|
||||
printf("Output: %s\n", outFilename);
|
||||
printf("Palette Size: %d\n", paletteSize);
|
||||
|
||||
|
||||
// Output to the file
|
||||
{
|
||||
FILE* outFile = fopen(outFilename, "w");
|
||||
assert(outFile);
|
||||
|
||||
fprintf(outFile, "// AUTOMATICALLY GENERATED. DO NOT EDIT.\n");
|
||||
fprintf(outFile, "\n");
|
||||
fprintf(outFile, "#pragma once\n");
|
||||
fprintf(outFile, "\n");
|
||||
fprintf(outFile, "\n");
|
||||
|
||||
fprintf(outFile, "uint16_t palette[%d] =\n", paletteSize);
|
||||
fprintf(outFile, "{\n");
|
||||
|
||||
for (int i = 0; i < paletteSize; ++i)
|
||||
{
|
||||
fprintf(outFile, " 0x%04X,\n", palette[i]);
|
||||
}
|
||||
|
||||
fprintf(outFile, "};\n");
|
||||
fprintf(outFile, "\n");
|
||||
|
||||
fclose(outFile);
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
printf("DONE\n");
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,132 @@
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
if (argc != 3)
|
||||
{
|
||||
fprintf(stderr, "Usage: %s <input image> <output header>\n", argv[0]);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
const char* inFilename = argv[1];
|
||||
const char* outFilename = argv[2];
|
||||
|
||||
|
||||
int tileWidth;
|
||||
int tileHeight;
|
||||
uint8_t* tileBuffer;
|
||||
{
|
||||
FILE* inFile = fopen(inFilename, "rb");
|
||||
assert(inFile);
|
||||
|
||||
#pragma pack(push,1)
|
||||
struct BmpHeader
|
||||
{
|
||||
char magic[2];
|
||||
uint32_t totalSize;
|
||||
uint32_t reserved;
|
||||
uint32_t offset;
|
||||
uint32_t headerSize;
|
||||
int32_t width;
|
||||
int32_t height;
|
||||
uint16_t planes;
|
||||
uint16_t depth;
|
||||
uint32_t compression;
|
||||
uint32_t imageSize;
|
||||
int32_t horizontalResolution;
|
||||
int32_t verticalResolution;
|
||||
uint32_t paletteColorCount;
|
||||
uint32_t importantColorcount;
|
||||
} bmpHeader;
|
||||
#pragma pack(pop)
|
||||
|
||||
// Read the BMP header so we know where the image data is located
|
||||
fread(&bmpHeader, 1, sizeof(bmpHeader), inFile);
|
||||
assert(bmpHeader.magic[0] == 'B' && bmpHeader.magic[1] == 'M');
|
||||
assert(bmpHeader.depth == 8);
|
||||
assert(bmpHeader.headerSize == 40);
|
||||
|
||||
// Go to location in file of image data
|
||||
fseek(inFile, bmpHeader.offset, SEEK_SET);
|
||||
|
||||
// Read in the image data
|
||||
tileBuffer = malloc(bmpHeader.imageSize);
|
||||
assert(tileBuffer);
|
||||
fread(tileBuffer, 1, bmpHeader.imageSize, inFile);
|
||||
|
||||
tileWidth = bmpHeader.width;
|
||||
tileHeight = bmpHeader.height;
|
||||
|
||||
fclose(inFile);
|
||||
}
|
||||
|
||||
|
||||
printf("Input: %s\n", inFilename);
|
||||
printf("Output: %s\n", outFilename);
|
||||
printf("Width: %d\n", tileWidth);
|
||||
printf("Height: %d\n", tileHeight);
|
||||
|
||||
FILE* outFile = fopen(outFilename, "w");
|
||||
assert(outFile);
|
||||
|
||||
|
||||
// Generate the preamble
|
||||
{
|
||||
fprintf(outFile, "// AUTOMATICALLY GENERATED. DO NOT EDIT.\n");
|
||||
fprintf(outFile, "\n");
|
||||
fprintf(outFile, "#pragma once\n");
|
||||
fprintf(outFile, "\n");
|
||||
fprintf(outFile, "#include <stdint.h>\n");
|
||||
fprintf(outFile, "\n");
|
||||
fprintf(outFile, "\n");
|
||||
}
|
||||
|
||||
|
||||
// Generate the font map with the calculated glyph bytes
|
||||
{
|
||||
fprintf(outFile, "static const uint8_t tile[%d][%d] =\n", tileHeight, tileWidth);
|
||||
fprintf(outFile, "{\n");
|
||||
fprintf(outFile, " ");
|
||||
|
||||
int count = 0;
|
||||
|
||||
for (int row = 0; row < tileHeight; ++row)
|
||||
{
|
||||
for (int col = 0; col < tileWidth; ++col)
|
||||
{
|
||||
// BMP is laid out bottom-to-top, but we want top-to-bottom (0-indexed)
|
||||
int y = tileHeight - row - 1;
|
||||
|
||||
uint8_t paletteIndex = tileBuffer[y * tileWidth + col];
|
||||
|
||||
fprintf(outFile, "%d,", paletteIndex);
|
||||
++count;
|
||||
|
||||
// Put a newline after sixteen values to keep it orderly
|
||||
if ((count % 16) == 0)
|
||||
{
|
||||
fprintf(outFile, "\n");
|
||||
fprintf(outFile, " ");
|
||||
|
||||
count = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fprintf(outFile, "};\n");
|
||||
}
|
||||
|
||||
fclose(outFile);
|
||||
|
||||
printf("\n");
|
||||
printf("DONE\n");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user