I just get started with sfml and the graphic library and tried a little program which sorts number and displays it.
the problem is that the std::vector is sorted, but the RenderWindow doesn't show it correctly.
here is a minmal program:
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>
#include <algorithm>
sf::RenderWindow App(sf::VideoMode(800, 600), "J4F");
typedef std::vector<int> fieldType;
class Field
{
std::vector<sf::Shape> m_points;
public:
// fill 'm_points'
void fill(const fieldType& field)
{
std::size_t y = 0;
for(std::size_t i = 0; i < field.size(); i++)
{
sf::Shape point(sf::Shape::Circle(0.0, 0.0, 2, sf::Color::Red));
point.SetPosition(field[i], y++);
std::cout << "x: " << point.GetPosition().x << " y: " << point.GetPosition().y << std::endl;
App.Draw(point);
App.Display();
this->m_points.push_back(point);
}
}
void sort()//bubble
{
for(std::size_t o = this->m_points.size()-1;o > 0; --o)
{
for(std::size_t pos = 0; pos < o; ++pos)
{
if(this->m_points[pos].GetPosition().x > this->m_points[pos+1].GetPosition().x)
{
sf::Vector2f tmp_pos = this->m_points[pos].GetPosition();
this->m_points[pos].SetPosition(this->m_points[pos+1].GetPosition());
this->m_points[pos+1].SetPosition(tmp_pos);
App.Display();
}
}
}
// sort&debug
for(std::size_t i = 0; i < this->m_points.size(); i++)
{
this->m_points[i].SetPosition(this->m_points[i].GetPosition().x, i);
std::cout << "x: " << this->m_points[i].GetPosition().x << " y: " << this->m_points[i].GetPosition().y << std::endl;
App.Display();
}
}
};
int main()
{
Field fieldContext;
std::vector<int> field;
for(int i = 0; i < 20; i++)
field.push_back(i);
std::random_shuffle(field.begin(), field.end());
std::cout << "Filling..." << std::endl;
fieldContext.fill(field);
std::cin.get();
std::cout << "Sorting..." << std::endl;
fieldContext.sort();
App.Display();
std::cin.get();
return 0;
}
just copy + paste and compile it. :idea:
i hope you can help me!