#include "glex.h"
#include"simplefont.h"
#include"glness.h"

#include <stdarg.h>
#include <stdio.h>
#include <string.h>

int simplefontT::SetSize(float x, float y)       // Set the size of the font
{
    sizex=x;
    sizey=y;

    return 1;
}

int simplefontT::BuildFont(char *file)          // Build Our Font Display List
{
    int loop1;
    float cx, cy;
    float charsize = 1.f/16.f;

    fonttexture = LoadGLTexture(file);

    base=glGenLists(256);
    glBindTexture(GL_TEXTURE_2D, fonttexture);
    for (loop1=0; loop1<256; loop1++)
    {
        cx=(float)(loop1%16)/16.0f;
        cy=(float)(loop1/16)/16.0f;

        glNewList(base+loop1,GL_COMPILE);
        glBegin(GL_QUADS);
        glTexCoord2f(cx+charsize,1.0f-cy);
        glVertex2i(16,0);
        glTexCoord2f(cx,1.0f-cy-0.01f);
        glVertex2i(0,0);
        glTexCoord2f(cx,1.0f-cy-charsize);
        glVertex2d(0,16);
        glTexCoord2f(cx+charsize,1.0f-cy-charsize-0.01f);
        glVertex2i(16,16);
        glEnd();
        glTranslated(12,0,0);
        glEndList();
    }
    return 1;
}

simplefontT::~simplefontT() {
    glDeleteLists(base,256);
}
int simplefontT::PrintText(float x, float y, char *fmt,...)// Where The Printing Happens
{
    char    text[1024];                            // Holds Our String
    int blendOn,scissorOn,textureOn,lightOn;  // Holds the previous GL settings
    int depthOn,matrixMode,screenStats[4],blendSrc,blendDst;

    va_list ap;                                    // Pointer To List Of Arguments

    if (fmt == NULL)                               // If There's No Text
        return 1;                                      // Do Nothing

    va_start(ap, fmt);                             // Parses The String For Variables
    vsprintf(text, fmt, ap);                       // And Converts Symbols To Actual Numbers
    va_end(ap);                                    // Results Are Stored In Text


    /*textureOn = glIsEnabled(GL_TEXTURE_2D); // Were textures enabled?
    depthOn = glIsEnabled(GL_DEPTH_TEST);  // Was GL_DEPTH_TEST enabled?
    lightOn = glIsEnabled(GL_LIGHTING); // Was GL_LIGHTING enabled?
    scissorOn = glIsEnabled(GL_SCISSOR_TEST);  // etc.*/
    glGetIntegerv(GL_MATRIX_MODE, &matrixMode);
    glGetIntegerv(GL_VIEWPORT, screenStats);
    //blendOn= glIsEnabled(GL_BLEND);
    //glGetIntegerv(GL_BLEND_SRC, &blendSrc);
    //glGetIntegerv(GL_BLEND_DST, &blendDst);

    //if (depthOn) glDisable(GL_DEPTH_TEST);  // If they were enabled/disabled
    //if (!textureOn) glEnable(GL_TEXTURE_2D); // then enable/disable them
    //if (!blendOn) glEnable(GL_BLEND);
    //if (!scissorOn) glEnable(GL_SCISSOR_TEST);
    //if (lightOn) glDisable(GL_LIGHTING);
    //glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); // Set the correct blending mode

    //go2d(0.f,screenStats[2],screenStats[3],0.f);
    go2d(0.f,1,1,0.f);

    glBindTexture(GL_TEXTURE_2D, fonttexture);

    glLoadIdentity();
    glTranslated(x,y,0);
    glListBase(base-32);
    glScalef(sizex/512,sizey/512,1.0f);
    glCallLists(strlen(text),GL_UNSIGNED_BYTE, text); // Write The Text To The Screen

    leave2d();
/*
    if (depthOn) glEnable(GL_DEPTH_TEST);
    if (!textureOn) glDisable(GL_TEXTURE_2D);
    if (!blendOn) glDisable(GL_BLEND);
    if (!scissorOn) glDisable(GL_SCISSOR_TEST);
    if (lightOn) glEnable(GL_LIGHTING);
*/
    //glBlendFunc(blendSrc, blendDst);
    glMatrixMode(matrixMode);

    return 1;
}
