SFML community forums

Help => Graphics => Topic started by: chris67 on January 06, 2010, 08:50:57 pm

Title: Huge problem with SFML
Post by: chris67 on January 06, 2010, 08:50:57 pm
This is a total game breaker for me unless I am missing something huge here.

The problem is when I add GetEvent(Event) in my code it slows or even STOPS my program when I move the mouse around.

I display the fps and even added a counter to increment each time the program loops and they both stop dead when I move the mouse.

I have been puzzling over this for quite a while and I cannot find anything on the forum about it.

Here is my code, someone else please run it and see if you get the same results:

Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{
    sf::Font MyFont;
    sf::String fps;
    sf::String counter;
    char buffer[200];
    int num = 0;

    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

    if (!MyFont.LoadFromFile("font/small/XPAIDERP.TTF", 8))
        return EXIT_FAILURE;

    fps.SetFont(MyFont);
    fps.SetSize(8);
    fps.SetPosition(100.f, 100.f);

    counter.SetFont(MyFont);
    counter.SetSize(8);
    counter.SetPosition(100.f, 200.f);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        App.Clear();

        sprintf( buffer, "FPS = %f", 100.f / App.GetFrameTime() );
        fps.SetText( buffer );
        App.Draw( fps );

        sprintf( buffer, "COUNTER = %d", num );
        counter.SetText( buffer );
        App.Draw( counter );

        App.Display();

        num++;
    }

    return EXIT_SUCCESS;
}


I move the mouse around and both the counter and the fps freeze. This can only mean the program itself is freezing. If I comment out while (App.GetEvent(Event)) then everything goes smoothly and I can move the mouse around with no freezing.

System:

WindowsXP service pack 3
DirectX 9.0c
AMD athlon 2.21 GHZ
2 GB RAM

Using Code::blocks IDE with MingW
Title: Huge problem with SFML
Post by: Imbue on January 06, 2010, 09:22:56 pm
Your program ran fine for me (after commenting out the font stuff).
Title: Huge problem with SFML
Post by: Laurent on January 06, 2010, 09:23:22 pm
Maybe you receive a huge amount of unexpected events. Can you add some logs in your event loop to know what is received exactly?
Title: Huge problem with SFML
Post by: chris67 on January 07, 2010, 12:56:31 am
Quote from: "Laurent"
Maybe you receive a huge amount of unexpected events. Can you add some logs in your event loop to know what is received exactly?


Ok here is what I did. I added a counter to the event loop to increment each time there is an event. Then I displayed the counter on screen to see how many times the event loop was running.

The result was that the event looped maybe 3 times when I moved the mouse cursor a small distance. However this was still freezing both the fps and the main program loop counter. So for some reason App.GetEvent(Event) is running hella slow on my system.

Here is the new code:

Code: [Select]

#include <SFML/Graphics.hpp>

int main()
{
    sf::Font MyFont;
    sf::String fps;
    sf::String counter;
    sf::String EventCount;
    char buffer[200];
    int num = 0;
    int evnum = 0;

    // Create the main window
    sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Window");

    if (!MyFont.LoadFromFile("font/small/XPAIDERP.TTF", 8))
        return EXIT_FAILURE;

    fps.SetFont(MyFont);
    fps.SetSize(8);
    fps.SetPosition(100.f, 100.f);

    counter.SetFont(MyFont);
    counter.SetSize(8);
    counter.SetPosition(100.f, 200.f);

    EventCount.SetFont(MyFont);
    EventCount.SetSize(8);
    EventCount.SetPosition(100.f, 300.f);

    while (App.IsOpened())
    {
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            if( Event.Type == sf::Event::Closed )
                App.Close();

            evnum++;
        }

        App.Clear();

        sprintf( buffer, "FPS = %f", 100.f / App.GetFrameTime() );
        fps.SetText( buffer );
        App.Draw( fps );

        sprintf( buffer, "COUNTER = %d", num );
        counter.SetText( buffer );
        App.Draw( counter );

        sprintf( buffer, "EVENTS = %d", evnum );
        EventCount.SetText( buffer );
        App.Draw( EventCount );

        App.Display();

        num++;
    }

    return EXIT_SUCCESS;
}


I am using sfml version 1.5
Title: Huge problem with SFML
Post by: Laurent on January 07, 2010, 09:03:01 am
Did you try the samples provided in the SFML SDK?
Title: Huge problem with SFML
Post by: chris67 on January 07, 2010, 12:22:32 pm
Quote from: "Laurent"
Did you try the samples provided in the SFML SDK?


Yep, just ran pong and same problem. When I move the mouse the ball freezes.

Same thing with opengl, the rotating cube freezes.
Title: Huge problem with SFML
Post by: Laurent on January 07, 2010, 12:59:45 pm
What happens when you stop moving the mouse? Does the program run smoothly again, or does it stay frozen?
Title: Huge problem with SFML
Post by: chris67 on January 07, 2010, 04:09:12 pm
Quote from: "Laurent"
What happens when you stop moving the mouse? Does the program run smoothly again, or does it stay frozen?


It starts running smoothly again.

The mouse movement makes the most visible effect, but it also gets choppy when I hold down a key on the keyboard.
Title: Huge problem with SFML
Post by: Laurent on January 07, 2010, 04:58:07 pm
How many events do you have when you press a single key?
Title: Huge problem with SFML
Post by: chris67 on January 07, 2010, 06:06:02 pm
Quote from: "Laurent"
How many events do you have when you press a single key?


I go through the event loop twice. Looks like one for the press and one for the release.
Title: Huge problem with SFML
Post by: Laurent on January 07, 2010, 07:02:45 pm
Yes, it looks ok :?
Title: Huge problem with SFML
Post by: phear- on January 08, 2010, 04:18:33 am
use a log file or something other than sf::strings to see if it has something to do with that.
Title: Huge problem with SFML
Post by: chris67 on January 08, 2010, 05:08:21 am
Quote from: "phear-"
use a log file or something other than sf::strings to see if it has something to do with that.


It has nothing to do with sf::strings.

If I comment out the event loop everything runs smoothly.
Title: Huge problem with SFML
Post by: Laurent on January 08, 2010, 07:45:32 am
Have you tried switching between static and dynamic libraries? Debug and release?
Title: Huge problem with SFML
Post by: chris67 on January 08, 2010, 07:04:54 pm
I ran it again and everything is running smoothly now. I have no idea why :?

Yesterday I installed Visual C++ 2008 express. Then I uninstalled microsoft presentation foundation, which I think came with VC++ 2008 express.

Those are the only things I remember doing between the time it wasn't working and now.

Very strange.
Title: Huge problem with SFML
Post by: Oz1571 on January 11, 2010, 04:32:33 pm
I have had similar issues during the past two months or so, sometimes it will freeze whenever I move the mouse cursor (GetEvent), and it won't go away until I restart my computer. Most of the time it works fine, though.
Title: Huge problem with SFML
Post by: chris67 on January 12, 2010, 11:26:11 pm
Quote from: "Oz1571"
I have had similar issues during the past two months or so, sometimes it will freeze whenever I move the mouse cursor (GetEvent), and it won't go away until I restart my computer. Most of the time it works fine, though.


Ah yes, that was something else I did, I restarted my computer. Still, every other application ran fine, so it was something specifically to do with SFML.


Just happened again in fact. My computer had been running for a day or two, and I started one of the samples ( the box one ) and same thing, mouse movement slowed down the rotating cube. I reset my computer and it was all smooth again.
Title: Huge problem with SFML
Post by: heishe on May 23, 2010, 03:39:08 pm
I have the same problem. No idea how to fix it (1.6 here)
Title: Huge problem with SFML
Post by: Antidote on May 24, 2010, 03:18:47 am
Check the task manager and see what the memory usage is when this happens. It may be a memory leak if so some debugging is in order.

Laurent, maybe you should check the Mouse event functions in the SDK and make sure you have no stray loops.
Title: Huge problem with SFML
Post by: Kingdom of Fish on May 24, 2010, 12:52:28 pm
I've had similar problem but I just solved it with a for-loop instead

Code: [Select]
for (int i = 0 ; (i < 3) && (app.GetEvent(event) ; i++) {
    //handle events
}