-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtexture.go
112 lines (94 loc) · 2.18 KB
/
texture.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package bohm
import (
"image"
"os"
)
type textureDef struct {
name string
size, cardinality int
}
func (def textureDef) Open() (*texture, error) {
if t, ok := textureCache[def.name]; ok {
return t, nil
}
f, err := os.Open(def.name)
if err != nil {
return nil, err
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
return nil, err
}
t := &texture{cardCache: [4][]uint8{img.(*image.RGBA).Pix}}
t.cardCache[0] = img.(*image.RGBA).Pix
t.Size = def.size
textureCache[def.name] = t
return t, nil
}
type texture struct {
Size int
cardCache [4][]uint8
}
func (t *texture) CarTile(cardinality int) []byte {
if t.cardCache[cardinality] == nil {
t.buildCache(cardinality)
}
return t.cardCache[cardinality]
}
func (t *texture) buildCache(cardinality int) {
switch cardinality {
case 1:
t.cardCache[1] = make([]uint8, len(t.cardCache[0]))
for y := 0; y < t.Size; y++ {
row := y * t.Size
for x := 0; x < t.Size; x++ {
to := t.cardCache[1][(row+x)*4:]
from := t.cardCache[0][(x*t.Size+(t.Size-1-y))*4:]
to[0], to[1], to[2], to[3] = from[0], from[1], from[2], from[3]
}
}
case 2:
t.cardCache[2] = make([]uint8, len(t.cardCache[0]))
for y := 0; y < t.Size; y++ {
row := y * t.Size
for x := 0; x < t.Size; x++ {
to := t.cardCache[2][(row+x)*4:]
from := t.cardCache[0][((t.Size-1-y)*t.Size+(t.Size-1-x))*4:]
to[0], to[1], to[2], to[3] = from[0], from[1], from[2], from[3]
}
}
case 3:
t.cardCache[3] = make([]uint8, len(t.cardCache[0]))
for y := 0; y < t.Size; y++ {
row := y * t.Size
for x := 0; x < t.Size; x++ {
to := t.cardCache[3][(row+x)*4:]
from := t.cardCache[0][4*(t.Size*(t.Size-1-x)+y):]
to[0], to[1], to[2], to[3] = from[0], from[1], from[2], from[3]
}
}
}
}
var (
TextureCache = true
textureCache = make(map[string]*texture)
)
func _OpenTexture(name string) *texture {
if t, ok := textureCache[name]; ok && TextureCache {
return t
}
f, err := os.Open(name)
if err != nil {
panic(err)
}
defer f.Close()
img, _, err := image.Decode(f)
if err != nil {
panic(err)
}
t := &texture{}
t.cardCache[0] = img.(*image.RGBA).Pix
textureCache[name] = t
return t
}