From cf6bfd78bb437d8e4fd16c96c5b3cfc4f5018c43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Lucas=20Golini?= Date: Sun, 18 Aug 2013 21:06:47 -0300 Subject: [PATCH] cTTFFont outline is created by glyph. More code cleanup. --- include/eepp/graphics/ctexturepacker.hpp | 23 +++--- projects/linux/ee.includes | 1 + src/eepp/graphics/ctexturepacker.cpp | 6 +- src/eepp/graphics/cttffont.cpp | 91 ++++++++++++++---------- 4 files changed, 69 insertions(+), 52 deletions(-) diff --git a/include/eepp/graphics/ctexturepacker.hpp b/include/eepp/graphics/ctexturepacker.hpp index 3ba5c30dd..b4baf20d0 100644 --- a/include/eepp/graphics/ctexturepacker.hpp +++ b/include/eepp/graphics/ctexturepacker.hpp @@ -62,16 +62,16 @@ class EE_API cTexturePacker { /** Adds a image/texture from its path to the texture atlas. * @param TexturePath The image path. */ - bool AddTexture( const std::string& TexturePath ); + bool AddTexture( const std::string& TexturePath ); /** Adds a image to the texture atlas. The image instance must remain in memory until the texture atlas is saved. */ - bool AddImage( cImage * Img, const std::string& Name ); + bool AddImage( cImage * Img, const std::string& Name ); /** Adds a directory with images. It will try to add all the images inside that directory to the texture atlas. */ - bool AddTexturesPath( std::string TexturesPath ); + bool AddTexturesPath( std::string TexturesPath ); /** After adding all the images that will be used to create the texture atlas. Packing the textures will generate the texture atlas information ( it will fit the images inside the texture atlas, etc ). */ - Int32 PackTextures(); + Int32 PackTextures(); /** @brief Save the texture atlas to a file, in the indicated format. * If PackTexture() has not been called, it will be called automatically by the function ( so you don't need to call it ). @@ -79,9 +79,10 @@ class EE_API cTexturePacker { * @param Format The image format of the new texture atlas. * @param SaveExtensions Indicates if the extensions of the image files must be saved. Usually you wan't to find the SubTextures by its name without extension, but this can be changed here. */ - void Save( const std::string& Filepath, const EE_SAVE_TYPE& Format = SAVE_TYPE_PNG, const bool& SaveExtensions = false ); + void Save( const std::string& Filepath, const EE_SAVE_TYPE& Format = SAVE_TYPE_PNG, const bool& SaveExtensions = false ); - void Close(); + /** Clear all the textures added */ + void Close(); /** First of all you need to set at least the max dimensions of the texture atlas. * If the instance of the texture packer was created without indicating this data, this must be called before adding any texture or image. @@ -91,16 +92,16 @@ class EE_API cTexturePacker { * @param PixelBorder Indicates how many pixels will be added to separate one image to another in the texture atlas. Usefull to avoid artifacts when rendered scaled SubTextures. Use at least 1 pixel to separate images if you will scale any SubTexture. * @param AllowFlipping Indicates if the images can be flipped inside the texture atlas. This is not compatible with eepp ( since it can't flip the textures back to the original orientation ). So avoid it for eepp. */ - void SetOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, const bool& ForcePowOfTwo = true, const Uint32& PixelBorder = 0, const bool& AllowFlipping = false ); + void SetOptions( const Uint32& MaxWidth, const Uint32& MaxHeight, const bool& ForcePowOfTwo = true, const Uint32& PixelBorder = 0, const bool& AllowFlipping = false ); /** @return The texture atlas to generate width. */ - const Int32& Width() const; + const Int32& Width() const; /** @return The texture atlas to generate height */ - const Int32& Height() const; + const Int32& Height() const; /** @return If the texture atlas has already been saved, returns the file path to the texture atlas. */ - const std::string& GetFilepath() const; + const std::string& GetFilepath() const; protected: enum PackStrategy { PackBig, @@ -168,7 +169,7 @@ class EE_API cTexturePacker { void CreateChild(); - bool AddPackerText( cTexturePackerTex * TPack ); + bool AddPackerTex( cTexturePackerTex * TPack ); }; }} diff --git a/projects/linux/ee.includes b/projects/linux/ee.includes index 02a2a3994..eb8e42b7d 100644 --- a/projects/linux/ee.includes +++ b/projects/linux/ee.includes @@ -1 +1,2 @@ ../../src/ +../../include/ diff --git a/src/eepp/graphics/ctexturepacker.cpp b/src/eepp/graphics/ctexturepacker.cpp index 1bd836823..e3a9753f6 100644 --- a/src/eepp/graphics/ctexturepacker.cpp +++ b/src/eepp/graphics/ctexturepacker.cpp @@ -391,7 +391,7 @@ bool cTexturePacker::AddTexturesPath( std::string TexturesPath ) { return false; } -bool cTexturePacker::AddPackerText( cTexturePackerTex * TPack ) { +bool cTexturePacker::AddPackerTex( cTexturePackerTex * TPack ) { if ( TPack->LoadedInfo() ) { // Only add the texture if can fit inside the atlas, otherwise it will ignore it if ( ( TPack->Width() + mPixelBorder <= mWidth && TPack->Height() + mPixelBorder <= mHeight ) || @@ -426,13 +426,13 @@ bool cTexturePacker::AddPackerText( cTexturePackerTex * TPack ) { bool cTexturePacker::AddImage( cImage * Img, const std::string& Name ) { cTexturePackerTex * TPack = eeNew( cTexturePackerTex, ( Img, Name ) ); - return AddPackerText( TPack ); + return AddPackerTex( TPack ); } bool cTexturePacker::AddTexture( const std::string& TexturePath ) { if ( FileSystem::FileExists( TexturePath ) ) { cTexturePackerTex * TPack = eeNew( cTexturePackerTex, ( TexturePath ) ); - return AddPackerText( TPack ); + return AddPackerTex( TPack ); } return false; diff --git a/src/eepp/graphics/cttffont.cpp b/src/eepp/graphics/cttffont.cpp index dc45c1c9c..640b9db76 100755 --- a/src/eepp/graphics/cttffont.cpp +++ b/src/eepp/graphics/cttffont.cpp @@ -137,7 +137,6 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONT_STYLE Style, const Uint16& Uint32 * TexGlyph; Uint32 w = (Uint32)mTexWidth; - Uint32 h = (Uint32)mTexHeight; eeColorA fFontColor( FontColor ); //Loop through all chars @@ -166,6 +165,7 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONT_STYLE Style, const Uint16& //Blit the glyph onto the glyph sheet TexGlyph = reinterpret_cast ( TempGlyphSurface ); + // Copy the glyph to the texture for (int y = 0; y < GlyphRect.y; ++y ) { // Copy per row memcpy( &mPixels[ CurrentPos.Left + (CurrentPos.Top + y) * w ], &TexGlyph[ y * GlyphRect.x ], GlyphRect.x * sizeof(eeColorA) ); @@ -183,8 +183,8 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONT_STYLE Style, const Uint16& tR.Right = (eeFloat)(CurrentPos.Left + CurrentPos.Right + OutlineSize ) / mTexWidth; tR.Bottom = (eeFloat)(CurrentPos.Top + CurrentPos.Bottom + OutlineSize ) / mTexHeight; - GlyphRect.y += OutlineSize; - TempGlyph.Advance += OutlineSize; + GlyphRect.y += OutlineSize; + TempGlyph.Advance += OutlineSize; // Translate the Glyph coordinates to the new texture coordinates TempGlyph.MinX -= OutlineSize; @@ -206,6 +206,56 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONT_STYLE Style, const Uint16& CurrentPos.Top += mHeight; } + // Create the outline for the glyph and copy the outline to the texture + if ( OutlineSize ) { + eeRecti nGlyphR( + TempGlyph.CurX, + TempGlyph.CurY, + TempGlyph.CurX + TempGlyph.CurW, + TempGlyph.CurY + TempGlyph.CurH + ); + eeSize nGlyphS( nGlyphR.Size() ); + + if ( nGlyphS.x > 0 && nGlyphS.y > 0 ) { + Uint32 Pos = 0; + Uint32 RPos = 0; + Uint32 alphaSize = nGlyphS.x * nGlyphS.y; + Uint8 * alpha_init = eeNewArray( Uint8, alphaSize ); + Uint8 * alpha_final = eeNewArray( Uint8, alphaSize ); + + // Fill the alpha_init ( the default font alpha channels ) and the alpha_final ( the new outline ) + for ( Int32 y = 0; y < nGlyphS.y; y++ ) { + for( Int32 x = 0; x < nGlyphS.x; x++) { + RPos = ( nGlyphR.Left + x ) + ( nGlyphR.Top + y ) * w; + Pos = x + y * nGlyphS.x; + + alpha_init[ Pos ] = mPixels[ RPos ].A(); + alpha_final[ Pos ] = 0; + } + } + + // Create the outline + MakeOutline( alpha_init, alpha_final, nGlyphS.x, nGlyphS.y, OutlineSize ); + + for ( Int32 y = 0; y < nGlyphS.y; y++ ) { + for( Int32 x = 0; x < nGlyphS.x; x++) { + RPos = ( nGlyphR.Left + x ) + ( nGlyphR.Top + y ) * w; + Pos = x + y * nGlyphS.x; + + // Fill the outline color + mPixels[ RPos ] = eeColorA( OutlineColor, alpha_final[ Pos ] ); + + // Fill the font color + if ( alpha_init[ Pos ] > 50 ) + mPixels[ RPos ] = eeColorA( FontColor, alpha_init[ Pos ] ); + } + } + + eeSAFE_DELETE_ARRAY( alpha_init ); + eeSAFE_DELETE_ARRAY( alpha_final ); + } + } + //Push back to glyphs vector mGlyphs[i] = TempGlyph; @@ -213,41 +263,6 @@ bool cTTFFont::iLoad( const eeUint& Size, EE_TTF_FONT_STYLE Style, const Uint16& hkSAFE_DELETE_ARRAY( TempGlyphSurface ); } - if ( OutlineSize ) { - Uint32 Pos = 0; - - Uint8 * alpha_init = eeNewArray( Uint8, TexSize ); - Uint8 * alpha_final = eeNewArray( Uint8, TexSize ); - - // Fill the alpha_init ( the default font alpha channels ) and the alpha_final ( the new outline ) - for ( Uint32 y = 0; y < h; y++ ) { - for( Uint32 x = 0; x < w; x++) { - Pos = x + y * w; - alpha_init[ Pos ] = mPixels[ Pos ].A(); - alpha_final[ Pos ] = 0; - } - } - - // Create the outline - MakeOutline( alpha_init, alpha_final, w, h, OutlineSize ); - - for ( Uint32 y = 0; y < h; y++ ) { - for( Uint32 x = 0; x < w; x++) { - Pos = x + y * w; - - // Fill the outline color - mPixels[ Pos ] = eeColorA( OutlineColor, alpha_final[ Pos ] ); - - // Fill the font color - if ( alpha_init[ Pos ] > 50 ) - mPixels[ Pos ] = eeColorA( FontColor, alpha_init[ Pos ] ); - } - } - - eeSAFE_DELETE_ARRAY( alpha_init ); - eeSAFE_DELETE_ARRAY( alpha_final ); - } - hkFontManager::instance()->CloseFont( mFont ); mTexReady = true;