From 99b3c679a14566e1bb7a376f38c2abb76d73afba Mon Sep 17 00:00:00 2001 From: "spartanj@gmail.com" Date: Thu, 23 Feb 2012 15:23:07 -0300 Subject: [PATCH] Added some important access to some data in fonts, changed how the font texture coordinates are saved ( so you'll need to regenerate them ). Fixed some minor bugs. --- src/gaming/cisomap.cpp | 2 +- src/gaming/mapeditor/cmapeditor.cpp | 5 ++ src/graphics/cconsole.cpp | 8 ++- src/graphics/cfont.cpp | 27 ++++++-- src/graphics/cfont.hpp | 14 +++- src/graphics/ctextcache.cpp | 2 +- src/graphics/ctexturefont.cpp | 100 ++++++++++++++++------------ src/graphics/ctexturefont.hpp | 7 +- src/graphics/ctexturefontloader.cpp | 2 +- src/graphics/ctexturefontloader.hpp | 4 +- src/graphics/cttffont.cpp | 88 ++++++++++++++++-------- src/graphics/cttffont.hpp | 35 +++++----- src/graphics/fonthelper.hpp | 13 ++++ src/system/clog.cpp | 16 +++-- src/system/clog.hpp | 5 +- src/test/eetest.cpp | 51 ++++++++++++-- src/test/eetest.hpp | 2 +- src/ui/cuilistbox.cpp | 8 +-- src/ui/cuimenu.cpp | 2 +- src/ui/cuitextedit.cpp | 2 +- src/ui/cuitextinput.cpp | 2 +- src/ui/cuitooltip.cpp | 4 +- 22 files changed, 278 insertions(+), 121 deletions(-) diff --git a/src/gaming/cisomap.cpp b/src/gaming/cisomap.cpp index 4eeebacc4..e673a8a78 100755 --- a/src/gaming/cisomap.cpp +++ b/src/gaming/cisomap.cpp @@ -182,7 +182,7 @@ void cIsoMap::Draw() { for ( eeInt y = Ty; y < Ty2; y++ ) { for ( eeInt x = Tx; x < Tx2; x++ ) { T = &Tile( (eeUint)x, (eeUint)y ); - mFont->Draw( T->TilePosStr, mOffsetX + T->Q.V[1].x + ( T->Q.V[3].x - T->Q.V[1].x ) * 0.5f - T->TilePosStr.size() * mFont->GetFontSize() * 0.5f, mOffsetY + T->Q.V[0].y + ( T->Q.V[2].y - T->Q.V[0].y ) * 0.5f - mFont->GetFontHeight() * 0.5f ); + mFont->Draw( T->TilePosStr, mOffsetX + T->Q.V[1].x + ( T->Q.V[3].x - T->Q.V[1].x ) * 0.5f - T->TilePosStr.size() * mFont->GetFontHeight() * 0.5f, mOffsetY + T->Q.V[0].y + ( T->Q.V[2].y - T->Q.V[0].y ) * 0.5f - mFont->GetFontHeight() * 0.5f ); } } } diff --git a/src/gaming/mapeditor/cmapeditor.cpp b/src/gaming/mapeditor/cmapeditor.cpp index f59ff553f..e28a797eb 100644 --- a/src/gaming/mapeditor/cmapeditor.cpp +++ b/src/gaming/mapeditor/cmapeditor.cpp @@ -216,6 +216,7 @@ void cMapEditor::CreateShapeContainer( Int32 Width ) { FillGotyList(); mBtnGOTypeAdd = mTheme->CreatePushButton( mShapeCont, eeSize( 24, 21 ), eeVector2i( mGOTypeList->Pos().x + mGOTypeList->Size().Width() + 2, mGOTypeList->Pos().y ), UI_CONTROL_ALIGN_CENTER | UI_AUTO_SIZE | UI_ANCHOR_RIGHT | UI_ANCHOR_TOP, mTheme->GetIconByName( "add" ) ); + mBtnGOTypeAdd->TooltipText( "Adds a new game object type\nunknown by the map editor." ); mBtnGOTypeAdd->AddEventListener( cUIEvent::EventMouseClick, cb::Make1( this, &cMapEditor::AddNewGOType ) ); if ( NULL == mBtnGOTypeAdd->Icon()->Shape() ) @@ -240,10 +241,12 @@ void cMapEditor::CreateShapeContainer( Int32 Width ) { mChkBlocked = mTheme->CreateCheckBox( mShapeCont, eeSize(), eeVector2i( mChkMirrored->Pos().x, mChkMirrored->Pos().y + mChkMirrored->Size().Height() + 4 ), ChkFlags ); mChkBlocked->Text( "Blocked" ); + mChkBlocked->TooltipText( "Blocks the tile occupied by the sprite." ); mChkBlocked->AddEventListener( cUIEvent::EventMouseClick, cb::Make1( this, &cMapEditor::ChkClickBlocked ) ); mChkAnim = mTheme->CreateCheckBox( mShapeCont, eeSize(), eeVector2i( mChkFliped->Pos().x, mChkFliped->Pos().y + mChkFliped->Size().Height() + 4 ), ChkFlags ); mChkAnim->Text( "Animated" ); + mChkAnim->TooltipText( "Indicates if the Sprite is animated." ); mChkAnim->AddEventListener( cUIEvent::EventMouseClick, cb::Make1( this, &cMapEditor::ChkClickAnimated ) ); mChkRot90 = mTheme->CreateCheckBox( mShapeCont, eeSize(), eeVector2i( mChkBlocked->Pos().x, mChkBlocked->Pos().y + mChkBlocked->Size().Height() + 4 ), ChkFlags ); @@ -252,12 +255,14 @@ void cMapEditor::CreateShapeContainer( Int32 Width ) { mChkAutoFix = mTheme->CreateCheckBox( mShapeCont, eeSize(), eeVector2i( mChkAnim->Pos().x, mChkAnim->Pos().y + mChkAnim->Size().Height() + 4 ), ChkFlags ); mChkAutoFix->Text( "AutoFix TilePos" ); + mChkAutoFix->TooltipText( "In a tiled layer if the sprite is moved,\nit will update the current tile position automatically." ); mChkAutoFix->AddEventListener( cUIEvent::EventMouseClick, cb::Make1( this, &cMapEditor::ChkClickAutoFix ) ); Txt = mTheme->CreateTextBox( "Game Object Data:", mShapeCont, eeSize( Width, 16 ), eeVector2i( 0, mChkRot90->Pos().y + mChkRot90->Size().Height() + 8 ), TxtFlags ); mChkDI = mTheme->CreateCheckBox( mShapeCont, eeSize(), eeVector2i( 0, Txt->Pos().y + Txt->Size().Height() + 4 ), ChkFlags ); mChkDI->Text( "Add as DataId" ); + mChkDI->TooltipText( "If the resource it's not a sprite,\nyou can reference it with a data id" ); mChkDI->AddEventListener( cUIEvent::EventMouseClick, cb::Make1( this, &cMapEditor::ChkClickDI ) ); cUIComplexControl::CreateParams SGParams; diff --git a/src/graphics/cconsole.cpp b/src/graphics/cconsole.cpp index bb533303a..30dfb687e 100755 --- a/src/graphics/cconsole.cpp +++ b/src/graphics/cconsole.cpp @@ -64,7 +64,10 @@ void cConsole::Create( cFont* Font, const bool& MakeDefaultCommands, const eeUin mFont = Font; - mFontSize = (eeFloat)mFont->GetFontSize(); + mFontSize = (eeFloat)( mFont->GetFontSize() * 1.25 ); + + if ( mFont->GetFontHeight() < mFontSize && ( mFont->GetFontHeight() != mFont->GetFontSize() || mFont->GetLineSkip() != (Int32)mFont->GetFontHeight() ) ) + mFontSize = mFont->GetFontHeight(); if ( TextureId > 0 ) mTexId = TextureId; @@ -73,7 +76,6 @@ void cConsole::Create( cFont* Font, const bool& MakeDefaultCommands, const eeUin mMaxAlpha = (eeFloat)mConColor.A(); mEnabled = true; - mFontSize *= 1.25f; if ( MakeDefaultCommands ) CreateDefaultCommands(); @@ -150,7 +152,7 @@ void cConsole::Draw() { } } - CurY = mTempY + mY + mCurHeight - mFontSize; + CurY = mTempY + mY + mCurHeight - mFontSize - 1; mFont->Color( eeColorA ( mFontLineColor.R(), mFontLineColor.G(), mFontLineColor.B(), static_cast(mA) ) ); mFont->SetText( "> " + mTBuf.Buffer() ); diff --git a/src/graphics/cfont.cpp b/src/graphics/cfont.cpp index 81686df60..81e624d09 100644 --- a/src/graphics/cfont.cpp +++ b/src/graphics/cfont.cpp @@ -12,7 +12,10 @@ cFont::cFont( const Uint32& Type, const std::string& Name ) : mTexId(0), mCachedWidth(0.f), mHeight(0), - mSize(0) + mSize(0), + mLineSkip(0), + mAscent(0), + mDescent(0) { this->Name( Name ); cFontManager::instance()->Add( this ); @@ -89,6 +92,18 @@ Uint32 cFont::GetFontHeight() const { return mHeight; } +Int32 cFont::GetLineSkip() const { + return mLineSkip; +} + +Int32 cFont::GetFontAscent() const { + return mAscent; +} + +Int32 cFont::GetFontDescent() const { + return mDescent; +} + String cFont::GetText() { return mText; } @@ -102,7 +117,7 @@ const bool& cFont::CacheData() const { } eeFloat cFont::GetTextHeight() { - return (eeFloat)GetFontSize() * (eeFloat)GetNumLines(); + return (eeFloat)GetFontHeight() * (eeFloat)GetNumLines(); } const std::vector& cFont::GetLinesWidth() const { @@ -213,7 +228,7 @@ void cFont::Draw( cTextCache& TextCache, const eeFloat& X, const eeFloat& Y, con case '\n': { if (mVerticalDraw) { - nX += (GetFontSize() * Scale); + nX += (GetFontHeight() * Scale); nY = 0; } else { if ( i + 1 < TextCache.Text().size() ) { @@ -229,7 +244,7 @@ void cFont::Draw( cTextCache& TextCache, const eeFloat& X, const eeFloat& Y, con } } - nY += (GetFontSize() * Scale); + nY += (GetFontHeight() * Scale); Line++; } @@ -403,7 +418,7 @@ void cFont::SubDraw( const String& Text, const eeFloat& X, const eeFloat& Y, con break; case '\n': if (mVerticalDraw) { - nX += (GetFontSize() * Scale); + nX += (GetFontHeight() * Scale); nY = 0; } else { if ( i + 1 < Text.size() ) { @@ -419,7 +434,7 @@ void cFont::SubDraw( const String& Text, const eeFloat& X, const eeFloat& Y, con } } - nY += (GetFontSize() * Scale); + nY += (GetFontHeight() * Scale); Line++; } diff --git a/src/graphics/cfont.hpp b/src/graphics/cfont.hpp index 8ea631abe..6e3fb0354 100755 --- a/src/graphics/cfont.hpp +++ b/src/graphics/cfont.hpp @@ -52,6 +52,15 @@ class EE_API cFont { /** @return The current font height */ Uint32 GetFontHeight() const; + /** @return The recommended line spacing */ + Int32 GetLineSkip() const; + + /** @return The font highest ascent (height above base) */ + Int32 GetFontAscent() const; + + /** @return The font lowest descent (height below base) */ + Int32 GetFontDescent() const; + /** @return The current text */ String GetText(); @@ -126,7 +135,7 @@ class EE_API cFont { std::string mFontName; Uint32 mFontHash; - String mText; + String mText; bool mCacheData; eeColorA mColor; eeColorA mShadowColor; @@ -136,6 +145,9 @@ class EE_API cFont { eeFloat mCachedWidth; Uint32 mHeight; Uint32 mSize; + Int32 mLineSkip; + Int32 mAscent; + Int32 mDescent; std::vector mLinesWidth; std::vector mGlyphs; diff --git a/src/graphics/ctextcache.cpp b/src/graphics/ctextcache.cpp index 9bb8c7b98..0e96651a6 100644 --- a/src/graphics/ctextcache.cpp +++ b/src/graphics/ctextcache.cpp @@ -115,7 +115,7 @@ eeFloat cTextCache::GetTextWidth() { } eeFloat cTextCache::GetTextHeight() { - return (eeFloat)mFont->GetFontSize() * (eeFloat)mNumLines; + return (eeFloat)mFont->GetFontHeight() * (eeFloat)mNumLines; } const eeInt& cTextCache::GetNumLines() const { diff --git a/src/graphics/ctexturefont.cpp b/src/graphics/ctexturefont.cpp index 238013bb7..de9ab6e87 100755 --- a/src/graphics/ctexturefont.cpp +++ b/src/graphics/ctexturefont.cpp @@ -1,4 +1,6 @@ #include "ctexturefont.hpp" +#include "../system/ciostreamfile.hpp" +#include "../system/ciostreammemory.hpp" namespace EE { namespace Graphics { @@ -19,19 +21,19 @@ bool cTextureFont::Load( const Uint32& TexId, const eeUint& StartChar, const eeU mTexId = TexId; if ( NULL != Tex ) { - mTexColumns = TexColumns; - mTexRows = TexRows; - mStartChar = StartChar; - mNumChars = NumChars; + mTexColumns = TexColumns; + mTexRows = TexRows; + mStartChar = StartChar; + mNumChars = NumChars; - mtX = ( 1 / static_cast( mTexColumns ) ); - mtY = ( 1 / static_cast( mTexRows ) ); + mtX = ( 1 / static_cast( mTexColumns ) ); + mtY = ( 1 / static_cast( mTexRows ) ); - mFWidth = (eeFloat)( Tex->Width() / mTexColumns ); - mFHeight = (eeFloat)( Tex->Height() / mTexRows ); - mHeight = mSize = (eeUint)mFHeight; + mFWidth = (eeFloat)( Tex->Width() / mTexColumns ); + mFHeight = (eeFloat)( Tex->Height() / mTexRows ); + mHeight = mSize = mLineSkip = (eeUint)mFHeight; - mVerticalDraw = VerticalDraw; + mVerticalDraw = VerticalDraw; if ( Spacing == 0 ) mSpacing = static_cast( mFWidth ); @@ -85,7 +87,7 @@ void cTextureFont::BuildFont() { } } -void cTextureFont::BuildFontFromDat() { +void cTextureFont::BuildFromGlyphs() { eeFloat Top, Bottom; eeRectf tR; @@ -95,15 +97,19 @@ void cTextureFont::BuildFontFromDat() { cTextureFactory::instance()->Bind( Tex ); + eeGlyph tGlyph; + for (eeUint i = 0; i < mNumChars; i++) { - tR.Left = (eeFloat)mGlyphs[i].CurX / Tex->Width(); - tR.Top = (eeFloat)mGlyphs[i].CurY / Tex->Height(); + tGlyph = mGlyphs[i]; - tR.Right = (eeFloat)(mGlyphs[i].CurX + mGlyphs[i].CurW) / Tex->Width(); - tR.Bottom = (eeFloat)(mGlyphs[i].CurY + mGlyphs[i].CurH) / Tex->Height(); + tR.Left = (eeFloat)tGlyph.CurX / Tex->Width(); + tR.Top = (eeFloat)tGlyph.CurY / Tex->Height(); - Top = mFHeight - mGlyphs[i].GlyphH - mGlyphs[i].MinY; - Bottom = mFHeight + mGlyphs[i].GlyphH - mGlyphs[i].MaxY; + tR.Right = (eeFloat)(tGlyph.CurX + tGlyph.CurW) / Tex->Width(); + tR.Bottom = (eeFloat)(tGlyph.CurY + tGlyph.CurH) / Tex->Height(); + + Top = mHeight + mDescent - tGlyph.GlyphH - tGlyph.MinY; + Bottom = mHeight + mDescent + tGlyph.GlyphH - tGlyph.MaxY; mTexCoords[i].TexCoords[0] = tR.Left; mTexCoords[i].TexCoords[1] = tR.Top; @@ -113,24 +119,22 @@ void cTextureFont::BuildFontFromDat() { mTexCoords[i].TexCoords[5] = tR.Bottom; mTexCoords[i].TexCoords[6] = tR.Right; mTexCoords[i].TexCoords[7] = tR.Top; - mTexCoords[i].Vertex[0] = (eeFloat) mGlyphs[i].MinX; + mTexCoords[i].Vertex[0] = (eeFloat) tGlyph.MinX; mTexCoords[i].Vertex[1] = Top; - mTexCoords[i].Vertex[2] = (eeFloat) mGlyphs[i].MinX; + mTexCoords[i].Vertex[2] = (eeFloat) tGlyph.MinX; mTexCoords[i].Vertex[3] = Bottom; - mTexCoords[i].Vertex[4] = (eeFloat) mGlyphs[i].MaxX; + mTexCoords[i].Vertex[4] = (eeFloat) tGlyph.MaxX; mTexCoords[i].Vertex[5] = Bottom; - mTexCoords[i].Vertex[6] = (eeFloat) mGlyphs[i].MaxX; + mTexCoords[i].Vertex[6] = (eeFloat) tGlyph.MaxX; mTexCoords[i].Vertex[7] = Top; } } bool cTextureFont::Load( const Uint32& TexId, const std::string& CoordinatesDatPath, const bool& VerticalDraw ) { if ( FileExists( CoordinatesDatPath ) ) { - SafeDataPointer PData; + cIOStreamFile IOS( CoordinatesDatPath, std::ios::in | std::ios::binary ); - FileGet( CoordinatesDatPath, PData ); - - return LoadFromMemory( TexId, reinterpret_cast ( PData.Data ), PData.DataSize, VerticalDraw ); + return LoadFromStream( TexId, IOS, VerticalDraw ); } else if ( cPackManager::instance()->FallbackToPacks() ) { std::string tPath( CoordinatesDatPath ); @@ -145,38 +149,52 @@ bool cTextureFont::Load( const Uint32& TexId, const std::string& CoordinatesDatP } bool cTextureFont::LoadFromPack( const Uint32& TexId, cPack* Pack, const std::string& FilePackPath, const bool& VerticalDraw ) { - SafeDataPointer PData; + if ( NULL != Pack && Pack->IsOpen() && -1 != Pack->Exists( FilePackPath ) ) { + SafeDataPointer PData; - if ( Pack->IsOpen() && Pack->ExtractFileToMemory( FilePackPath, PData ) ) { - return LoadFromMemory( TexId, reinterpret_cast ( PData.Data ), PData.DataSize, VerticalDraw ); + Pack->ExtractFileToMemory( FilePackPath, PData ); + + return LoadFromMemory( TexId, reinterpret_cast ( PData.Data ), PData.DataSize, VerticalDraw ); } return false; } -bool cTextureFont::LoadFromMemory( const Uint32& TexId, const Uint8* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw ) { +bool cTextureFont::LoadFromMemory( const Uint32& TexId, const char* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw ) { + cIOStreamMemory IOS( CoordData, CoordDataSize ); + + return LoadFromStream( TexId, IOS, VerticalDraw ); +} + +bool cTextureFont::LoadFromStream( const Uint32& TexId, cIOStream& IOS, const bool& VerticalDraw ) { mTexId = TexId; if ( mTexId > 0 ) { mVerticalDraw = VerticalDraw; - if ( CoordData != NULL ) { - mNumChars = static_cast ( ( CoordDataSize - 2 ) / 32 ); + if ( IOS.IsOpen() ) { + sFntHdr FntHdr; + + IOS.Read( (char*)&FntHdr, sizeof(sFntHdr) ); + + if ( EE_TTF_FONT_MAGIC != FntHdr.Magic ) + return false; + + mStartChar = FntHdr.FirstChar; + mNumChars = FntHdr.NumChars; + mSize = FntHdr.Size; + mHeight = FntHdr.Height; + mLineSkip = FntHdr.LineSkip; + mAscent = FntHdr.Ascent; + mDescent = FntHdr.Descent; mGlyphs.resize( mNumChars ); - // Read the number of the first char represented on the texture - mStartChar = CoordData[0]; + // Read the glyphs + IOS.Read( (char*)&mGlyphs[0], sizeof(eeGlyph) * mNumChars ); - // Read the default size of every char - mFWidth = CoordData[1]; - mFHeight = CoordData[1]; - mHeight = mSize = (Uint32)mFHeight; + BuildFromGlyphs(); - // Read every char coordinates - memcpy( reinterpret_cast (&mGlyphs[0]), reinterpret_cast (&CoordData[2]), sizeof(eeGlyph) * mNumChars ); - - BuildFontFromDat(); mLoadedCoords = true; return true; diff --git a/src/graphics/ctexturefont.hpp b/src/graphics/ctexturefont.hpp index 7b4abc0ab..cfab104d2 100755 --- a/src/graphics/ctexturefont.hpp +++ b/src/graphics/ctexturefont.hpp @@ -50,14 +50,17 @@ class EE_API cTextureFont : public cFont { * @param VerticalDraw If true render the string verticaly * @return True if success */ - bool LoadFromMemory( const Uint32& TexId, const Uint8* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw = false ); + bool LoadFromMemory( const Uint32& TexId, const char* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw = false ); + + bool LoadFromStream( const Uint32& TexId, cIOStream& IOS, const bool& VerticalDraw ); private: eeUint mBase, mStartChar, mTexColumns, mTexRows, mSpacing, mNumChars; eeFloat mtX, mtY, mFWidth, mFHeight; bool mLoadedCoords; void BuildFont(); - void BuildFontFromDat(); + + void BuildFromGlyphs(); }; }} diff --git a/src/graphics/ctexturefontloader.cpp b/src/graphics/ctexturefontloader.cpp index 24eb2ac95..4c095ec90 100644 --- a/src/graphics/ctexturefontloader.cpp +++ b/src/graphics/ctexturefontloader.cpp @@ -44,7 +44,7 @@ cTextureFontLoader::cTextureFontLoader( const std::string FontName, cTextureLoad mTexLoader = TexLoader; } -cTextureFontLoader::cTextureFontLoader( const std::string FontName, cTextureLoader * TexLoader, const Uint8* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw ) : +cTextureFontLoader::cTextureFontLoader( const std::string FontName, cTextureLoader * TexLoader, const char* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw ) : cObjectLoader( FontTexLoader ), mLoadType( TEF_LT_MEM ), mFontName( FontName ), diff --git a/src/graphics/ctexturefontloader.hpp b/src/graphics/ctexturefontloader.hpp index b02c2428b..76a66474c 100644 --- a/src/graphics/ctexturefontloader.hpp +++ b/src/graphics/ctexturefontloader.hpp @@ -22,7 +22,7 @@ class EE_API cTextureFontLoader : public cObjectLoader { cTextureFontLoader( const std::string FontName, cTextureLoader * TexLoader, cPack * Pack, const std::string& FilePackPath, const bool& VerticalDraw = false ); - cTextureFontLoader( const std::string FontName, cTextureLoader * TexLoader, const Uint8* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw = false ); + cTextureFontLoader( const std::string FontName, cTextureLoader * TexLoader, const char* CoordData, const Uint32& CoordDataSize, const bool& VerticalDraw = false ); virtual ~cTextureFontLoader(); @@ -52,7 +52,7 @@ class EE_API cTextureFontLoader : public cObjectLoader { cPack * mPack; - const Uint8 * mData; + const char * mData; Uint32 mDataSize; void Start(); diff --git a/src/graphics/cttffont.cpp b/src/graphics/cttffont.cpp index 2e2bcabff..03063b0fd 100755 --- a/src/graphics/cttffont.cpp +++ b/src/graphics/cttffont.cpp @@ -86,6 +86,10 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONTSTYLE Style, const bool& Ve mVerticalDraw = VerticalDraw; mSize = Size; mHeight = mFont->Height() + OutlineTotal; + mLineSkip = mFont->LineSkip(); + mAscent = mFont->Ascent(); + mDescent = mFont->Descent(); + mNumChars = NumCharsToGen; mFontColor = FontColor; mOutlineColor = OutlineColor; @@ -99,12 +103,30 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONTSTYLE Style, const bool& Ve bool lastWasWidth = false; Uint32 ReqSize; - // Find the best size for the texture ( aprox ) + // Find the larger glyph + /* + int maxWidth = 0, maxHeight = 0; - mSize += PixelSep; + for ( eeUint i = 0; i < mNumChars; i++) { + TempGlyphSurface = mFont->GlyphRender( i, 0x00000000 ); + + maxWidth = eemax( mFont->Current()->Pixmap()->width, maxWidth ); + maxHeight = eemax( mFont->Current()->Pixmap()->rows, maxHeight ); + + hkSAFE_DELETE_ARRAY( TempGlyphSurface ); + } + + //ReqSize = mNumChars * mHeight * mHeight; + */ + + // Find the best size for the texture ( aprox ) + // Totally wild guessing, but it's working + Int32 tWildGuessW = ( mAscent + PixelSep + OutlineTotal ); + Int32 tWildGuessH = tWildGuessW; //( mHeight ); + + ReqSize = mNumChars * tWildGuessW * tWildGuessH; do { - ReqSize = mNumChars * mSize * mSize; TexSize = (Uint32)mTexWidth * (Uint32)mTexHeight; if ( TexSize < ReqSize ) { @@ -117,10 +139,6 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONTSTYLE Style, const bool& Ve } } while ( TexSize < ReqSize ); - mSize -= PixelSep; - - TexSize = (Uint32)mTexWidth * (Uint32)mTexHeight; - mPixels = eeNewArray( eeColorA, TexSize ); memset( mPixels, 0x00000000, TexSize * 4 ); @@ -288,15 +306,19 @@ void cTTFFont::RebuildFromGlyphs() { cTextureFactory::instance()->Bind( Tex ); + eeGlyph tGlyph; + for (eeUint i = 0; i < mNumChars; i++) { - tR.Left = (eeFloat)mGlyphs[i].CurX / Tex->Width(); - tR.Top = (eeFloat)mGlyphs[i].CurY / Tex->Height(); + tGlyph = mGlyphs[i]; - tR.Right = (eeFloat)(mGlyphs[i].CurX + mGlyphs[i].CurW) / Tex->Width(); - tR.Bottom = (eeFloat)(mGlyphs[i].CurY + mGlyphs[i].CurH) / Tex->Height(); + tR.Left = (eeFloat)tGlyph.CurX / Tex->Width(); + tR.Top = (eeFloat)tGlyph.CurY / Tex->Height(); - Top = (eeFloat)mSize - mGlyphs[i].GlyphH - mGlyphs[i].MinY; - Bottom = (eeFloat)mSize + mGlyphs[i].GlyphH - mGlyphs[i].MaxY; + tR.Right = (eeFloat)(tGlyph.CurX + tGlyph.CurW) / Tex->Width(); + tR.Bottom = (eeFloat)(tGlyph.CurY + tGlyph.CurH) / Tex->Height(); + + Top = (eeFloat)mHeight + mDescent - tGlyph.GlyphH - tGlyph.MinY; + Bottom = (eeFloat)mHeight + mDescent + tGlyph.GlyphH - tGlyph.MaxY; mTexCoords[i].TexCoords[0] = tR.Left; mTexCoords[i].TexCoords[1] = tR.Top; @@ -306,13 +328,13 @@ void cTTFFont::RebuildFromGlyphs() { mTexCoords[i].TexCoords[5] = tR.Bottom; mTexCoords[i].TexCoords[6] = tR.Right; mTexCoords[i].TexCoords[7] = tR.Top; - mTexCoords[i].Vertex[0] = (eeFloat) mGlyphs[i].MinX; + mTexCoords[i].Vertex[0] = (eeFloat) tGlyph.MinX; mTexCoords[i].Vertex[1] = Top; - mTexCoords[i].Vertex[2] = (eeFloat) mGlyphs[i].MinX; + mTexCoords[i].Vertex[2] = (eeFloat) tGlyph.MinX; mTexCoords[i].Vertex[3] = Bottom; - mTexCoords[i].Vertex[4] = (eeFloat) mGlyphs[i].MaxX; + mTexCoords[i].Vertex[4] = (eeFloat) tGlyph.MaxX; mTexCoords[i].Vertex[5] = Bottom; - mTexCoords[i].Vertex[6] = (eeFloat) mGlyphs[i].MaxX; + mTexCoords[i].Vertex[6] = (eeFloat) tGlyph.MaxX; mTexCoords[i].Vertex[7] = Top; } } @@ -327,21 +349,25 @@ bool cTTFFont::SaveTexture( const std::string& Filepath, const EE_SAVE_TYPE& For } bool cTTFFont::SaveCoordinates( const std::string& Filepath ) { - Uint8 chars; - cIOStreamFile fs( Filepath, std::ios::out | std::ios::binary ); if ( fs.IsOpen() ) { - // Write the number of the fist char represented on the texture - chars = 0; - fs.Write( reinterpret_cast (&chars), sizeof(Uint8) ); + sFntHdr FntHdr; - // Write the default size of every char - chars = static_cast ( mSize ); - fs.Write( reinterpret_cast (&chars), sizeof(Uint8) ); + FntHdr.Magic = EE_TTF_FONT_MAGIC; + FntHdr.FirstChar = 0; + FntHdr.NumChars = mGlyphs.size(); + FntHdr.Size = mSize; + FntHdr.Height = mHeight; + FntHdr.LineSkip = mLineSkip; + FntHdr.Ascent = mAscent; + FntHdr.Descent = mDescent; - for (eeUint i = 0; i < mGlyphs.size(); i++) - fs.Write( reinterpret_cast (&mGlyphs[i]), sizeof(eeGlyph) ); + // Write the header + fs.Write( reinterpret_cast( &FntHdr ), sizeof(sFntHdr) ); + + // Write the glyphs + fs.Write( reinterpret_cast (&mGlyphs[0]), sizeof(eeGlyph) * mGlyphs.size() ); RebuildFromGlyphs(); @@ -381,4 +407,12 @@ void cTTFFont::MakeOutline( Uint8 *in, Uint8 *out, Int16 w, Int16 h) { } } +bool cTTFFont::ThreadedLoading() const { + return mThreadedLoading; +} + +void cTTFFont::ThreadedLoading( const bool& isThreaded ) { + mThreadedLoading = isThreaded; +} + }} diff --git a/src/graphics/cttffont.hpp b/src/graphics/cttffont.hpp index af1e58349..78409b748 100755 --- a/src/graphics/cttffont.hpp +++ b/src/graphics/cttffont.hpp @@ -70,32 +70,35 @@ class EE_API cTTFFont : public cFont { protected: friend class cTTFFontLoader; - bool ThreadedLoading() const { return mThreadedLoading; } + hkFont * mFont; + eeColorA * mPixels; - void ThreadedLoading( const bool& isThreaded ) { mThreadedLoading = isThreaded; } + std::string mFilepath; + Uint32 mNumChars; + Uint8 mOutlineSize; - void UpdateLoading(); - private: - hkFont * mFont; - eeColorA * mPixels; + eeColor mFontColor; + eeColor mOutlineColor; - std::string mFilepath; - Uint32 mBase; - Uint32 mNumChars; - Uint8 mOutlineSize; - - eeColor mFontColor, mOutlineColor; EE_TTF_FONTSTYLE mStyle; - eeFloat mTexWidth, mTexHeight; + eeFloat mTexWidth; + eeFloat mTexHeight; - bool mLoadedFromMemory; + bool mLoadedFromMemory; + bool mThreadedLoading; + bool mTexReady; - bool mThreadedLoading; - bool mTexReady; + bool ThreadedLoading() const; + + void ThreadedLoading( const bool& isThreaded ); + + void UpdateLoading(); bool iLoad( const eeUint& Size, EE_TTF_FONTSTYLE Style, const bool& VerticalDraw, const Uint16& NumCharsToGen, const eeColor& FontColor, Uint8 OutlineSize, const eeColor& OutlineColor, const bool& AddPixelSeparator ); + void MakeOutline( Uint8 *in, Uint8 *out, Int16 w, Int16 h); + void RebuildFromGlyphs(); }; diff --git a/src/graphics/fonthelper.hpp b/src/graphics/fonthelper.hpp index bc28fe1e9..a185793eb 100644 --- a/src/graphics/fonthelper.hpp +++ b/src/graphics/fonthelper.hpp @@ -44,6 +44,19 @@ struct eeTexCoords { eeFloat Vertex[8]; }; +typedef struct sFntHdrS { + Uint32 Magic; + Uint32 FirstChar; + Uint32 NumChars; + Uint32 Size; + Uint32 Height; + Int32 LineSkip; + Int32 Ascent; + Int32 Descent; +} sFntHdr; + +#define EE_TTF_FONT_MAGIC ( ( 'E' << 0 ) | ( 'E' << 8 ) | ( 'F' << 16 ) | ( 'N' << 24 ) ) + }} #endif diff --git a/src/system/clog.cpp b/src/system/clog.cpp index d6137aacf..c38adca29 100755 --- a/src/system/clog.cpp +++ b/src/system/clog.cpp @@ -24,7 +24,7 @@ cLog::~cLog() { mFS->Write( mData.c_str(), mData.size() ); } - eeSAFE_DELETE( mFS ); + closefs(); } void cLog::Save( const std::string& filepath ) { @@ -69,7 +69,7 @@ void cLog::Write( const std::string& Text, const bool& newLine ) { mFS->Write( "\n", 1 ); } - eeSAFE_DELETE( mFS ); + closefs(); } } @@ -78,7 +78,7 @@ void cLog::openfs() { mFilePath = GetProcessPath(); } - eeSAFE_DELETE( mFS ); + closefs(); if ( NULL == mFS ) { std::string str = mFilePath + "log.log"; @@ -87,6 +87,14 @@ void cLog::openfs() { } } +void cLog::closefs() { + Lock(); + + eeSAFE_DELETE( mFS ); + + Unlock(); +} + void cLog::Writef( const char* format, ... ) { int n, size = 256; std::string tstr( size, '\0' ); @@ -124,7 +132,7 @@ void cLog::Writef( const char* format, ... ) { mFS->Write( tstr.c_str(), tstr.size() ); - eeSAFE_DELETE( mFS ); + closefs(); } return; diff --git a/src/system/clog.hpp b/src/system/clog.hpp index a3a8c0e6e..752f11c29 100755 --- a/src/system/clog.hpp +++ b/src/system/clog.hpp @@ -4,10 +4,11 @@ #include "base.hpp" #include "tsingleton.hpp" #include "ciostreamfile.hpp" +#include "cmutex.hpp" namespace EE { namespace System { -class EE_API cLog { +class EE_API cLog : protected cMutex { SINGLETON_DECLARE_HEADERS(cLog) public: @@ -39,6 +40,8 @@ class EE_API cLog { cIOStreamFile * mFS; void openfs(); + + void closefs(); }; }} diff --git a/src/test/eetest.cpp b/src/test/eetest.cpp index 3d8c6ddac..49009a0bf 100644 --- a/src/test/eetest.cpp +++ b/src/test/eetest.cpp @@ -25,7 +25,7 @@ void cEETest::Init() { mAxisX = 0; mAxisY = 0; - mCurDemo = 0xFFFFFF; + mCurDemo = eeINDEX_NOT_FOUND; mMapEditor = NULL; mETGEditor = NULL; Mus = NULL; @@ -181,6 +181,10 @@ void cEETest::LoadFonts() { mFontLoader.Add( eeNew( cTTFFontLoader, ( "arial", PAK, "arial.ttf", 12, EE_TTF_STYLE_NORMAL, false, 256, eeColor(255,255,255) ) ) ); mFontLoader.Add( eeNew( cTTFFontLoader, ( "arialb", PAK, "arial.ttf", 12, EE_TTF_STYLE_NORMAL, false, 256, eeColor(255,255,255), 1, eeColor(0,0,0), true ) ) ); + //mFontLoader.Add( eeNew( cTextureFontLoader, ( "ProggySquareSZ", eeNew( cTextureLoader, ( MyPath + "data/ProggySquareSZ.png" ) ), MyPath + "data/ProggySquareSZ.fnt" ) ) ); + //mFontLoader.Add( eeNew( cTTFFontLoader, ( "ProggySquareSZ", MyPath + "data/ProggySquareSZ.ttf", 16, EE_TTF_STYLE_NORMAL, false, 256 ) ) ); + //mFontLoader.Add( eeNew( cTTFFontLoader, ( "DejaVuSansMono", MyPath + "fonts/DejaVuSansMono.ttf", 12, EE_TTF_STYLE_NORMAL, false, 512, eeColor(), 1, eeColor(0,0,0) ) ) ); + mFontLoader.Load( cb::Make1( this, &cEETest::OnFontLoaded ) ); } @@ -190,6 +194,32 @@ void cEETest::OnFontLoaded( cResourceLoader * ObjLoaded ) { TTF = cFontManager::instance()->GetByName( "arial" ); TTFB = cFontManager::instance()->GetByName( "arialb" ); + //cFont * FF3 = cFontManager::instance()->GetByName( "DejaVuSansMono" ); + +/* + std::string MyFontPath = MyPath + "fonts" + GetOSlash(); + cTTFFont * TTFD, * TTFMon; + TTFD = eeNew( cTTFFont, ( "DejaVuSans" ) ); + TTFMon = eeNew( cTTFFont, ( "DejaVuSansMono" ) ); + + TTFD->Load( MyFontPath + "DejaVuSans.ttf", 12, EE_TTF_STYLE_NORMAL, false, 512, eeColor(), 1, eeColor(0,0,0) ); + TTFMon->Load( MyFontPath + "DejaVuSansMono.ttf", 12, EE_TTF_STYLE_NORMAL, false, 512, eeColor(), 1, eeColor(0,0,0) ); + + TTFD->Save( MyFontPath + "DejaVuSans.png", MyFontPath + "DejaVuSans.dat", EE_SAVE_TYPE_PNG ); + TTFMon->Save( MyFontPath + "DejaVuSansMono.png", MyFontPath + "DejaVuSansMono.dat", EE_SAVE_TYPE_PNG ); +*/ + + /* + cTTFFont * Fnt = reinterpret_cast ( FF2 ); + Fnt->Save( MyPath + "data/ProggySquareSZ.png", MyPath + "data/ProggySquareSZ.fnt" ); + + Fnt = reinterpret_cast ( TTF ); + Fnt->Save( MyPath + "data/arial.png", MyPath + "data/arial.fnt" ); + + Fnt = reinterpret_cast ( TTFB ); + Fnt->Save( MyPath + "data/arialb.png", MyPath + "data/arialb.fnt" ); + */ + Log->Writef( "Fonts loading time: %f", mFTE.Elapsed() ); eeASSERT( TTF != NULL ); @@ -454,9 +484,20 @@ void cEETest::CreateUI() { mComboBox->ListBox()->AddListBoxItems( combostrs ); mComboBox->ListBox()->SetSelected( 0 ); - +/* + cUIPopUpMenu::CreateParams MenuParams; + MenuParams.Flags = UI_CONTROL_DEFAULT_FLAGS | UI_AUTO_SIZE | UI_AUTO_PADDING; + MenuParams.RowHeight = 0; + MenuParams.PaddingContainer = eeRecti(); + MenuParams.MinWidth = 100; + MenuParams.MinSpaceForIcons = 24; + MenuParams.MinRightMargin = 8; + MenuParams.Font = FF2; + Menu = eeNew( cUIPopUpMenu, ( MenuParams ) ); +*/ Menu = mTheme->CreatePopUpMenu(); Menu->Add( "New", mTheme->GetIconByName( "document-new" ) ); + Menu->Add( "Open...", mTheme->GetIconByName( "document-open" ) ); Menu->AddSeparator(); Menu->Add( "Map Editor" ); @@ -1206,7 +1247,7 @@ void cEETest::Render() { PR.DrawRectangle( 0.f, - (eeFloat)mWindow->GetHeight() - (eeFloat)mEEText.GetNumLines() * (eeFloat)mEEText.Font()->GetFontSize(), + (eeFloat)mWindow->GetHeight() - mEEText.GetTextHeight(), mEEText.GetTextWidth(), mEEText.GetTextHeight(), ColRR1, ColRR2, ColRR3, ColRR4 @@ -1222,7 +1263,7 @@ void cEETest::Render() { FF2->Draw( "_", 6.f + FF2->GetTextWidth(), 180.f ); } else { FF2->SetText( InBuf.Buffer().substr( NLPos, InBuf.CurPos() - NLPos ) ); - FF2->Draw( "_", 6.f + FF2->GetTextWidth(), 180.f + (eeFloat)LineNum * (eeFloat)FF2->GetFontSize() ); + FF2->Draw( "_", 6.f + FF2->GetTextWidth(), 180.f + (eeFloat)LineNum * (eeFloat)FF2->GetFontHeight() ); } FF2->SetText( "FPS: " + toStr( mWindow->FPS() ) ); @@ -1704,7 +1745,7 @@ void cEETest::Demo2Destroy() { void cEETest::ChangeDemo( Uint32 num ) { if ( num < mDemo.size() ) { - if ( 0xFFFFFF != mCurDemo ) + if ( eeINDEX_NOT_FOUND != mCurDemo ) mDemo[ mCurDemo ].destroy(); mCurDemo = num; diff --git a/src/test/eetest.hpp b/src/test/eetest.hpp index 72d2c523e..f46c41d34 100644 --- a/src/test/eetest.hpp +++ b/src/test/eetest.hpp @@ -269,7 +269,7 @@ class cEETest : private cThread { }; std::vector mDemo; - Int32 mCurDemo; + Uint32 mCurDemo; cSprite * mBoxSprite; cSprite * mCircleSprite; diff --git a/src/ui/cuilistbox.cpp b/src/ui/cuilistbox.cpp index 7218b7c02..27d18d2de 100644 --- a/src/ui/cuilistbox.cpp +++ b/src/ui/cuilistbox.cpp @@ -300,15 +300,15 @@ void cUIListBox::SetRowHeight() { Uint32 FontSize = 12; if ( NULL != cUIThemeManager::instance()->DefaultFont() ) - FontSize = cUIThemeManager::instance()->DefaultFont()->GetFontSize(); + FontSize = cUIThemeManager::instance()->DefaultFont()->GetFontHeight(); if ( NULL != mSkinState && NULL != mSkinState->GetSkin() && NULL != mSkinState->GetSkin()->Theme() && NULL != mSkinState->GetSkin()->Theme()->Font() ) - FontSize = mSkinState->GetSkin()->Theme()->Font()->GetFontSize(); + FontSize = mSkinState->GetSkin()->Theme()->Font()->GetFontHeight(); if ( NULL != mFont ) - FontSize = mFont->GetFontSize(); + FontSize = mFont->GetFontHeight(); - mRowHeight = (Uint32)( FontSize * 1.5f ); + mRowHeight = (Uint32)( FontSize + 4 ); } if ( tOldRowHeight != mRowHeight ) { diff --git a/src/ui/cuimenu.cpp b/src/ui/cuimenu.cpp index d698b69b5..cfd9bfb84 100644 --- a/src/ui/cuimenu.cpp +++ b/src/ui/cuimenu.cpp @@ -353,7 +353,7 @@ Uint32 cUIMenu::OnMessage( const cUIMessage * Msg ) { void cUIMenu::OnSizeChange() { if ( NULL != mFont && ( ( mFlags & UI_AUTO_SIZE ) || 0 == mRowHeight ) ) { - mRowHeight = mFont->GetFontSize() * 1.5f; + mRowHeight = mFont->GetFontHeight() + 8; } if ( 0 != mMinWidth && mSize.Width() < (Int32)mMinWidth ) { diff --git a/src/ui/cuitextedit.cpp b/src/ui/cuitextedit.cpp index e58e295b1..3623541ce 100644 --- a/src/ui/cuitextedit.cpp +++ b/src/ui/cuitextedit.cpp @@ -348,7 +348,7 @@ void cUITextEdit::FixScrollToCursor() { mSkipValueChange = true; eeFloat tW = mTextInput->GetTextCache()->Font()->GetTextWidth(); - eeFloat tH = (eeFloat)(LineNum + 1) * (eeFloat)mTextInput->GetTextCache()->Font()->GetFontSize(); + eeFloat tH = (eeFloat)(LineNum + 1) * (eeFloat)mTextInput->GetTextCache()->Font()->GetFontHeight(); if ( tW > Width ) { mTextInput->Pos( mPadding.Left + Width - tW, mTextInput->Pos().y ); diff --git a/src/ui/cuitextinput.cpp b/src/ui/cuitextinput.cpp index 44aa1c7fb..5b9eb5898 100644 --- a/src/ui/cuitextinput.cpp +++ b/src/ui/cuitextinput.cpp @@ -145,7 +145,7 @@ void cUITextInput::AlignFix() { eeFloat tX = mAlignOffset.x + tW; mCurPos.x = tW; - mCurPos.y = (eeFloat)LineNum * (eeFloat)mTextCache->Font()->GetFontSize(); + mCurPos.y = (eeFloat)LineNum * (eeFloat)mTextCache->Font()->GetFontHeight(); if ( !mTextBuffer.SupportNewLine() ) { if ( tX < 0.f ) diff --git a/src/ui/cuitooltip.cpp b/src/ui/cuitooltip.cpp index 72dd93e1e..3fa556b51 100644 --- a/src/ui/cuitooltip.cpp +++ b/src/ui/cuitooltip.cpp @@ -24,7 +24,7 @@ cUITooltip::cUITooltip( cUITooltip::CreateParams& Params, cUIControl * TooltipOf eePRINT( "cUITooltip::cUITextBox : Created a UI TextBox without a defined font.\n" ); } - AutoAlign(); + AutoPadding(); if ( Params.ParentCtrl != cUIManager::instance()->MainControl() ) Parent( cUIManager::instance()->MainControl() ); @@ -161,7 +161,7 @@ void cUITooltip::AutoSize() { void cUITooltip::AutoAlign() { Uint32 Width = mSize.Width() - mPadding.Left - mPadding.Right; - Uint32 Height = mSize.Height() - mPadding.Top - mPadding.Right; + Uint32 Height = mSize.Height() - mPadding.Top - mPadding.Bottom; switch ( FontHAlignGet( Flags() ) ) { case UI_HALIGN_CENTER: