Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation for timers #587

Merged
merged 9 commits into from
Mar 9, 2020
Merged

Add implementation for timers #587

merged 9 commits into from
Mar 9, 2020

Conversation

lenscas
Copy link
Contributor

@lenscas lenscas commented Mar 4, 2020

I added a basic Timer struct to help with #580 . However, before I continue on making sure it actually works and update the documentation, I want to know if this the API you had in mind. As I deviate a bit from the given example (mostly using duration instead of f32)

Motivation and Context

It solves not being able to know when to render or update

Checks

  • I have read CONTRIBUTING.md.
  • I have updated CHANGES.md, with [BREAKING] next to all breaking changes
  • I have updated the documentation if necessary
  • The example found in README.md compiles and functions like expected

@lenscas lenscas changed the title Add untested implementation for timers Add implementation for timers Mar 4, 2020
@johnpmayer
Copy link
Contributor

Two comments

  1. This feels less "directionally correct" than introducing something async - https://github.com/johnpmayer/quicksilver-utils/blob/master/quicksilver-utils-async/src/time.rs
  2. Does this guarantee that render happens during an animation frame? Or does quicksilver 0.4 already handle that some other way?

@lenscas
Copy link
Contributor Author

lenscas commented Mar 4, 2020

I was looking at #580 (comment) when making this. So, if there is other stuff I need to keep in mind, it probably doesn't (yet).

Copy link
Owner

@ryanisaacg ryanisaacg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work so far! This is a good start, and I've made a few comments for changes before merging.

src/timer.rs Outdated Show resolved Hide resolved
src/timer.rs Outdated Show resolved Hide resolved
src/timer.rs Outdated Show resolved Hide resolved
@ryanisaacg
Copy link
Owner

@johnpmayer

This feels less "directionally correct" than introducing something async - https://github.com/johnpmayer/quicksilver-utils/blob/master/quicksilver-utils-async/src/time.rs

This isn't quite equivalent to sleeping. Imagine you want to render at rate R frame/second and update at rate U frame/second. You don't want to sleep, because these rates may not be equivalent (so you may be waiting on a render but need to update, or vice-versa.) Fix your timestep is the canonical text here.

Does this guarantee that render happens during an animation frame? Or does quicksilver 0.4 already handle that some other way?

That's not currently guaranteed, though I think it would be a useful / important feature. I've opened an issue to handle it in the underlying windowing library. It would be out of scope here.

@lenscas
Copy link
Contributor Author

lenscas commented Mar 4, 2020

One question: If you are late with calling tick(), should it still take the full time until the next frame like it does now? Or should you being late be taken into account for the next frame?

If it being late should be taken into account, what should happen if you are an entire tick (or more) late?

@lenscas lenscas marked this pull request as ready for review March 5, 2020 11:15
@lenscas lenscas requested a review from ryanisaacg March 5, 2020 11:20
@ryanisaacg
Copy link
Owner

If it being late should be taken into account, what should happen if you are an entire tick (or more) late?

We may want to use the accumulator strategy from the aforementioned 'Fix your Timestep!'. In that case, instead of re-setting the timer to now on tick, we could add the period to it. Each time you tick, you catch up one period. Then the usage pattern would look like:

while timer.tick() {
    let delta_time = timer.remaining();
    update(delta_time);
}

Does that make sense?

@lenscas
Copy link
Contributor Author

lenscas commented Mar 7, 2020

If it being late should be taken into account, what should happen if you are an entire tick (or more) late?

We may want to use the accumulator strategy from the aforementioned 'Fix your Timestep!'. In that case, instead of re-setting the timer to now on tick, we could add the period to it. Each time you tick, you catch up one period. Then the usage pattern would look like:

while timer.tick() {
    let delta_time = timer.remaining();
    update(delta_time);
}

Does that make sense?

That should do it

Copy link
Owner

@ryanisaacg ryanisaacg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than a quick doc update, all looks done here! 👍

src/timer.rs Outdated Show resolved Hide resolved
@lenscas
Copy link
Contributor Author

lenscas commented Mar 8, 2020

Something I just thought about while updating the example to show how to catch up. To my knowledge it doesn't really matter to catch up to the rendering, does it? So, maybe a function that doesn't mind dropping ticks is nice?

That way, users can choose between dropping updates/frames and solve being late in another way if they so desire? However, that is probably a discussion that should happen after this pr got merged :)

examples/06_timers.rs Outdated Show resolved Hide resolved
@ryanisaacg ryanisaacg merged commit e2f10ce into ryanisaacg:master Mar 9, 2020
@ryanisaacg
Copy link
Owner

Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants