// DrawText.cpp: implementation of the CTextFont class.
//

#include "stdhdr.h"

#include "Debug.h"
#include "Draw.h"
#include "DrawText.h"

//////////////////////////////////////////////////////////////////////
// Construction/Destruction

CTextFont::CTextFont()
{
    m_szCharSet = _tcsdup(_T(" "));
    m_cDefaultChar = TEXTSPRITE_DEFAULTCHAR;
}

CTextFont::CTextFont(IDirectDrawSurface7 *lpSurface, 
                     int xImage, int yImage, int cx, int cy, 
                     LPCTSTR lpCharSet, TCHAR cDefaultChar, 
                     int nFramesAcross)
{
    
    m_szCharSet = _tcsdup(lpCharSet);
    m_cDefaultChar = cDefaultChar;
    
    // Check that the default character is in the character set
    if ((m_cDefaultChar != _T('\0')) && (_tcschr(m_szCharSet, m_cDefaultChar) == NULL))
    {   
        TRACE1("Warning: default character '%c' not found in character set", m_cDefaultChar);
        m_cDefaultChar = m_szCharSet[0];
    }

    m_pSurface = lpSurface;
    m_xImage = xImage;
    m_yImage = yImage;
    m_cx = cx;
    m_cy = cy;
    if (nFramesAcross > 0)
        m_nFramesAcross = nFramesAcross;
    else
        m_nFramesAcross = _tcslen(lpCharSet);
}

CTextFont::CTextFont(CTextFont& s)
{
    m_szCharSet = _tcsdup(s.m_szCharSet);
    m_cDefaultChar = s.m_cDefaultChar;

    m_pSurface = s.m_pSurface;
    m_xImage = s.m_xImage;
    m_yImage = s.m_yImage;
    m_cx = s.m_cx;
    m_cy = s.m_cy;
    m_nFramesAcross = s.m_nFramesAcross;
}

CTextFont::~CTextFont()
{
    free((void *)m_szCharSet);
}

//////////////////////////////////////////////////////////////////////
// Operations

BOOL CTextFont::DrawText(LPCTSTR lpText, int x, int y, UINT nFormat)
{
    UINT    i, nFrame;
    RECT    r;
    LPTSTR  pChar;

    if (nFormat & DT_RIGHT)
        x -= GetTextWidth(lpText);
    else if (nFormat & DT_CENTER)
        x -= (GetTextWidth(lpText) / 2);

    if (nFormat & DT_BOTTOM)
        y -= m_cy;
    else if (nFormat & DT_VCENTER)
        y -= (m_cy / 2);

    for (i = 0; i < _tcslen(lpText); i++)
    {
        pChar = _tcschr(m_szCharSet, lpText[i]);
        if (pChar == NULL)
        {
            // If the default char is \0, just skip any unknown characters.
            if (m_cDefaultChar == _T('\0'))
                continue;

            pChar = _tcschr(m_szCharSet, lpText[i]);
        }
        if (pChar == NULL)
        {
            TRACE1("Warning: default character '%c' not found in character set", m_cDefaultChar);
            continue;
        }
        nFrame = pChar - m_szCharSet;

        r.left = (nFrame % m_nFramesAcross) * m_cx + m_xImage;
        r.top =  (nFrame / m_nFramesAcross) * m_cy + m_yImage;
        r.right = r.left + m_cx;
        r.bottom = r.top + m_cy;
        BackBlt(x, y, m_pSurface, &r, DDBLTFAST_SRCCOLORKEY);
        x += m_cx;
    }
    return TRUE;
}

int CTextFont::GetTextWidth(LPCTSTR lpText)
{
    if (m_cDefaultChar != '\0')
        return _tcslen(lpText) * m_cx;

    int     nLength = 0;
    size_t  i;

    for (i = 0; i < _tcslen(lpText); i++)
        if (_tcschr(m_szCharSet, lpText[i]) != NULL)
            nLength += m_cx;

    return nLength;
}