
#include <ft2build.h>

#include <freetype/freetype.h>

#include <text.h>

#include <texture2D.h>

#include <vector2.h>

#define GL_GLEXT_PROTOTYPES

#include <GL/glext.h>

#include <GL/gl.h> // GL 1.1 functions 

#include <GL/glx.h>


class fontRenderer{

	GLubyte * data 	= malloc( 512 * 512 * 4 );
	
	texture2D * loadFont( char character ) {

		FT_Library ft;

		if ( FT_Init_FreeType( &ft ) )
		{

			printf("ERROR::FREETYPE: Could not init FreeType Library");

			//return -1;

		} else {

		//	printf("FreeType Library loaded\n\n");

		}



		FT_Face face;

		if ( FT_New_Face( ft, "assets/fonts/WorkSans/WorkSans-Regular.ttf", 0, &face ) )
		{

			printf("ERROR::FREETYPE: Failed to load font\n\n");
	
			//return -1;

		} else {

		//	printf("FREETYPE: font loaded\n\n");

		}

		int fontSize = 118;

		if ( FT_Set_Pixel_Sizes( face, 0, fontSize ) ) {

			printf("ERROR::FREETYPE: Failed to set font size\n\n");

		} else {

			//printf("FREETYPE: font size set\n\n");

		} 



		FT_Matrix		matrix;

		FT_UInt			glyph_index;

		FT_Vector		pen;

		int				n;

		FT_GlyphSlot	slot 			= face->glyph;		/* a small shortcut */


		pen.x = 0;
		
		pen.y = 0;

		if( FT_Load_Char( face, character, FT_LOAD_RENDER ) ) {

			printf("ERROR error loading char.");

		} else {

			//printf("FREETYPE: Char loaded	%c \n", character );

		}


		//printf( "%c:		bitmap_left:	%i,  bitmap_top:	%i\n", ( char ) character, slot->bitmap_left, slot->bitmap_top );


		FT_Bitmap * bitmap 			= &slot->bitmap;

		texture2D * texture 		= new texture2D();

		texture->width 				= bitmap->width;

		texture->height 			= bitmap->rows;

		texture->hasAlpha			= 1;

		texture->offset				= new vector2( ( float ) slot->bitmap_left, ( float ) slot->bitmap_top );


		//printf("x_max:		%i\n", texture->width);

		//printf("y_max:		%i\n", texture->height);

		


		texture->data 				= bitmap->buffer;//bitmap->buffer;

		for (int x = 0; x < texture->width; ++x)
		{
			//printf("%u ", (unsigned int)bitmap->buffer[0]);
		}

		//FT_Done_Face(face);

		//FT_Done_FreeType(ft);



		return texture;

		//this->show_image();


	}

	void draw_bitmap( FT_Bitmap * bitmap, FT_Int x,  FT_Int y ) {

		FT_Int  i, j, p, q;

		FT_Int  x_max = x + bitmap->width;
		FT_Int  y_max = y + bitmap->rows;

		int WIDTH	= 512;

		int HEIGHT  = 512;

		printf("x_max:		%i\n", x_max);

		printf("y_max:		%i\n", y_max);


		for ( i = x, p = 0; i < x_max; i++, p++ )
		{
			for ( j = y, q = 0; j < y_max; j++, q++ )
			{
				//if ( i < 0 || j < 0 || i >= WIDTH || j >= HEIGHT )
				//continue;

				//this->data[j + (i * 512)] |= bitmap->buffer[q * bitmap->width + p];

			}
		}
		
	}

	void show_image( void )
	{
	}

}