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.cppwindow.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.cppsf::RectangleShape Sortable::shape() {
return sf::RectangleShape(sf::Vector2f(width, height));
}
SortController.cppvoid 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