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

Author Topic: [SOLVED] Can't update shapes on window  (Read 2450 times)

0 Members and 1 Guest are viewing this topic.

sfmlearner

  • Newbie
  • *
  • Posts: 9
    • View Profile
[SOLVED] Can't update shapes on window
« on: April 13, 2022, 03:17:11 pm »
Hello,

Im trying to create a sorting visualizer. I have a vector of objects, and each object represents an element in the array to be sorted, im displaying the objects iterating the vector and calling the method shape() of the object that I create that returns a sf::RectangleShape, the result is the one that I was expecting:



Here is the part when I render the shapes:

main.cpp
window.clear(sf::Color::Black);
std::cout << "Window cleared!" << std::endl;

// Draw sortables
for (Sortable sortable : sortController.sortElements) {
    sf::RectangleShape shape = sortable.shape();

    shape.setPosition(sf::Vector2f(sortable.width * sortable.value, sortController.winHeight - sortable.height));
    window.draw(shape);
    std::cout << "Shape n" << sortable.value << " drawed with height " << shape.getSize().y << std::endl;
}

    std::cout << "ended for and displaying" << std::endl;

    window.display();

As you can see, Im storing the objects in sortElements, a vector inside the sortController object.

The problem is that i have a method in sortController that randomizes the array when I press space, I made the std::cout just after draw to see if the shapes height and values are being randomized, and yes, the output changes, here is an example:

Window cleared!
Shape n0 drawed with height 40
Shape n1 drawed with height 80
Shape n2 drawed with height 120
Shape n3 drawed with height 160
Shape n4 drawed with height 200
Shape n5 drawed with height 240
Shape n6 drawed with height 280
Shape n7 drawed with height 320
Shape n8 drawed with height 360
Shape n9 drawed with height 400
ended for and displaying
Random method called!
Window cleared!
Shape n3 drawed with height 160
Shape n1 drawed with height 80
Shape n6 drawed with height 280
Shape n2 drawed with height 120
Shape n0 drawed with height 40
Shape n7 drawed with height 320
Shape n5 drawed with height 240
Shape n8 drawed with height 360
Shape n4 drawed with height 200
Shape n9 drawed with height 400
ended for and displaying

So the thing is that the window is not changing at all. I also have this event to change the number of elements to sort and updates perfectly:

// Change number of sortables (increase)
case sf::Keyboard::Right:
    numOfElements++;
    sortController.clear(); // empty array
    sortController.populate(numOfElements); // fill array with numOfElements shapes
    std::cout << "Num of elements changed to: " << numOfElements << std::endl;
    break;

If you need it, here is the shape method and the method used to randomize the array of objects:
Sortable.cpp
sf::RectangleShape Sortable::shape() {
        return sf::RectangleShape(sf::Vector2f(width, height));
}

SortController.cpp
void SortController::randomize() {
        auto rd = std::random_device{};
        auto rng = std::default_random_engine{ rd() };
        std::shuffle(std::begin(sortElements), std::end(sortElements), rng);
};

I can provide more code if needed, but I wanted to do it more readable, ty
« Last Edit: April 13, 2022, 03:39:43 pm by sfmlearner »

kojack

  • Sr. Member
  • ****
  • Posts: 343
  • C++/C# game dev teacher.
    • View Profile
Re: Can't update shapes on window
« Reply #1 on: April 13, 2022, 03:34:08 pm »
When you render each object, you are using value as the x position, instead of the order in the vector. So when the objects are shuffled, they still have the same value, so they will always be in the same spot.

Try this:
int index = 0;
for (Sortable sortable : sortController.sortElements) {
    sf::RectangleShape shape = sortable.shape();

    shape.setPosition(sf::Vector2f(sortable.width * index++, sortController.winHeight - sortable.height));
    window.draw(shape);
    std::cout << "Shape n" << sortable.value << " drawed with height " << shape.getSize().y << std::endl;
}

sfmlearner

  • Newbie
  • *
  • Posts: 9
    • View Profile
Re: Can't update shapes on window
« Reply #2 on: April 13, 2022, 03:39:25 pm »
When you render each object, you are using value as the x position, instead of the order in the vector. So when the objects are shuffled, they still have the same value, so they will always be in the same spot.

Try this:
int index = 0;
for (Sortable sortable : sortController.sortElements) {
    sf::RectangleShape shape = sortable.shape();

    shape.setPosition(sf::Vector2f(sortable.width * index++, sortController.winHeight - sortable.height));
    window.draw(shape);
    std::cout << "Shape n" << sortable.value << " drawed with height " << shape.getSize().y << std::endl;
}

Works perfectly now, thank you so much!  ;D

 

anything