/*
	Wheels Of Steel
	Copyright (c) 2006, 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:   BitmapFont
// Purpose:      Bitmap font. Loads a bitmap interpreted as a font. Draws
//               strings of text using this font. Fonts are stored in an image
//               containing 8 columns x 16 rows, or a total of 128 characters.
//               Font characters must be ordered according to the ascii char set.
//
//
//##############################################################################
//##############################################################################
//##############################################################################
//##############################################################################

#include "BitmapFont.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     BitmapFont::BitmapFont
Purpose:  Constructs the bitmap font.
Params:   Bitmap file name for the bitmap font. Window needed to load font.
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

BitmapFont::BitmapFont( char * imageName, Window * window )
{
    //--------------------------------------------------------------------------
    // load the font image and create a bitmap
    //--------------------------------------------------------------------------

    this->bmpFont = window->loadBitmap(imageName,true);

    //--------------------------------------------------------------------------
    // Get the character width and height of the font. There must be 8 columns
    // and 16 rows of characters in the font bitmap
    //--------------------------------------------------------------------------

    this->charW = bmpFont->getWidth() / 8;
    this->charH = bmpFont->getHeight() / 16;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     BitmapFont::BitmapFont
Purpose:  Destructor. Frees up the bitmap resource.
Params:   none
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

BitmapFont::~BitmapFont()
{
    // delete the bitmap

    delete bmpFont;
}

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Name:     BitmapFont::drawText
Purpose:  Draws the given text at the given position to the given window in this
          bitmap font style.
Params:   text to draw, position, window to draw to.
Returns:  none

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

void BitmapFont::drawText( const char * text , int x , int y , Window * window )
{
    //--------------------------------------------------------------------------
    // Abort if text or window pointer null
    //--------------------------------------------------------------------------

    if ( text == 0 || window == 0 ) return;

    //--------------------------------------------------------------------------
    // Draw each character while not reached the end of the string
    //--------------------------------------------------------------------------

    while ( * text )
    {
        //----------------------------------------------------------------------
        // Get the current character and clamp from 0-127 ascii value
        //----------------------------------------------------------------------

        unsigned character = unsigned( (*text) & 0x7f );

        //----------------------------------------------------------------------
        // Decide what row and column the character is at in the bitmap font
        // image. first 3 bits give column, next 4 bits give row.
        //----------------------------------------------------------------------

        int col = character & 0x07;
        int row = ( character & 0x78 ) >> 3;

        //----------------------------------------------------------------------
        // Now draw the character of the bitmap at the current position.
        //----------------------------------------------------------------------

        window->drawBitmapSection
        (
            bmpFont,        // bitmap to draw
            x,              // destination x
            y,              // destination y
            col * charW,    // source x
            row * charH,    // source y
            charW,          // source width
            charH           // source height
        );

        //----------------------------------------------------------------------
        // Move the x coordinate along by the width of a character
        //----------------------------------------------------------------------

        x += charW;

        //----------------------------------------------------------------------
        // Move onto the next character
        //----------------------------------------------------------------------

        text++;

    } // end while there is still text to draw

}
