SFML community forums

Help => Graphics => Topic started by: Ignatius_Z on February 09, 2021, 01:33:34 pm

Title: Why can't I transform from a function?
Post by: Ignatius_Z 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.
Title: Re: Why can't I transform from a function?
Post by: Laurent 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.
Title: Re: Why can't I transform from a function?
Post by: Ignatius_Z 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" ?
Title: Re: Why can't I transform from a function?
Post by: Laurent on February 09, 2021, 04:02:18 pm
Yes.
Title: Re: Why can't I transform from a function?
Post by: Ignatius_Z on February 10, 2021, 02:23:50 am
Yes.

Thank you! :)