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 - tayyab91

Pages: [1]
1
Window / Re: Window blinking
« on: April 06, 2012, 09:44:49 pm »
Thanks guys. It seems to be working

2
Window / Re: Window blinking
« on: April 05, 2012, 11:51:40 pm »
And i have another question. Won't it be a little bit heavy to draw everything, everytime in the loop?

3
Window / Re: Window blinking
« on: April 05, 2012, 11:19:40 pm »
I tried to do that but the program stops working after execution.

4
Window / Window blinking
« on: April 05, 2012, 08:35:35 pm »
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.

Pages: [1]
anything