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

Author Topic: Problem with drawing from a vector.  (Read 1039 times)

0 Members and 1 Guest are viewing this topic.

ggiap

  • Newbie
  • *
  • Posts: 2
    • View Profile
Problem with drawing from a vector.
« on: January 29, 2019, 11:21:18 am »
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! :D

#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;
}

Mortal

  • Sr. Member
  • ****
  • Posts: 284
    • View Profile
Re: Problem with drawing from a vector.
« Reply #1 on: January 29, 2019, 02:18:04 pm »
sfml does't copy the transform if you want sorting them, so you have to swap the positions too like so:
if (rods.at(j).getSize().y > rods.at(j + 1).getSize().y)
{
        swap(rods[j], rods[j + 1]);
        auto p1 = rods[j].getPosition();
        auto p2 = rods[j + 1].getPosition();
        rods[j].setPosition(p2);
        rods[j + 1].setPosition(p1);
        swapped = true;
}

ggiap

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Problem with drawing from a vector.
« Reply #2 on: January 29, 2019, 03:29:04 pm »
Thank you very much!!! Problem solved!   ;D

 

anything