2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-09-08 15:58:35 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2cache"
|
2020-02-01 18:55:56 -05:00
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2020-07-08 17:46:45 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2enum"
|
2020-07-03 14:00:56 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-01 18:55:56 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
animationBudget = 64
|
|
|
|
)
|
|
|
|
|
2020-07-13 09:05:04 -04:00
|
|
|
// Static checks to confirm struct conforms to interface
|
2020-08-04 22:53:36 -04:00
|
|
|
var _ d2interface.AnimationManager = &animationManager{}
|
2020-07-13 09:05:04 -04:00
|
|
|
var _ d2interface.Cacher = &animationManager{}
|
|
|
|
|
2020-02-01 18:55:56 -05:00
|
|
|
type animationManager struct {
|
2020-07-03 21:23:44 -04:00
|
|
|
cache d2interface.Cache
|
2020-07-03 14:00:56 -04:00
|
|
|
renderer d2interface.Renderer
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 13:01:44 -04:00
|
|
|
func (am *animationManager) ClearCache() {
|
2020-07-13 20:30:03 -04:00
|
|
|
am.cache.Clear()
|
2020-07-05 13:01:44 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (am *animationManager) GetCache() d2interface.Cache {
|
2020-07-13 20:30:03 -04:00
|
|
|
return am.cache
|
2020-07-05 13:01:44 -04:00
|
|
|
}
|
|
|
|
|
2020-07-03 14:00:56 -04:00
|
|
|
func createAnimationManager(renderer d2interface.Renderer) *animationManager {
|
|
|
|
return &animationManager{
|
|
|
|
renderer: renderer,
|
2020-09-08 15:58:35 -04:00
|
|
|
cache: d2cache.CreateCache(animationBudget),
|
2020-07-03 14:00:56 -04:00
|
|
|
}
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 13:01:44 -04:00
|
|
|
func (am *animationManager) LoadAnimation(
|
|
|
|
animationPath, palettePath string,
|
2020-07-13 09:05:04 -04:00
|
|
|
effect d2enum.DrawEffect) (d2interface.Animation, error) {
|
2020-07-08 17:46:45 -04:00
|
|
|
cachePath := fmt.Sprintf("%s;%s;%d", animationPath, palettePath, effect)
|
2020-02-01 18:55:56 -05:00
|
|
|
if animation, found := am.cache.Retrieve(cachePath); found {
|
2020-07-06 20:13:06 -04:00
|
|
|
return animation.(d2interface.Animation).Clone(), nil
|
2020-02-01 18:55:56 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 13:01:44 -04:00
|
|
|
var animation d2interface.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 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-08 21:57:35 -04:00
|
|
|
animation, err = CreateDC6Animation(am.renderer, animationPath, palette, d2enum.DrawEffectNone)
|
2020-02-01 18:55:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
case ".dcc":
|
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-08 17:46:45 -04:00
|
|
|
animation, err = CreateDCCAnimation(am.renderer, animationPath, palette, effect)
|
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
|
|
|
|
}
|