2020-07-06 20:13:06 -04:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"math"
|
|
|
|
|
2020-07-08 17:46:45 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-08-11 18:01:33 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2math"
|
2020-07-08 17:46:45 -04:00
|
|
|
|
2020-07-06 20:13:06 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2fileformats/d2dcc"
|
|
|
|
d2iface "github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
)
|
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
var _ d2iface.Animation = &DCCAnimation{} // Static check to confirm struct conforms to interface
|
|
|
|
|
|
|
|
// DCCAnimation represents an animation decoded from DCC
|
2020-07-06 20:13:06 -04:00
|
|
|
type DCCAnimation struct {
|
|
|
|
animation
|
2020-09-12 16:51:30 -04:00
|
|
|
*animationManager
|
2020-07-08 17:46:45 -04:00
|
|
|
dccPath string
|
|
|
|
palette d2iface.Palette
|
|
|
|
renderer d2iface.Renderer
|
2020-07-06 20:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Clone creates a copy of the animation
|
|
|
|
func (a *DCCAnimation) Clone() d2iface.Animation {
|
|
|
|
animation := *a
|
|
|
|
return &animation
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetDirection places the animation in the direction of an animation
|
|
|
|
func (a *DCCAnimation) SetDirection(directionIndex int) error {
|
|
|
|
const smallestInvalidDirectionIndex = 64
|
|
|
|
if directionIndex >= smallestInvalidDirectionIndex {
|
|
|
|
return errors.New("invalid direction index")
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := d2dcc.Dir64ToDcc(directionIndex, len(a.directions))
|
|
|
|
if !a.directions[direction].decoded {
|
|
|
|
err := a.decodeDirection(direction)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
a.directionIndex = direction
|
|
|
|
a.frameIndex = 0
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *DCCAnimation) decodeDirection(directionIndex int) error {
|
2020-09-12 16:51:30 -04:00
|
|
|
dcc, err := a.loadDCC(a.dccPath)
|
2020-07-06 20:13:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
direction := dcc.DecodeDirection(directionIndex)
|
|
|
|
|
|
|
|
minX, minY := math.MaxInt32, math.MaxInt32
|
|
|
|
maxX, maxY := math.MinInt32, math.MinInt32
|
|
|
|
|
|
|
|
for _, dccFrame := range direction.Frames {
|
2020-08-05 00:03:33 -04:00
|
|
|
minX = d2math.MinInt(minX, dccFrame.Box.Left)
|
|
|
|
minY = d2math.MinInt(minY, dccFrame.Box.Top)
|
|
|
|
maxX = d2math.MaxInt(maxX, dccFrame.Box.Right())
|
|
|
|
maxY = d2math.MaxInt(maxY, dccFrame.Box.Bottom())
|
2020-07-06 20:13:06 -04:00
|
|
|
}
|
|
|
|
|
2020-07-31 21:22:56 -04:00
|
|
|
frameWidth := maxX - minX
|
|
|
|
frameHeight := maxY - minY
|
2020-07-06 20:13:06 -04:00
|
|
|
|
2020-07-31 21:22:56 -04:00
|
|
|
for _, dccFrame := range direction.Frames {
|
2020-07-08 17:46:45 -04:00
|
|
|
pixels := ImgIndexToRGBA(dccFrame.PixelData, a.palette)
|
2020-07-06 20:13:06 -04:00
|
|
|
|
2020-07-06 21:26:08 -04:00
|
|
|
sfc, err := a.renderer.NewSurface(frameWidth, frameHeight, d2enum.FilterNearest)
|
2020-07-06 20:13:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := sfc.ReplacePixels(pixels); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
a.directions[directionIndex].decoded = true
|
|
|
|
a.directions[directionIndex].frames = append(a.directions[directionIndex].frames, &animationFrame{
|
2020-07-31 21:22:56 -04:00
|
|
|
width: frameWidth,
|
|
|
|
height: frameHeight,
|
2020-07-06 20:13:06 -04:00
|
|
|
offsetX: minX,
|
|
|
|
offsetY: minY,
|
|
|
|
image: sfc,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|