Files
itgmania212121/stepmania/src/BitmapText.cpp
T

356 lines
9.7 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-07-27 19:29:51 +00:00
#include "RageException.h"
#include "RageTimer.h"
#include "RageDisplay.h"
#include "PrefsManager.h"
#include "ThemeManager.h"
#include "GameConstantsAndTypes.h"
2002-06-14 22:25:22 +00:00
2002-04-16 17:31:00 +00:00
#define RAINBOW_COLOR_1 THEME->GetMetricC("BitmapText","RainbowColor1")
#define RAINBOW_COLOR_2 THEME->GetMetricC("BitmapText","RainbowColor2")
#define RAINBOW_COLOR_3 THEME->GetMetricC("BitmapText","RainbowColor3")
#define RAINBOW_COLOR_4 THEME->GetMetricC("BitmapText","RainbowColor4")
#define RAINBOW_COLOR_5 THEME->GetMetricC("BitmapText","RainbowColor5")
#define RAINBOW_COLOR_6 THEME->GetMetricC("BitmapText","RainbowColor6")
#define RAINBOW_COLOR_7 THEME->GetMetricC("BitmapText","RainbowColor7")
2002-04-16 17:31:00 +00:00
const int NUM_RAINBOW_COLORS = 7;
RageColor RAINBOW_COLORS[NUM_RAINBOW_COLORS];
2001-11-03 10:52:42 +00:00
BitmapText::BitmapText()
{
// Loading these theme metrics is slow, so only do it ever 20th time.
static int iReloadCounter = 0;
if( iReloadCounter%20==0 )
{
RAINBOW_COLORS[0] = RAINBOW_COLOR_1;
RAINBOW_COLORS[1] = RAINBOW_COLOR_2;
RAINBOW_COLORS[2] = RAINBOW_COLOR_3;
RAINBOW_COLORS[3] = RAINBOW_COLOR_4;
RAINBOW_COLORS[4] = RAINBOW_COLOR_5;
RAINBOW_COLORS[5] = RAINBOW_COLOR_6;
RAINBOW_COLORS[6] = RAINBOW_COLOR_7;
}
iReloadCounter++;
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_sTexturePath );
2001-11-25 04:31:44 +00:00
}
2001-11-03 10:52:42 +00:00
bool BitmapText::LoadFromFont( CString sFontFilePath )
2001-11-25 04:31:44 +00:00
{
LOG->Trace( "BitmapText::LoadFromFontName(%s)", sFontFilePath.GetString() );
2002-01-16 10:01:32 +00:00
if( m_pFont ) {
FONT->UnloadFont( m_pFont->m_sTexturePath );
m_pFont = NULL;
}
// load font
m_pFont = FONT->LoadFont( sFontFilePath );
2002-01-16 10:01:32 +00:00
return true;
}
bool BitmapText::LoadFromTextureAndChars( CString sTexturePath, CString sChars )
{
LOG->Trace( "BitmapText::LoadFromTextureAndChars(%s)", sTexturePath.GetString() );
if( m_pFont ) {
FONT->UnloadFont( m_pFont->m_sTexturePath );
m_pFont = NULL;
}
// load font
m_pFont = FONT->LoadFont( sTexturePath, sChars );
2002-09-03 06:33:08 +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 )
{
//LOG->Trace( "BitmapText::SetText()" );
2002-04-28 20:42:32 +00:00
ASSERT( m_pFont );
2002-03-31 07:55:25 +00:00
if( m_pFont->m_bCapitalsOnly )
sText.MakeUpper();
//
// save the string and crop if necessary
//
2002-06-29 11:59:09 +00:00
ASSERT( sText.GetLength() < MAX_TEXT_CHARS/2 );
strncpy( m_szText, sText, MAX_TEXT_CHARS );
m_szText[MAX_TEXT_CHARS-1] = '\0';
2002-09-22 20:34:11 +00:00
/*
Do NOT stop out all extended ASCII chars. Why was this here? -Chris
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-09-22 20:34:11 +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-08-22 09:31:32 +00:00
void BitmapText::CropToWidth( int iMaxWidthInSourcePixels )
{
iMaxWidthInSourcePixels = max( 0, iMaxWidthInSourcePixels );
for( int l=0; l<m_iNumLines; l++ ) // for each line
{
while( m_iLineWidths[l] > iMaxWidthInSourcePixels )
{
m_iLineLengths[l]--;
m_iLineWidths[l] = m_pFont->GetLineWidthInSourcePixels( m_szTextLines[l], m_iLineLengths[l] );
}
}
}
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
static RageVertex v[4000];
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 iLineSpacing = m_pFont->m_iLineSpacing; // spacing between lines
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_bottom: iY = -(m_iNumLines) * iLineSpacing + iLineSpacing/2; break;
case align_middle: iY = -(m_iNumLines-1) * iLineSpacing/2; break;
case align_top: iY = + iLineSpacing/2; break;
default: ASSERT( false ); return;
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
{
const char *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 ); return;
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];
2002-09-22 20:34:11 +00:00
const int iFrameNo = m_pFont->m_iCharToFrameNo[ (unsigned char)c ];
if( iFrameNo == -1 ) // this font doesn't impelemnt this character
2002-12-21 19:32:38 +00:00
RageException::Throw( "The font '%s' does not implement the character '%c'", m_sFontFilePath.GetString(), c );
const int iCharWidth = m_pFont->m_iFrameNoToWidth[iFrameNo];
2002-01-16 10:01:32 +00:00
// 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.
const int iDrawExtraPixelsLeft = min( m_pFont->m_iDrawExtraPixelsLeft, (iFrameWidth-iCharWidth)/2 );
const int iDrawExtraPixelsRight = min( m_pFont->m_iDrawExtraPixelsRight, (iFrameWidth-iCharWidth)/2 );
2002-04-28 20:42:32 +00:00
//
// set vertex positions
//
v[iNumV++].p = RageVector3( (float)iX-iDrawExtraPixelsLeft, iY-iHeight/2.0f, 0 ); // top left
v[iNumV++].p = RageVector3( (float)iX-iDrawExtraPixelsLeft, iY+iHeight/2.0f, 0 ); // bottom left
v[iNumV++].p = RageVector3( (float)iX-iDrawExtraPixelsLeft+iCharWidth+iDrawExtraPixelsRight, iY+iHeight/2.0f, 0 ); // bottom right
v[iNumV++].p = RageVector3( (float)iX-iDrawExtraPixelsLeft+iCharWidth+iDrawExtraPixelsRight, iY-iHeight/2.0f, 0 ); // top right
2002-01-16 10:01:32 +00:00
iX += iCharWidth;
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
//
iNumV -= 4;
2002-01-16 10:01:32 +00:00
2002-10-31 06:00:30 +00:00
RectF 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
const float fExtraTexCoordsLeft = iDrawExtraPixelsLeft / (float)pTexture->GetSourceWidth();
const float fExtraTexCoordsRight = iDrawExtraPixelsRight / (float)pTexture->GetSourceWidth();
2002-01-16 10:01:32 +00:00
v[iNumV++].t = RageVector2( frectTexCoords.left - fExtraTexCoordsLeft, frectTexCoords.top ); // top left
v[iNumV++].t = RageVector2( frectTexCoords.left - fExtraTexCoordsLeft, frectTexCoords.bottom ); // bottom left
v[iNumV++].t = RageVector2( frectTexCoords.right + fExtraTexCoordsRight, frectTexCoords.bottom ); // bottom right
v[iNumV++].t = RageVector2( frectTexCoords.right + fExtraTexCoordsRight, frectTexCoords.top ); // top right
2002-01-16 10:01:32 +00:00
}
iY += iLineSpacing;
2001-12-19 01:50:57 +00:00
}
2002-01-16 10:01:32 +00:00
DISPLAY->SetTexture( pTexture );
DISPLAY->SetTextureModeModulate();
if( m_bBlendAdd )
DISPLAY->SetBlendModeAdd();
else
DISPLAY->SetBlendModeNormal();
2002-01-16 10:01:32 +00:00
/* Draw if we're not fully transparent or the zbuffer is enabled (which ignores
* alpha). */
if( m_temp.diffuse[0].a != 0 || DISPLAY->ZBufferEnabled())
2002-01-16 10:01:32 +00:00
{
//////////////////////
// render the shadow
//////////////////////
if( m_bShadow )
{
DISPLAY->PushMatrix();
DISPLAY->TranslateLocal( m_fShadowLength, m_fShadowLength, 0 ); // shift by 5 units
2002-01-16 10:01:32 +00:00
2002-11-11 20:36:39 +00:00
RageColor dim(0,0,0,0.5f*m_temp.diffuse[0].a); // semi-transparent black
2002-08-19 20:02:30 +00:00
2002-11-14 06:56:31 +00:00
for( int i=0; i<iNumV; i++ )
2002-11-11 20:36:39 +00:00
v[i].c = dim;
DISPLAY->DrawQuads( v, iNumV );
2002-01-16 10:01:32 +00:00
DISPLAY->PopMatrix();
}
2002-05-29 09:47:24 +00:00
//////////////////////
// render the diffuse pass
//////////////////////
if( m_bRainbow )
2002-05-29 09:47:24 +00:00
{
2002-12-19 23:07:20 +00:00
int color_index = int(RageTimer::GetTimeSinceStart() / 0.200) % NUM_RAINBOW_COLORS;
for( int i=0; i<iNumV; i+=4 )
{
const RageColor color = RAINBOW_COLORS[color_index];
for( int j=i; j<i+4; j++ )
v[j].c = color;
color_index = (color_index+1)%NUM_RAINBOW_COLORS;
}
2002-05-29 09:47:24 +00:00
}
else
2002-05-29 09:47:24 +00:00
{
for( int i=0; i<iNumV; i+=4 )
{
v[i+0].c = m_temp.diffuse[0]; // top left
v[i+1].c = m_temp.diffuse[2]; // bottom left
v[i+2].c = m_temp.diffuse[3]; // bottom right
v[i+3].c = m_temp.diffuse[1]; // top right
}
2002-05-29 09:47:24 +00:00
}
2002-01-16 10:01:32 +00:00
DISPLAY->DrawQuads( v, iNumV );
}
2002-01-16 10:01:32 +00:00
//////////////////////
// render the glow pass
2002-01-16 10:01:32 +00:00
//////////////////////
if( m_temp.glow.a != 0 )
2002-01-16 10:01:32 +00:00
{
DISPLAY->SetTextureModeGlow();
2002-08-19 20:02:30 +00:00
int i;
for( i=0; i<iNumV; i++ )
v[i].c = m_temp.glow;
DISPLAY->DrawQuads( v, iNumV );
2002-01-16 10:01:32 +00:00
}
2001-12-19 01:50:57 +00:00
2002-11-16 08:54:15 +00:00
}