Clean up.

--HG--
branch : dev-font
This commit is contained in:
Martí­n Lucas Golini
2017-03-12 02:51:12 -03:00
parent 16c48d40c4
commit f07c98fee3
8 changed files with 152 additions and 198 deletions

View File

@@ -13,4 +13,4 @@ JoystickEnabled = 0
ParticlesNum = 1000
UseShaders = 1
Music = 0
PixelDensity = 1
PixelDensity = 2

View File

@@ -67,9 +67,9 @@ class EE_API Font {
/** @return The cursor position inside the string */
Vector2i getCursorPos( const String& Text, const Uint32& Pos );
const eeGlyph& getGlyph( const Uint32& index );
const Glyph& getGlyph( const Uint32& index );
const eeTexCoords& getTexCoords( const Uint32& index );
const TextureCoords& getTextureCoords( const Uint32& index );
Uint32 getGlyphCount() const;
protected:
@@ -83,8 +83,8 @@ class EE_API Font {
Int32 mAscent;
Int32 mDescent;
std::vector<eeGlyph> mGlyphs;
std::vector<eeTexCoords> mTexCoords;
std::vector<Glyph> mGlyphs;
std::vector<TextureCoords> mTexCoords;
TextCache mTextCache;

View File

@@ -36,17 +36,12 @@ inline Uint32 fontVAlignGet( Uint32 Flags ) {
#define FONT_DRAW_ALIGN_MASK ( FONT_DRAW_VALIGN_MASK | FONT_DRAW_HALIGN_MASK )
/** Basic Glyph structure used by the engine */
struct eeGlyph {
struct Glyph {
Int32 MinX, MaxX, MinY, MaxY, Advance;
Uint16 CurX, CurY, CurW, CurH, GlyphH;
};
struct eeVertexCoords {
Float TexCoords[2];
Float Vertex[2];
};
struct eeTexCoords {
struct TextureCoords {
Float TexCoords[8];
Float Vertex[8];
};

View File

@@ -42,9 +42,6 @@ class EE_API TextCache {
/** @return Every cached text line width */
const std::vector<Float>& getLinesWidth();
/** @return The vertex coordinates cached */
std::vector<eeVertexCoords>& getVertextCoords();
/** @return The text colors cached */
std::vector<ColorA>& getColors();
@@ -86,6 +83,11 @@ class EE_API TextCache {
/** Force to cache the width of the current text */
void cacheWidth();
protected:
struct VertexCoords {
Float TexCoords[2];
Float Vertex[2];
};
friend class Font;
String mText;
@@ -104,22 +106,12 @@ class EE_API TextCache {
bool mCachedCoords;
std::vector<Float> mLinesWidth;
std::vector<eeVertexCoords> mRenderCoords;
std::vector<VertexCoords> mRenderCoords;
std::vector<ColorA> mColors;
void internalDraw( const Float& X, const Float& Y, const Vector2f& Scale, const Float& Angle, EE_BLEND_MODE Effect );
void cacheVerts(const Int32 & X, const Int32 & Y);
void cacheVerts();
void updateCoords();
const bool& cachedCoords() const;
void cachedCoords( const bool& cached );
const unsigned int& cachedVerts() const;
void cachedVerts( const unsigned int& num );
};
}}

View File

@@ -177,11 +177,13 @@ Vector2i Font::getCursorPos( const String& Text, const Uint32& Pos ) {
return Vector2i( Width, Height );
}
const eeGlyph& Font::getGlyph(const Uint32 & index) {
const Glyph& Font::getGlyph(const Uint32 & index) {
eeASSERT( index < mGlyphs.size() );
return mGlyphs[ index ];
}
const eeTexCoords& Font::getTexCoords(const Uint32 & index) {
const TextureCoords& Font::getTextureCoords(const Uint32 & index) {
eeASSERT( index < mTexCoords.size() );
return mTexCoords[ index ];
}
@@ -232,55 +234,55 @@ void Font::shrinkText( std::string& Str, const Uint32& MaxWidth ) {
if ( !Str.size() )
return;
Float tCurWidth = 0.f;
Float tWordWidth = 0.f;
Float tMaxWidth = (Float) MaxWidth;
char * tStringLoop = &Str[0];
char * tLastSpace = NULL;
Uint32 tGlyphSize = (Uint32)mGlyphs.size();
Float tCurWidth = 0.f;
Float tWordWidth = 0.f;
Float tMaxWidth = (Float) MaxWidth;
char * tChar = &Str[0];
char * tLastSpace = NULL;
Uint32 tGlyphSize = (Uint32)mGlyphs.size();
while ( *tStringLoop ) {
if ( (Uint32)( *tStringLoop ) < tGlyphSize ) {
eeGlyph * pChar = &mGlyphs[ ( *tStringLoop ) ];
while ( *tChar ) {
if ( (Uint32)( *tChar ) < tGlyphSize ) {
Glyph * pChar = &mGlyphs[ ( *tChar ) ];
Float fCharWidth = (Float)pChar->Advance;
if ( ( *tStringLoop ) == '\t' )
if ( ( *tChar ) == '\t' )
fCharWidth += pChar->Advance * 3;
tWordWidth += fCharWidth;
if ( ' ' == *tStringLoop || '\0' == *( tStringLoop + 1 ) ) {
if ( ' ' == *tChar || '\0' == *( tChar + 1 ) ) {
if ( tCurWidth + tWordWidth < tMaxWidth ) {
tCurWidth += tWordWidth;
tLastSpace = tStringLoop;
tLastSpace = tChar;
tStringLoop++;
tChar++;
} else {
if ( NULL != tLastSpace ) {
*tLastSpace = '\n';
tStringLoop = tLastSpace + 1;
tChar = tLastSpace + 1;
} else {
*tStringLoop = '\n';
*tChar = '\n';
}
if ( '\0' == *( tStringLoop + 1 ) )
tStringLoop++;
if ( '\0' == *( tChar + 1 ) )
tChar++;
tLastSpace = NULL;
tCurWidth = 0.f;
}
tWordWidth = 0.f;
} else if ( '\n' == *tStringLoop ) {
} else if ( '\n' == *tChar ) {
tWordWidth = 0.f;
tCurWidth = 0.f;
tLastSpace = NULL;
tStringLoop++;
tChar++;
} else {
tStringLoop++;
tChar++;
}
} else {
*tStringLoop = ' ';
*tChar = ' ';
}
}
}
@@ -289,43 +291,43 @@ void Font::shrinkText( String& Str, const Uint32& MaxWidth ) {
if ( !Str.size() )
return;
Float tCurWidth = 0.f;
Float tWordWidth = 0.f;
Float tMaxWidth = (Float) MaxWidth;
String::StringBaseType * tStringLoop = &Str[0];
String::StringBaseType * tLastSpace = NULL;
Float tCurWidth = 0.f;
Float tWordWidth = 0.f;
Float tMaxWidth = (Float) MaxWidth;
String::StringBaseType * tChar = &Str[0];
String::StringBaseType * tLastSpace = NULL;
while ( *tStringLoop ) {
if ( (String::StringBaseType)( *tStringLoop ) < mGlyphs.size() ) {
eeGlyph * pChar = &mGlyphs[ ( *tStringLoop ) ];
while ( *tChar ) {
if ( (String::StringBaseType)( *tChar ) < mGlyphs.size() ) {
Glyph * pChar = &mGlyphs[ ( *tChar ) ];
Float fCharWidth = (Float)pChar->Advance;
if ( ( *tStringLoop ) == '\t' )
if ( ( *tChar ) == '\t' )
fCharWidth += pChar->Advance * 3;
// Add the new char width to the current word width
tWordWidth += fCharWidth;
if ( ' ' == *tStringLoop || '\0' == *( tStringLoop + 1 ) ) {
if ( ' ' == *tChar || '\0' == *( tChar + 1 ) ) {
// If current width plus word width is minor to the max width, continue adding
if ( tCurWidth + tWordWidth < tMaxWidth ) {
tCurWidth += tWordWidth;
tLastSpace = tStringLoop;
tLastSpace = tChar;
tStringLoop++;
tChar++;
} else {
// If it was an space before, replace that space for an new line
// Start counting from the new line first character
if ( NULL != tLastSpace ) {
*tLastSpace = '\n';
tStringLoop = tLastSpace + 1;
tChar = tLastSpace + 1;
} else { // The word is larger than the current possible width
*tStringLoop = '\n';
*tChar = '\n';
}
if ( '\0' == *( tStringLoop + 1 ) )
tStringLoop++;
if ( '\0' == *( tChar + 1 ) )
tChar++;
// Set the last spaces as null, because is a new line
tLastSpace = NULL;
@@ -336,16 +338,16 @@ void Font::shrinkText( String& Str, const Uint32& MaxWidth ) {
// New word, so we reset the current word width
tWordWidth = 0.f;
} else if ( '\n' == *tStringLoop ) {
} else if ( '\n' == *tChar ) {
tWordWidth = 0.f;
tCurWidth = 0.f;
tLastSpace = NULL;
tStringLoop++;
tChar++;
} else {
tStringLoop++;
tChar++;
}
} else { // Replace any unknown char as spaces.
*tStringLoop = ' ';
*tChar = ' ';
}
}
}

View File

@@ -153,10 +153,6 @@ void TextCache::setShadowColor(const ColorA& color) {
mFontShadowColor = color;
}
std::vector<eeVertexCoords>& TextCache::getVertextCoords() {
return mRenderCoords;
}
std::vector<ColorA>& TextCache::getColors() {
return mColors;
}
@@ -171,81 +167,8 @@ void TextCache::cacheWidth() {
mCachedCoords = false;
}
void TextCache::internalDraw( const Float& X, const Float& Y, const Vector2f & Scale, const Float & Angle, EE_BLEND_MODE Effect ) {
GlobalBatchRenderer::instance()->draw();
TextureFactory::instance()->bind( mFont->getTexId() );
BlendMode::setMode( Effect );
if ( mFlags & FONT_DRAW_SHADOW ) {
Uint32 f = mFlags;
mFlags &= ~FONT_DRAW_SHADOW;
ColorA Col = getColor();
if ( Col.a() != 255 ) {
ColorA ShadowColor = getShadowColor();
ShadowColor.Alpha = (Uint8)( (Float)ShadowColor.Alpha * ( (Float)Col.a() / (Float)255 ) );
setColor( ShadowColor );
} else {
setColor( getShadowColor() );
}
Float pd = PixelDensity::getPixelDensity();
GLi->translatef( 1 * pd , 1 * pd, 0.f );
internalDraw( X, Y, Scale, Angle, Effect );
GLi->translatef( -1 * pd , -1 * pd, 0.f );
mFlags = f;
setColor( Col );
}
Float cX = (Float) ( (Int32)X );
Float cY = (Float) ( (Int32)Y );
unsigned int numvert = 0;
if ( Angle != 0.0f || Scale != 1.0f ) {
GLi->pushMatrix();
Vector2f Center( cX + getTextWidth() * 0.5f, cY + getTextHeight() * 0.5f );
GLi->translatef( Center.x , Center.y, 0.f );
GLi->rotatef( Angle, 0.0f, 0.0f, 1.0f );
GLi->scalef( Scale.x, Scale.y, 1.0f );
GLi->translatef( -Center.x + X, -Center.y + Y, 0.f );
}
if ( !cachedCoords() ) {
cacheVerts( X, Y );
}
numvert = cachedVerts();
Uint32 alloc = numvert * sizeof(eeVertexCoords);
Uint32 allocC = numvert * GLi->quadVertexs();
GLi->colorPointer ( 4, GL_UNSIGNED_BYTE , 0 , reinterpret_cast<char*>( &mColors[0] ) , allocC );
GLi->texCoordPointer( 2, GL_FP , sizeof(eeVertexCoords), reinterpret_cast<char*>( &mRenderCoords[0] ) , alloc );
GLi->vertexPointer ( 2, GL_FP , sizeof(eeVertexCoords), reinterpret_cast<char*>( &mRenderCoords[0] ) + sizeof(Float) * 2 , alloc );
if ( GLi->quadsSupported() ) {
GLi->drawArrays( GL_QUADS, 0, numvert );
} else {
GLi->drawArrays( GL_TRIANGLES, 0, numvert );
}
if ( Angle != 0.0f || Scale != 1.0f ) {
GLi->popMatrix();
}
}
void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
if ( cachedCoords() )
void TextCache::cacheVerts() {
if ( mCachedCoords )
return;
Float nX = 0;
@@ -267,8 +190,6 @@ void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
}
Uint32 tGlyphSize = mFont->getGlyphCount();
Float cX = (Float) ( (Int32)X );
Float cY = (Float) ( (Int32)Y );
unsigned int numvert = 0;
for ( unsigned int i = 0; i < getText().size(); i++ ) {
@@ -278,8 +199,8 @@ void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
Char = 256 + Char;
if ( Char >= 0 && Char < tGlyphSize ) {
eeTexCoords C = mFont->getTexCoords( Char );
eeGlyph Glyph = mFont->getGlyph( Char );
TextureCoords C = mFont->getTextureCoords( Char );
Glyph Glyph = mFont->getGlyph( Char );
switch( Char ) {
case '\v':
@@ -301,7 +222,7 @@ void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
case '\n':
{
if ( mFlags & FONT_DRAW_VERTICAL ) {
nX += (mFont->getFontHeight());
nX += mFont->getFontHeight();
nY = 0;
} else {
if ( i + 1 < getText().size() ) {
@@ -317,7 +238,7 @@ void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
}
}
nY += (mFont->getFontHeight());
nY += mFont->getLineSkip();
Line++;
}
@@ -329,45 +250,45 @@ void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
for ( Uint8 z = 0; z < 8; z+=2 ) {
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[z];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ z + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[z] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ z + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[z] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ z + 1 ] + nY;
numvert++;
}
} else {
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[2];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 2 + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[2] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 2 + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[2] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ 2 + 1 ] + nY;
numvert++;
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[0];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 0 + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[0] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 0 + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[0] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ 0 + 1 ] + nY;
numvert++;
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[6];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 6 + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[6] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 6 + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[6] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ 6 + 1 ] + nY;
numvert++;
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[2];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 2 + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[2] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 2 + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[2] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ 2 + 1 ] + nY;
numvert++;
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[4];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 4 + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[4] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 4 + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[4] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ 4 + 1 ] + nY;
numvert++;
mRenderCoords[ numvert ].TexCoords[0] = C.TexCoords[6];
mRenderCoords[ numvert ].TexCoords[1] = C.TexCoords[ 6 + 1 ];
mRenderCoords[ numvert ].Vertex[0] = cX + C.Vertex[6] + nX;
mRenderCoords[ numvert ].Vertex[1] = cY + C.Vertex[ 6 + 1 ] + nY;
mRenderCoords[ numvert ].Vertex[0] = C.Vertex[6] + nX;
mRenderCoords[ numvert ].Vertex[1] = C.Vertex[ 6 + 1 ] + nY;
numvert++;
}
@@ -380,8 +301,8 @@ void TextCache::cacheVerts( const Int32& X, const Int32& Y ) {
}
}
cachedCoords( true );
cachedVerts( numvert );
mCachedCoords = true;
mVertexNumCached = numvert;
}
Float TextCache::getTextWidth() {
@@ -403,35 +324,79 @@ const std::vector<Float>& TextCache::getLinesWidth() {
void TextCache::draw( const Float& X, const Float& Y, const Vector2f& Scale, const Float& Angle, EE_BLEND_MODE Effect ) {
if ( NULL != mFont ) {
GlobalBatchRenderer::instance()->draw();
TextureFactory::instance()->bind( mFont->getTexId() );
BlendMode::setMode( Effect );
if ( mFlags & FONT_DRAW_SHADOW ) {
Uint32 f = mFlags;
mFlags &= ~FONT_DRAW_SHADOW;
ColorA Col = getColor();
if ( Col.a() != 255 ) {
ColorA ShadowColor = getShadowColor();
ShadowColor.Alpha = (Uint8)( (Float)ShadowColor.Alpha * ( (Float)Col.a() / (Float)255 ) );
setColor( ShadowColor );
} else {
setColor( getShadowColor() );
}
Float pd = PixelDensity::dpToPx(1);
draw( X + pd, Y + pd, Scale, Angle, Effect );
mFlags = f;
setColor( Col );
}
unsigned int numvert = 0;
if ( Angle != 0.0f || Scale != 1.0f ) {
internalDraw( X, Y, Scale, Angle, Effect );
Float cX = (Float) ( (Int32)X );
Float cY = (Float) ( (Int32)Y );
GLi->pushMatrix();
Vector2f Center( cX + getTextWidth() * 0.5f, cY + getTextHeight() * 0.5f );
GLi->translatef( Center.x , Center.y, 0.f );
GLi->rotatef( Angle, 0.0f, 0.0f, 1.0f );
GLi->scalef( Scale.x, Scale.y, 1.0f );
GLi->translatef( -Center.x + X, -Center.y + Y, 0.f );
} else {
GLi->translatef( X, Y, 0.f );
internalDraw( 0, 0, Scale, Angle, Effect );
GLi->translatef( -X, -Y, 0.f );
GLi->translatef( X, Y, 0 );
}
if ( !mCachedCoords ) {
cacheVerts();
}
numvert = mVertexNumCached;
Uint32 alloc = numvert * sizeof(VertexCoords);
Uint32 allocC = numvert * GLi->quadVertexs();
GLi->colorPointer ( 4, GL_UNSIGNED_BYTE , 0 , reinterpret_cast<char*>( &mColors[0] ) , allocC );
GLi->texCoordPointer( 2, GL_FP , sizeof(VertexCoords), reinterpret_cast<char*>( &mRenderCoords[0] ) , alloc );
GLi->vertexPointer ( 2, GL_FP , sizeof(VertexCoords), reinterpret_cast<char*>( &mRenderCoords[0] ) + sizeof(Float) * 2 , alloc );
if ( GLi->quadsSupported() ) {
GLi->drawArrays( GL_QUADS, 0, numvert );
} else {
GLi->drawArrays( GL_TRIANGLES, 0, numvert );
}
if ( Angle != 0.0f || Scale != 1.0f ) {
GLi->popMatrix();
} else {
GLi->translatef( -X, -Y, 0 );
}
}
}
const bool& TextCache::cachedCoords() const {
return mCachedCoords;
}
void TextCache::cachedCoords( const bool& cached ) {
mCachedCoords = cached;
}
const unsigned int& TextCache::cachedVerts() const {
return mVertexNumCached;
}
void TextCache::cachedVerts( const unsigned int& num ) {
mVertexNumCached = num;
}
void TextCache::setFlags( const Uint32& flags ) {
if ( mFlags != flags ) {
mFlags = flags;

View File

@@ -109,7 +109,7 @@ void TextureFont::buildFromGlyphs() {
TextureFactory::instance()->bind( Tex );
eeGlyph tGlyph;
Glyph tGlyph;
for (unsigned int i = 0; i < mNumChars; i++) {
tGlyph = mGlyphs[i];
@@ -201,7 +201,7 @@ bool TextureFont::loadFromStream( const Uint32& TexId, IOStream& IOS ) {
mGlyphs.resize( mNumChars );
// Read the glyphs
IOS.read( (char*)&mGlyphs[0], sizeof(eeGlyph) * mNumChars );
IOS.read( (char*)&mGlyphs[0], sizeof(Glyph) * mNumChars );
buildFromGlyphs();

View File

@@ -169,7 +169,7 @@ bool TTFFont::iLoad( const unsigned int& Size, EE_TTF_FONT_STYLE Style, const Ui
TempGlyphSurface = mFont->renderGlyph( i, fFontColor.getValue() );
//New temp glyph
eeGlyph TempGlyph;
Glyph TempGlyph;
//Get the glyph attributes
mFont->getGlyphMetrics( i, &TempGlyph.MinX, &TempGlyph.MaxX, &TempGlyph.MinY, &TempGlyph.MaxY, &TempGlyph.Advance );
@@ -340,7 +340,7 @@ void TTFFont::rebuildFromGlyphs() {
TextureFactory::instance()->bind( Tex );
eeGlyph tGlyph;
Glyph tGlyph;
for (unsigned int i = 0; i < mNumChars; i++) {
tGlyph = mGlyphs[i];
@@ -401,7 +401,7 @@ bool TTFFont::saveCoordinates( const std::string& Filepath ) {
fs.write( reinterpret_cast<const char*>( &FntHdr ), sizeof(sFntHdr) );
// Write the glyphs
fs.write( reinterpret_cast<const char*> (&mGlyphs[0]), sizeof(eeGlyph) * mGlyphs.size() );
fs.write( reinterpret_cast<const char*> (&mGlyphs[0]), sizeof(Glyph) * mGlyphs.size() );
rebuildFromGlyphs();