Quellcode durchsuchen

Refactor drawing glyphs (both Ui and Debug) into a separate function.
Minor style cleanup, use c++11 style forloop. No functional change.

Bryce Groff vor 11 Jahren
Ursprung
Commit
3a6cbdc424

+ 28 - 1
QGearsMain/include/core/UiTextArea.h

@@ -76,6 +76,33 @@ private:
     TiXmlNode*                          m_TextNode;
 };
 
-
+inline
+void WriteTriangleVertex(float *&writeIterator, const Ogre::Vector3 &coord,
+                const Ogre::ColourValue &colour, const float u, const float v)
+{
+    *writeIterator++ = coord.x;
+    *writeIterator++ = coord.y;
+    *writeIterator++ = coord.z;
+    *writeIterator++ = colour.r;
+    *writeIterator++ = colour.g;
+    *writeIterator++ = colour.b;
+    *writeIterator++ = colour.a;
+    *writeIterator++ = u;
+    *writeIterator++ = v;
+}
+
+inline void
+WriteGlyph(float *&writeIterator, const Ogre::Vector3 coords[],
+           const Ogre::ColourValue &colour, const Ogre::FloatRect &texture)
+{
+    // draw two triangles with a colour and texture.
+    WriteTriangleVertex(writeIterator, coords[0], colour, texture.left, texture.top);
+    WriteTriangleVertex(writeIterator, coords[1], colour, texture.right, texture.top);
+    WriteTriangleVertex(writeIterator, coords[2], colour, texture.right, texture.bottom);
+    // Triangle two
+    WriteTriangleVertex(writeIterator, coords[0], colour, texture.left, texture.top);
+    WriteTriangleVertex(writeIterator, coords[2], colour, texture.right, texture.bottom);
+    WriteTriangleVertex(writeIterator, coords[3], colour, texture.left, texture.bottom);
+}
 
 #endif // UI_TEXT_AREA_H

+ 24 - 70
QGearsMain/src/core/DebugDraw.cpp

@@ -6,6 +6,7 @@
 
 #include "core/CameraManager.h"
 #include "core/Logger.h"
+#include "core/UiTextArea.h"
 
 
 
@@ -337,11 +338,15 @@ DebugDraw::Text( const float x, const float y, const Ogre::String& text )
 {
     if( m_TextRenderOp.vertexData->vertexCount + text.size() * 6 > m_TextMaxVertexCount )
     {
-        LOG_ERROR( "Max number of text reached. Can't add text \"" + text + "\". Max number of letters is " + Ogre::StringConverter::toString( m_TextMaxVertexCount / 6 ) + "." );
+        LOG_ERROR("Max number of text reached. Can't add text \"" + text + "\". Max number of letters is " +
+                   Ogre::StringConverter::toString( m_TextMaxVertexCount / 6 ) + ".");
         return;
     }
 
+    // get a pointer to the vertex buffer and lock buffer
     float* writeIterator = ( float* ) m_TextVertexBuffer->lock( Ogre::HardwareBuffer::HBL_NORMAL );
+
+    // move the buffer one line forward
     writeIterator += m_TextRenderOp.vertexData->vertexCount * 9;
 
     Ogre::Viewport *viewport( CameraManager::getSingleton().getViewport() );
@@ -349,14 +354,14 @@ DebugDraw::Text( const float x, const float y, const Ogre::String& text )
     float height = static_cast<float>(viewport->getActualHeight());
 
     float length = 0;
-    if( m_TextAlignment != LEFT )
+    if(m_TextAlignment != LEFT)
     {
-        for( unsigned int i = 0; i < text.size(); ++i )
+        for( auto &c : text )
         {
-            length += ( ( m_Font->getGlyphAspectRatio( text[ i ] ) * m_FontHeight ) / width ) * 2;
+            length += ((m_Font->getGlyphAspectRatio(c) * m_FontHeight) / width) * 2;
         }
 
-        if( m_TextAlignment == CENTER )
+        if(m_TextAlignment == CENTER)
         {
             length /= 2;
         }
@@ -367,9 +372,12 @@ DebugDraw::Text( const float x, const float y, const Ogre::String& text )
     float current_y =  ( m_ScreenSpace == true ) ? -( ( ( int ) y / height ) * 2 - 1 ) : y;
     float char_height = -( m_FontHeight / height ) * 2;
 
-    for( unsigned int i = 0; i < text.size(); ++i )
+    for( auto &c : text )
     {
-        float char_width = ( ( m_Font->getGlyphAspectRatio( text[ i ] ) * m_FontHeight ) / width ) * 2;
+        // for each character, compute the size of the glyph.
+        // create a square from 2 triangles for the glyph and
+        // set x, y, color and texture location
+        float char_width = ((m_Font->getGlyphAspectRatio(c) * m_FontHeight) / width) * 2;
 
         float new_x1 = current_x;
         float new_y1 = current_y;
@@ -383,70 +391,16 @@ DebugDraw::Text( const float x, const float y, const Ogre::String& text )
         float new_x4 = current_x;
         float new_y4 = current_y + char_height;
 
-        current_x += char_width;
-
-        const Ogre::Font::UVRect& uv = m_Font->getGlyphTexCoords( text[ i ] );
-
-        *writeIterator++ = new_x1;
-        *writeIterator++ = new_y1;
-        *writeIterator++ = m_Z;
-        *writeIterator++ = m_Colour.r;
-        *writeIterator++ = m_Colour.g;
-        *writeIterator++ = m_Colour.b;
-        *writeIterator++ = m_Colour.a;
-        *writeIterator++ = uv.left;
-        *writeIterator++ = uv.top;
-
-        *writeIterator++ = new_x2;
-        *writeIterator++ = new_y2;
-        *writeIterator++ = m_Z;
-        *writeIterator++ = m_Colour.r;
-        *writeIterator++ = m_Colour.g;
-        *writeIterator++ = m_Colour.b;
-        *writeIterator++ = m_Colour.a;
-        *writeIterator++ = uv.right;
-        *writeIterator++ = uv.top;
-
-        *writeIterator++ = new_x3;
-        *writeIterator++ = new_y3;
-        *writeIterator++ = m_Z;
-        *writeIterator++ = m_Colour.r;
-        *writeIterator++ = m_Colour.g;
-        *writeIterator++ = m_Colour.b;
-        *writeIterator++ = m_Colour.a;
-        *writeIterator++ = uv.right;
-        *writeIterator++ = uv.bottom;
-
-        *writeIterator++ = new_x1;
-        *writeIterator++ = new_y1;
-        *writeIterator++ = m_Z;
-        *writeIterator++ = m_Colour.r;
-        *writeIterator++ = m_Colour.g;
-        *writeIterator++ = m_Colour.b;
-        *writeIterator++ = m_Colour.a;
-        *writeIterator++ = uv.left;
-        *writeIterator++ = uv.top;
-
-        *writeIterator++ = new_x3;
-        *writeIterator++ = new_y3;
-        *writeIterator++ = m_Z;
-        *writeIterator++ = m_Colour.r;
-        *writeIterator++ = m_Colour.g;
-        *writeIterator++ = m_Colour.b;
-        *writeIterator++ = m_Colour.a;
-        *writeIterator++ = uv.right;
-        *writeIterator++ = uv.bottom;
-
-        *writeIterator++ = new_x4;
-        *writeIterator++ = new_y4;
-        *writeIterator++ = m_Z;
-        *writeIterator++ = m_Colour.r;
-        *writeIterator++ = m_Colour.g;
-        *writeIterator++ = m_Colour.b;
-        *writeIterator++ = m_Colour.a;
-        *writeIterator++ = uv.left;
-        *writeIterator++ = uv.bottom;
 
+        Ogre::Vector3 coords[4] = {
+            Ogre::Vector3(current_x, current_y, m_Z),
+            Ogre::Vector3(current_x + char_width, current_y, m_Z),
+            Ogre::Vector3(current_x + char_width, current_y + char_height, m_Z),
+            Ogre::Vector3(current_x, current_y + char_height, m_Z),
+        };
+        current_x += char_width;
+        const Ogre::Font::UVRect& uv = m_Font->getGlyphTexCoords(c);
+        WriteGlyph(writeIterator, coords, m_Colour, m_Font->getGlyphTexCoords(c));
         m_TextRenderOp.vertexData->vertexCount += 6;
     }
 

+ 18 - 95
QGearsMain/src/core/UiTextArea.cpp

@@ -301,9 +301,9 @@ UiTextArea::SetTextGeometry( const Ogre::UTFString& text, TextBlockData& data, c
     float x = m_FinalTranslate.x;
     float y = m_FinalTranslate.y;
 
-    for( size_t i = 0; i < text.size(); ++i )
+    for(auto &c : text )
     {
-        UiCharData char_data = m_Font->GetCharData( text[ i ] );
+        UiCharData char_data = m_Font->GetCharData(c);
 
         local_x1 += char_data.pre * m_FinalScale.x * m_ScreenHeight / 720.0f;
         float local_x2 = local_x1 + char_data.width * m_FinalScale.x * m_ScreenHeight / 720.0f;
@@ -311,10 +311,6 @@ UiTextArea::SetTextGeometry( const Ogre::UTFString& text, TextBlockData& data, c
 
         int x1, y1, x2, y2, x3, y3, x4, y4;
 
-        //LOG_ERROR( m_Name );
-        //LOG_ERROR( "local_x1 = " + Ogre::StringConverter::toString( local_x1 ) + ", local_y1 = " + Ogre::StringConverter::toString( local_y1 ) );
-        //LOG_ERROR( "local_x2 = " + Ogre::StringConverter::toString( local_x2 ) + ", local_y2 = " + Ogre::StringConverter::toString( local_y2 ) );
-
         if( m_FinalRotation != 0 )
         {
             float cos = Ogre::Math::Cos( Ogre::Radian( Ogre::Degree( m_FinalRotation ) ) );
@@ -341,100 +337,27 @@ UiTextArea::SetTextGeometry( const Ogre::UTFString& text, TextBlockData& data, c
             y4 = static_cast<int>(local_y2 + y);
         }
 
-        //LOG_ERROR( "x1 = " + Ogre::StringConverter::toString( x1 ) + ", y1 = " + Ogre::StringConverter::toString( y1 ) );
-        //LOG_ERROR( "x2 = " + Ogre::StringConverter::toString( x2 ) + ", y2 = " + Ogre::StringConverter::toString( y2 ) );
-        //LOG_ERROR( "x3 = " + Ogre::StringConverter::toString( x3 ) + ", y3 = " + Ogre::StringConverter::toString( y3 ) );
-        //LOG_ERROR( "x4 = " + Ogre::StringConverter::toString( x4 ) + ", y4 = " + Ogre::StringConverter::toString( y4 ) );
-
-        float new_x1 = ( x1 / m_ScreenWidth ) * 2 - 1;
-        float new_y1 = -( ( y1 / m_ScreenHeight ) * 2 - 1 );
-        float new_x2 = ( x2 / m_ScreenWidth ) * 2 - 1;
-        float new_y2 = -( ( y2 / m_ScreenHeight ) * 2 - 1 );
-        float new_x3 = ( x3 / m_ScreenWidth ) * 2 - 1;
-        float new_y3 = -( ( y3 / m_ScreenHeight ) * 2 - 1 );
-        float new_x4 = ( x4 / m_ScreenWidth ) * 2 - 1;
-        float new_y4 = -( ( y4 / m_ScreenHeight ) * 2 - 1 );
-
         local_x1 += ( char_data.width + char_data.post ) * m_FinalScale.x * m_ScreenHeight / 720.0f;
 
         float width = static_cast<float>(m_Font->GetImageWidth());
         float height = static_cast<float>(m_Font->GetImageHeight());
-        float left = ( float )char_data.x / width;
-        float right = ( float )( char_data.x + char_data.width ) / width;
-        float top = ( float )char_data.y / height;
-        float bottom = ( float )( char_data.y + char_data.height ) / height;
-
-        //LOG_ERROR( "width = " + Ogre::StringConverter::toString( width ) + "." );
-        //LOG_ERROR( "height = " + Ogre::StringConverter::toString( height ) + "." );
-        //LOG_ERROR( "char_data.x = " + Ogre::StringConverter::toString( char_data.x ) + "." );
-        //LOG_ERROR( "char_data.y = " + Ogre::StringConverter::toString( char_data.y ) + "." );
-        //LOG_ERROR( "char_data.width = " + Ogre::StringConverter::toString( char_data.width ) + "." );
-        //LOG_ERROR( "char_data.height = " + Ogre::StringConverter::toString( char_data.height ) + "." );
-        //LOG_ERROR( "left = " + Ogre::StringConverter::toString( left ) + "." );
-        //LOG_ERROR( "right = " + Ogre::StringConverter::toString( right ) + "." );
-        //LOG_ERROR( "top = " + Ogre::StringConverter::toString( top ) + "." );
-        //LOG_ERROR( "bottom = " + Ogre::StringConverter::toString( bottom ) + "." );
-
-        *writeIterator++ = new_x1;
-        *writeIterator++ = new_y1;
-        *writeIterator++ = m_FinalZ;
-        *writeIterator++ = style.colour.r;
-        *writeIterator++ = style.colour.g;
-        *writeIterator++ = style.colour.b;
-        *writeIterator++ = style.colour.a;
-        *writeIterator++ = left;
-        *writeIterator++ = top;
-
-        *writeIterator++ = new_x2;
-        *writeIterator++ = new_y2;
-        *writeIterator++ = m_FinalZ;
-        *writeIterator++ = style.colour.r;
-        *writeIterator++ = style.colour.g;
-        *writeIterator++ = style.colour.b;
-        *writeIterator++ = style.colour.a;
-        *writeIterator++ = right;
-        *writeIterator++ = top;
-
-        *writeIterator++ = new_x3;
-        *writeIterator++ = new_y3;
-        *writeIterator++ = m_FinalZ;
-        *writeIterator++ = style.colour.r;
-        *writeIterator++ = style.colour.g;
-        *writeIterator++ = style.colour.b;
-        *writeIterator++ = style.colour.a;
-        *writeIterator++ = right;
-        *writeIterator++ = bottom;
-
-        *writeIterator++ = new_x1;
-        *writeIterator++ = new_y1;
-        *writeIterator++ = m_FinalZ;
-        *writeIterator++ = style.colour.r;
-        *writeIterator++ = style.colour.g;
-        *writeIterator++ = style.colour.b;
-        *writeIterator++ = style.colour.a;
-        *writeIterator++ = left;
-        *writeIterator++ = top;
-
-        *writeIterator++ = new_x3;
-        *writeIterator++ = new_y3;
-        *writeIterator++ = m_FinalZ;
-        *writeIterator++ = style.colour.r;
-        *writeIterator++ = style.colour.g;
-        *writeIterator++ = style.colour.b;
-        *writeIterator++ = style.colour.a;
-        *writeIterator++ = right;
-        *writeIterator++ = bottom;
-
-        *writeIterator++ = new_x4;
-        *writeIterator++ = new_y4;
-        *writeIterator++ = m_FinalZ;
-        *writeIterator++ = style.colour.r;
-        *writeIterator++ = style.colour.g;
-        *writeIterator++ = style.colour.b;
-        *writeIterator++ = style.colour.a;
-        *writeIterator++ = left;
-        *writeIterator++ = bottom;
 
+        Ogre::Vector3 coords[4] = {
+            Ogre::Vector3((x1 / m_ScreenWidth) * 2 - 1, -((y1 / m_ScreenHeight) * 2 - 1), m_FinalZ),
+            Ogre::Vector3((x2 / m_ScreenWidth) * 2 - 1, -((y2 / m_ScreenHeight ) * 2 - 1), m_FinalZ),
+            Ogre::Vector3((x3 / m_ScreenWidth) * 2 - 1, -((y3 / m_ScreenHeight ) * 2 - 1), m_FinalZ),
+            Ogre::Vector3((x4 / m_ScreenWidth) * 2 - 1, -((y4 / m_ScreenHeight ) * 2 - 1), m_FinalZ)
+        };
+        auto texture = Ogre::FloatRect(
+            ( float )char_data.x / width, // left
+            ( float )char_data.y / height, // top
+            ( float )( char_data.x + char_data.width ) / width, // right
+            ( float )( char_data.y + char_data.height ) / height // bottom
+        );
+        Ogre::ColourValue colour = style.colour;
+
+        // draw two triangles with a colour and texture.
+        WriteGlyph(writeIterator, coords, colour, texture);
         m_RenderOp.vertexData->vertexCount += 6;
         data.position += 1;
     }