/*
 * Kangaroo Punch MultiPlayer 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>

#define MEMWATCH
#include "memwatch/memwatch.h"

#define STB_IMAGE_IMPLEMENTATION
#define STBI_ONLY_PNG
#include "stb_image.h"

#include "font.h"
#include "stddclmr.h"


void drawChar(FontT *font, char c);
void makeFont(char *source, char *target, int pixelsW, int pixelsH, int charsW, int charCount);
void test(char *fontfile);


void drawChar(FontT *font, char c) {
	uint8_t  cx;
	uint8_t  cy;
	uint16_t offset;
	uint8_t  yl;
	uint8_t  data;
	uint8_t  odd;
	uint16_t t;

	// Find character position in font grid.
	cx = c % font->span;
	cy = c / font->span;

	// Which half of the byte do we want?
	odd = ((cx & 1) == 1);

	// Find offset byte based on font bits.
	offset = cy * (font->span / 2) * font->height;
	t = cx * ((float)font->width / 8.0f);
	offset += t;

	for (yl=0; yl<font->height; yl++) {
		data = font->bits[offset];
		offset += (font->span / 2);

		if (odd) {
			printf("%c", data & 0x08 ? '#' : ' ');
			printf("%c", data & 0x04 ? '#' : ' ');
			printf("%c", data & 0x02 ? '#' : ' ');
			printf("%c", data & 0x01 ? '#' : ' ');
		} else {
			printf("%c", data & 0x80 ? '#' : ' ');
			printf("%c", data & 0x40 ? '#' : ' ');
			printf("%c", data & 0x20 ? '#' : ' ');
			printf("%c", data & 0x10 ? '#' : ' ');
		}
		printf("\n");
	}
}


void makeFont(char *source, char *target, int pixelsW, int pixelsH, int charsW, int charCount) {
	unsigned char *font  = NULL;
	unsigned char  data  = 0;
	FILE          *out   = NULL;
	int            bits  = 0;
	int            x;
	int            y;
	int            n;
	int            w;
	int            h;

	// Load font atlas from disk.
	font = stbi_load(source, (int *)&w, (int *)&h, (int *)&n, 3);
	if (!font) return;

	// Create data file for font.
	out = fopen(target, "wb");
	if (!out) {
		stbi_image_free(font);
		return;
	}

	// Provide some metadata for enhancement later.
	fputc(pixelsW,       out);  // Width of characters in pixels
	fputc(pixelsH,       out);  // Height of characters in pixels
	fputc(charsW,        out);  // Number of characters per row
	fputc(charCount - 1, 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);
}


void test(char *fontfile) {
	FontT *font = fontLoad(fontfile);
	printf("Solid Smile\n");    drawChar(font, 0x02);
	printf("Question Mark\n");  drawChar(font, '?');
	printf("x\n");              drawChar(font, 'x');
	fontUnload(&font);
}


int main(int argc, char *argv[]) {
	(void)argc;
	(void)argv;

	makeFont("/home/scott/code/kpmpgsmkii/font/in/vga4x8.png",  "/home/scott/code/kpmpgsmkii/font/out/vga4x8.dat",  4,  8, 16, 256);
	makeFont("/home/scott/code/kpmpgsmkii/font/in/vga8x8.png",  "/home/scott/code/kpmpgsmkii/font/out/vga8x8.dat",  8,  8, 16, 256);
	makeFont("/home/scott/code/kpmpgsmkii/font/in/vga8x14.png", "/home/scott/code/kpmpgsmkii/font/out/vga8x14.dat", 8, 14, 16, 256);
	makeFont("/home/scott/code/kpmpgsmkii/font/in/vga8x16.png", "/home/scott/code/kpmpgsmkii/font/out/vga8x16.dat", 8, 16, 16, 256);

	test("/home/scott/code/kpmpgsmkii/font/out/vga4x8.dat");

	return 0;
}
