1
0
Fork 0

Adding upstream version 0.0~git20250520.a1d9079+dfsg.

Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
Daniel Baumann 2025-05-24 19:46:29 +02:00
parent 590ac7ff5f
commit 20149b7f3a
Signed by: daniel
GPG key ID: FBB4F0E80A80222F
456 changed files with 70406 additions and 0 deletions

26
exp/sprite/clock/clock.go Normal file
View file

@ -0,0 +1,26 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package clock provides a clock and time functions for a sprite engine.
package clock
// A Time represents an instant in sprite time.
//
// The application using the sprite engine is responsible for
// determining sprite time.
//
// Typically time 0 is when the app is initialized and time is
// quantized at the intended frame rate. For example, an app may
// record wall time when it is initialized
//
// var start = time.Now()
//
// and then compute the current instant in time for 60 FPS:
//
// now := clock.Time(time.Since(start) * 60 / time.Second)
//
// An application can pause or reset sprite time, but it must be aware
// of any stateful sprite.Arranger instances that expect time to
// continue.
type Time int32

83
exp/sprite/clock/tween.go Normal file
View file

@ -0,0 +1,83 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package clock
// Standard tween functions.
//
// Easing means a slowing near the timing boundary, as defined by
// a cubic bezier curve. Exact parameters match the CSS properties.
var (
EaseIn = CubicBezier(0.42, 0, 1, 1)
EaseOut = CubicBezier(0, 0, 0.58, 1)
EaseInOut = CubicBezier(0.42, 0, 0.58, 1)
)
// Linear computes the fraction [0,1] that t lies between [t0,t1].
func Linear(t0, t1, t Time) float32 {
if t >= t1 {
return 1
}
if t <= t0 {
return 0
}
return float32(t-t0) / float32(t1-t0)
}
// CubicBezier generates a tween function determined by a Cubic Bézier curve.
//
// The parameters are cubic control parameters. The curve starts at (0,0)
// going toward (x0,y0), and arrives at (1,1) coming from (x1,y1).
func CubicBezier(x0, y0, x1, y1 float32) func(t0, t1, t Time) float32 {
return func(start, end, now Time) float32 {
// A Cubic-Bezier curve restricted to starting at (0,0) and
// ending at (1,1) is defined as
//
// B(t) = 3*(1-t)^2*t*P0 + 3*(1-t)*t^2*P1 + t^3
//
// with derivative
//
// B'(t) = 3*(1-t)^2*P0 + 6*(1-t)*t*(P1-P0) + 3*t^2*(1-P1)
//
// Given a value x ∈ [0,1], we solve for t using Newton's
// method and solve for y using t.
x := Linear(start, end, now)
// Solve for t using x.
t := x
for i := 0; i < 5; i++ {
t2 := t * t
t3 := t2 * t
d := 1 - t
d2 := d * d
nx := 3*d2*t*x0 + 3*d*t2*x1 + t3
dxdt := 3*d2*x0 + 6*d*t*(x1-x0) + 3*t2*(1-x1)
if dxdt == 0 {
break
}
t -= (nx - x) / dxdt
if t <= 0 || t >= 1 {
break
}
}
if t < 0 {
t = 0
}
if t > 1 {
t = 1
}
// Solve for y using t.
t2 := t * t
t3 := t2 * t
d := 1 - t
d2 := d * d
y := 3*d2*t*y0 + 3*d*t2*y1 + t3
return y
}
}

View file

@ -0,0 +1,53 @@
// Copyright 2014 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package clock
import "testing"
func TestLinear(t *testing.T) {
t0 := Time(0)
t1 := Time(6 * 60)
now := Time(3 * 60)
if c := Linear(t0, t1, now); c != 0.5 {
t.Errorf("c=%.2f, want 0.5", c)
}
}
func TestCubicBezier(t *testing.T) {
t0 := Time(0)
t1 := Time(1e6)
tests := []struct {
x0, y0, x1, y1 float32
x, y float32
}{
{0.00, 0.1, 0.4, 1.00, 0.0, 0.00},
{0.00, 0.1, 0.4, 1.00, 0.1, 0.26},
{0.00, 0.1, 0.4, 1.00, 0.5, 0.79},
{0.00, 0.1, 0.4, 1.00, 0.9, 0.99},
{0.00, 0.1, 0.4, 1.00, 1.0, 1.00},
{0.36, 0.2, 0.3, 0.85, 0.0, 0.0},
{0.36, 0.2, 0.3, 0.85, 0.3059, 0.3952},
{0.36, 0.2, 0.3, 0.85, 0.4493, 0.6408},
{0.36, 0.2, 0.3, 0.85, 0.8116, 0.9410},
{0.00, 0.0, 1.0, 1.00, 0.1, 0.1},
{0.00, 0.0, 1.0, 1.00, 0.5, 0.5},
{0.00, 0.0, 1.0, 1.00, 0.9, 0.9},
{0.42, 0.0, 1.0, 1.00, 0.0, 0.0},
}
for _, test := range tests {
cb := CubicBezier(test.x0, test.y0, test.x1, test.y1)
now := t0 + Time(float32(t1-t0)*test.x)
y := cb(t0, t1, now)
const epsilon = 0.01
diff := y - test.y
if diff < -epsilon || +epsilon < diff {
t.Errorf("CubicBezier(%.2f,%.2f,%.2f,%.2f): for x=%.2f got y=%.2f, want %.2f", test.x0, test.y0, test.x1, test.y1, test.x, y, test.y)
}
}
}