kpmpgsmkii/font/src/main.c
2021-10-13 20:07:25 -05:00

83 lines
2 KiB
C

/*
* Kangaroo Punch Multi Player Game Server Mark II
* Copyright (C) 2020-2021 Scott Duensing
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
#include <stdio.h>
#include "stb.h"
#define BITMAP_FILE "/home/scott/code/kpmpgsmkii/font/data/vga8x8.png"
#define FONT_FILE "/home/scott/code/kpmpgsmkii/client/bin/vga8x8.dat"
int main(int argc, char *argv[]) {
unsigned char *font = NULL;
unsigned char data = 0;
FILE *out = NULL;
int bits = 0;
int x;
int y;
int n;
int w;
int h;
(void)argc;
(void)argv;
// Load 8x8 font from disk. Font is in a 16x16 grid.
font = stbi_load(BITMAP_FILE, (int *)&w, (int *)&h, (int *)&n, 3);
if (!font) return 1;
// Create data file for font.
out = fopen(FONT_FILE, "wb");
if (!out) {
stbi_image_free(font);
return 2;
}
// Provide some metadata for enhancement later.
fputc(8, out); // Width of characters
fputc(8, out); // Height of characters
fputc(16, out); // Number of characters per row
fputc(255, out); // Number of characters - 1
// Convert bitmap to actual bits.
for (y=0; y<h; y++) {
for (x=0; x<w; x++) {
data <<= 1;
data |= (font[(y * w * 3) + (x * 3)] != 0);
bits++;
if (bits > 7) {
bits = 0;
fputc(data, out);
}
}
}
fclose(out);
// Clean up.
stbi_image_free(font);
// Prove we cleaned up.
stb_leakcheck_dumpmem();
return 0;
}