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

Author Topic: Flickering image using sfml 2.0  (Read 4032 times)

0 Members and 1 Guest are viewing this topic.

dignus93

  • Newbie
  • *
  • Posts: 3
    • View Profile
Flickering image using sfml 2.0
« on: February 12, 2017, 12:53:54 pm »
Hi there,
I'm using CodeBlocks, sfml 2.0, Windows 10.
I'm having some flickering image troubles. If I set the background the image doesn't even appear, if i remove the background the image appears but it flickers.
I show my main and my Graphic class.

  #include <iostream>
    #include "Card.h"
    #include "Special_Card.h"
    #include "Deck.h"
    #include "time.h"
    #include "Graphic.h"
    #include <SFML/Window.hpp>
    #include <SFML/Graphics.hpp>
    #include <string>
    #include <vector>
    #include <memory>
    #include <string>
    #include <sstream>
    using namespace std;

    int main()
    {
        Graphic * graphic = new Graphic(0);
        sf::RenderWindow window;
        Deck deck0;
        deck0.buildDeck();
        graphic->CreateWindow(window, deck0);
    }
 

And this my graphic class definition

    #include "Graphic.h"
    #include "SFML/Graphics.hpp"
    #include <iostream>
    #include <string>
    #include "Card.h"
    #include <string>
    #include <sstream>

    Graphic::Graphic(int offset)
    {
        this->offset = offset;
    }

    int Graphic::CreateWindow(sf::RenderWindow& window, Deck &deck0)
    {
        sf::Vector2i screenDimensions(800, 600);
       
        //Dimension of Window
        window.create(sf::VideoMode(screenDimensions.x, screenDimensions.y), "BlackJack", sf::Style::Titlebar | sf::Style::Close);

        int index = 0;
        window.setKeyRepeatEnabled(false);

        //set the background
        sf::Texture bTexture;
        sf::Sprite bImage;
        if(!bTexture.loadFromFile("Background.png"))
            std::cout<< "Error" <<std::endl;

        bImage.setTexture(bTexture);
        bImage.setScale(1.0, (float)screenDimensions.y / bTexture.getSize().y);

        //MAIN LOOP----------------------
        while(window.isOpen())
        {
            sf::Event Event;
            while(window.pollEvent(Event))
            {
                switch(Event.type)
                {
                //chiude la finestra quando premi il tasto chiudi
                case sf::Event::Closed:
                    window.close();
                    break;

                //fa qualcosa quando premi il tasto h sulla tastiera
                case sf::Event::KeyPressed:
                    if(Event.key.code == sf::Keyboard::H)
                    {

                            Card * y = deck0.dealFirst();
                            drawCard(window,y->graphNumber,y->getSeed(),offset);   //THIS IS THE FUNCTION WHICH DRAW THE IMAGE
                            offset = offset + 100;
                            window.display();

                            while(window.pollEvent(Event))
                            {
                                if( Event.type == sf::Event::Closed)
                                {
                                    window.close();
                                }
                            }
                        }

                    break;
                }
            }
            window.draw(bImage);   //if I comment this the images appears but they flickers
            window.display();
        }
    }
 

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Flickering image using sfml 2.0
« Reply #1 on: February 12, 2017, 01:05:38 pm »
Don't forget to clear the window.

Remember:
window.clear();
window.draw();
window.display();
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

dignus93

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Flickering image using sfml 2.0
« Reply #2 on: February 12, 2017, 06:14:04 pm »
I solved a part of the problem. You have illuminate me with this window.clear() function (it is really important). Now I want to ask: if i set a background and an image on it, i will call window.clear() at the beginning of the main while then I will call the draw function for the background and after the draw function for the image. Finally i will call the window.display() function? Is this the right order?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Flickering image using sfml 2.0
« Reply #3 on: February 12, 2017, 06:41:43 pm »
Yes.

dignus93

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Flickering image using sfml 2.0
« Reply #4 on: February 16, 2017, 04:04:04 pm »
ok I have solved the problem of flickering. Now the problem is that the function don't do what I want.
This "CreateWindow" function has the main loop. In the main loop I want a fixed background and, every time I press the H button, I want to draw a Card on the background. What's the problem here? My function draw the cards but when I press H the previous card is deleted and the next card is drawn.
This is something about the event I think, because every time an event happens (I move the mouse, I press an other key etc) the previous card is deleted...   :-[

Hapax

  • Hero Member
  • *****
  • Posts: 3379
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Flickering image using sfml 2.0
« Reply #5 on: February 16, 2017, 08:08:08 pm »
I suppose it would depend on what your drawCard function does...

Remember that you need to draw everything visible every single frame. If there are multiple cards, you need to draw multiple cards between clear and display.

Also, only clear and display the window once per frame.
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*