ysbaddaden / sdl.cr

SDL2 bindings for Crystal
102 stars 32 forks source link

Adding in Timer #17

Open jwoertink opened 7 years ago

jwoertink commented 7 years ago

This adds in the SDL_Timer stuff. Used for checking elapsed time in milliseconds, and creating delayed callbacks. I added 2 samples for doing a simple timer, and a more advanced timer from the Lazy Foo tutorial 22 & 23.

jwoertink commented 6 years ago

I'm not sure since I don't know what a monotonic clock is, but if SDL provides it, then the API should as well. I don't think the callbacks are in a crystal context since they're defined in C. But I didn't need to use any callbacks in the samples.

ysbaddaden commented 6 years ago

Well, SDL provides a network library, which is nice for C (platform abstraction) but they'll mess up with the Crystal event loop. We shouldn't integrate every SDL library, especially when Crystal already has an alternative.

The following will probably behave better (crystal context, integrated into the event loop), for example:

def interval(seconds : Time::Span)
  spawn do
    sleep(seconds)
    yield
  end
end

interval(5.milliseconds) do
  # ...
end

Instead of SDL ticks, there is ticks = Time.monotonic.total_milliseconds and span = Time.measure { ... } which won't be affected by time fluctuations (e.g. changing the computer's date) for computing elapsed time.

jwoertink commented 6 years ago

Ah, I see! Ok. That makes a lot of sense. If that's the case, then maybe we can just have the sample timer run the same way, but with a crystal context, and then just ditch the whole SDL Timer addition? If you're cool with that, I will make that change. Keeping the samples with the same result, but using straight Crystal, and remove the timer.