Files
itgmania212121/stepmania/src/BitmapText.cpp
T

371 lines
11 KiB
C++
Raw Normal View History

2001-11-03 10:52:42 +00:00
#include "stdafx.h"
/*
-----------------------------------------------------------------------------
File: BitmapText
2001-11-03 10:52:42 +00:00
Desc: See header.
2002-05-19 01:59:48 +00:00
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
Chris Danford
-----------------------------------------------------------------------------
*/
2001-11-03 10:52:42 +00:00
#include "BitmapText.h"
#include "IniFile.h"
#include "FontManager.h"
2002-05-01 19:14:55 +00:00
#include "RageLog.h"
2002-04-16 17:31:00 +00:00
#include "ErrorCatcher/ErrorCatcher.h"
D3DXCOLOR RAINBOW_COLORS[] = {
D3DXCOLOR( 1.0f, 0.2f, 0.4f, 1 ), // red
D3DXCOLOR( 0.8f, 0.2f, 0.6f, 1 ), // pink
D3DXCOLOR( 0.4f, 0.3f, 0.5f, 1 ), // purple
D3DXCOLOR( 0.2f, 0.6f, 1.0f, 1 ), // sky blue
D3DXCOLOR( 0.2f, 0.8f, 0.8f, 1 ), // sea green
D3DXCOLOR( 0.2f, 0.8f, 0.4f, 1 ), // green
D3DXCOLOR( 1.0f, 0.8f, 0.2f, 1 ), // orange
};
const int NUM_RAINBOW_COLORS = sizeof(RAINBOW_COLORS) / sizeof(D3DXCOLOR);
2001-11-03 10:52:42 +00:00
BitmapText::BitmapText()
{
2002-01-16 10:01:32 +00:00
m_HorizAlign = align_center;
m_VertAlign = align_middle;
2001-12-19 01:50:57 +00:00
m_pFont = NULL;
2002-02-02 05:11:12 +00:00
m_iNumLines = 0;
m_iWidestLineWidth = 0;
for( int i=0; i<MAX_TEXT_LINES; i++ )
2002-02-02 07:42:52 +00:00
{
m_szTextLines[i] = '\0';
m_iLineLengths[i] = -1;
m_iLineWidths[i] = 1;
2002-02-02 07:42:52 +00:00
}
2002-04-16 17:31:00 +00:00
2002-05-01 19:14:55 +00:00
m_bShadow = true;
2002-04-16 17:31:00 +00:00
m_bRainbow = false;
2002-02-02 05:11:12 +00:00
}
BitmapText::~BitmapText()
{
if( m_pFont )
FONT->UnloadFont( m_pFont->m_sFontFilePath );
2001-11-25 04:31:44 +00:00
}
2001-11-03 10:52:42 +00:00
bool BitmapText::Load( const CString &sFontFilePath )
2001-11-25 04:31:44 +00:00
{
2002-05-01 19:14:55 +00:00
LOG->WriteLine( "BitmapText::LoadFromFontName(%s)", sFontFilePath );
2002-01-16 10:01:32 +00:00
// load font
m_pFont = FONT->LoadFont( sFontFilePath );
2002-01-16 10:01:32 +00:00
return true;
}
2002-01-16 10:01:32 +00:00
2002-03-31 07:55:25 +00:00
void BitmapText::SetText( CString sText )
{
2002-05-01 19:14:55 +00:00
//LOG->WriteLine( "BitmapText::SetText()" );
2002-04-28 20:42:32 +00:00
2002-03-31 07:55:25 +00:00
if( m_pFont->m_bCapitalsOnly )
sText.MakeUpper();
//
// save the string and crop if necessary
//
strncpy( m_szText, sText, MAX_TEXT_CHARS );
m_szText[MAX_TEXT_CHARS-1] = '\0';
int iLength = strlen( m_szText );
//
// strip out non-low ASCII chars
//
for( int i=0; i<iLength; i++ ) // for each character
2002-01-16 10:01:32 +00:00
{
if( m_szText[i] < 0 || m_szText[i] > MAX_FONT_CHARS-1 )
m_szText[i] = ' ';
}
2002-01-16 10:01:32 +00:00
//
// break the string into lines
//
m_iNumLines = 0;
LPSTR token;
2002-01-16 10:01:32 +00:00
/* Establish string and get the first token: */
token = strtok( m_szText, "\n" );
while( token != NULL )
2002-01-16 10:01:32 +00:00
{
m_szTextLines[m_iNumLines++] = token;
token = strtok( NULL, "\n" );
2002-01-16 10:01:32 +00:00
}
//
// calculate line lengths and widths
//
m_iWidestLineWidth = 0;
for( int l=0; l<m_iNumLines; l++ ) // for each line
2002-01-16 10:01:32 +00:00
{
m_iLineLengths[l] = strlen( m_szTextLines[l] );
m_iLineWidths[l] = m_pFont->GetLineWidthInSourcePixels( m_szTextLines[l], m_iLineLengths[l] );
if( m_iLineWidths[l] > m_iWidestLineWidth )
m_iWidestLineWidth = m_iLineWidths[l];
2002-01-16 10:01:32 +00:00
}
2002-04-28 20:42:32 +00:00
// fill the RageScreen's vertex buffer with what we're going to draw
//RebuildVertexBuffer();
}
2001-11-03 10:52:42 +00:00
2002-05-19 01:59:48 +00:00
// draw text at x, y using colorTop blended down to colorBottom, with size multiplied by scale
void BitmapText::DrawPrimitives()
{
2002-05-19 01:59:48 +00:00
if( m_iNumLines == 0 )
return;
2002-04-28 20:42:32 +00:00
RageTexture* pTexture = m_pFont->m_pTexture;
2001-12-28 10:15:59 +00:00
2002-01-16 10:01:32 +00:00
// make the object in logical units centered at the origin
2002-05-19 01:59:48 +00:00
LPDIRECT3DVERTEXBUFFER8 pVB = DISPLAY->GetVertexBuffer();
RAGEVERTEX* v;
2002-02-02 07:42:52 +00:00
pVB->Lock( 0, 0, (BYTE**)&v, 0 );
2002-05-19 01:59:48 +00:00
int iNumV = 0; // the current vertex number
2002-01-16 10:01:32 +00:00
const int iHeight = pTexture->GetSourceFrameHeight(); // height of a character
const int iFrameWidth = pTexture->GetSourceFrameWidth(); // width of a character frame in logical units
int iY; // the center position of the first row of characters
2002-01-16 10:01:32 +00:00
switch( m_VertAlign )
2001-12-28 10:15:59 +00:00
{
case align_top: iY = -(m_iNumLines-1) * iHeight; break;
case align_middle: iY = -(m_iNumLines-1) * iHeight/2; break;
case align_bottom: iY = 0; break;
2002-02-11 04:46:31 +00:00
default: ASSERT( false );
2002-01-16 10:01:32 +00:00
}
2001-12-19 01:50:57 +00:00
for( int i=0; i<m_iNumLines; i++ ) // foreach line
2002-01-16 10:01:32 +00:00
{
LPCTSTR szLine = m_szTextLines[i];
const int iLineLength = m_iLineLengths[i];
const int iLineWidth = m_iLineWidths[i];
int iX;
2002-01-16 10:01:32 +00:00
switch( m_HorizAlign )
2001-12-28 10:15:59 +00:00
{
case align_left: iX = 0; break;
case align_center: iX = -(iLineWidth/2); break;
case align_right: iX = -iLineWidth; break;
default: ASSERT( false );
2001-12-19 01:50:57 +00:00
}
for( int j=0; j<iLineLength; j++ ) // for each character in the line
2002-01-16 10:01:32 +00:00
{
const char c = szLine[j];
const int iFrameNo = m_pFont->m_iCharToFrameNo[c];
if( iFrameNo == -1 ) // this font doesn't impelemnt this character
2002-05-20 08:59:37 +00:00
FatalError( "The font '%s' does not implement the character '%c'", m_sFontFilePath, c );
const int iCharWidth = m_pFont->m_iFrameNoToWidth[iFrameNo];
2002-01-16 10:01:32 +00:00
// HACK:
// The right side of any italic letter is being cropped. So, we're going to draw a little bit
2002-01-16 10:01:32 +00:00
// to the right of the normal character.
2002-04-28 20:42:32 +00:00
const float fPercentExtra = min( m_pFont->m_fDrawExtraPercent, (iFrameWidth-iCharWidth)/(float)iFrameWidth ); // don't draw from the adjacent frame
//
// set vertex positions
//
const float fExtraPixels = fPercentExtra * pTexture->GetSourceFrameWidth();
2002-01-16 10:01:32 +00:00
// first triangle
2002-05-19 01:59:48 +00:00
v[iNumV++].p = D3DXVECTOR3( (float)iX, iY-iHeight/2.0f, 0 ); // top left
v[iNumV++].p = D3DXVECTOR3( (float)iX, iY+iHeight/2.0f, 0 ); // bottom left
iX += iCharWidth;
2002-05-19 01:59:48 +00:00
v[iNumV++].p = D3DXVECTOR3( iX+fExtraPixels, iY-iHeight/2.0f, 0 ); // top right
// 2nd triangle
2002-05-19 01:59:48 +00:00
v[iNumV++].p = v[iNumV-1].p; // top right
v[iNumV++].p = v[iNumV-3].p; // bottom left
v[iNumV++].p = D3DXVECTOR3( iX+fExtraPixels, iY+iHeight/2.0f, 0 ); // bottom right
2002-01-16 10:01:32 +00:00
2002-04-28 20:42:32 +00:00
//
2002-01-16 10:01:32 +00:00
// set texture coordinates
2002-04-28 20:42:32 +00:00
//
2002-05-19 01:59:48 +00:00
iNumV -= 6;
2002-01-16 10:01:32 +00:00
2002-04-28 20:42:32 +00:00
FRECT frectTexCoords = *pTexture->GetTextureCoordRect( iFrameNo );
2002-01-16 10:01:32 +00:00
2002-04-28 20:42:32 +00:00
// Tweak the textures frame rectangles so we don't draw extra
// to the left and right of the character, saving us fill rate.
float fPixelsToChopOff = pTexture->GetSourceFrameWidth() - (float)iCharWidth;
float fTexCoordsToChopOff = fPixelsToChopOff / pTexture->GetSourceWidth();
frectTexCoords.left += fTexCoordsToChopOff/2;
frectTexCoords.right -= fTexCoordsToChopOff/2;
2002-01-16 10:01:32 +00:00
2002-04-28 20:42:32 +00:00
const float fExtraTexCoords = fPercentExtra * pTexture->GetTextureFrameWidth() / pTexture->GetTextureWidth();
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
v[iNumV++].t = D3DXVECTOR2( frectTexCoords.left, frectTexCoords.top ); // top left
v[iNumV++].t = D3DXVECTOR2( frectTexCoords.left, frectTexCoords.bottom ); // bottom left
v[iNumV++].t = D3DXVECTOR2( frectTexCoords.right + fExtraTexCoords, frectTexCoords.top ); // top right
v[iNumV++].t = v[iNumV-1].t; // top right
v[iNumV++].t = v[iNumV-3].t; // bottom left
v[iNumV++].t = D3DXVECTOR2( frectTexCoords.right + fExtraTexCoords, frectTexCoords.bottom ); // bottom right
2002-01-16 10:01:32 +00:00
}
iY += iHeight;
2001-12-19 01:50:57 +00:00
}
2002-01-16 10:01:32 +00:00
pVB->Unlock();
// Set the stage...
2002-05-19 01:59:48 +00:00
LPDIRECT3DDEVICE8 pd3dDevice = DISPLAY->GetDevice();
pd3dDevice->SetTexture( 0, pTexture->GetD3DTexture() );
2002-01-16 10:01:32 +00:00
pd3dDevice->SetRenderState( D3DRS_SRCBLEND, m_bBlendAdd ? D3DBLEND_ONE : D3DBLEND_SRCALPHA );
pd3dDevice->SetRenderState( D3DRS_DESTBLEND, m_bBlendAdd ? D3DBLEND_ONE : D3DBLEND_INVSRCALPHA );
2002-05-19 01:59:48 +00:00
pd3dDevice->SetVertexShader( D3DFVF_RAGEVERTEX );
pd3dDevice->SetStreamSource( 0, pVB, sizeof(RAGEVERTEX) );
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
//////////////////////
// render the shadow
//////////////////////
if( m_bShadow && m_temp_colorDiffuse[0].a != 0 )
2002-01-16 10:01:32 +00:00
{
2002-05-19 01:59:48 +00:00
DISPLAY->PushMatrix();
DISPLAY->TranslateLocal( m_fShadowLength, m_fShadowLength, 0 ); // shift by 5 units
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
DWORD dwColor = D3DXCOLOR(0,0,0,0.5f*m_temp_colorDiffuse[0].a); // semi-transparent black
2002-05-29 09:47:24 +00:00
/*
// YUCK. It is very common in crapier drivers that the texture factor doesn't work.
// Too bad because using the texture factor instead of recoloring the verticies is very fast.
2002-05-19 01:59:48 +00:00
pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, dwColor );
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TFACTOR );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
2002-05-29 09:47:24 +00:00
*/
// recolor the verticies for a shadow
pVB->Lock( 0, 0, (BYTE**)&v, 0 );
for( int i=0; i<iNumV; i++ )
v[i].color = dwColor;
pVB->Unlock();
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, iNumV/3 );
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
DISPLAY->PopMatrix();
}
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
//////////////////////
// render the diffuse pass
//////////////////////
2002-05-29 09:47:24 +00:00
//
// set vertex colors for the diffuse pass
//
pVB->Lock( 0, 0, (BYTE**)&v, 0 );
if( m_bRainbow )
{
int color_index = (GetTickCount() / 200) % NUM_RAINBOW_COLORS;
for( int i=0; i<iNumV; i+=6 )
{
const D3DXCOLOR color = RAINBOW_COLORS[color_index];
for( int j=i; j<i+6; j++ )
v[j].color = color;
color_index = (color_index+1)%NUM_RAINBOW_COLORS;
}
}
else
{
for( int i=0; i<iNumV; i+=6 )
{
v[i ].color = m_temp_colorDiffuse[0]; // top left
v[i+1].color = m_temp_colorDiffuse[2]; // bottom left
v[i+2].color = m_temp_colorDiffuse[1]; // top right
v[i+3].color = m_temp_colorDiffuse[1]; // top right
v[i+4].color = m_temp_colorDiffuse[2]; // bottom left
v[i+5].color = m_temp_colorDiffuse[3]; // bottom right
}
}
pVB->Unlock();
2002-05-19 01:59:48 +00:00
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, iNumV/3 );
2002-01-16 10:01:32 +00:00
//////////////////////
// render the add pass
//////////////////////
if( m_temp_colorAdd.a != 0 )
2002-01-16 10:01:32 +00:00
{
2002-05-19 01:59:48 +00:00
DWORD dwColor = m_temp_colorAdd;
2002-05-29 09:47:24 +00:00
/*
// See above comment about some cards not correctly handling the
// texture factor render state.
2002-05-19 01:59:48 +00:00
pd3dDevice->SetRenderState( D3DRS_TEXTUREFACTOR, dwColor );
2002-01-16 10:01:32 +00:00
2002-05-19 01:59:48 +00:00
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TFACTOR );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
2002-01-16 10:01:32 +00:00
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
2002-05-19 01:59:48 +00:00
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_TFACTOR );
2002-01-16 10:01:32 +00:00
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
2002-05-29 09:47:24 +00:00
*/
// recolor the verticies for
pVB->Lock( 0, 0, (BYTE**)&v, 0 );
for( int i=0; i<iNumV; i++ )
v[i].color = dwColor;
pVB->Unlock();
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG2 );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE );
pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE );
2002-05-19 01:59:48 +00:00
pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, iNumV/3 );
2002-01-16 10:01:32 +00:00
}
2001-12-19 01:50:57 +00:00
}