فهرست منبع

Merge pull request #498 from blackberry-gaming/next-dgough

Next dgough
Sean Paul Taylor 13 سال پیش
والد
کامیت
e49bacc4f3

+ 12 - 0
gameplay-encoder/src/Base.cpp

@@ -11,4 +11,16 @@ void fillArray(float values[], float value, size_t length)
     }
 }
 
+std::string getBaseName(const std::string& filepath)
+{
+    size_t index1 = filepath.find_last_of('\\');
+    size_t index2 = filepath.find_last_of('/');
+    size_t index = (index1 != -1 && index1 > index2 ? index1 : index2);
+    size_t length = filepath.length();
+    std::string filename = filepath.substr(index + 1, length);
+    length = filename.length();
+    std::string output = filename.substr(0, (length-4));
+    return output;
+}
+
 }

+ 10 - 0
gameplay-encoder/src/Base.h

@@ -76,6 +76,16 @@ enum VertexUsage
 
 void fillArray(float values[], float value, size_t length);
 
+/**
+ * Returns the base name of the given path.
+ * Example: "c:/foo/bar/model.dae" returns "model.dae"
+ * 
+ * @param filepath File path.
+ * 
+ * @return Base file name.
+ */
+std::string getBaseName(const std::string& filepath);
+
 #define ISZERO(x) (fabs(x) < 0.000001f)
 
 // Object deletion macro

+ 8 - 14
gameplay-encoder/src/TTFFontEncoder.cpp

@@ -1,6 +1,7 @@
 #include "Base.h"
 #include "TTFFontEncoder.h"
 #include "GPBFile.h"
+#include "StringUtil.h"
 
 namespace gameplay
 {
@@ -33,7 +34,7 @@ static void writeString(FILE* fp, const char* str)
     }
 }
 
-int writeFont(const char* filename, unsigned int fontSize, const char* id, bool fontpreview = false)
+int writeFont(const char* inFilePath, const char* outFilePath, unsigned int fontSize, const char* id, bool fontpreview = false)
 {
     Glyph glyphArray[END_INDEX - START_INDEX];
     
@@ -48,7 +49,7 @@ int writeFont(const char* filename, unsigned int fontSize, const char* id, bool
     
     // Initialize font face.
     FT_Face face;
-    error = FT_New_Face(library, filename, 0, &face);
+    error = FT_New_Face(library, inFilePath, 0, &face);
     if (error)
     {
         fprintf(stderr, "FT_New_Face error: %d \n", error);
@@ -249,15 +250,9 @@ int writeFont(const char* filename, unsigned int fontSize, const char* id, bool
         penX += advance; // Move X to next glyph position
         i++;
     }
-    
-    unsigned int idlen = strlen(id);
 
-    // Write it to the id.gpb file.
-    char* fileName = (char*)malloc(idlen + 4);
-    strcpy(fileName, id);
-    strcat(fileName, ".gpb");
 
-    FILE *gpbFp = fopen(fileName, "wb");
+    FILE *gpbFp = fopen(outFilePath, "wb");
     
     // File header and version.
     char fileHeader[9]     = {'«', 'G', 'P', 'B', '»', '\r', '\n', '\x1A', '\n'};
@@ -303,15 +298,14 @@ int writeFont(const char* filename, unsigned int fontSize, const char* id, bool
     // Close file.
     fclose(gpbFp);
 
-    printf("%s.gpb created successfully! \n", id);
+    printf("%s.gpb created successfully. \n", getBaseName(outFilePath).c_str());
 
     if (fontpreview)
     {
         // Write out font map to an image.
-        strcpy(fileName, id);
-        strcat(fileName, ".pgm");
-
-        FILE *imageFp = fopen(fileName, "wb");
+        std::string pgmFilePath = getFilenameNoExt(outFilePath);
+        pgmFilePath.append(".pgm");
+        FILE *imageFp = fopen(pgmFilePath.c_str(), "wb");
         fprintf(imageFp, "P5 %d %d 255\n", imageWidth, imageHeight);
         fwrite((const char *)imageBuffer, sizeof(unsigned char), imageWidth * imageHeight, imageFp);
         fclose(imageFp);

+ 12 - 2
gameplay-encoder/src/TTFFontEncoder.h

@@ -3,7 +3,6 @@
 
 #define START_INDEX     32
 #define END_INDEX       127
-
 #define GLYPH_PADDING   4
 
 namespace gameplay
@@ -18,6 +17,17 @@ public:
     float uvCoords[4];
 };
 
-int writeFont(const char* filename, unsigned int fontSize, const char* id, bool fontpreview);
+/**
+ * Writes the font gpb file.
+ * 
+ * @param inFilePath Input file path to the tiff file.
+ * @param outFilePath Output file path to write the gpb to.
+ * @param fontSize Size of the font.
+ * @param id ID string of the font in the ref table.
+ * @param fontpreview True if the pgm font preview file should be written. (For debugging)
+ * 
+ * @return 0 if successful, -1 if error.
+ */
+int writeFont(const char* inFilePath, const char* outFilePath, unsigned int fontSize, const char* id, bool fontpreview);
 
 }

+ 28 - 12
gameplay-encoder/src/main.cpp

@@ -7,16 +7,28 @@
 
 using namespace gameplay;
 
-static std::string getFileName(const std::string& filepath)
+/**
+ * Prompts the user for a font size until a valid font size is entered.
+ * 
+ * @return A valid font size.
+ */
+static unsigned int promptUserFontSize()
 {
-    size_t index1 = filepath.find_last_of('\\');
-    size_t index2 = filepath.find_last_of('/');
-    size_t index = (index1 != -1 && index1 > index2 ? index1 : index2);
-    size_t length = filepath.length();
-    std::string filename = filepath.substr(index + 1, length);
-    length = filename.length();
-    std::string output = filename.substr(0, (length-4));
-    return output;
+    static const int lowerBound = 8;
+    static const int upperBound = 500;
+    unsigned int fontSize = 0;
+    char buffer[80];
+    do
+    {
+        printf("Enter font size (between %d and %d):\n", lowerBound, upperBound);
+        std::cin.getline(buffer, 80);
+        int i = atoi(buffer);
+        if (i >= lowerBound && i <= upperBound)
+        {
+            fontSize = (unsigned int)i;
+        }
+    } while (fontSize == 0);
+    return fontSize;
 }
 
 /**
@@ -74,9 +86,13 @@ int main(int argc, const char** argv)
         }
     case EncoderArguments::FILEFORMAT_TTF:
         {
-            std::string realpath(arguments.getFilePath());
-            std::string id = getFileName(realpath);
-            writeFont(realpath.c_str(), arguments.getFontSize(), id.c_str(), arguments.fontPreviewEnabled());
+            unsigned int fontSize = arguments.getFontSize();
+            if (fontSize == 0)
+            {
+                fontSize = promptUserFontSize();
+            }
+            std::string id = getBaseName(arguments.getFilePath());
+            writeFont(arguments.getFilePath().c_str(), arguments.getOutputFilePath().c_str(), fontSize, id.c_str(), arguments.fontPreviewEnabled());
             break;
         }
     case EncoderArguments::FILEFORMAT_GPB: