Set up texture coords for fonts in Font, so we don't have to deal with
it in BitmapText.
This commit is contained in:
@@ -160,7 +160,6 @@ void BitmapText::DrawPrimitives()
|
|||||||
// make the object in logical units centered at the origin
|
// make the object in logical units centered at the origin
|
||||||
const int iHeight = pTexture->GetSourceFrameHeight(); // height of a character
|
const int iHeight = pTexture->GetSourceFrameHeight(); // height of a character
|
||||||
const int iLineSpacing = m_pFont->m_iLineSpacing; // spacing between lines
|
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
|
int iY; // the center position of the first row of characters
|
||||||
switch( m_VertAlign )
|
switch( m_VertAlign )
|
||||||
@@ -191,44 +190,25 @@ void BitmapText::DrawPrimitives()
|
|||||||
const int iFrameNo = m_pFont->m_iCharToFrameNo[ (unsigned char)c ];
|
const int iFrameNo = m_pFont->m_iCharToFrameNo[ (unsigned char)c ];
|
||||||
if( iFrameNo == -1 ) // this font doesn't impelemnt this character
|
if( iFrameNo == -1 ) // this font doesn't impelemnt this character
|
||||||
RageException::Throw( "The font '%s' does not implement the character '%c'", m_sFontFilePath.GetString(), c );
|
RageException::Throw( "The font '%s' does not implement the character '%c'", m_sFontFilePath.GetString(), c );
|
||||||
const int iCharWidth = m_pFont->m_iFrameNoToWidth[iFrameNo];
|
|
||||||
|
|
||||||
// The right side of any italic letter is being cropped. So, we're going to draw a little bit
|
/* set vertex positions */
|
||||||
// to the right of the normal character.
|
v[iNumV++].p = RageVector3( (float)iX-m_pFont->m_Left[iFrameNo], iY-iHeight/2.0f, 0 ); // top left
|
||||||
const int iDrawExtraPixelsLeft = min( m_pFont->m_iDrawExtraPixelsLeft, (iFrameWidth-iCharWidth)/2 );
|
v[iNumV++].p = RageVector3( (float)iX-m_pFont->m_Left[iFrameNo], iY+iHeight/2.0f, 0 ); // bottom left
|
||||||
const int iDrawExtraPixelsRight = min( m_pFont->m_iDrawExtraPixelsRight, (iFrameWidth-iCharWidth)/2 );
|
v[iNumV++].p = RageVector3( (float)iX+m_pFont->m_Right[iFrameNo], iY+iHeight/2.0f, 0 ); // bottom right
|
||||||
|
v[iNumV++].p = RageVector3( (float)iX+m_pFont->m_Right[iFrameNo], iY-iHeight/2.0f, 0 ); // top right
|
||||||
|
|
||||||
//
|
/* Advance the cursor. */
|
||||||
// set vertex positions
|
iX += m_pFont->m_iFrameNoToWidth[iFrameNo];
|
||||||
//
|
|
||||||
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
|
|
||||||
|
|
||||||
iX += iCharWidth;
|
/* set texture coordinates */
|
||||||
|
|
||||||
//
|
|
||||||
// set texture coordinates
|
|
||||||
//
|
|
||||||
iNumV -= 4;
|
iNumV -= 4;
|
||||||
|
|
||||||
RectF frectTexCoords = *pTexture->GetTextureCoordRect( iFrameNo );
|
RectF frectTexCoords = m_pFont->GetTextureCoordRect( iFrameNo );
|
||||||
|
|
||||||
// Tweak the textures frame rectangles so we don't draw extra
|
v[iNumV++].t = RageVector2( frectTexCoords.left, frectTexCoords.top ); // top left
|
||||||
// to the left and right of the character, saving us fill rate.
|
v[iNumV++].t = RageVector2( frectTexCoords.left, frectTexCoords.bottom ); // bottom left
|
||||||
float fPixelsToChopOff = pTexture->GetSourceFrameWidth() - (float)iCharWidth;
|
v[iNumV++].t = RageVector2( frectTexCoords.right, frectTexCoords.bottom ); // bottom right
|
||||||
float fTexCoordsToChopOff = fPixelsToChopOff / pTexture->GetSourceWidth();
|
v[iNumV++].t = RageVector2( frectTexCoords.right, frectTexCoords.top ); // top right
|
||||||
frectTexCoords.left += fTexCoordsToChopOff/2;
|
|
||||||
frectTexCoords.right -= fTexCoordsToChopOff/2;
|
|
||||||
|
|
||||||
const float fExtraTexCoordsLeft = iDrawExtraPixelsLeft / (float)pTexture->GetSourceWidth();
|
|
||||||
const float fExtraTexCoordsRight = iDrawExtraPixelsRight / (float)pTexture->GetSourceWidth();
|
|
||||||
|
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
iY += iLineSpacing;
|
iY += iLineSpacing;
|
||||||
|
|||||||
+68
-26
@@ -21,10 +21,8 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
|
||||||
Font::Font( const CString &sASCIITexturePath )
|
void Font::Init()
|
||||||
{
|
{
|
||||||
//LOG->Trace( "Font::LoadFromFontName(%s)", sASCIITexturePath.GetString() );
|
|
||||||
|
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
for( i=0; i<MAX_FONT_CHARS; i++ )
|
for( i=0; i<MAX_FONT_CHARS; i++ )
|
||||||
@@ -33,7 +31,16 @@ Font::Font( const CString &sASCIITexturePath )
|
|||||||
m_iFrameNoToWidth[i] = -1;
|
m_iFrameNoToWidth[i] = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_bCapitalsOnly = false;
|
||||||
|
|
||||||
m_iRefCount = 1;
|
m_iRefCount = 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
Font::Font( const CString &sASCIITexturePath )
|
||||||
|
{
|
||||||
|
//LOG->Trace( "Font::LoadFromFontName(%s)", sASCIITexturePath.GetString() );
|
||||||
|
|
||||||
|
Init();
|
||||||
|
|
||||||
// load texture
|
// load texture
|
||||||
m_sTexturePath = sASCIITexturePath;
|
m_sTexturePath = sASCIITexturePath;
|
||||||
@@ -43,6 +50,7 @@ Font::Font( const CString &sASCIITexturePath )
|
|||||||
if( m_pTexture->GetNumFrames() != 16*16 )
|
if( m_pTexture->GetNumFrames() != 16*16 )
|
||||||
RageException::Throw( "The font '%s' has only %d frames. All fonts must have 16*16 frames.", m_sTexturePath.GetString() );
|
RageException::Throw( "The font '%s' has only %d frames. All fonts must have 16*16 frames.", m_sTexturePath.GetString() );
|
||||||
|
|
||||||
|
int i;
|
||||||
for( i=0; i<256; i++ )
|
for( i=0; i<256; i++ )
|
||||||
m_iCharToFrameNo[i] = i;
|
m_iCharToFrameNo[i] = i;
|
||||||
|
|
||||||
@@ -59,14 +67,11 @@ Font::Font( const CString &sASCIITexturePath )
|
|||||||
if( !ini.GetValueI( "Char Widths", ssprintf("%d",i), m_iFrameNoToWidth[i] ) )
|
if( !ini.GetValueI( "Char Widths", ssprintf("%d",i), m_iFrameNoToWidth[i] ) )
|
||||||
RageException::Throw( "Error reading width value '%d' from '%s'.", i, sIniPath.GetString() );
|
RageException::Throw( "Error reading width value '%d' from '%s'.", i, sIniPath.GetString() );
|
||||||
|
|
||||||
m_bCapitalsOnly = false;
|
|
||||||
ini.GetValueB( "Char Widths", "CapitalsOnly", m_bCapitalsOnly );
|
ini.GetValueB( "Char Widths", "CapitalsOnly", m_bCapitalsOnly );
|
||||||
|
int DrawExtraPixelsLeft = 0, DrawExtraPixelsRight = 0;
|
||||||
|
|
||||||
m_iDrawExtraPixelsLeft = 0;
|
ini.GetValueI( "Char Widths", "DrawExtraPixelsLeft", DrawExtraPixelsLeft );
|
||||||
ini.GetValueI( "Char Widths", "DrawExtraPixelsLeft", m_iDrawExtraPixelsLeft );
|
ini.GetValueI( "Char Widths", "DrawExtraPixelsRight", DrawExtraPixelsRight );
|
||||||
|
|
||||||
m_iDrawExtraPixelsRight = 0;
|
|
||||||
ini.GetValueI( "Char Widths", "DrawExtraPixelsRight", m_iDrawExtraPixelsRight );
|
|
||||||
|
|
||||||
int iAddToAllWidths = 0;
|
int iAddToAllWidths = 0;
|
||||||
if( ini.GetValueI( "Char Widths", "AddToAllWidths", iAddToAllWidths ) )
|
if( ini.GetValueI( "Char Widths", "AddToAllWidths", iAddToAllWidths ) )
|
||||||
@@ -85,28 +90,16 @@ Font::Font( const CString &sASCIITexturePath )
|
|||||||
m_iLineSpacing = m_pTexture->GetSourceFrameHeight();
|
m_iLineSpacing = m_pTexture->GetSourceFrameHeight();
|
||||||
ini.GetValueI( "Char Widths", "LineSpacing", m_iLineSpacing );
|
ini.GetValueI( "Char Widths", "LineSpacing", m_iLineSpacing );
|
||||||
|
|
||||||
// force widths to even number
|
SetTextureCoords();
|
||||||
for( i=0; i<256; i++ )
|
SetExtraPixels(DrawExtraPixelsLeft, DrawExtraPixelsRight);
|
||||||
if( m_iFrameNoToWidth[i]%2 == 1 )
|
|
||||||
m_iFrameNoToWidth[i]++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Font::Font( const CString &sTexturePath, const CString& sCharacters )
|
Font::Font( const CString &sTexturePath, const CString& sCharacters )
|
||||||
{
|
{
|
||||||
//LOG->Trace( "Font::LoadFromFontName(%s)", sFontFilePath.GetString() );
|
//LOG->Trace( "Font::LoadFromFontName(%s)", sFontFilePath.GetString() );
|
||||||
int i;
|
|
||||||
for( i=0; i<MAX_FONT_CHARS; i++ )
|
|
||||||
{
|
|
||||||
m_iCharToFrameNo[i] = -1;
|
|
||||||
m_iFrameNoToWidth[i] = -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
m_bCapitalsOnly = false;
|
Init();
|
||||||
m_iDrawExtraPixelsLeft = 0;
|
|
||||||
m_iDrawExtraPixelsRight = 0;
|
|
||||||
|
|
||||||
m_iRefCount = 1;
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// load texture
|
// load texture
|
||||||
@@ -114,12 +107,10 @@ Font::Font( const CString &sTexturePath, const CString& sCharacters )
|
|||||||
m_sTexturePath = sTexturePath; // save
|
m_sTexturePath = sTexturePath; // save
|
||||||
m_sTexturePath.MakeLower();
|
m_sTexturePath.MakeLower();
|
||||||
|
|
||||||
|
|
||||||
m_pTexture = TEXTUREMAN->LoadTexture( m_sTexturePath );
|
m_pTexture = TEXTUREMAN->LoadTexture( m_sTexturePath );
|
||||||
ASSERT( m_pTexture != NULL );
|
ASSERT( m_pTexture != NULL );
|
||||||
m_iLineSpacing = m_pTexture->GetSourceFrameHeight();
|
m_iLineSpacing = m_pTexture->GetSourceFrameHeight();
|
||||||
|
|
||||||
|
|
||||||
//
|
//
|
||||||
// find out what characters are in this font
|
// find out what characters are in this font
|
||||||
//
|
//
|
||||||
@@ -129,6 +120,7 @@ Font::Font( const CString &sTexturePath, const CString& sCharacters )
|
|||||||
m_sTexturePath.GetString(), m_pTexture->GetNumFrames(), sCharacters.GetLength() );
|
m_sTexturePath.GetString(), m_pTexture->GetNumFrames(), sCharacters.GetLength() );
|
||||||
|
|
||||||
// set the char to frame number map
|
// set the char to frame number map
|
||||||
|
int i;
|
||||||
for( i=0; i<sCharacters.GetLength(); i++ )
|
for( i=0; i<sCharacters.GetLength(); i++ )
|
||||||
{
|
{
|
||||||
char c = sCharacters[i];
|
char c = sCharacters[i];
|
||||||
@@ -140,8 +132,58 @@ Font::Font( const CString &sTexturePath, const CString& sCharacters )
|
|||||||
// Assume each character is the width of the frame.
|
// Assume each character is the width of the frame.
|
||||||
for( i=0; i<(int)m_pTexture->GetNumFrames(); i++ )
|
for( i=0; i<(int)m_pTexture->GetNumFrames(); i++ )
|
||||||
m_iFrameNoToWidth[i] = m_pTexture->GetSourceFrameWidth();
|
m_iFrameNoToWidth[i] = m_pTexture->GetSourceFrameWidth();
|
||||||
|
|
||||||
|
SetTextureCoords();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Font::SetTextureCoords()
|
||||||
|
{
|
||||||
|
// force widths to even number
|
||||||
|
// Why do this? It seems to just artificially widen some characters a little and
|
||||||
|
// make it look a little worse in 640x480 ...
|
||||||
|
/* for( i=0; i<256; i++ )
|
||||||
|
if( m_iFrameNoToWidth[i]%2 == 1 )
|
||||||
|
m_iFrameNoToWidth[i]++;
|
||||||
|
*/
|
||||||
|
|
||||||
|
for(int i = 0; i < m_pTexture->GetNumFrames(); ++i)
|
||||||
|
{
|
||||||
|
/* Make a copy of each texture rect, reducing each to the actual dimensions
|
||||||
|
* of the character (most characters don't take a full block). */
|
||||||
|
RectF r = *m_pTexture->GetTextureCoordRect(i);;
|
||||||
|
|
||||||
|
float fPixelsToChopOff = m_pTexture->GetSourceFrameWidth() - (float)m_iFrameNoToWidth[i];
|
||||||
|
float fTexCoordsToChopOff = fPixelsToChopOff / m_pTexture->GetSourceWidth();
|
||||||
|
|
||||||
|
r.left += fTexCoordsToChopOff/2;
|
||||||
|
r.right -= fTexCoordsToChopOff/2;
|
||||||
|
m_TextureCoordRects.push_back(r);
|
||||||
|
|
||||||
|
/* Add normal widths. */
|
||||||
|
m_Left.push_back(0);
|
||||||
|
m_Right.push_back(m_iFrameNoToWidth[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void Font::SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight)
|
||||||
|
{
|
||||||
|
/* Adjust for DrawExtraPixelsLeft and DrawExtraPixelsRight. */
|
||||||
|
for(unsigned i = 0; i < m_TextureCoordRects.size(); ++i)
|
||||||
|
{
|
||||||
|
int iFrameWidth = m_pTexture->GetSourceFrameWidth();
|
||||||
|
int iCharWidth = m_iFrameNoToWidth[i];
|
||||||
|
|
||||||
|
/* Extra pixels to draw to the left and right. */
|
||||||
|
int ExtraLeft = min( DrawExtraPixelsLeft, (iFrameWidth-iCharWidth)/2 );
|
||||||
|
int ExtraRight = min( DrawExtraPixelsRight, (iFrameWidth-iCharWidth)/2 ) + ExtraLeft;
|
||||||
|
|
||||||
|
/* Move left and expand right. */
|
||||||
|
m_TextureCoordRects[i].left -= float(ExtraLeft) / m_pTexture->GetSourceWidth();
|
||||||
|
m_TextureCoordRects[i].right += float(ExtraRight) / m_pTexture->GetSourceWidth();
|
||||||
|
m_Left[i] += ExtraLeft;
|
||||||
|
m_Right[i] += ExtraRight;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Font::~Font()
|
Font::~Font()
|
||||||
{
|
{
|
||||||
|
|||||||
+11
-2
@@ -22,6 +22,7 @@ public:
|
|||||||
Font( const CString &sASCIITexturePath );
|
Font( const CString &sASCIITexturePath );
|
||||||
Font( const CString &sTexturePath, const CString& sChars );
|
Font( const CString &sTexturePath, const CString& sChars );
|
||||||
~Font();
|
~Font();
|
||||||
|
void Init();
|
||||||
|
|
||||||
int GetLineWidthInSourcePixels( const CString &szLine );
|
int GetLineWidthInSourcePixels( const CString &szLine );
|
||||||
|
|
||||||
@@ -31,13 +32,21 @@ public:
|
|||||||
|
|
||||||
RageTexture* m_pTexture;
|
RageTexture* m_pTexture;
|
||||||
bool m_bCapitalsOnly;
|
bool m_bCapitalsOnly;
|
||||||
int m_iDrawExtraPixelsLeft, m_iDrawExtraPixelsRight; // for italic fonts
|
|
||||||
int m_iLineSpacing;
|
int m_iLineSpacing;
|
||||||
|
|
||||||
int m_iCharToFrameNo[MAX_FONT_CHARS];
|
int m_iCharToFrameNo[MAX_FONT_CHARS];
|
||||||
int m_iFrameNoToWidth[MAX_FONT_CHARS]; // in soure coordinate space
|
int m_iFrameNoToWidth[MAX_FONT_CHARS]; // in soure coordinate space
|
||||||
|
|
||||||
protected:
|
const RectF &GetTextureCoordRect( int frameNo ) const { return m_TextureCoordRects[frameNo]; }
|
||||||
|
|
||||||
|
/* Source pixels to print to the left and right of each character.
|
||||||
|
* m_Left[] is usually 0. */
|
||||||
|
vector<int> m_Left, m_Right;
|
||||||
|
|
||||||
|
private:
|
||||||
|
vector<RectF> m_TextureCoordRects;
|
||||||
|
void SetExtraPixels(int DrawExtraPixelsLeft, int DrawExtraPixelsRight);
|
||||||
|
void SetTextureCoords();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user