Pārlūkot izejas kodu

Fixed the default UIBatch constructor to init all new member variables. Reapply speed optimisation logic in Text batch.

Wei Tjong Yao 12 gadi atpakaļ
vecāks
revīzija
cb357e8859
2 mainītis faili ar 53 papildinājumiem un 26 dzēšanām
  1. 51 26
      Engine/UI/Text.cpp
  2. 2 0
      Engine/UI/UIBatch.cpp

+ 51 - 26
Engine/UI/Text.cpp

@@ -45,6 +45,20 @@ static const char* horizontalAlignments[] =
     ""
 };
 
+struct GlyphLocation
+{
+    int x_;
+    int y_;
+    const FontGlyph* glyph_;
+
+    GlyphLocation(int x, int y, const FontGlyph* glyph) :
+        x_(x),
+        y_(y),
+        glyph_(glyph)
+    {
+    }
+};
+
 OBJECTTYPESTATIC(Text);
 
 Text::Text(Context* context) :
@@ -151,40 +165,51 @@ void Text::GetBatches(PODVector<UIBatch>& batches, PODVector<float>& vertexData,
         const FontFace* face = font_->GetFace(fontSize_);
         if (!face)
             return;
+
+        // Only traversing thru the printText once regardless of number of textures/pages in the font
+        Vector<PODVector<GlyphLocation> > pageGlyphLocations(face->textures_.Size());
         
+        unsigned rowIndex = 0;
+        int x = GetRowStartPosition(rowIndex);
+        int y = 0;
+
+        for (unsigned i = 0; i < printText_.Size(); ++i)
+        {
+            unsigned c = printText_[i];
+
+            if (c != '\n')
+            {
+                const FontGlyph* p = face->GetGlyph(c);
+                if (!p)
+                    continue;
+
+                pageGlyphLocations[p->page_].Push(GlyphLocation(x, y, p));
+
+                x += p->advanceX_;
+                if (i < printText_.Size() - 1)
+                    x += face->GetKerning(c, printText_[i + 1]);
+            }
+            else
+            {
+                x = GetRowStartPosition(++rowIndex);
+                y += rowHeight_;
+            }
+        }
+
         for (unsigned n = 0; n < face->textures_.Size(); ++n)
         {
+        	// One batch per texture/page
             UIBatch pageBatch(this, BLEND_ALPHA, currentScissor, face->textures_[n], &vertexData);
 
-            unsigned rowIndex = 0;
-            int x = GetRowStartPosition(rowIndex);
-            int y = 0;
-        
-            for (unsigned i = 0; i < printText_.Size(); ++i)
+            const PODVector<GlyphLocation>& pageGlyphLocation = pageGlyphLocations[n];
+            for (unsigned i = 0; i < pageGlyphLocation.Size(); ++i)
             {
-                unsigned c = printText_[i];
-            
-                if (c != '\n')
-                {
-                    const FontGlyph* p = face->GetGlyph(c);
-                    if (!p || p->page_ != n)
-                        continue;
-                                 
-                    const FontGlyph& glyph = *p;
-                    pageBatch.AddQuad(x + glyph.offsetX_, y + glyph.offsetY_, glyph.width_, glyph.height_, glyph.x_, glyph.y_);
-                
-                    x += glyph.advanceX_;
-                    if (i < printText_.Size() - 1)
-                        x += face->GetKerning(c, printText_[i + 1]);
-                }
-                else
-                {
-                    x = GetRowStartPosition(++rowIndex);
-                    y += rowHeight_;
-                }
+                const GlyphLocation& glyphLocation = pageGlyphLocation[i];
+                const FontGlyph& glyph = *glyphLocation.glyph_;
+                pageBatch.AddQuad(glyphLocation.x_ + glyph.offsetX_, glyphLocation.y_ + glyph.offsetY_, glyph.width_, glyph.height_, glyph.x_, glyph.y_);
             }
 
-            UIBatch::AddOrMerge(pageBatch, batches);
+            batches.Push(pageBatch);
         }
     }
     

+ 2 - 0
Engine/UI/UIBatch.cpp

@@ -44,6 +44,8 @@ UIBatch::UIBatch() :
     element_(0),
     blendMode_(BLEND_REPLACE),
     texture_(0),
+    invTextureSize_(Vector2::ONE),
+    fixedColor_(0),
     vertexData_(0),
     vertexStart_(0),
     vertexEnd_(0)