SFML community forums

Help => Graphics => Topic started by: uGhster on January 05, 2013, 02:49:35 am

Title: Drawing vertexarrays always start from 0,0
Post by: uGhster 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]
Title: Re: Drawing vertexarrays always start from 0,0
Post by: FRex on January 05, 2013, 02:50:31 am
Code.
Title: Re: Drawing vertexarrays always start from 0,0
Post by: uGhster 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 ;)
Title: Re: Drawing vertexarrays always start from 0,0
Post by: uGhster 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).
Title: Re: Drawing vertexarrays always start from 0,0
Post by: FRex 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.
Title: Re: Drawing vertexarrays always start from 0,0
Post by: uGhster on January 05, 2013, 03:11:55 am
That did the trick, Thanks!  :D
Title: Re: Drawing vertexarrays always start from 0,0
Post by: uGhster 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.



 
Title: Re: Drawing vertexarrays always start from 0,0
Post by: FRex 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.
Title: Re: Drawing vertexarrays always start from 0,0
Post by: Laurent 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.
Title: Re: Drawing vertexarrays always start from 0,0
Post by: uGhster on January 05, 2013, 10:21:59 am
Obviously! what was I thinking?  :o