Hello everyone,
I started using sfml a few days ago. I have to use it for a project.
Here is a code that i wrote just to understand its features.
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std ;
const int MAX = 100 ;
const float rectSize = 40 ;
const float nextRect = 40 ;
bool contains(const sf::RectangleShape & rect, const sf::Vector2i& position );
int main()
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(800,600), "BattleShip");
sf::RectangleShape r1[MAX] ;
int nbRect=0 ;
for(int lig = 0 ; lig < 10 ; lig++)
{
for(int col = 0 ; col < 10 ; col++)
{
r1[nbRect].setSize(sf::Vector2f(rectSize,rectSize));
r1[nbRect].setPosition(200+(col*nextRect),100+(lig*nextRect));
r1[nbRect].setFillColor(sf::Color::Cyan);
r1[nbRect].setOutlineThickness(1);
r1[nbRect].setOutlineColor(sf::Color::Black);
window.draw(r1[nbRect]);
nbRect++ ;
}
}
// Update the window
window.display();
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window : exit
if (event.type == sf::Event::Closed)
window.close();
if(event.type == sf::Event::MouseButtonPressed && sf::Event::MouseLeft)
{
int cpt=0 ;
while(!(contains(r1[cpt],sf::Mouse::getPosition(window))))
{
cpt++ ;
}
r1[cpt].setFillColor(sf::Color::Red) ;
window.draw(r1[cpt]);
r1[cpt].setOutlineThickness(1);
r1[cpt].setOutlineColor(sf::Color::Black);
}
}
window.display();
}
return EXIT_SUCCESS;
}
bool contains(const sf::RectangleShape & rect, const sf::Vector2i& position )
{
sf::Vector2f vec ;
vec = rect.getPosition();
return((vec.x <= position.x) && (vec.x + rectSize >= position.x) && (vec.y <= position.y) && (vec.y + rectSize >= position.y)) ;
}
There is no error at compilation but the rectangles created blink. I searched for the problem and what i understood is that it is due to double buffering. I tried to follow some suggestions(using clear() ) but i couldn't solve the problem. I would really appreciate if someone could help me(an example code).
Thanks in advance.