This repository has been archived by the owner on Jun 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextureAtlas.go
95 lines (86 loc) · 2.37 KB
/
TextureAtlas.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
package hebiten
import (
"hgo"
"io/ioutil"
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
)
type TextureAtlas struct {
texture *ebiten.Image
size Int2
currentPos Int2
currentRowHeight int
images map[string]TIntRect
}
func (this *TextureAtlas) SetSize(size Int2) {
hgo.Assert(this.size.CheckIf0())
this.size = size
}
func (this *TextureAtlas) Create() *TextureAtlas {
if this.size.CheckIf0() {
this.size = Int2{X: 512, Y: 512}
}
var texture, newImageResult = ebiten.NewImage(this.size.X, this.size.Y, ebiten.FilterNearest)
hgo.AssertResult(newImageResult)
this.texture = texture
this.images = make(map[string]TIntRect)
return this
}
func (this *TextureAtlas) Write(name string, image *ebiten.Image) {
var imageWidth, imageHeight = image.Size()
var movePos = func() {
hgo.Assert(imageWidth <= this.size.X)
hgo.Assert(imageHeight <= this.size.Y)
if this.size.X < this.currentPos.X+imageWidth {
this.currentPos.X = 0
this.currentPos.Y += this.currentRowHeight
this.currentRowHeight = 0
}
hgo.Assert(this.currentPos.Y+imageHeight <= this.size.Y)
}
movePos()
var drawImage = func() {
var draw TDraw
draw.Position = this.currentPos.ToBigFloat()
draw.Target = this.texture
draw.Image = image
draw.Draw()
}
drawImage()
var storePos = func() {
var rect TIntRect
rect.X = this.currentPos.X
rect.Y = this.currentPos.Y
rect.W = imageWidth
rect.H = imageHeight
this.images[name] = rect
if this.currentRowHeight < imageHeight {
this.currentRowHeight = imageHeight
}
}
storePos()
this.currentPos.X += imageWidth
}
func (this *TextureAtlas) LoadFromDir(path string) int {
var files, readDirResult = ioutil.ReadDir(path)
hgo.AssertResult(readDirResult)
for _, file := range files {
var fileName = file.Name()
var filePath = path + "/" + fileName
var image, _, imageResult = ebitenutil.NewImageFromFile(filePath, ebiten.FilterNearest)
hgo.AssertResult(imageResult)
this.Write(fileName, image)
}
return len(files)
}
func (this *TextureAtlas) Get(name string) (result AtlasTexture) {
var rect, readResult = this.images[name]
if readResult {
result.Image = this.texture
result.Rect = rect
}
return
}
func (this *TextureAtlas) GetCurrentPos() Int2 {
return this.currentPos
}