2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
animationBudget = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
type animationManager struct {
|
2020-07-03 14:00:56 -04:00
|
|
|
cache *d2common.Cache
|
|
|
|
renderer d2interface.Renderer
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
func createAnimationManager(renderer d2interface.Renderer) *animationManager {
|
|
|
|
return &animationManager{
|
|
|
|
renderer: renderer,
|
|
|
|
cache: d2common.CreateCache(animationBudget),
|
|
|
|
}
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (am *animationManager) loadAnimation(animationPath, palettePath string, transparency int) (*Animation, error) {
|
|
|
|
cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, transparency)
|
|
|
|
if animation, found := am.cache.Retrieve(cachePath); found {
|
|
|
|
return animation.(*Animation).Clone(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var animation *Animation
|
2020-07-01 14:09:18 -04:00
|
|
|
|
2020-02-22 20:44:30 -05:00
|
|
|
ext := strings.ToLower(filepath.Ext(animationPath))
|
|
|
|
switch ext {
|
2020-02-01 18:55:56 -05:00
|
|
|
case ".dc6":
|
2020-02-26 08:39:38 -05:00
|
|
|
dc6, err := loadDC6(animationPath)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:46:47 -05:00
|
|
|
palette, err := LoadPalette(palettePath)
|
2020-02-26 08:39:38 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
animation, err = CreateAnimationFromDC6(am.renderer, dc6, palette)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case ".dcc":
|
|
|
|
dcc, err := loadDCC(animationPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-26 22:46:47 -05:00
|
|
|
palette, err := LoadPalette(palettePath)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
animation, err = CreateAnimationFromDCC(am.renderer, dcc, palette, transparency)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
default:
|
2020-02-22 20:44:30 -05:00
|
|
|
return nil, fmt.Errorf("unknown animation format: %s", ext)
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := am.cache.Insert(cachePath, animation.Clone(), 1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return animation, nil
|
|
|
|
}
|