瀏覽代碼

Merge branch 'next' of https://github.com/blackberry/GamePlay into next

sgrenier 11 年之前
父節點
當前提交
94f7c35b1e

+ 41 - 5
gameplay/src/Bundle.cpp

@@ -28,7 +28,7 @@
 #define BUNDLE_MAX_STRING_LENGTH        5000
 
 #define BUNDLE_VERSION_MAJOR_FONT_FORMAT  1
-#define BUNDLE_VERSION_MINOR_FONT_FORMAT  4
+#define BUNDLE_VERSION_MINOR_FONT_FORMAT  5
 
 namespace gameplay
 {
@@ -1698,11 +1698,47 @@ Font* Bundle::loadFont(const char* id)
         }
 
         Font::Glyph* glyphs = new Font::Glyph[glyphCount];
-        if (_stream->read(glyphs, sizeof(Font::Glyph), glyphCount) != glyphCount)
+        for (unsigned j = 0; j < glyphCount; j++)
         {
-            GP_ERROR("Failed to read glyphs for font '%s'.", id);
-            SAFE_DELETE_ARRAY(glyphs);
-            return NULL;
+            if (_stream->read(&glyphs[j].code, 4, 1) != 1)
+            {
+                GP_ERROR("Failed to read glyph #%d code for font '%s'.", j, id);
+                SAFE_DELETE_ARRAY(glyphs);
+                return NULL;
+            }
+            if (_stream->read(&glyphs[j].width, 4, 1) != 1)
+            {
+                GP_ERROR("Failed to read glyph #%d width for font '%s'.", j, id);
+                SAFE_DELETE_ARRAY(glyphs);
+                return NULL;
+            }
+            if (getVersionMajor() >= 1 && getVersionMinor() >= 5)
+            {
+                if (_stream->read(&glyphs[j].bearingX, 4, 1) != 1)
+                {
+                    GP_ERROR("Failed to read glyph #%d bearingX for font '%s'.", j, id);
+                    SAFE_DELETE_ARRAY(glyphs);
+                    return NULL;
+                }
+                if (_stream->read(&glyphs[j].advance, 4, 1) != 1)
+                {
+                    GP_ERROR("Failed to read glyph #%d advance for font '%s'.", j, id);
+                    SAFE_DELETE_ARRAY(glyphs);
+                    return NULL;
+                }
+            }
+            else
+            {
+                // Fallback values for older GBP format.
+                glyphs[j].bearingX = 0;
+                glyphs[j].advance = glyphs[j].width;
+            }
+            if (_stream->read(&glyphs[j].uvs, 4, 4) != 4)
+            {
+                GP_ERROR("Failed to read glyph #%d uvs for font '%s'.", j, id);
+                SAFE_DELETE_ARRAY(glyphs);
+                return NULL;
+            }
         }
 
         // Read texture attributes.

+ 5 - 2
gameplay/src/Container.cpp

@@ -240,6 +240,7 @@ unsigned int Container::addControl(Control* control)
 	control->_parent = this;
 
 	sortControls();
+    setDirty(Control::DIRTY_BOUNDS);
 
 	return (unsigned int)( _controls.size() - 1 );
 }
@@ -259,6 +260,7 @@ void Container::insertControl(Control* control, unsigned int index)
         _controls.insert(it, control);
         control->addRef();
         control->_parent = this;
+        setDirty(Control::DIRTY_BOUNDS);
     }
 }
 
@@ -270,6 +272,7 @@ void Container::removeControl(unsigned int index)
     Control* control = *it;
     _controls.erase(it);
     control->_parent = NULL;
+    setDirty(Control::DIRTY_BOUNDS);
 
     if (_activeControl == control)
         _activeControl = NULL;
@@ -551,7 +554,7 @@ void Container::updateBounds()
             for (size_t i = 0, count = _controls.size(); i < count; ++i)
             {
                 Control* ctrl = _controls[i];
-                if (!ctrl->isXPercentage() && !ctrl->isWidthPercentage())
+                if (ctrl->isVisible() && !ctrl->isXPercentage() && !ctrl->isWidthPercentage())
                 {
                     float w = ctrl->getX() + ctrl->getWidth();
                     if (width < w)
@@ -569,7 +572,7 @@ void Container::updateBounds()
             for (size_t i = 0, count = _controls.size(); i < count; ++i)
             {
                 Control* ctrl = _controls[i];
-                if (!ctrl->isYPercentage() && !ctrl->isHeightPercentage())
+                if (ctrl->isVisible() && !ctrl->isYPercentage() && !ctrl->isHeightPercentage())
                 {
                     float h = ctrl->getY() + ctrl->getHeight();
                     if (height < h)

+ 8 - 0
gameplay/src/Control.cpp

@@ -509,6 +509,14 @@ void Control::setVisible(bool visible)
             Form::controlDisabled(this);
 
         setDirty(DIRTY_BOUNDS);
+
+        // force to update parent boundaries when child is hidden
+        Control* parent = _parent;
+        while (parent && (parent->_autoSize != AUTO_SIZE_NONE || static_cast<Container *>(parent)->getLayout()->getType() != Layout::LAYOUT_ABSOLUTE))
+        {
+            parent->setDirty(DIRTY_BOUNDS);
+            parent = parent->_parent;
+        }
     }
 }
 

+ 28 - 28
gameplay/src/Font.cpp

@@ -16,7 +16,7 @@ static std::vector<Font*> __fontCache;
 static Effect* __fontEffect = NULL;
 
 Font::Font() :
-    _format(BITMAP), _style(PLAIN), _size(0), _spacing(0.125f), _glyphs(NULL), _glyphCount(0), _texture(NULL), _batch(NULL), _cutoffParam(NULL)
+    _format(BITMAP), _style(PLAIN), _size(0), _spacing(0.0f), _glyphs(NULL), _glyphCount(0), _texture(NULL), _batch(NULL), _cutoffParam(NULL)
 {
 }
 
@@ -345,24 +345,24 @@ Font::Text* Font::createText(const char* text, const Rectangle& area, const Vect
             {
                 Glyph& g = _glyphs[glyphIndex];
 
-                if (xPos + (int)(g.width*scale) > area.x + area.width)
+                if (xPos + (int)(g.advance*scale) > area.x + area.width)
                 {
                     // Truncate this line and go on to the next one.
                     truncated = true;
                     break;
                 }
-                else if (xPos >= area.x)
+                else if (xPos >= (int)area.x)
                 {
                     // Draw this character.
                     if (draw)
                     {
                         if (clip)
                         {
-                            _batch->addSprite(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, *clip, &batch->_vertices[batch->_vertexCount]);
+                            _batch->addSprite(xPos + (int)(g.bearingX * scale), yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, *clip, &batch->_vertices[batch->_vertexCount]);
                         }
                         else
                         {
-                            _batch->addSprite(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, &batch->_vertices[batch->_vertexCount]);
+                            _batch->addSprite(xPos + (int)(g.bearingX * scale), yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, &batch->_vertices[batch->_vertexCount]);
                         }
 
                         if (batch->_vertexCount == 0)
@@ -395,7 +395,7 @@ Font::Text* Font::createText(const char* text, const Rectangle& area, const Vect
 
                     }
                 }
-                xPos += (int)(g.width)*scale + spacing;
+                xPos += (int)(g.advance)*scale + spacing;
             }
         }
 
@@ -558,7 +558,7 @@ void Font::drawText(const char* text, int x, int y, const Vector4& color, unsign
                 switch (delimiter)
                 {
                 case ' ':
-                    xPos += size >> 1;
+                    xPos += _glyphs[0].advance;
                     break;
                 case '\r':
                 case '\n':
@@ -566,7 +566,7 @@ void Font::drawText(const char* text, int x, int y, const Vector4& color, unsign
                     xPos = x;
                     break;
                 case '\t':
-                    xPos += (size >> 1)*4;
+                    xPos += _glyphs[0].advance * 4;
                     break;
                 case 0:
                     done = true;
@@ -609,7 +609,7 @@ void Font::drawText(const char* text, int x, int y, const Vector4& color, unsign
             switch (c)
             {
             case ' ':
-                xPos += size >> 1;
+                xPos += _glyphs[0].advance;
                 break;
             case '\r':
             case '\n':
@@ -617,7 +617,7 @@ void Font::drawText(const char* text, int x, int y, const Vector4& color, unsign
                 xPos = x;
                 break;
             case '\t':
-                xPos += (size >> 1)*4;
+                xPos += _glyphs[0].advance * 4;
                 break;
             default:
                 int index = c - 32; // HACK for ASCII
@@ -632,8 +632,8 @@ void Font::drawText(const char* text, int x, int y, const Vector4& color, unsign
                         // TODO: Fix me so that smaller font are much smoother
                         _cutoffParam->setVector2(Vector2(1.0, 1.0));
                     }
-                    _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
-                    xPos += floor(g.width * scale + spacing);
+                    _batch->draw(xPos + (int)(g.bearingX * scale), yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
+                    xPos += floor(g.advance * scale + spacing);
                     break;
                 }
                 break;
@@ -778,13 +778,13 @@ void Font::drawText(const char* text, const Rectangle& area, const Vector4& colo
             {
                 Glyph& g = _glyphs[glyphIndex];
 
-                if (xPos + (int)(g.width*scale) > area.x + area.width)
+                if (xPos + (int)(g.advance*scale) > area.x + area.width)
                 {
                     // Truncate this line and go on to the next one.
                     truncated = true;
                     break;
                 }
-                else if (xPos >= area.x)
+                else if (xPos >= (int)area.x)
                 {
                     // Draw this character.
                     if (draw)
@@ -798,15 +798,15 @@ void Font::drawText(const char* text, const Rectangle& area, const Vector4& colo
                         }
                         if (clip)
                         {
-                            _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, *clip);
+                            _batch->draw(xPos + (int)(g.bearingX * scale), yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color, *clip);
                         }
                         else
                         {
-                            _batch->draw(xPos, yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
+                            _batch->draw(xPos + (int)(g.bearingX * scale), yPos, g.width * scale, size, g.uvs[0], g.uvs[1], g.uvs[2], g.uvs[3], color);
                         }
                     }
                 }
-                xPos += (int)(g.width)*scale + spacing;
+                xPos += (int)(g.advance)*scale + spacing;
             }
         }
 
@@ -995,7 +995,7 @@ void Font::measureText(const char* text, const Rectangle& clip, unsigned int siz
                 switch (delimiter)
                 {
                     case ' ':
-                        delimWidth += size >> 1;
+                        delimWidth += _glyphs[0].advance;
                         break;
                     case '\r':
                     case '\n':
@@ -1031,7 +1031,7 @@ void Font::measureText(const char* text, const Rectangle& clip, unsigned int siz
                         delimWidth = 0;
                         break;
                     case '\t':
-                        delimWidth += (size >> 1)*4;
+                        delimWidth += _glyphs[0].advance * 4;
                         break;
                     case 0:
                         reachedEOF = true;
@@ -1331,7 +1331,7 @@ void Font::getMeasurementInfo(const char* text, const Rectangle& area, unsigned
                     switch (delimiter)
                     {
                         case ' ':
-                            delimWidth += size >> 1;
+                            delimWidth += _glyphs[0].advance;
                             lineLength++;
                             break;
                         case '\r':
@@ -1348,7 +1348,7 @@ void Font::getMeasurementInfo(const char* text, const Rectangle& area, unsigned
                             delimWidth = 0;
                             break;
                         case '\t':
-                            delimWidth += (size >> 1)*4;
+                            delimWidth += _glyphs[0].advance * 4;
                             lineLength++;
                             break;
                         case 0:
@@ -1627,7 +1627,7 @@ int Font::getIndexOrLocation(const char* text, const Rectangle& area, unsigned i
             {
                 Glyph& g = _glyphs[glyphIndex];
 
-                if (xPos + (int)(g.width*scale) > area.x + area.width)
+                if (xPos + (int)(g.advance*scale) > area.x + area.width)
                 {
                     // Truncate this line and go on to the next one.
                     truncated = true;
@@ -1645,7 +1645,7 @@ int Font::getIndexOrLocation(const char* text, const Rectangle& area, unsigned i
                     return charIndex;
                 }
 
-                xPos += floor(g.width*scale + spacing);
+                xPos += floor(g.advance*scale + spacing);
                 charIndex++;
             }
         }
@@ -1748,17 +1748,17 @@ unsigned int Font::getTokenWidth(const char* token, unsigned int length, unsigne
         switch (c)
         {
         case ' ':
-            tokenWidth += size >> 1;
+            tokenWidth += _glyphs[0].advance;
             break;
         case '\t':
-            tokenWidth += (size >> 1)*4;
+            tokenWidth += _glyphs[0].advance * 4;
             break;
         default:
             int glyphIndex = c - 32;
             if (glyphIndex >= 0 && glyphIndex < (int)_glyphCount)
             {
                 Glyph& g = _glyphs[glyphIndex];
-                tokenWidth += floor(g.width * scale + spacing);
+                tokenWidth += floor(g.advance * scale + spacing);
             }
             break;
         }
@@ -1822,7 +1822,7 @@ int Font::handleDelimiters(const char** token, const unsigned int size, const in
         switch (delimiter)
         {
             case ' ':
-                *xPos += size >> 1;
+                *xPos += _glyphs[0].advance;
                 (*lineLength)++;
                 if (charIndex)
                 {
@@ -1854,7 +1854,7 @@ int Font::handleDelimiters(const char** token, const unsigned int size, const in
                 }
                 break;
             case '\t':
-                *xPos += (size >> 1)*4;
+                *xPos += _glyphs[0].advance * 4;
                 (*lineLength)++;
                 if (charIndex)
                 {

+ 13 - 4
gameplay/src/Font.h

@@ -264,15 +264,14 @@ public:
     float getCharacterSpacing() const;
 
     /**
-     * Sets the fixed character spacing for this font.
+     * Sets the additional character spacing for this font.
      *
-     * Character spacing is the fixed amount of space that is inserted between characters. This is a simplified
-     * type of kerning and does not take adjacent characters into consideration. Character spacing is defined
+     * Character spacing is the additional amount of space that is inserted between characters. Character spacing is defined
      * as a floating point value that is interpreted as a percentage of size used to draw the font. For example,
      * a value of 0.1 would cause a spacing of 10% of the font size to be inserted between adjacent characters.
      * For a font size of 20, this would equate to 2 pixels of extra space between characters.
      *
-     * The default character spacing for fonts is 0.125.
+     * The default additional character spacing for fonts is 0.0.
      *
      * @param spacing New fixed character spacing, expressed as a percentage of font size.
      */
@@ -327,6 +326,16 @@ private:
          */
         unsigned int width;
 
+        /**
+         * Glyph left side bearing (in pixels).
+         */
+        int bearingX;
+
+        /**
+         * Glyph horizontal advance (in pixels).
+         */
+        unsigned int advance;
+
         /**
          * Glyph texture coordinates.
          */

+ 3 - 1
gameplay/src/ParticleEmitter.cpp

@@ -54,7 +54,9 @@ ParticleEmitter* ParticleEmitter::create(const char* textureFile, TextureBlendin
     GP_ASSERT(texture->getWidth());
     GP_ASSERT(texture->getHeight());
 
-    return ParticleEmitter::create(texture, textureBlending, particleCountMax);
+    ParticleEmitter* emitter = ParticleEmitter::create(texture, textureBlending, particleCountMax);
+    SAFE_RELEASE(texture);
+    return emitter;
 }
 
 ParticleEmitter* ParticleEmitter::create(Texture* texture, TextureBlending textureBlending,  unsigned int particleCountMax)

+ 1 - 1
gameplay/src/Slider.cpp

@@ -169,7 +169,7 @@ void Slider::updateValue(int x, int y)
     const Rectangle& maxCapRegion = _maxImage->getRegion();
     const Rectangle& markerRegion = _markerImage->getRegion();
 
-    float markerPosition = x / (_viewportBounds.width - markerRegion.width);
+    float markerPosition = (x - markerRegion.width * 0.5f) / (_viewportBounds.width - markerRegion.width);
             
     if (markerPosition > 1.0f)
     {

+ 2 - 5
gameplay/src/Vector4.inl

@@ -61,12 +61,9 @@ inline bool Vector4::operator<(const Vector4& v) const
     {
         if (y == v.y)
         {
-            if (z < v.z)
+            if (z == v.z)
             {
-                if (w < v.w)
-                {
-                    return w < v.w;
-                }
+                return w < v.w;
             }
             return z < v.z;
         }

+ 1 - 1
tools/encoder/src/GPBFile.h

@@ -21,7 +21,7 @@ namespace gameplay
  * Increment the version number when making a change that break binary compatibility.
  * [0] is major, [1] is minor.
  */
-const unsigned char GPB_VERSION[2] = {1, 4};
+const unsigned char GPB_VERSION[2] = {1, 5};
 
 /**
  * The GamePlay Binary file class handles writing the GamePlay Binary file.

+ 10 - 1
tools/encoder/src/TTFFontEncoder.cpp

@@ -361,6 +361,8 @@ int writeFont(const char* inFilePath, const char* outFilePath, std::vector<unsig
 
             glyphArray[i].index = ascii;
             glyphArray[i].width = advance - GLYPH_PADDING;
+            glyphArray[i].bearingX = slot->metrics.horiBearingX >> 6;
+            glyphArray[i].advance = slot->metrics.horiAdvance >> 6;
 
             // Generate UV coords.
             glyphArray[i].uvCoords[0] = (float)penX / (float)imageWidth;
@@ -415,7 +417,14 @@ int writeFont(const char* inFilePath, const char* outFilePath, std::vector<unsig
         // Glyphs.
         unsigned int glyphSetSize = END_INDEX - START_INDEX;
         writeUint(gpbFp, glyphSetSize);
-        fwrite(&font->glyphArray, sizeof(TTFGlyph), glyphSetSize, gpbFp);
+        for (unsigned int j = 0; j < glyphSetSize; j++)
+        {
+            writeUint(gpbFp, font->glyphArray[j].index);
+            writeUint(gpbFp, font->glyphArray[j].width);
+            fwrite(&font->glyphArray[j].bearingX, sizeof(int), 1, gpbFp);
+            writeUint(gpbFp, font->glyphArray[j].advance);
+            fwrite(&font->glyphArray[j].uvCoords, sizeof(float), 4, gpbFp);
+        }
 
         // Image dimensions
         unsigned int imageSize = font->imageWidth * font->imageHeight;

+ 2 - 0
tools/encoder/src/TTFFontEncoder.h

@@ -15,6 +15,8 @@ class TTFGlyph
 public:
     unsigned int index;
     unsigned int width;
+    int bearingX;
+    unsigned int advance;
     float uvCoords[4];
 };