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

Author Topic: problem with drawing multiple objects  (Read 2910 times)

0 Members and 1 Guest are viewing this topic.

iMountDuneed

  • Newbie
  • *
  • Posts: 3
    • View Profile
problem with drawing multiple objects
« 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.

iMountDuneed

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: problem with drawing multiple objects
« Reply #1 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!


G.

  • Hero Member
  • *****
  • Posts: 1590
    • View Profile
Re: problem with drawing multiple objects
« Reply #2 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.

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: problem with drawing multiple objects
« Reply #3 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.

iMountDuneed

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: problem with drawing multiple objects
« Reply #4 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

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: problem with drawing multiple objects
« Reply #5 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)