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

Author Topic: Drawing vertexarrays always start from 0,0  (Read 2112 times)

0 Members and 1 Guest are viewing this topic.

uGhster

  • Newbie
  • *
  • Posts: 23
    • View Profile
Drawing vertexarrays always start from 0,0
« on: January 05, 2013, 02:49:35 am »
Hi,
I don't know if I am missing something but when I try to draw lines using a sf::VertexArray it always starts drawing from (0,0).
So let's say I choose to draw a box with theese coords:

(10,10)
(110,10)
(110,110)
(10,110)

I have attached an image of the result.
Running SFML2.0 on a LinuxBox.

Is this the desired result or a bug? And is there a workaround?



[attachment deleted by admin]

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing vertexarrays always start from 0,0
« Reply #1 on: January 05, 2013, 02:50:31 am »
Code.
Back to C++ gamedev with SFML in May 2023

uGhster

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Drawing vertexarrays always start from 0,0
« Reply #2 on: January 05, 2013, 02:57:09 am »

   sf::VertexArray lines(sf::LinesStrip, m_Verts.size()+1);
   std::vector<sf::Vector2<float> >::const_iterator it = m_Verts.begin();
   for(;it!=m_Verts.end();++it) {
      lines.append(sf::Vertex(*it, sf::Color(255,255,255,255)));
   }
   lines.append(sf::Vertex(m_Verts[0], sf::Color(255,255,255,255)));
   target.draw(lines);

 

and m_Verts is fed like this


   m_Verts.push_back(sf::Vector2f(10,10));
   m_Verts.push_back(sf::Vector2f(110,10));
   m_Verts.push_back(sf::Vector2f(110,110));
   m_Verts.push_back(sf::Vector2f(10,110));

 

This example out of context may seem like a detour though ;)

uGhster

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Drawing vertexarrays always start from 0,0
« Reply #3 on: January 05, 2013, 03:02:26 am »
The box is just an simplified example, any arbitrary shape drawn this way will start off from (0,0).

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing vertexarrays always start from 0,0
« Reply #4 on: January 05, 2013, 03:08:06 am »
Read this:
http://en.sfml-dev.org/forums/index.php?topic=5559.0
sf::VertexArray lines(sf::LinesStrip, m_Verts.size()+1);
This assings m_Verts.size()+1 default constructed vertices(white, located at 0.0) to this vertexarray. Remove second parameter and it'll be fine.
Back to C++ gamedev with SFML in May 2023

uGhster

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Drawing vertexarrays always start from 0,0
« Reply #5 on: January 05, 2013, 03:11:55 am »
That did the trick, Thanks!  :D

uGhster

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Drawing vertexarrays always start from 0,0
« Reply #6 on: January 05, 2013, 03:23:20 am »
I guess the size is for initial allocation of the container. Why put an initial value into it? Will appends upon appends not have to reallocate at some point otherwise?
Still, the documenation does not mention the initial (0,0) vertex there.



 

FRex

  • Hero Member
  • *****
  • Posts: 1845
  • Back to C++ gamedev with SFML in May 2023
    • View Profile
    • Email
Re: Drawing vertexarrays always start from 0,0
« Reply #7 on: January 05, 2013, 03:26:59 am »
Yes, it will. If you worry about it, use vector.
There are five too many vertices there, and they are initialized by their default c-tor so they have WHITE as color, and 0,0 as position and texcoords.
Back to C++ gamedev with SFML in May 2023

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Drawing vertexarrays always start from 0,0
« Reply #8 on: January 05, 2013, 08:25:37 am »
You either set an initial size and access vertices directly (with operator []), or you start with an empty VertexArray and use append. Like in std::vector.
Laurent Gomila - SFML developer

uGhster

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Drawing vertexarrays always start from 0,0
« Reply #9 on: January 05, 2013, 10:21:59 am »
Obviously! what was I thinking?  :o