

#include "renderPass.h"

#include "../event.h"

#include "../vector2.h"

#include "../shader.h"

#include "int.h"

#include "../fontRenderer.h"

#include "../program.h"

#include "../mesh.h"

class font extends renderPass{


	struct program * program;

	struct mesh * mesh;

	fontRenderer * font = new fontRenderer();

	sampler2D * samplerArray;

	char * textFromNumber( int i) {

		char * fileName = malloc( sizeof( char ) * 100 );

		sprintf( fileName, "%d", i );

		return fileName;

	}

	prepare() {

		printf("\n\n\n		Prepare renderPass Font\n\n\n\n\n");

		


		shader * vertexShader = new shader( GL_VERTEX_SHADER );

		vertexShader->loadFromFile( "assets/shaders/quad.vertex" );


		shader * fragmentShader = new shader( GL_FRAGMENT_SHADER );

		fragmentShader->loadFromFile( "assets/shaders/quad.fragment" );



		this->program 	= new program();

		this->program->addShader( vertexShader );

		this->program->addShader( fragmentShader );

		this->program->create();


		sampler2D * samplerArray		= new sampler2D();

		samplerArray->target			= GL_TEXTURE_2D_ARRAY;

		samplerArray->format  			= GL_RED;

		samplerArray->internalFormat  	= GL_RED;

		samplerArray->WRAP_S  			= GL_CLAMP_TO_EDGE;

		samplerArray->WRAP_S  			= GL_CLAMP_TO_EDGE;

		samplerArray->cubeSize			= new vector3( 128, 128, 170 );

		samplerArray->UNPACK_ALIGNMENT	= true;


		this->samplerArray = samplerArray;

		block * fontBlock				= this->program->getBlock( "fontData" );


		this->mesh = new mesh();

		this->mesh->setProgram( this->program );

		this->mesh->createBuffers();


		glUseProgram( this->program->glProgram );

		for ( int i = 1; i < 170; ++i )
		{
			texture2D * characterTexture 	= this->font->loadFont( 34 + i );

			vector2 * offset 				= characterTexture->offset;

			printf("%c  offset left:	%f,  offset top:	%f  bitmap->rows:	%i\n", (char) 34 +  i, offset->x, offset->y, characterTexture->height );

			vector2 * size					= new vector2( characterTexture->width, characterTexture->height );

			fontBlock->setMemberItem( "fontOffsets[0]", i - 35, offset );

			//if( i > 34 ) {

				fontBlock->setMemberItem( "fontSizes[0]", i - 35, size );
			
			//}

			samplerArray->addTexture( characterTexture );

		}

		fontBlock->upload();

		this->program->setUniform( "samplerArray", samplerArray );



		vector<int> * textArray = new vector();

		char * someText			= "Wauw this is myp first text.";

		for (int i = 0; i < strlen(someText); ++i)
		{

			int charNumber = ( char ) someText[i] - 35;

			printf("	%i\n", charNumber   );

			textArray->add( charNumber );//

		}

		block * inputBlock				= this->program->getBlock( "inputBlock" );

		inputBlock->setMemberArray( "characters[0]", ( float * ) textArray->items );

		inputBlock->upload();

	}

	render() {

		int numItems	= 12;


		glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		
		this->program->use();

		this->program->bindBlock( "inputBlock");

		this->program->bindBlock( "fontData");


		this->program->setUniform( "samplerArray", this->samplerArray );



		glBindVertexArray( this->mesh->vertexArrayObject );

		glDrawElements( GL_TRIANGLES, numItems, GL_UNSIGNED_INT, ( void * ) 0 ); 

	}

}