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

Author Topic: Screen flickering on other computer  (Read 3771 times)

0 Members and 1 Guest are viewing this topic.

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Screen flickering on other computer
« on: June 17, 2013, 11:04:57 am »
Hi, i am new to SFML/C++ (but have used other programming languages like java etc before) and it seems great so far but i have noticed what seems to be a fairly big problem.

When i run/build my SFML test app it worked great however i then sent it over to a friend to see how it would run for them and they told me there was screen flickering. I then transferred the project to my laptop and exactly the same thing happened on my laptop also.

I don't know if i am building/packaging it wrong or something maybe. My steps in Codeblocks were to set the build target to Release, then hit the Build and Run button. At this point while i test on this computer it seems perfectly fine. So next i go to the bin\Release folder and copy the exe to a new folder and then i also copy all of the .dll files like sfml-system-d-2.dll etc (all 12 dlls) and also a "assets" folder which i had for a font to that folder also. I then double click the exe and it works great however as soon as this folder is transferred to my laptop the app is flickering and does not look correct. :(

Does anyone have any suggestions?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Screen flickering on other computer
« Reply #1 on: June 17, 2013, 11:15:49 am »
Try to activate vertical synchronization.
window.setVerticalSyncEnabled(true);

Does it happen also with SFML samples, or only with your program?

Quote
i also copy all of the .dll files like sfml-system-d-2.dll etc (all 12 dlls)
You don't need the -d files (they are the debug version).
And you only need openal and libsndfile if you're using sfml-audio.
Laurent Gomila - SFML developer

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Screen flickering on other computer
« Reply #2 on: June 17, 2013, 11:27:53 am »
Wow, thanks for the quick replay :D

I will try this option and let you know how things go. To be honest i haven't tried the samples yet but i was planning on doing so soon. Also thanks for the tip about the dlls, i have now reduced the amount to 3.

My projects code was -

#include <SFML/Graphics.hpp>

int main() {

    // Create the SFML window
    sf::RenderWindow window(sf::VideoMode(640, 480), "SFML App");

    // Setup font and text
    sf::Font font;
    font.loadFromFile("assets/arial.ttf");
    sf::Text text("", font);
    text.setCharacterSize(18);

    // Write default text
    window.clear();
    text.setString("Please press a arrow key or click a mouse button");
    window.draw(text);

    while (window.isOpen()) {

        sf::Event event;
        while (window.pollEvent(event)) {

            // Window closed
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            // Window lost focus
            if (event.type == sf::Event::LostFocus) {
                window.clear();
                text.setString("Window lost focus");
                window.draw(text);
            }

            // Window gained focus
            if (event.type == sf::Event::GainedFocus) {
                window.clear();
                text.setString("Window gained focus");
                window.draw(text);
            }

            // Window resized
            if (event.type == sf::Event::Resized) {
                window.clear();
                text.setString("Window resized");
                window.draw(text);
            }

            // Keyboard Keys Pressed
            if (event.type == sf::Event::KeyPressed) {
                if (event.key.code == sf::Keyboard::Up) {
                    window.clear();
                    text.setString("Up key was pressed");
                    window.draw(text);
                } else if (event.key.code == sf::Keyboard::Down) {
                    window.clear();
                    text.setString("Down key was pressed");
                    window.draw(text);
                } else if (event.key.code == sf::Keyboard::Left) {
                    window.clear();
                    text.setString("Left key was pressed");
                    window.draw(text);
                } else if (event.key.code == sf::Keyboard::Right) {
                    window.clear();
                    text.setString("Right key was pressed");
                    window.draw(text);
                }
            }

            // Keyboard Keys Released
            if (event.type == sf::Event::KeyReleased) {
                if (event.key.code == sf::Keyboard::Up) {
                    window.clear();
                    text.setString("Up key was released");
                    window.draw(text);
                } else if (event.key.code == sf::Keyboard::Down) {
                    window.clear();
                    text.setString("Down key was released");
                    window.draw(text);
                } else if (event.key.code == sf::Keyboard::Left) {
                    window.clear();
                    text.setString("Left key was released");
                    window.draw(text);
                } else if (event.key.code == sf::Keyboard::Right) {
                    window.clear();
                    text.setString("Right key was released");
                    window.draw(text);
                }
            }

            // Mouse Buttons Pressed
            if (event.type == sf::Event::MouseButtonPressed) {
                if (event.mouseButton.button == sf::Mouse::Left) {
                    window.clear();
                    text.setString("Left mouse button is pressed");
                    window.draw(text);
                } else if (event.mouseButton.button == sf::Mouse::Middle) {
                    window.clear();
                    text.setString("Middle mouse button is pressed");
                    window.draw(text);
                } else if (event.mouseButton.button == sf::Mouse::Right) {
                    window.clear();
                    text.setString("Right mouse button is pressed");
                    window.draw(text);
                }
            }

            // Mouse Buttons Released
            if (event.type == sf::Event::MouseButtonReleased) {
                if (event.mouseButton.button == sf::Mouse::Left) {
                    window.clear();
                    text.setString("Left mouse button is released");
                    window.draw(text);
                } else if (event.mouseButton.button == sf::Mouse::Middle) {
                    window.clear();
                    text.setString("Middle mouse button is released");
                    window.draw(text);
                } else if (event.mouseButton.button == sf::Mouse::Right) {
                    window.clear();
                    text.setString("Right mouse button is released");
                    window.draw(text);
                }
            }

            // Mouse wheel moved
            if (event.type == sf::Event::MouseWheelMoved) {
                window.clear();
                text.setString("Mouse wheel moved");
                window.draw(text);
            }

            /*
            // Mouse moved
            if (event.type == sf::Event::MouseMoved) {
                window.clear();
                text.setString("Mouse moved");
                window.draw(text);
            }

            // Mouse entered the window
            if (event.type == sf::Event::MouseEntered) {
                window.clear();
                text.setString("Mouse entered the window");
                window.draw(text);
            }

            // Mouse left the window
            if (event.type == sf::Event::MouseLeft) {
                window.clear();
                text.setString("Mouse left the window");
                window.draw(text);
            }
            */


        }
        window.display();
    }
    return 0;
}
 

And i had set graphics, window and system in the linker settings after following the tutorial, i was not sure if this app needed more things set in that panel but it seems ok so i just left it as in the tutorial.

I will try using "window.setVerticalSyncEnabled(true);" and let you know how things go, thanks for the help

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Screen flickering on other computer
« Reply #3 on: June 17, 2013, 11:38:48 am »
Unfortunately using

window.setVerticalSyncEnabled(true);

does not seem to fix the flickering problem :(

I placed the line of code under

sf::RenderWindow window(sf::VideoMode(640, 480), "SFML App");
« Last Edit: June 17, 2013, 11:40:36 am by addel »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Screen flickering on other computer
« Reply #4 on: June 17, 2013, 12:08:16 pm »
What's wrong with your code is that you draw things only once, when an event occurs. This is not how things are supposed to work. You must refresh the whole screen every frame, which means that all your clear/draw calls must be put outside the event loop. So, of course, you'll need to adapt the logic, you'll most likely need to keep track of the current state to know what to draw based on which events occured previously.
Laurent Gomila - SFML developer

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Screen flickering on other computer
« Reply #5 on: June 17, 2013, 12:39:52 pm »
I updated my code and thought maybe to make a update function instead of using the same commands also but i am getting errors as my C++ skills are poor currently. I am planning to learn C++ code by using SFML. :)

Here was my failed function code -

    void update (string status) {
        window.clear();
        text.setString(status);
        window.draw(text);
    }
 

which gives me warnings like "error: a function-definition is not allowed here before '{' token"

Do you have a example of the correct code format if possible?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Screen flickering on other computer
« Reply #6 on: June 17, 2013, 01:21:59 pm »
You should really learn C++ first, and write simpler programs.
Laurent Gomila - SFML developer

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Screen flickering on other computer
« Reply #7 on: June 17, 2013, 01:45:15 pm »
Edit - I have fixed it now :D
« Last Edit: June 17, 2013, 01:53:04 pm by addel »

addel

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: Screen flickering on other computer
« Reply #8 on: June 17, 2013, 01:56:16 pm »
I have fixed it, i forgot that since it's a loop i only need to use this code -

window.clear();
window.draw(text);

once at the end. So i removed those lines from each event and there is no more flickering on my other computer now. Thanks

 

anything