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

Author Topic: thor::CallbackTimer with SFML 2.1  (Read 1416 times)

0 Members and 2 Guests are viewing this topic.

Dyno3421

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
thor::CallbackTimer with SFML 2.1
« on: October 02, 2013, 01:51:25 am »
Hey guys, i'm writing a little snake game to get acquainted with SFML 2.1, Thor, and some of the new c++11 features.

I'm having some trouble getting a thor::CallbackTimer to fire.  The timer is counting down, being updated every frame, and the connection is valid, but the callbacks aren't being called when the timer expires.

This is how i'm connecting the callback. In the documentation of Thor it looked like it needed a thor::CallbackTimer parameter, but that didn't solve the problem.
mSpecialFoodTimer.connect(std::bind(&Snake::setUpSpecialFood, this));

This is very similar to what is in the examples for Thor, yet it doesn't fire either.
mSpecialFoodTimer.connect(std::bind(&sf::RectangleShape::setFillColor, &gameBoard, sf::Color::Blue));

I also tried it by just putting a lambda statement in there, same behavior.  (i'm not sure if this would work in the first place, no compile errors though.)
connect = mSpecialFoodTimer.connect([this] (thor::CallbackTimer & timer) -> void
                                              {
                                                  std::cout << "food timer called\n";
                                                  food new_food;
                                                  sf::Vector2i pos = this->getOpenPosition();
                                                  new_food.mapPos = pos;
                                                  new_food.type = (thor::random(0,1) == 0 ? FOOD_TYPE::GRAPE : FOOD_TYPE::ORANGE);
                                                  mFood.push_back(new_food);
                                              });

Any help would be appreciated !

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Re: thor::CallbackTimer with SFML 2.1
« Reply #1 on: October 02, 2013, 11:51:34 am »
Are you sure you call update() every frame (see documentation)? The timer is initialized with a time bigger than zero and runs initially?

Does the following example work, i.e. "expired" is written on the console every second?
#include <Thor/Time/CallbackTimer.hpp>
#include <iostream>

void listener(thor::CallbackTimer& trigger)
{
        std::cout << "expired" << std::endl;
        trigger.restart(sf::seconds(1.f));
}

int main()
{
        thor::CallbackTimer timer;
        timer.connect(&listener);
        timer.restart(sf::seconds(1.f));

        for (;;)
                timer.update();
}

If it doesn't work, can you reproduce your problem in a different minimal complete example?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Dyno3421

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: thor::CallbackTimer with SFML 2.1
« Reply #2 on: October 02, 2013, 05:43:39 pm »
I must have looked at the documentation at least three times, turns out I was restarting the timer before my call to update(), so as far as update knew, the timer was never expired.  The problem was solved by updating before restarting the timer.  Thanks for the help.

 

anything