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

Author Topic: Why can't I transform from a function?  (Read 2116 times)

0 Members and 1 Guest are viewing this topic.

Ignatius_Z

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Why can't I transform from a function?
« on: February 09, 2021, 01:33:34 pm »
Pretty simple question. For example:

float deltaTime;

void rotateShape(RectangleShape currentShape);

int main()
{
        Clock theClock;
       
        RectangleShape myShape(Vector2f(100.f, 100.f));
       
        while (window.isOpen())
        {
                deltaTime = theClock.restart().asSeconds() * 60;
               
                rotateShape(myShape);
               
                myShape.rotate(5.f * deltaTime); // < this works
               
                myShape.move(10.f * deltaTime, 0.f); // < this works
               
                myShape.scale(1.01f, 1.01f); // < this works
;              
                Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
               
                window.clear(Color(0,0,0));
        window.draw(myShape);
        window.display();
        }
}

void rotateShape(RectangleShape currentShape)
{
        cout << currentShape.getRotation() << endl; // < this works
       
        currentShape.rotate(5.f * deltaTime); // < this doesn't work
       
        currentShape.move(10.f * deltaTime, 0.f); // < this doesn't work
       
        currentShape.scale(1.01f, 1.01f); // < this doesn't work
       
        // etc.
}

Why don't the transform functions work when called from my own function?

As usual apologies for the probably noob question and thanks in advance.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why can't I transform from a function?
« Reply #1 on: February 09, 2021, 02:01:35 pm »
You pass the shape to the function by value, ie. inside the function you're working on a copy of it. To work on the original instance, pass it by reference.
Laurent Gomila - SFML developer

Ignatius_Z

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Why can't I transform from a function?
« Reply #2 on: February 09, 2021, 02:11:45 pm »
You pass the shape to the function by value, ie. inside the function you're working on a copy of it. To work on the original instance, pass it by reference.

so I just put a "&" after "RectangleShape" ?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Why can't I transform from a function?
« Reply #3 on: February 09, 2021, 04:02:18 pm »
Yes.
Laurent Gomila - SFML developer

Ignatius_Z

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Why can't I transform from a function?
« Reply #4 on: February 10, 2021, 02:23:50 am »