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

Author Topic: How to draw grid using VertexArray?  (Read 1095 times)

0 Members and 1 Guest are viewing this topic.

wixy0

  • Newbie
  • *
  • Posts: 4
    • View Profile
How to draw grid using VertexArray?
« on: March 12, 2021, 12:39:36 am »
 I realized that I must use VertexArray to draw grid overlay in my level editor but I can't get it right. Can I do it by using one VertexArray (if yes how?) or do I need to use more VertexArrays (perhaps std::vector)?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: How to draw grid using VertexArray?
« Reply #1 on: March 12, 2021, 02:13:24 am »
a VertexArray is a Vector of sf::Vertext.
if you want just the grid, you could use lines instead (of course VertexArray will also work)
draw a line from (0, 0) to (0, 100)
another from (10, 0) to (10, 100)
another from (20, 0) to (20, 100)
[repeat]

then you do horizontal lines (its the same logic):
draw a line from (0, 0) to (100, 0)
another from (0, 10) to (100, 10)
another from (0, 20) to (100, 20)

(use 'for' loops to avoid repetition of code)
Visit my game site (and hopefully help funding it? )
Website | IndieDB

wixy0

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: How to draw grid using VertexArray?
« Reply #2 on: March 12, 2021, 02:27:59 am »
Yes, I know. I actually did it like this:
sf::Vertex line[2];
                for (int i = m_ViewDefaultCenter.y - m_MapHeight * m_TileSize; i <= m_ViewDefaultCenter.y + m_MapHeight * m_TileSize; i += m_TileSize)
                {
                        line[0] = sf::Vector2f(m_ViewDefaultCenter.x - m_MapWidth * m_TileSize, i);
                        line[1] = sf::Vector2f(m_ViewDefaultCenter.x + m_MapWidth * m_TileSize, i);
                        m_Window.draw(line, 2, sf::Lines);
                }

                for (int i = m_ViewDefaultCenter.x - m_MapWidth * m_TileSize; i <= m_ViewDefaultCenter.x + m_MapWidth * m_TileSize; i += m_TileSize)
                {
                        line[0] = sf::Vector2f(i, m_ViewDefaultCenter.y - m_MapHeight * m_TileSize);
                        line[1] = sf::Vector2f(i, m_ViewDefaultCenter.y + m_MapHeight * m_TileSize);
                        m_Window.draw(line, 2, sf::Lines);
                }
I thought there is a better, more optimized way because of this post: https://en.sfml-dev.org/forums/index.php?topic=22021.0

 

anything