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

Author Topic: Drawing list members  (Read 2482 times)

0 Members and 1 Guest are viewing this topic.

dkno466

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Drawing list members
« 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!

Verra

  • Newbie
  • *
  • Posts: 23
    • View Profile
Re: Drawing list members
« Reply #1 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.
« Last Edit: August 05, 2015, 05:12:35 am by Verra »

dkno466

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Drawing list members
« Reply #2 on: August 06, 2015, 12:59:43 am »
Thank you so much for your help! My problem is resolved! Thank you!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: Drawing list members
« Reply #3 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.

By the way, you can use the range-based for loop instead of iterators.
for (sf::RectangleShape& shape : rects)
{
    window.draw(shape);
}
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: