2020-02-17 22:11:52 -05:00
|
|
|
package d2asset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-07-03 21:23:44 -04:00
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common/d2interface"
|
2020-02-17 22:11:52 -05:00
|
|
|
|
|
|
|
"github.com/OpenDiablo2/OpenDiablo2/d2common"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
fontBudget = 64
|
|
|
|
)
|
|
|
|
|
|
|
|
type fontManager struct {
|
2020-07-03 21:23:44 -04:00
|
|
|
cache d2interface.Cache
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
2020-07-05 15:56:48 -04:00
|
|
|
func createFontManager() d2interface.ArchivedFontManager {
|
2020-02-17 22:11:52 -05:00
|
|
|
return &fontManager{d2common.CreateCache(fontBudget)}
|
|
|
|
}
|
|
|
|
|
2020-07-05 15:56:48 -04:00
|
|
|
// LoadFont loads a font from the archives managed by the ArchiveManager
|
|
|
|
func (fm *fontManager) LoadFont(tablePath, spritePath, palettePath string) (d2interface.Font,
|
|
|
|
error) {
|
2020-02-17 22:11:52 -05:00
|
|
|
cachePath := fmt.Sprintf("%s;%s;%s", tablePath, spritePath, palettePath)
|
|
|
|
if font, found := fm.cache.Retrieve(cachePath); found {
|
2020-07-05 15:56:48 -04:00
|
|
|
return font.(d2interface.Font).Clone(), nil
|
2020-02-17 22:11:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
font, err := loadFont(tablePath, spritePath, palettePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fm.cache.Insert(cachePath, font.Clone(), 1); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return font, nil
|
|
|
|
}
|
2020-07-05 15:56:48 -04:00
|
|
|
|
|
|
|
// ClearCache clears the font cache
|
|
|
|
func (fm *fontManager) ClearCache() {
|
|
|
|
fm.cache.Clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetCache returns the font managers cache
|
|
|
|
func (fm *fontManager) GetCache() d2interface.Cache {
|
|
|
|
return fm.cache
|
|
|
|
}
|