#ifndef FONT_H
#define FONT_H

#include <stdint.h>

#include <SDL2/SDL.h>

struct font {
	SDL_Surface *sur;
	SDL_Texture *tex;
	SDL_Rect *glyphs;
	size_t glyphs_num;
	double scale;
	uint32_t w;
	uint32_t h;
	uint32_t rows;
	uint32_t cols;
	uint32_t ch_w;
	uint32_t ch_h;
};

struct font font_create(SDL_Renderer *rdr, const char *filepath,
			uint32_t n_rows, uint32_t n_cols);

#if defined(FONT_IMP) || defined(IMP)

#include "surface.h"

struct font
font_create(SDL_Renderer *rdr, const char *filepath,
	    uint32_t n_rows, uint32_t n_cols)
{
	struct font f = {
		.rows = n_rows,
		.cols = n_cols
	};
	f.sur = surface_from_file(filepath);
	SCP(f.sur);

	SDL_SetColorKey(f.sur, SDL_TRUE, 0x00000000);

	if ( rdr != NULL ) {
		f.tex = SDL_CreateTextureFromSurface(rdr, f.sur);
		SCP(f.tex);
	}

	f.w = (uint32_t) f.sur->w;
	f.h = (uint32_t) f.sur->h;

	f.ch_w = (uint32_t) (f.w / f.cols);
	f.ch_h = (uint32_t) (f.h / f.rows);

	f.glyphs_num = 128;
	f.glyphs = calloc(sizeof(SDL_Rect), f.glyphs_num);
	for ( size_t i = 0; i < f.glyphs_num; ++i ) {
		const size_t col = i % f.cols;
		const size_t row = i / f.cols;

		f.glyphs[i] = (SDL_Rect){
			.x = (int32_t) (col * f.ch_w),
			.y = (int32_t) (row * f.ch_h),
			.w = (int32_t) f.ch_w,
			.h = (int32_t) f.ch_h
		};
	}

	return f;
}

#endif /* defined(FONT_IMP) || defined(IMP) */


#endif /* FONT_H */
