SFML community forums
Help => System => Topic started by: uonder 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?
-
No, but to me this is not the most efficient/clean solution anyway.
I'd do it this way:
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.