/*
	Rogue Trader.
	Copyright (c) 2007, Darragh Coy.
	All rights reserved.
	Original work(s), source code & author(s) details available at: http://www.darraghcoy.com

	Redistribution and use in source and binary forms, with or without modification, are permitted 
	provided that the following conditions are met:

		* 	Redistributions of source code must retain the above copyright notice, this list of 
			conditions and the following disclaimer.
		* 	Redistributions in binary form must reproduce the above copyright notice, this list of 
			conditions and the following disclaimer in the documentation.
		* 	Neither the name of the author nor the names of contributors may be used to endorse or 
			promote products derived from this software without specific prior written permission.
		* 	The use of any third party libraries, modules or data files provided with the source 
			form of the program must follow and respect the individual licensing terms of these 
			components. You do not have permission to modify or circumvent the licensing terms for 
			provided third party components unless the third party licenses specifically allow this. 
			If you do not agree to the terms of provided third party components then you do not have 
			permission to use them in your derived work(s).
		* 	The terms of this license do not apply to separate (but related) distributions if supplied 
			by the author(s), only to the contents of this source distribution, derived works, and the 
			binaries produced by either modified or unmodified versions of the source code. The terms 
			of this license do not also apply to any third party libraries or components provided with 
			the source distribution. Such third party elements are licensed under their own separate 
			agreements.

	THIS SOFTWARE IS PROVIDED BY DARRAGH COY ''AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
	BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
	ARE DISCLAIMED. IN NO EVENT SHALL DARRAGH COY BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
	EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 
	OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 
	OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING 
	IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################
//
//
// Class Name:   GLFont
// Purpose:      Class that stores a bitmap font and displays strings in this
//               font type. Expects font image to have 8 columns and 16 rows of
//               characters, ordered by the layout of the regular Ascii char set.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "GLFont.h"

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GLFont::GLFont
Purpose:  Creates the OpenGL bitmap font, loading the specified font texture
Params:   font texture to use
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

GLFont::GLFont( char * fontFile )
{
    // Load and save the given font texture

    fontTexture = new GLTexture(fontFile);
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GLFont::~GLFont
Purpose:  Destructor. Free font texture memory.
Params:   n/a
Returns:  n/a

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

GLFont::~GLFont()
{
    // Delete the memory used by the font texture

    delete fontTexture;
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GLFont::drawString
Purpose:  Draws the given string of text of this font at given location
Params:   Text to draw, location to draw at
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GLFont::drawString
(
    const char * string ,   // string to draw
    float x ,               // position: x
    float y ,               // position: y
    float size ,            // size of output text
    float r,                // text color: r
    float g,                // text color: g
    float b                 // text color: b

) const
{
    // Abort if a null pointer was given

    if ( string == 0 ) return;

    // Abort if size is invalid:

    if ( size <= 0 ) return;

    // Current x and y values to draw text at:

    float xcurrent = x;
    float ycurrent = y;

    // Save previous GL state: save all enables- eg. glEnable(GL_BLEND) and so forth

    glPushAttrib(GL_ENABLE_BIT);

    // Setup openGL state

    glEnable    ( GL_BLEND      );
    glEnable    ( GL_TEXTURE_2D );
    glDisable   ( GL_LIGHTING   );
    glDisable   ( GL_CULL_FACE  );
    glDisable   ( GL_DEPTH_TEST );
    glDisable   ( GL_ALPHA_TEST );

    glBlendFunc ( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );

    // Bind the font texture with openGL

    fontTexture->bind();

    // Draw every character in the string

    for ( ; * string ; string ++ )
    {
        // Get the current character

        char drawChar = * string;

        // If the draw character is a carriage return then move onto a new line:

        if ( drawChar == '\n' )
        {
            // Move onto a new line:

            ycurrent += size;
            xcurrent  = x;

            continue;
        }

        // Get what row and column of the font texture the character is located at.
        // There are 16 columns and 16 rows in a font texture. First 4 bits of character code
        // give the column, next 4 bits give the row.

        unsigned col = ( drawChar >> 0 ) & 0xf;
        unsigned row = ( drawChar >> 4 ) & 0xf;

        // Now convert these to u and v openGL floating point texture coordinates
        // for the top, bottom and left/right of the textured quad we will draw for this character.

        float ul = float(col  ) / 16.0f;   // calc u coords
        float ur = float(col+1) / 16.0f;

        float vt = float(row  ) / 16.0f;   // calc v coords
        float vb = float(row+1) / 16.0f;

        // Now draw the textured quad representing the character

        glBegin(GL_QUADS);

            glColor4f(r,g,b,1);

            glTexCoord2f(ul,vt); glVertex3f(xcurrent        ,ycurrent       ,0);
            glTexCoord2f(ur,vt); glVertex3f(xcurrent+size   ,ycurrent       ,0);
            glTexCoord2f(ur,vb); glVertex3f(xcurrent+size   ,ycurrent+size  ,0);
            glTexCoord2f(ul,vb); glVertex3f(xcurrent        ,ycurrent+size  ,0);

        glEnd();

        // Move output location for next character onwards

        xcurrent += size;
    }

    // Restore previous GL state: retore all enables- eg. glEnable(GL_BLEND) and so forth

    glPopAttrib();
}

/*@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

Name:     GLFont::drawStringCentered
Purpose:  Draws the given string of text, centered in the specified box.
Params:   Text to draw, box to draw centered in.
Returns:  none

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@*/

void GLFont::drawStringCentered
(
    const char * string ,   // string to draw
    float box_x,            // box position: x
    float box_y,            // box position: y
    float box_w,            // box width
    float box_h,            // box height
    float size,             // text size
    float r,                // text color: r
    float g,                // text color: g
    float b                 // text color: b

) const
{
    // Abort on null string pointer

    if ( string == 0 ) return;

    // Abort if size is invalid:

    if ( size <= 0 ) return;

    // Figure out how many pixels long the string will be and how high the string is

    float w = size * float( strlen(string) );
    float h = size;

    // Now figure out what coordinates to draw it at to center the string in box

    float x = box_x + ( box_w / 2 ) - ( w / 2 );
    float y = box_y + ( box_h / 2 ) - ( h / 2 );

    x = mlRound(x);
    y = mlRound(y);

    // Now just use the regular draw string function to draw at figured out position

    drawString
    (
        string,
        x,
        y,
        size,
        r,
        g,
        b
    );
}

