tins2020/fonts.go

42 lines
657 B
Go
Raw Normal View History

2020-05-08 16:38:26 +00:00
package tins2020
import (
"github.com/veandco/go-sdl2/ttf"
"opslag.de/schobers/fs/vfs"
)
type Fonts struct {
dir vfs.CopyDir
fonts map[string]*ttf.Font
}
func (f *Fonts) Init(dir vfs.CopyDir) {
f.dir = dir
f.fonts = map[string]*ttf.Font{}
}
func (f *Fonts) Load(name, path string, size int) error {
fontPath, err := f.dir.Retrieve(path)
if err != nil {
return err
}
font, err := ttf.OpenFont(fontPath, size)
if err != nil {
return err
}
if font, ok := f.fonts[name]; ok {
font.Close()
}
f.fonts[name] = font
return nil
}
func (f *Fonts) Destroy() {
if f.fonts == nil {
return
}
for _, f := range f.fonts {
f.Close()
}
}