gg/ax/ani.go

52 lines
1,008 B
Go
Raw Permalink Normal View History

2024-05-28 11:24:12 +03:00
package ax
import (
"surdeus.su/core/gg"
)
// Unique identifier for animation
// in the animation set.
2024-06-01 16:07:28 +03:00
type AnimationID int
2024-05-28 11:24:12 +03:00
// The type describes set of animations
// to switch between.
2024-06-01 16:07:28 +03:00
type AnimationSet map[AnimationID] Animation
2024-05-28 11:24:12 +03:00
// Make new animation set from an image.
func AnimationSetFromImage(
img *gg.Image,
// Width and height of one frame respectively.
w, h int,
// Gaps for X and Y respectively.
gx, gy int,
defines ...AnimationDefine,
) (AnimationSet, error) {
set := AnimationSet{}
r := img.Bounds()
fw, fh := r.Dx()/w, r.Dy()/h
for _, define := range defines {
animation := make(Animation, len(define.Indexes))
for i := range animation {
idx := define.Indexes[i]
rect := gg.ImageRect{
Min: gg.ImagePoint{
idx.X*fw+gx,
idx.Y*fh+gy,
},
Max: gg.ImagePoint{
(idx.X+1)*fw-gx,
(idx.Y+1)*fh-gy,
},
}
subImg := img.SubImage(rect)
animation[i] = gg.NewImageFromImage(subImg)
}
set[define.Id] = animation
}
return set, nil
}