

#include "Syst.hpp"

#include <cstdio>
#include "CFonte.hpp"

bool CFonte::Build(const char *fonte)
{
  int     loop;
  float   cx;
  float   cy;

  if (gl_GenTexture(font_text_ID, fonte, GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR_MIPMAP_LINEAR, GL_REPEAT, GL_REPEAT, 1))
  {
    
    c_log.printf ("Construction de la fonte %s | id_fonte=%i",fonte,font_text_ID);

    font_base =   glGenLists (256);
    glPixelStorei (GL_UNPACK_ALIGNMENT, 1);
    glBindTexture (GL_TEXTURE_2D, font_text_ID);

    for (loop = 0; loop < 256; loop++)
    {
      cx=(float) (loop % 16) / 16.0f;
      cy=(float) (loop / 16) / 16.0f;

      glNewList (font_base + loop, GL_COMPILE);
#define cwx     (float)(1.0f/16.0f)
#define cwy     cwx
#define fWidth  16
#define fHeight 16
				glBegin(GL_QUADS); // Each char
					glTexCoord2f(cx,1-cy-cwy);
					glVertex2i(0,0);
					glTexCoord2f(cx+cwx,1-cy-cwy);
					glVertex2i(fWidth - 1,0);
					glTexCoord2f(cx+cwx,1-cy);
					glVertex2i(fWidth - 1,fHeight -1);
					glTexCoord2f(cx,1-cy);
					glVertex2i(0,fHeight -1);
				glEnd();
				glTranslated(16,0,0); // Next char
      glEndList ();
    }
    return true;
  }
  
  return false;
}

void CFonte::Kill()
{
	glDeleteLists(font_base,256);
}

void CFonte::Printf(GLuint x, GLuint y, int set, GLfloat scale[2], char *fmt,...)
{
  char a[1024];
	va_list	ap; // on récupère la ligne apres fmt
	va_start(ap, fmt);
	    vsprintf(a, fmt, ap);
	va_end(ap);

	if (set>1)    set=1;

  glEnable    (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);

	glBindTexture   (GL_TEXTURE_2D, font_text_ID);		// On selectionne la texture
	glDisable       (GL_DEPTH_TEST);
  	glMatrixMode    (GL_PROJECTION);
    glPushMatrix    ();
    	glLoadIdentity();
    	glOrtho     (0,wc,0,hc,-1,1);
    	glMatrixMode(GL_MODELVIEW);
      	glPushMatrix();
      	glLoadIdentity();
        	    glTranslated(x,hc-y,0);
        	    glListBase(font_base-32+(128*set));
        	    glScaled(scale[0],scale[1], 0);
        	    glCallLists(strlen(a),GL_UNSIGNED_BYTE,a);
      	glMatrixMode(GL_PROJECTION);
      	glPopMatrix();
   glMatrixMode(GL_MODELVIEW);
   glPopMatrix();
   glDisable(GL_BLEND);

   glEnable(GL_DEPTH_TEST);
}


void CFonte::Print(GLint x, GLint y, int set, GLfloat scale[2], char *string)
{
  glEnable    (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);

	glBindTexture(GL_TEXTURE_2D, font_text_ID);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
		glLoadIdentity();
		glOrtho(0,wc,0,hc,-1,1);
		glMatrixMode(GL_MODELVIEW);

		glPushMatrix();  // sauvegarde de la matrice !!!!
			glLoadIdentity();
			glTranslated(x,hc - y,0);  // la position est bien TAILLE_Y - y !!!!
			                           // ( ct mon erreur :'( )
			glListBase(font_base - 32 + 128*set);
			glCallLists(strlen(string),GL_UNSIGNED_BYTE,string);
			glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);

	glPopMatrix(); // On recharge la matrice sauvegardée prédédement
  glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
}

void CFonte::DrawChar(GLint x, GLint y, int set, GLfloat scale[2], char c)
{
  glEnable    (GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_COLOR);

	glBindTexture(GL_TEXTURE_2D, font_text_ID);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
		glLoadIdentity();
		glOrtho(0,wc,0,hc,-1,1);
		glMatrixMode(GL_MODELVIEW);

		glPushMatrix();  // sauvegarde de la matrice !!!!
			glLoadIdentity();
			glTranslated(x,hc - y,0);  // la position est bien TAILLE_Y - y !!!!
			                           // ( ct mon erreur :'( )

			// On séléctionne directement notre caractère
      glCallList(font_base - 32 + 128*set + c);

			glMatrixMode(GL_PROJECTION);
		glPopMatrix();
		glMatrixMode(GL_MODELVIEW);

	glPopMatrix(); // On recharge la matrice sauvegardée prédédement
  glDisable(GL_BLEND);
	glEnable(GL_DEPTH_TEST);
}

