/
texture_manager.go
73 lines (60 loc) · 1.35 KB
/
texture_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package splendor
import (
"io/ioutil"
"path"
"strings"
"github.com/veandco/go-sdl2/img"
"github.com/veandco/go-sdl2/sdl"
)
type TextureManager struct {
tm map[string]*sdl.Texture
skulls map[SkullType]*sdl.Texture
r *sdl.Renderer
}
func (t *TextureManager) Initialize(r *sdl.Renderer, dirpath string) error {
t.r = r
t.tm = map[string]*sdl.Texture{}
t.skulls = map[SkullType]*sdl.Texture{}
fis, err := ioutil.ReadDir(dirpath)
if err != nil {
return err
}
for _, fi := range fis {
if !fi.IsDir() {
err = t.loadTexture(path.Join(dirpath, fi.Name()), strings.Split(fi.Name(), ".")[0])
if err != nil {
return err
}
}
}
t.skulls[SBlack] = t.tm["t-black"]
t.skulls[SBlue] = t.tm["t-blue"]
t.skulls[SGreen] = t.tm["t-green"]
t.skulls[SRed] = t.tm["t-red"]
t.skulls[SWhite] = t.tm["t-white"]
t.skulls[SGold] = t.tm["t-yellow"]
return nil
}
func (t *TextureManager) Get(key string) *sdl.Texture {
return t.tm[key]
}
func (t *TextureManager) loadTexture(filepath string, name string) error {
s, err := img.Load(filepath)
if err != nil {
return err
}
t.tm[name], err = t.r.CreateTextureFromSurface(s)
if err != nil {
return err
}
s.Free()
return nil
}
func (t *TextureManager) Destroy() {
for _, tx := range t.tm {
tx.Destroy()
}
}
func (t *TextureManager) Skull(st SkullType) *sdl.Texture {
return t.skulls[st]
}