Ver código fonte

add bitmap fonts

Paul Jan 11 anos atrás
pai
commit
9f68dc45c4

+ 1 - 0
engine/source/game/defaultGame.cc

@@ -145,6 +145,7 @@ bool initializeLibraries()
     ResourceManager->registerExtension(".jpeg", constructBitmapJPEG);
     ResourceManager->registerExtension(".png", constructBitmapPNG);
     ResourceManager->registerExtension(".uft", constructNewFont);
+    ResourceManager->registerExtension(".fnt", constructBMFont);
 
 #ifdef TORQUE_OS_IOS
     ResourceManager->registerExtension(".pvr", constructBitmapPVR);

+ 230 - 2
engine/source/graphics/gFont.cc

@@ -29,6 +29,7 @@
 #include "graphics/gBitmap.h"
 #include "io/fileStream.h"
 #include "string/findMatch.h"
+#include "string/stringUnit.h"
 #include "graphics/TextureManager.h"
 #include "graphics/gFont.h"
 #include "memory/safeDelete.h"
@@ -101,9 +102,20 @@ void GFont::getFontCacheFilename(const char *faceName, U32 size, U32 buffLen, ch
 Resource<GFont> GFont::create(const char *faceName, U32 size, const char *cacheDirectory, U32 charset /* = TGE_ANSI_CHARSET */)
 {
    char buf[256];
+   Resource<GFont> ret;
+    
+    dSprintf(buf, sizeof(buf), "%s/%s %d (%s).fnt", cacheDirectory, faceName, size, getFontCharSetName(charset));
+    
+    ret = ResourceManager->load(buf);
+    if(bool(ret))
+    {
+        ret->mGFTFile = StringTable->insert(buf);
+        return ret;
+    }
+
    dSprintf(buf, sizeof(buf), "%s/%s %d (%s).uft", cacheDirectory, faceName, size, getFontCharSetName(charset));
 
-   Resource<GFont> ret = ResourceManager->load(buf);
+   ret = ResourceManager->load(buf);
    if(bool(ret))
    {
       ret->mGFTFile = StringTable->insert(buf);
@@ -654,7 +666,7 @@ bool GFont::read(Stream& io_rStream)
        mTextureSheets.increment();
        constructInPlace(&mTextureSheets.last());
        mTextureSheets.last() = TextureHandle(buf, bmp, TextureHandle::BitmapKeepTexture);
-       mTextureSheets.last().setFilter(GL_NEAREST);;
+       mTextureSheets.last().setFilter(GL_NEAREST);
    }
    
    // Read last position info
@@ -1008,3 +1020,219 @@ void GFont::importStrip(const char *fileName, U32 padding, U32 kerning)
    for(S32 i=0; i<sheetSizes.size(); i++)
       mTextureSheets[i].refresh();
 }
+
+// ==================================
+// bmFont support
+// ==================================
+
+ResourceInstance* constructBMFont(Stream& stream)
+{
+
+    GFont *ret = new GFont;
+
+    if(!ret->readBMFont(stream))
+    {
+        SAFE_DELETE(ret);
+        ret = NULL;
+    }
+
+    return ret;
+}
+
+bool GFont::readBMFont(Stream& io_rStream)
+{
+    for (U32 i = 0; i < (sizeof(mRemapTable) / sizeof(S32)); i++)
+        mRemapTable[i] = -1;
+    
+    U32 bmWidth = 0;
+    U32 bmHeight = 0;
+    U32 numSheets = 0;
+    U32 currentPage = 0;
+    StringTableEntry fileName = StringTable->insert("");
+    
+    U32 numBytes = io_rStream.getStreamSize() - io_rStream.getPosition();
+    while((io_rStream.getStatus() != Stream::EOS) && numBytes > 0)
+    {
+        char Read[256];
+        char Token[256];
+        char *buffer = Con::getReturnBuffer(256);
+        io_rStream.readLine((U8 *)buffer, 256);
+        
+        char temp[256];
+        U32 tokenCount = StringUnit::getUnitCount(buffer, "\"");
+        
+        if (tokenCount > 1)
+        {
+            dSprintf(Token, 256, "%s", StringUnit::getUnit(buffer, 1, "\""));
+            dSprintf(temp, 256, "tok1");
+            dSprintf(buffer, 256, "%s", (char*)StringUnit::setUnit(buffer, 1, temp, "\""));
+        }
+        
+        U32 wordCount = StringUnit::getUnitCount(buffer, " \t\n");
+        
+        dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, 0, " \t\n"));
+        if( dStrcmp( Read, "info") == 0 )
+        {
+            U32 currentWordCount = 1;
+            while( currentWordCount < wordCount )
+            {
+                dSprintf(Read, 256, StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
+                char temp[256];
+                char Key[256];
+                char Value[256];
+                
+                dSprintf(temp, 256, "%s", Read);
+                dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
+                dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
+                
+                if (dStrcmp( Value, "\"tok1\"") == 0) {
+                    dSprintf(Value, 256, "%s", Token);
+                }
+                
+                if( dStrcmp( Key, "size" ) == 0 )
+                    mSize = U16(dAtoi(Value));
+                currentWordCount++;
+            }
+        }
+        if( dStrcmp( Read, "common" ) == 0 )
+        {
+            U32 currentWordCount = 1;
+            //this holds common data
+            while( currentWordCount < wordCount )
+            {
+                dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
+                char temp[256];
+                char Key[256];
+                char Value[256];
+                
+                dSprintf(temp, 256, "%s", Read);
+                dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
+                dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
+                
+                if (dStrcmp( Value, "\"tok1\"") == 0) {
+                    dSprintf(Value, 256, "%s", Token);
+                }
+                
+                if( dStrcmp( Key, "lineHeight" ) == 0 )
+                     mHeight = U16(dAtoi(Value));
+                else if( dStrcmp( Key, "base" ) == 0 )
+                    mBaseline = U16(dAtoi(Value));
+                else if( dStrcmp( Key, "scaleW" ) == 0 )
+                    bmWidth = U16(dAtoi(Value));
+                else if( dStrcmp( Key, "scaleH" ) == 0 )
+                    bmHeight = U16(dAtoi(Value));
+                else if( dStrcmp( Key, "pages" ) == 0 )
+                    numSheets = U16(dAtoi(Value));
+                currentWordCount++;
+            }
+            mAscent = mBaseline;
+            mDescent = mHeight - mBaseline;
+        }
+        else if( dStrcmp( Read, "page" ) == 0 )
+        {
+            //this is data for a page
+            U32 currentWordCount = 1;
+            //this holds common data
+            char lineLeft[256];
+            dSprintf ( lineLeft, 256, "%s", StringUnit::getUnit(buffer, 1, " \t\n"));
+            
+            while( currentWordCount < wordCount )
+            {
+                dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
+                char temp[256];
+                char Key[256];
+                char Value[256];
+                
+                dSprintf(temp, 256, "%s", Read);
+                dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
+                dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
+                
+                if (dStrcmp( Value, "\"tok1\"") == 0) {
+                    dSprintf(Value, 256, "%s", Token);
+                }
+                
+                //assign the correct value
+                if( dStrcmp( Key, "id" ) == 0 )
+                    currentPage = U32(dAtoi(Value));
+                else if (dStrcmp( Key, "file" ) == 0 )
+                    fileName = StringTable->insert(Value);
+
+                currentWordCount++;
+            }
+        }
+        
+        else if( dStrcmp( Read, "char" ) == 0 )
+        {
+            PlatformFont::CharInfo ci; //  = &mCharInfoList[charIndex];
+            ci.bitmapData = NULL;
+            ci.bitmapIndex = currentPage;
+            //this is data for a character set
+            U16 CharID = 0;
+            U32 currentWordCount = 1;
+            //this holds common data
+            while( currentWordCount < wordCount )
+            {
+                dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
+                char temp[256];
+                char Key[256];
+                char Value[256];
+                
+                
+                dSprintf(temp, 256, "%s", Read);
+                dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
+                dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
+                
+                if (dStrcmp( Value, "\"tok1\"") == 0) {
+                    dSprintf(Value, 256, "%s", Token);
+                }
+                
+                //assign the correct value
+                if( dStrcmp( Key, "id" ) == 0 )
+                    CharID = U32(dAtoi(Value));
+                if( dStrcmp( Key, "x" ) == 0 )
+                    ci.xOffset = U32(dAtoi(Value));
+                else if( dStrcmp( Key, "y" ) == 0 )
+                    ci.yOffset = U32(dAtoi(Value));
+                else if( dStrcmp( Key, "width" ) == 0 )
+                    ci.width = U32(dAtoi(Value));
+                else if( dStrcmp( Key, "height" ) == 0 )
+                    ci.height = U32(dAtoi(Value));
+                else if( dStrcmp( Key, "xoffset" ) == 0 )
+                    ci.xOrigin = S32(dAtoi(Value));
+                else if( dStrcmp( Key, "yoffset" ) == 0 )
+                    ci.yOrigin = mBaseline - S32(dAtoi(Value));
+                else if( dStrcmp( Key, "xadvance" ) == 0 )
+                    ci.xIncrement = S32(dAtoi(Value));
+                currentWordCount++;
+            }
+            mCharInfoList.push_back(ci);
+            mRemapTable[ CharID ] = mCharInfoList.size()-1;
+        }
+    }
+    
+    for(U32 i = 0; i < numSheets; i++)
+    {
+        char buf[1024];
+        dSprintf(buf, sizeof(buf), "%s/%s", Con::getVariable("$GUI::fontCacheDirectory"), fileName);
+        Con::printf("Platform::makeFullPathName %s", buf);
+        
+        GBitmap *bmp = dynamic_cast<GBitmap*>(ResourceManager->loadInstance(buf));
+        
+        if(bmp == NULL)
+        {
+            return false;
+        }
+        
+        char buff[30];
+        dSprintf(buff, sizeof(buff), "font_%d", smSheetIdCount++);
+        
+        mTextureSheets.increment();
+        constructInPlace(&mTextureSheets.last());
+        mTextureSheets.last() = TextureHandle(buf, bmp, TextureHandle::BitmapKeepTexture);
+        mTextureSheets.last().setFilter(GL_NEAREST);
+    }
+    return (io_rStream.getStatus() == Stream::EOS);
+}
+
+
+

+ 4 - 0
engine/source/graphics/gFont.h

@@ -52,12 +52,14 @@
 
 
 extern ResourceInstance* constructNewFont(Stream& stream);
+extern ResourceInstance* constructBMFont(Stream& stream);
 
 class TextureHandle;
 
 class GFont : public ResourceInstance
 {
    friend ResourceInstance* constructNewFont(Stream& stream);
+   friend ResourceInstance* constructBMFont(Stream& stream);
 
    static const U32 csm_fileVersion;
    static S32 smSheetIdCount;
@@ -174,6 +176,8 @@ public:
    /// Get the face name of the font.
    StringTableEntry getFontFaceName() const { return mFaceName; };
 
+   bool readBMFont(Stream& io_rStream);
+
    bool read(Stream& io_rStream);
    bool write(Stream& io_rStream);