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

Author Topic: Window blinking  (Read 2881 times)

0 Members and 1 Guest are viewing this topic.

tayyab91

  • Newbie
  • *
  • Posts: 4
    • View Profile
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.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Window blinking
« Reply #1 on: April 05, 2012, 10:15:31 pm »
What you draw is not persistent, you must draw everything everytime you refresh the window (ie. every iteration of your main loop).
Laurent Gomila - SFML developer

tayyab91

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Window blinking
« Reply #2 on: April 05, 2012, 11:19:40 pm »
I tried to do that but the program stops working after execution.

tayyab91

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Window blinking
« Reply #3 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?

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Window blinking
« Reply #4 on: April 06, 2012, 12:27:18 am »
On every loop iteration do:
1) Clear the window ... window.clear()
2) Draw all your graphics
3) window.display ()


Won't it be a little bit heavy to draw everything, everytime in the loop?

No. This is how the video cards works currently.
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

tayyab91

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Window blinking
« Reply #5 on: April 06, 2012, 09:44:49 pm »
Thanks guys. It seems to be working