I'm trying to make a program to visualize bubble sort but i have a problem drawing the rectangles. The sorting procedure is working properly, the values in the vector are changing, but when the time comes to draw the triangles nothing changes. I have no idea what to do and i'm new to sfml. Thanks for your help in advance!
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
#include <algorithm>
#include <vector>
#include <stdlib.h>
using namespace std;
void createCanvas(vector<sf::RectangleShape> &rods)
{
srand(time(NULL));
for (unsigned i = 0, xPos = 8; i < 5; ++i, xPos = xPos + 10)
{
sf::RectangleShape rect(sf::Vector2f(5, rand() % 540 + 10));
rect.setPosition(sf::Vector2f(xPos, rect.getPosition().y + (600 - rect.getPosition().y)));
rect.setRotation(180.0f);
rect.setOutlineThickness(2);
rect.setOutlineColor(sf::Color(150, 150, 150, 255));
rods.push_back(rect);
}
}
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");
vector<sf::RectangleShape> rods;
createCanvas(rods);
sf::Event event;
// run the main loop
while (window.isOpen())
{
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
// end the program
window.close();
}
}
//Sort
size_t i, j;
bool swapped;
for (i = 0; i < rods.size() - 1; ++i)
{
swapped = false;
for (j = 0; j < rods.size() - i - 1; ++j)
{
if (rods.at(j).getSize().y > rods.at(j + 1).getSize().y)
{
swap(rods[j], rods[j+1]);
swapped = true;
}
//Draw
window.clear();
for (auto rect : rods)
window.draw(rect);
window.display();
}
// IF no two elements were swapped by inner loop, then break
if (swapped == false)
break;
}
}
return 0;
}