2020-02-01 18:55:56 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"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
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
animationBudget = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
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() {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (am *animationManager) GetCache() d2interface.Cache {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-07-05 13:01:44 -04:00
|
|
|
func (am *animationManager) LoadAnimation(
|
|
|
|
animationPath, palettePath string,
|
2020-07-08 17:46:45 -04:00
|
|
|
effect d2enum.DrawEffect ) (d2interface.Animation, error) {
|
|
|
|
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-06 20:13:06 -04:00
|
|
|
animation, err = CreateDC6Animation(am.renderer, animationPath, palette)
|
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
|
|
|
|
}
|