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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - board_sfml

Pages: [1]
1
Graphics / sort program
« on: October 06, 2010, 10:00:02 pm »
Thanks Mindiell!

it does what I want!  :shock:

I think I understand the "techniquè" of SFML.
I should better read the tutorials on the website and then start with graphic-programming, to avoid such a disaster like this one :oops:

thanks again dude :wink:

2
Graphics / sort program
« on: October 04, 2010, 07:03:47 pm »
Quote from: "mooglwy"
Yep this might help, but i still can't run this code and i dont't know if it's normal.


Maybe you forgot to link the 3 libraries? (system,graphics,window).
Paste more infos!

If I copy my code and compile it, it works.

@Mindiell:

Thank you  :D
I finally got it.
But it's not the result that I want.

Compile it and you will see that the points are going to be sorted.
But it's also flickering. I want it like this:



the current code is here:
Code: [Select]


#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cstdlib>

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;
this->m_points.push_back(point);
App.Draw(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);
}
}
}
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(); // bad?
sf::Sleep(0.01); // not really that what I wanted.
App.Draw(this->m_points[i]);
}
}

};


int main()
{

Field fieldContext;

std::vector<int> field;

for(int i = 0; i < 1000; i++)
field.push_back(i);

std::random_shuffle(field.begin(), field.end());

std::cout << "Filling..." << std::endl;

fieldContext.fill(field);

App.Display();

std::cin.get();

std::cout << "Sorting..." << std::endl;

fieldContext.sort();

App.Display();

std::cin.get();

return 0;
}


thanks again dude!  :wink:

3
Graphics / sort program
« on: October 04, 2010, 10:13:26 am »
hey mooglwy

it also doesn't work with the eventloop.
Is it a logic problem with the sorting algorithm or did I forget something?


thank you for helping me!  :oops:

4
Graphics / sort program
« on: October 03, 2010, 11:57:21 pm »
Look at this: sorting algorithm graphic

I want to do this too, just with little points.

First the points look like this:

Code: [Select]

.
   .
.
       .
   .
         .
.


then if it's sorted, it should like this:
Code: [Select]

.
 .
  .
   .
    .
     .
      .

Do you understand?

5
Graphics / sort program
« on: October 03, 2010, 07:03:37 pm »
no one an idea what it could be?  :(

6
Graphics / sort program
« on: October 03, 2010, 12:01:30 pm »
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:


Code: [Select]
#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!

Pages: [1]
anything