Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Need help with drawing Text using VertexArray  (Read 3995 times)

0 Members and 1 Guest are viewing this topic.

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
[Solved] Need help with drawing Text using VertexArray
« on: February 11, 2016, 02:51:47 pm »
I'm trying to draw text using the sf::VertexArray because then, my text is drawed in the same place and way as my sprites and its easier for me to manage things like draw order and its overal more consistent.

I have sprites working but with text it looks as if the vertices are in the wrong order (see attachment)

I have simply taken the origin sf::Text source code and modified it to fit my needs (I kept the vertex append order the same).

      // Add a quad for the current character
                        m_vertices.append(Vertex(Vector2f(x + left - italic * top, y + top), m_color, Vector2f(u1, v1)));
                        m_vertices.append(Vertex(Vector2f(x + right - italic * top, y + top), m_color, Vector2f(u2, v1)));
                        m_vertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), m_color, Vector2f(u1, v2)));
                        m_vertices.append(Vertex(Vector2f(x + left - italic * bottom, y + bottom), m_color, Vector2f(u1, v2)));
                        m_vertices.append(Vertex(Vector2f(x + right - italic * top, y + top), m_color, Vector2f(u2, v1)));
                        m_vertices.append(Vertex(Vector2f(x + right - italic * bottom, y + bottom), m_color, Vector2f(u2, v2)));

for (size_t i = 0; i < m_vertices.getVertexCount(); i++)
                        m_batch->addVertex(m_vertices[i]);

Can anyone help me figure out the correct order to add the vertices?

(the text should spell "Hello!")
« Last Edit: February 12, 2016, 11:56:23 am by Rob92 »

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Need help with drawing Text using VertexArray
« Reply #1 on: February 11, 2016, 07:08:31 pm »
Are you trying to draw text with a vertex array so that you can re-arrange a vector of vertex arrays or something?

its easier for me to manage things like draw order
more consistent
You could order a group of pointers to drawable objects and sort those into the order you want and then draw from them.
sf::RectangleShape rectangle;
sf::Sprite sprite;
sf::Text text;
std::vector<sf::Drawable*> pDrawableOrder;
pDrawableOrder.push_back(&rectangle);
pDrawableOrder.push_back(&sprite);
pDrawableOrder.push_back(&text);
{
    // re-arrange the order of the pointers as you see fit whenever you like (before each drawing)
    for (auto& pDrawable : pDrawableOrder)
        window.draw(*pDrawable);
}

That aside, which primitive type are you using to draw the text?
« Last Edit: February 11, 2016, 07:18:25 pm by Hapax »
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: Need help with drawing Text using VertexArray
« Reply #2 on: February 11, 2016, 09:22:03 pm »
Hi, thanks for your reply, im attempting to draw the text in the same way as the sprites. I created a spritebatch to reduce draw calls.

I use quads as primitive type

if(m_vertexCount > 0)
                        m_target->draw(&m_vertices[0], m_vertices.getVertexCount(), sf::PrimitiveType::Quads, m_states);

edit: I changed it to triangles and now it works perfectly! thanks I forgot about the primitive type being different for text.

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Need help with drawing Text using VertexArray
« Reply #3 on: February 11, 2016, 10:47:36 pm »
I use quads as primitive type
edit: I changed it to triangles and now it works perfectly! thanks
You're welcome.
I've seen this effect before by using the wrong primitive type.  :)

im attempting to draw the text in the same way as the sprites. I created a spritebatch to reduce draw calls.
So, you're batching texts? Or trying to batch text and sprites together?
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: Need help with drawing Text using VertexArray
« Reply #4 on: February 12, 2016, 11:00:24 am »
I'm not batching sprite and text together but I use the same batch class for sprites and text. so I make multiple batches, I change draw order by sorting the all the batches.

hobby

  • Newbie
  • *
  • Posts: 43
    • View Profile
Re: [Solved] Need help with drawing Text using VertexArray
« Reply #5 on: February 13, 2016, 09:54:21 am »
I have simply taken the origin sf::Text source code and modified it to fit my needs
If all you need is access to the vertices of sf::Text, you can manage without duplicating its code by doing something along the lines of:
#define getString getString() const; \
    friend class YourClass; \
    void doNothingJustAHack

#include <SFML/Graphics/Text.hpp>
#undef getString
 

Then YourClass will have access to the private members of sf::Text. It's really fun, and you may be nominated to the Nobel prize in software engineering ;)

Rob92

  • Newbie
  • *
  • Posts: 35
    • View Profile
    • Email
Re: [Solved] Need help with drawing Text using VertexArray
« Reply #6 on: February 13, 2016, 02:32:04 pm »
I dont really understand how that works but I have changed some other stuff in there as well