SFML community forums

Help => Graphics => Topic started by: dkno466 on August 05, 2015, 03:46:17 am

Title: Drawing list members
Post by: dkno466 on August 05, 2015, 03:46:17 am
Hi.

My problem is:

I want that when I press the space bar a RectangleShape is created and added to a list and I want to draw all the list's members.
Of course, my list is a sf::RectangleShape's list.
The list has a 10 maximum size.

Example:

I press the space bar:
list[0] = sf::RectangleShape(sf::Vector2f(100, 100));

Draw the list's members:
for (int i = 0; i < *list's size*; i++) {
    window.draw(list);
}


Of course, my list is a sf::RectangleShape's list.
The list has a 10 maximum size.

The problem is in the "window.draw(list);" line.
I don't know what did I make wrong.

Please help me!
Thank You!
Title: Re: Drawing list members
Post by: Verra on August 05, 2015, 05:10:59 am
Do you mean std::list or an array, because your syntax list[0] = sf::RectangleShape means array.
Anyways to draw a bunch of objects contained in an array/vector/list is pretty trivial.

for a vector/array:
std::vector<sf::RectangleShape> rects;
sf::RectangleShape box(sf::Vector2f(32, 32));
box.setFillColor(sf::Color::Red);
rects.push_back(box);

window.clear(sf::Color::White);

for(unsigned int i = 0; i < rect.size(); i++)
{
  window.draw(rects[i]);

}

window.display();
 


for a list:
std::list<sf::RectangleShape> rects;
sf::RectangleShape box(sf::Vector2f(32, 32));
box.setFillColor(sf::Color::Red);
list.push_back(box);

window.clear(sf::Color::White);

std::list<sf::RectangleShape>::iterator it;
for(it = rects.begin(); it != rects.end(); it++)
{
   sf::RectangleShape& rect = (*it);
   window.draw(rect);

}

window.display();
 

Hope that helps some.
Title: Re: Drawing list members
Post by: dkno466 on August 06, 2015, 12:59:43 am
Thank you so much for your help! My problem is resolved! Thank you!
Title: Re: Drawing list members
Post by: Nexus on August 06, 2015, 08:38:49 am
Verra, there's no reason to use indices for vectors/deques if you can as well use iterators, especially since they're potentially slower (http://en.sfml-dev.org/forums/index.php?topic=10084).

By the way, you can use the range-based for loop instead of iterators.
for (sf::RectangleShape& shape : rects)
{
    window.draw(shape);
}