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

Author Topic: Timer Callback  (Read 4006 times)

0 Members and 1 Guest are viewing this topic.

uonder

  • Newbie
  • *
  • Posts: 1
    • AOL Instant Messenger - cankaya
    • View Profile
Timer Callback
« on: March 30, 2011, 03:04:29 pm »
I am trying to create a program with a sliding menu...
if user clicks a button, or if she moves her mouse to the right of the window, a menu will appear in an animated fashion.


SDL has method for registering a user defined timer callback
SDL_AddTimer(UINT32 ,SDL_NewTimerCallback, void*)

So in SDL i managed to do it like this:

in event handler >
if button press
{
  start a timer
}
...
in timer callback >
if !end_of_animation
{
  update location of sliding menu in every timer callback
  paint/blit it to surface
}
else
 stop timer


Is there any way to do it in SFML in a similar manner?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Timer Callback
« Reply #1 on: March 30, 2011, 03:43:19 pm »
No, but to me this is not the most efficient/clean solution anyway.

I'd do it this way:
Code: [Select]
class Menu
{
public:

    void animate()
    {
        m_animated = true;
        m_timer.Start();
    }

    void update()
    {
        if (m_animated)
        {
            if (!end of animation)
                update things according to m_timer.GetElapsedtime()
            else
                m_animated = false;
        }
    }

    void draw()
    {
        ...
    }

private:

    bool m_animated;
    sf::Clock m_timer;
};

int main()
{
    sf::RenderWindow window(...);
    Menu menu;

    while (window.isOpened())
    {
        sf::Event event;
        while (window.GetEvent(event))
        {
            if (event is click)
                menu.animate();
        }

        menu.update();

        window.Clear();
        menu.draw();
        window.Display();
    }

    return 0;
}


But if you want a system with timers and callbacks anyway, there's no problem to implement it on your side.
Laurent Gomila - SFML developer