Rework TimeDelta func into a simpler FPS func and remove FPS consts

This commit is contained in:
Christian Rocha 2021-07-09 18:17:02 -04:00
parent 17bfa34c33
commit d56f9e806c
No known key found for this signature in database
GPG Key ID: D6CC7A16E5878018
3 changed files with 12 additions and 18 deletions

View File

@ -13,7 +13,7 @@ import (
)
const (
fps = time.Second / 60
fps = 60
spriteWidth = 12
spriteHeight = 5
frequency = 0.95
@ -34,7 +34,7 @@ var (
type frameMsg time.Time
func animate() tea.Cmd {
return tea.Tick(fps, func(t time.Time) tea.Msg {
return tea.Tick(time.Second/fps, func(t time.Time) tea.Msg {
return frameMsg(t)
})
}
@ -94,7 +94,7 @@ func (m model) View() string {
func main() {
m := model{
spring: harmonica.NewSpring(harmonica.TimeDelta(fps), frequency, damping),
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
}
if err := tea.NewProgram(m).Start(); err != nil {

View File

@ -50,7 +50,7 @@ func main() {
circ = Circle{
// Setup a new spring.
spring: harmonica.NewSpring(harmonica.TimeDelta(time.Second/fps), frequency, damping),
spring: harmonica.NewSpring(harmonica.FPS(fps), frequency, damping),
// Set target radius.
Radius: circleRadius,

View File

@ -4,8 +4,8 @@
//
// Example usage:
//
// // Run once to initialize. Note the FPS.
// spring := NewSpring(FPS60, 0.8, 1.0)
// // Run once to initialize.
// spring := NewSpring(FPS(60), 0.8, 1.0)
//
// // Update on every frame.
// pos := 0.0
@ -55,22 +55,16 @@ import (
"time"
)
// Standard time deltas that can be used when initializing a spring.
const (
FPS60 = float64(int64(time.Second/60)) / float64(int64(time.Second))
FPS30 = float64(int64(time.Second/30)) / float64(int64(time.Second))
)
// TimeDelta returns a time delta for a given FPS. This is usefaul for setting
// the time delta when initializing a Spring.
// FPS returns a time delta for a given number of frames per second. This value
// can be used as the time delta when initializing a Spring.
//
// Example:
//
// delta := TimeDelta(time.Second/60)
// spring := New(delta, 0.98, 0.8)
// spring := NewSpring(FPS(60), 0.8, 0.98)
//
func TimeDelta(fps time.Duration) float64 {
return float64(int64(fps)) / float64(int64(time.Second))
func FPS(n int) float64 {
d := time.Second / time.Duration(n)
return float64(int64(d)) / float64(int64(time.Second))
}
// In calculus ε is (in vague terms) an arbitrarily small positive number. In