2001-11-03 10:52:42 +00:00
|
|
|
#include "stdafx.h"
|
2001-11-04 19:34:28 +00:00
|
|
|
/*
|
|
|
|
|
-----------------------------------------------------------------------------
|
2002-03-30 20:00:13 +00:00
|
|
|
File: BitmapText
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
Desc: See header.
|
2001-11-04 19:34:28 +00:00
|
|
|
|
2002-05-19 01:59:48 +00:00
|
|
|
Copyright (c) 2001-2002 by the person(s) listed below. All rights reserved.
|
2002-03-30 20:00:13 +00:00
|
|
|
Chris Danford
|
2001-11-04 19:34:28 +00:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
|
*/
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
#include "BitmapText.h"
|
|
|
|
|
#include "IniFile.h"
|
2002-03-30 20:00:13 +00:00
|
|
|
#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"
|
2002-11-11 04:53:31 +00:00
|
|
|
#include "RageDisplay.h"
|
2002-08-13 23:26:46 +00:00
|
|
|
#include "PrefsManager.h"
|
2002-11-11 04:53:31 +00:00
|
|
|
#include "ThemeManager.h"
|
|
|
|
|
#include "GameConstantsAndTypes.h"
|
2002-12-29 22:55:26 +00:00
|
|
|
#include "Font.h"
|
2002-06-14 22:25:22 +00:00
|
|
|
|
2002-04-16 17:31:00 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
#define RAINBOW_COLOR(n) THEME->GetMetricC("BitmapText",ssprintf("RainbowColor%i", n+1))
|
2002-04-16 17:31:00 +00:00
|
|
|
|
2002-08-27 23:31:41 +00:00
|
|
|
const int NUM_RAINBOW_COLORS = 7;
|
2002-10-28 05:30:45 +00:00
|
|
|
RageColor RAINBOW_COLORS[NUM_RAINBOW_COLORS];
|
2001-11-03 10:52:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
BitmapText::BitmapText()
|
|
|
|
|
{
|
2002-08-27 23:31:41 +00:00
|
|
|
// Loading these theme metrics is slow, so only do it ever 20th time.
|
|
|
|
|
static int iReloadCounter = 0;
|
|
|
|
|
if( iReloadCounter%20==0 )
|
|
|
|
|
{
|
2002-12-29 22:55:26 +00:00
|
|
|
for(int i = 0; i < NUM_RAINBOW_COLORS; ++i)
|
|
|
|
|
RAINBOW_COLORS[i] = RAINBOW_COLOR(i);
|
2002-08-27 23:31:41 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
m_pFont = NULL;
|
2002-02-02 05:11:12 +00:00
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
m_iWidestLineWidth = 0;
|
|
|
|
|
|
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()
|
|
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
if( m_pFont )
|
2002-09-03 22:31:06 +00:00
|
|
|
FONT->UnloadFont( m_pFont->m_sTexturePath );
|
2001-11-25 04:31:44 +00:00
|
|
|
}
|
2001-11-03 10:52:42 +00:00
|
|
|
|
2002-08-13 23:26:46 +00:00
|
|
|
bool BitmapText::LoadFromFont( CString sFontFilePath )
|
2001-11-25 04:31:44 +00:00
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
LOG->Trace( "BitmapText::LoadFromFontName(%s)", sFontFilePath.GetString() );
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-10-13 22:58:31 +00:00
|
|
|
if( m_pFont ) {
|
|
|
|
|
FONT->UnloadFont( m_pFont->m_sTexturePath );
|
|
|
|
|
m_pFont = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
// load font
|
2002-09-03 22:31:06 +00:00
|
|
|
m_pFont = FONT->LoadFont( sFontFilePath );
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool BitmapText::LoadFromTextureAndChars( CString sTexturePath, CString sChars )
|
|
|
|
|
{
|
2002-10-29 07:58:44 +00:00
|
|
|
LOG->Trace( "BitmapText::LoadFromTextureAndChars(%s)", sTexturePath.GetString() );
|
2002-09-03 22:31:06 +00:00
|
|
|
|
2002-10-16 19:31:17 +00:00
|
|
|
if( m_pFont ) {
|
|
|
|
|
FONT->UnloadFont( m_pFont->m_sTexturePath );
|
|
|
|
|
m_pFont = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2002-09-03 22:31:06 +00:00
|
|
|
// load font
|
|
|
|
|
m_pFont = FONT->LoadFont( sTexturePath, sChars );
|
2002-09-03 06:33:08 +00:00
|
|
|
|
2002-03-30 20:00:13 +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-03-30 20:00:13 +00:00
|
|
|
{
|
2002-09-02 21:59:58 +00:00
|
|
|
ASSERT( m_pFont );
|
|
|
|
|
|
2002-03-31 07:55:25 +00:00
|
|
|
if( m_pFont->m_bCapitalsOnly )
|
|
|
|
|
sText.MakeUpper();
|
|
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
m_szText = sText;
|
2002-09-22 20:34:11 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
/* Break the string into lines. */
|
|
|
|
|
m_szTextLines.clear();
|
|
|
|
|
m_iLineWidths.clear();
|
2003-01-04 01:47:01 +00:00
|
|
|
m_iLineHeights.clear();
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
split(m_szText, "\n", m_szTextLines, false);
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
/* calculate line lengths and widths */
|
2002-03-30 20:00:13 +00:00
|
|
|
m_iWidestLineWidth = 0;
|
2002-12-29 22:55:26 +00:00
|
|
|
|
|
|
|
|
for( unsigned l=0; l<m_szTextLines.size(); l++ ) // for each line
|
2002-01-16 10:01:32 +00:00
|
|
|
{
|
2002-12-29 22:55:26 +00:00
|
|
|
m_iLineWidths.push_back(m_pFont->GetLineWidthInSourcePixels( m_szTextLines[l] ));
|
2003-01-04 01:47:01 +00:00
|
|
|
m_iLineHeights.push_back(m_pFont->GetLineHeightInSourcePixels( m_szTextLines[l] ));
|
2002-12-29 22:55:26 +00:00
|
|
|
m_iWidestLineWidth = max(m_iWidestLineWidth, m_iLineWidths.back());
|
2002-01-16 10:01:32 +00:00
|
|
|
}
|
2001-11-27 22:47:30 +00:00
|
|
|
}
|
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 );
|
|
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
for( unsigned l=0; l<m_szTextLines.size(); l++ ) // for each line
|
2002-08-22 09:31:32 +00:00
|
|
|
{
|
|
|
|
|
while( m_iLineWidths[l] > iMaxWidthInSourcePixels )
|
|
|
|
|
{
|
2002-12-29 22:55:26 +00:00
|
|
|
m_szTextLines[l].erase(m_szTextLines[l].end()-1, m_szTextLines[l].end());
|
|
|
|
|
m_iLineWidths[l] = m_pFont->GetLineWidthInSourcePixels( m_szTextLines[l] );
|
2002-08-22 09:31:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2001-11-27 22:47:30 +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()
|
2001-11-27 22:47:30 +00:00
|
|
|
{
|
2002-12-29 22:55:26 +00:00
|
|
|
if( m_szTextLines.empty() )
|
2002-05-19 01:59:48 +00:00
|
|
|
return;
|
2002-04-28 20:42:32 +00:00
|
|
|
|
2002-03-30 20:00:13 +00:00
|
|
|
RageTexture* pTexture = m_pFont->m_pTexture;
|
2001-12-28 10:15:59 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
static RageVertex *v = NULL;
|
|
|
|
|
static int vcnt = 0;
|
|
|
|
|
{
|
|
|
|
|
int charcnt = 0;
|
|
|
|
|
for( unsigned i=0; i<m_szTextLines.size(); i++ )
|
|
|
|
|
charcnt += m_szTextLines[i].size();
|
2001-12-28 10:15:59 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
charcnt *= 4; /* 4 vertices per char */
|
|
|
|
|
if(charcnt > vcnt)
|
|
|
|
|
{
|
|
|
|
|
vcnt = charcnt;
|
|
|
|
|
delete [] v;
|
|
|
|
|
v = new RageVertex[vcnt];
|
|
|
|
|
}
|
|
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
int iNumV = 0; // the current vertex number
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 01:47:01 +00:00
|
|
|
float TotalHeight = 0;
|
|
|
|
|
unsigned i;
|
|
|
|
|
for(i = 0; i < m_szTextLines.size(); ++i)
|
|
|
|
|
TotalHeight += m_iLineHeights[i];
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2003-01-04 01:47:01 +00:00
|
|
|
float iY; // the top 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
|
|
|
{
|
2003-01-04 01:47:01 +00:00
|
|
|
case align_top: iY = 0; break;
|
|
|
|
|
case align_middle: iY = -TotalHeight/2.0f; break;
|
|
|
|
|
case align_bottom: iY = -TotalHeight; break;
|
|
|
|
|
default: ASSERT( false ); return;
|
2002-01-16 10:01:32 +00:00
|
|
|
}
|
2001-12-19 01:50:57 +00:00
|
|
|
|
2003-01-04 01:47:01 +00:00
|
|
|
for( i=0; i<m_szTextLines.size(); i++ ) // foreach line
|
2002-01-16 10:01:32 +00:00
|
|
|
{
|
2002-12-29 22:55:26 +00:00
|
|
|
const CString &szLine = m_szTextLines[i];
|
2003-01-04 01:47:01 +00:00
|
|
|
const float fLineWidth = float(m_iLineWidths[i]);
|
2002-03-30 20:00:13 +00:00
|
|
|
|
2003-01-04 01:47:01 +00:00
|
|
|
float iX;
|
2002-01-16 10:01:32 +00:00
|
|
|
switch( m_HorizAlign )
|
2001-12-28 10:15:59 +00:00
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
case align_left: iX = 0; break;
|
2003-01-04 01:47:01 +00:00
|
|
|
case align_center: iX = -fLineWidth/2.0f; break;
|
|
|
|
|
case align_right: iX = -fLineWidth; break;
|
2002-09-07 10:01:42 +00:00
|
|
|
default: ASSERT( false ); return;
|
2001-12-19 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
for( unsigned j=0; j<szLine.size(); j++ ) // for each character in the line
|
2002-01-16 10:01:32 +00:00
|
|
|
{
|
2002-03-30 20:00:13 +00:00
|
|
|
const char c = szLine[j];
|
2003-01-03 22:53:22 +00:00
|
|
|
if(m_pFont->m_iCharToFrameNo.find(c) == m_pFont->m_iCharToFrameNo.end())
|
2002-12-21 19:32:38 +00:00
|
|
|
RageException::Throw( "The font '%s' does not implement the character '%c'", m_sFontFilePath.GetString(), c );
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-03 22:53:22 +00:00
|
|
|
const int iFrameNo = m_pFont->m_iCharToFrameNo[ (unsigned char)c ];
|
|
|
|
|
|
|
|
|
|
const glyph &g = m_pFont->GetGlyph(iFrameNo);
|
|
|
|
|
|
2003-01-03 20:59:50 +00:00
|
|
|
/* set vertex positions */
|
2003-01-04 01:47:01 +00:00
|
|
|
v[iNumV++].p = RageVector3( (float)iX+g.hshift, iY+g.vshift, 0 ); // top left
|
|
|
|
|
v[iNumV++].p = RageVector3( (float)iX+g.hshift, iY+g.vshift+g.height, 0 ); // bottom left
|
|
|
|
|
v[iNumV++].p = RageVector3( (float)iX+g.hshift+g.width, iY+g.vshift+g.height, 0 ); // bottom right
|
|
|
|
|
v[iNumV++].p = RageVector3( (float)iX+g.hshift+g.width, iY+g.vshift, 0 ); // top right
|
2003-01-03 20:59:50 +00:00
|
|
|
|
|
|
|
|
/* Advance the cursor. */
|
2003-01-04 01:47:01 +00:00
|
|
|
iX += g.advance;
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-03 20:59:50 +00:00
|
|
|
/* set texture coordinates */
|
|
|
|
|
iNumV -= 4;
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2003-01-04 01:47:01 +00:00
|
|
|
v[iNumV++].t = RageVector2( g.rect.left, g.rect.top );
|
|
|
|
|
v[iNumV++].t = RageVector2( g.rect.left, g.rect.bottom );
|
|
|
|
|
v[iNumV++].t = RageVector2( g.rect.right, g.rect.bottom );
|
|
|
|
|
v[iNumV++].t = RageVector2( g.rect.right, g.rect.top );
|
2002-01-16 10:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
2003-01-04 01:47:01 +00:00
|
|
|
iY += m_iLineHeights[i];
|
2001-12-19 01:50:57 +00:00
|
|
|
}
|
|
|
|
|
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-08-18 16:19:26 +00:00
|
|
|
DISPLAY->SetTexture( pTexture );
|
2002-11-11 04:53:31 +00:00
|
|
|
DISPLAY->SetTextureModeModulate();
|
2002-08-18 16:19:26 +00:00
|
|
|
if( m_bBlendAdd )
|
|
|
|
|
DISPLAY->SetBlendModeAdd();
|
|
|
|
|
else
|
|
|
|
|
DISPLAY->SetBlendModeNormal();
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-11-12 01:29:19 +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
|
|
|
{
|
2002-08-18 16:19:26 +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;
|
2002-11-11 04:53:31 +00:00
|
|
|
DISPLAY->DrawQuads( v, iNumV );
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-08-18 16:19:26 +00:00
|
|
|
DISPLAY->PopMatrix();
|
|
|
|
|
}
|
2002-05-29 09:47:24 +00:00
|
|
|
|
2002-08-18 16:19:26 +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;
|
2002-08-27 23:31:41 +00:00
|
|
|
for( int i=0; i<iNumV; i+=4 )
|
2002-08-18 16:19:26 +00:00
|
|
|
{
|
2002-10-28 05:30:45 +00:00
|
|
|
const RageColor color = RAINBOW_COLORS[color_index];
|
2002-08-27 23:31:41 +00:00
|
|
|
for( int j=i; j<i+4; j++ )
|
2002-10-28 05:30:45 +00:00
|
|
|
v[j].c = color;
|
2002-08-18 16:19:26 +00:00
|
|
|
|
|
|
|
|
color_index = (color_index+1)%NUM_RAINBOW_COLORS;
|
|
|
|
|
}
|
2002-05-29 09:47:24 +00:00
|
|
|
}
|
2002-08-18 16:19:26 +00:00
|
|
|
else
|
2002-05-29 09:47:24 +00:00
|
|
|
{
|
2002-08-18 16:19:26 +00:00
|
|
|
for( int i=0; i<iNumV; i+=4 )
|
|
|
|
|
{
|
2002-10-28 05:30:45 +00:00
|
|
|
v[i+0].c = m_temp.diffuse[0]; // top left
|
2002-11-11 04:53:31 +00:00
|
|
|
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-08-18 16:19:26 +00:00
|
|
|
}
|
2002-05-29 09:47:24 +00:00
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-11-11 04:53:31 +00:00
|
|
|
DISPLAY->DrawQuads( v, iNumV );
|
2002-08-18 16:19:26 +00:00
|
|
|
}
|
2002-01-16 10:01:32 +00:00
|
|
|
|
2002-12-29 22:55:26 +00:00
|
|
|
/* render the glow pass */
|
2002-09-02 21:59:58 +00:00
|
|
|
if( m_temp.glow.a != 0 )
|
2002-01-16 10:01:32 +00:00
|
|
|
{
|
2002-11-11 04:53:31 +00:00
|
|
|
DISPLAY->SetTextureModeGlow();
|
2002-08-19 20:02:30 +00:00
|
|
|
|
2002-08-19 18:59:58 +00:00
|
|
|
int i;
|
|
|
|
|
for( i=0; i<iNumV; i++ )
|
2002-10-28 05:30:45 +00:00
|
|
|
v[i].c = m_temp.glow;
|
2002-11-11 04:53:31 +00:00
|
|
|
DISPLAY->DrawQuads( v, iNumV );
|
2002-01-16 10:01:32 +00:00
|
|
|
}
|
2002-11-16 08:54:15 +00:00
|
|
|
}
|