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

Author Topic: Event sample rate  (Read 2634 times)

0 Members and 1 Guest are viewing this topic.

Thrusty

  • Newbie
  • *
  • Posts: 16
    • View Profile
Event sample rate
« on: April 02, 2011, 03:40:57 pm »
In terms of events, how often is the mouse move position collected?

I want to collect all the mouse move events while they're coming in, but sleep when there are no longer any events.

I'm wondering if I've done something wrong:

Code: [Select]
#include <SFML/Graphics.hpp>

sf::RenderWindow* win_ptr = 0;

int main()
{
    win_ptr = new sf::RenderWindow(sf::VideoMode(1024, 600, 32), "SFML");

    sf::Clock Clock;
    sf::Event event;
    sf::Color black(0, 0, 0);
    sf::Color white(255, 255, 255);
    sf::Image Image1(1024, 600, black);
    sf::Sprite Sprite;
    Sprite.SetImage(Image1);

    bool sleep = false;
    float counter = 0;
    float begin = 0;

    win_ptr->Clear();
    win_ptr->Display();

    while (win_ptr->IsOpened())
    {
        begin = Clock.GetElapsedTime();

        if (win_ptr->GetEvent(event))
        {
            switch (event.Type)
            {
                case (sf::Event::Closed): goto end;
                case (sf::Event::MouseMoved): Image1.SetPixel(event.MouseMove.X, event.MouseMove.Y, white); break;
            }
        }
        else
            sf::Sleep(0.01f);

        if (counter > .029f)
        {
            counter = 0;
            win_ptr->Clear();
            win_ptr->Draw(Sprite);
            win_ptr->Display();
        }

        counter += Clock.GetElapsedTime() - begin;
    }

    end:

    win_ptr->Close();
    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Event sample rate
« Reply #1 on: April 02, 2011, 04:00:30 pm »
Quote
how often is the mouse move position collected?

Depends on the OS, SFML doesn't control that.

Quote
I'm wondering if I've done something wrong

You mean that it doesn't work as expected?
Laurent Gomila - SFML developer

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Event sample rate
« Reply #2 on: April 03, 2011, 03:24:29 am »
Your code could be a lot more efficient. Also, you do not have to, and really shouldn't be, using pointers and new. Here, I sanitized your code for you.

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow win(sf::VideoMode(1024, 600, 32), "SFML");
    sf::Event event;
    sf::Color black(0, 0, 0);
    sf::Color white(255, 255, 255);
    sf::Image Image1(1024, 600, black);
    sf::Sprite Sprite;
    Sprite.SetImage(Image1);
    win.SetFramerateLimit(1./0.03);

    while (win.IsOpened())
    {
        while (win.GetEvent(event))
        {
            switch (event.Type)
            {
                case (sf::Event::Closed): win.Close();
                case (sf::Event::MouseMoved): Image1.SetPixel(event.MouseMove.X, event.MouseMove.Y, white); break;
            }
        }
        win.Clear();
        win.Draw(Sprite);
        win.Display();
    }
    return EXIT_SUCCESS;
}

I tried to emulate your coding style as best as possible. Notice that the event checking is in a while loop. Also notice the SetFramerateLimit().
I use the latest build of SFML2

Thrusty

  • Newbie
  • *
  • Posts: 16
    • View Profile
Event sample rate
« Reply #3 on: June 28, 2011, 10:20:50 am »
Efficiency problem? I don't mean to be rude, but my program was using 4% or less of CPU. Now, I don't even understand the code any more, since you have changed it. But thanks anyway.

Having run the program, don't you seem something wrong with the behaviour?

Relic

  • Newbie
  • *
  • Posts: 43
    • View Profile
Event sample rate
« Reply #4 on: June 28, 2011, 05:52:55 pm »
Quote
I'm wondering if I've done something wrong

1. You create the window in the heap using new but forget to destroy it with delete.
2. Using goto is a first step to spaghetty code.  :)

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Event sample rate
« Reply #5 on: June 28, 2011, 08:55:21 pm »
There is no need to use goto to jump out of your loop. Just call
Code: [Select]
sf::Window::Close(),
Code: [Select]
sf::Window::IsOpened() will stop returning
Code: [Select]
true and execution will leave your loop naturally.

As for your program not doing what it's supposed to, perhaps you should use real time inputs if you want to process mouse movement without getting held up by the event queue.

http://sfml-dev.org/tutorials/1.6/window-events.php
(The section entitled "Real Time Inputs")

PS: I notice you are using SFML 1.6. You should upgrade to 2.0 ;) Note that sf::Window::GetEvent has been renamed to sf::Window::PollEvent.

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Event sample rate
« Reply #6 on: June 29, 2011, 03:39:39 am »
Quote from: "Thrusty"
Efficiency problem? I don't mean to be rude, but my program was using 4% or less of CPU. Now, I don't even understand the code any more, since you have changed it. But thanks anyway.

Having run the program, don't you seem something wrong with the behaviour?
First of all, I wasn't using efficiency to mean, "how fast does it run," but instead to mean, "how easy is it to read?"

Secondly, not to be rude, but your code was one of the worst pieces of code I have personally ever seen. I'm sure it's not the worst, but it certainly wasn't the best. For example, you don't do realtime input and event capture (which is why you were having the problems, the fix was shown above), you use new (which is the worst thing you could do in a program unless it's ABSOLUTELY necessary. It isn't 99% of the time. Including this time), and you don't use SFML's built in framerate limiter, which is there for a reason. Instead you tried to hardcode your own, which is part of the cause of the problem.

Third, how do you not understand it? Or rather, what don't you understand? The only things I changed were:
1. Removed all use of new, using new is a horrible practice and should be avoided at all costs (there are instances where it must be used, but those have to do with inheritance, which you are not using).
2. Used SFML's built in framerate limiter instead of your hardcoded one.
3. Fixed the event loop so it actually updates correctly.
I use the latest build of SFML2

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Event sample rate
« Reply #7 on: June 29, 2011, 08:43:07 am »
Quote from: "OniLink10"
use of new ... there are instances where it must be used, but those have to do with inheritance

You were probably just trying to simplify things, but I feel I must point out that I have also had to use dynamic allocation when creating a vector of noncopiable types. And there are probably a load of other valid uses too, such as saving stack space. ;)

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Event sample rate
« Reply #8 on: June 29, 2011, 06:43:32 pm »
Quote from: "Xander314"
Quote from: "OniLink10"
use of new ... there are instances where it must be used, but those have to do with inheritance

You were probably just trying to simplify things, but I feel I must point out that I have also had to use dynamic allocation when creating a vector of noncopiable types. And there are probably a load of other valid uses too, such as saving stack space. ;)
I was trying to simplify. However, those uses have nothing to do with this situation. In fact, 99% of the time, you shouldn't use new.
I use the latest build of SFML2

Xander314

  • Jr. Member
  • **
  • Posts: 67
    • View Profile
    • http://sfmlcoder.wordpress.com/
    • Email
Event sample rate
« Reply #9 on: June 29, 2011, 08:29:05 pm »
I know - I just couldn't help but say something about your generalisation ;) I guess I should have refrained from pushing the thread off topic though  :oops: