SFML community forums

Help => Graphics => Topic started by: iMountDuneed on April 12, 2021, 01:12:36 am

Title: problem with drawing multiple objects
Post by: iMountDuneed on April 12, 2021, 01:12:36 am
Hi,
I need your help with this below issue:

If I only have 1 circle shape and I move it, the render updates it correctly.
mCircle.move(movement);

However, if I put multiple circle shapes in the vector std::<vector> myCircles; and use for loop to move each object
for (auto c: myCircles)
   c.move(movement);

the render doesn't update it correctly. And it looks like the position of each circle remains the same.

Do you have any idea please?

Cheers
Duneed.
Title: Re: problem with drawing multiple objects
Post by: iMountDuneed on April 12, 2021, 01:28:55 am
Hello everyone,
I've found the problem myself. Somehow, SFML 2.5 doesn't work with the
for (auto ... ) syntax,
If I use for (int i = 0; i < ..... ) then it works

Weird!

Title: Re: problem with drawing multiple objects
Post by: G. on April 12, 2021, 03:09:31 am
It works...
But you have to use a reference if you don't want to modify a copy.
Title: Re: problem with drawing multiple objects
Post by: kojack on April 13, 2021, 04:25:38 pm
Yep, as G. says, you need references.

for (auto c: myCircles)
   c.move(movement);
In this case, c is a circle. But it's not actually a circle inside of myCircles, it's a temporary copy of one. You move the temporary, then throw it away. Nothing in myCircles actually changes.
It's a bit like doing this:
int a=5;
int b = a;
b=6;
Changing b to 6 doesn't change a to 6 as well, because b is just a copy.

Instead, you can modify the contents of myCircle like this:
for (auto &c: myCircles)
   c.move(movement);
c is now a reference to a circle inside of myCircles instead of a temporary duplicate, and anything you do to it will be affecting the original.
Title: Re: problem with drawing multiple objects
Post by: iMountDuneed on April 22, 2021, 12:29:39 am
oh it was my bad - sorry I was a C++ beginner  - thank you very much for your help
Title: Re: problem with drawing multiple objects
Post by: kojack on April 22, 2021, 01:21:41 am
References and pointers are one of the harder parts of common C++ to get used to, especially for people who have used languages like C# that hide them. (All classes are references in C# already)