/*
	Asteroids
    Copyright (C) 2006  Darragh Coy.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/*##############################################################################

Module name: GLDrawString
Purpose:     Loading of fonts and drawing of strings.

##############################################################################*/

#include "GLDrawString.h"

/*******************************************************************************
Private module prototypes: Not for outside use!
*******************************************************************************/

   //..                           

/*******************************************************************************
Variables
*******************************************************************************/
   
   // Base display list number for the font display lists
   
   int fontLists = 0;
   
/*******************************************************************************

Name:       GLDrawString_Init
Purpose:    Initialises the games fonts and creates font display lists 
Parameters: A Hardware device context handle: pointer to hw device used to
            render the window
Returns:    None

*******************************************************************************/ 

void GLDrawString_Init ( HDC hwDevice )   
{
     // Request OpenGL to generate 127 display lists for the font. One display]
     // list for each character:

     fontLists = glGenLists(127);
     
     // Declare and create the games custom font
     
     HFONT myFont;

     myFont = CreateFont
       (
          FONT_HEIGHT,           // Height of the font
          FONT_WIDTH,            // Average character width: 0 means let windows choose
          0,                     // Angle of escapement: should be same as text angle
          0,                     // Text angle from x axis
          FW_NORMAL,             // Font weight
          FALSE,                 // Italic font ?
          FALSE,                 // Underline font ?
          FALSE,                 // Stikeout font ?
          ANSI_CHARSET,          // Ansi character set
          OUT_DEFAULT_PRECIS,    // Output precision
          CLIP_DEFAULT_PRECIS,   // Clipping precision
          DEFAULT_QUALITY,       // Display quality
          DEFAULT_PITCH,         // Pitch and family of font: dont care about this
          FONT_NAME              // Font name
       );
     
     // Set font as current font used by the GDI rendering device
     // This is the font which will be used when drawing text
     
     SelectObject( hwDevice , myFont );
     
     // Generate the font display lists using the opengl function to do so.
     // This function turns true type fonts into bitmap textures which opengl can use.
     
     wglUseFontBitmaps
       (
       hwDevice,      // hardware device context
       0,             // start of character range to convert 
       127,           // end of charcter range to conter 0-127 means normal unextended ascii keycodes
       fontLists      // Base display list number for range of font display lists
       );   
}

/*******************************************************************************

Name:       GLDrawString_Draw
Purpose:    Draws the input string to the screen
Parameters: x position on screen to start drawing
            y position on screen to start drawing
            pointer to text
Returns:    None

*******************************************************************************/ 

void GLDrawString_Draw ( int x , int y , char * text )
{
     // Figure out lenght of text

     int lenght = strlen(text);

     // Set base list number for a range of possible lists to be called
     
     glListBase(fontLists);

     // Set drawing position for text
     
     glRasterPos2i(x,y);
     
     // Call the appropriate display list for each character in the text.
     // Display list number actually used is fontLists + characterKeyCode
     
     glCallLists
        (
        lenght,             // Amount of display lists to call
        GL_UNSIGNED_BYTE,   // Data type of offset from base list
        text                // Pointer to array of data type indicated, each cell
                            // is the number of a display list represented as an offset
                            // from the base display list number
        );
}
