-
-
Notifications
You must be signed in to change notification settings - Fork 135
/
Texture.go
60 lines (48 loc) · 1.51 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
package giu
import (
"image"
"runtime"
"github.com/AllenDang/cimgui-go/backend"
"github.com/AllenDang/cimgui-go/imgui"
)
// Texture represents imgui.TextureID.
// It is base unit of images in imgui.
type Texture struct {
tex *backend.Texture
}
type textureLoadRequest struct {
img image.Image
cb func(*Texture)
}
type textureFreeRequest struct {
tex *Texture
}
// EnqueueNewTextureFromRgba adds loading texture request to loading queue
// it allows us to run this method in main loop
// NOTE: remember to call it after NewMasterWindow!
func EnqueueNewTextureFromRgba(rgba image.Image, loadCb func(t *Texture)) {
Assert((Context.textureLoadingQueue != nil), "", "EnqueueNewTextureFromRgba", "you need to call EnqueueNewTextureFromRgba after giu.NewMasterWindow call!")
Context.textureLoadingQueue.Add(textureLoadRequest{rgba, loadCb})
}
// NewTextureFromRgba creates a new texture from image.Image and, when it is done, calls loadCallback(loadedTexture).
func NewTextureFromRgba(rgba image.Image, loadCallback func(*Texture)) {
tex := backend.NewTextureFromRgba(ImageToRgba(rgba))
giuTex := &Texture{
tex,
}
runtime.SetFinalizer(giuTex, func(tex *Texture) {
Context.textureFreeingQueue.Add(textureFreeRequest{tex})
})
loadCallback(giuTex)
}
// ToTexture converts backend.Texture to Texture.
func ToTexture(texture *backend.Texture) *Texture {
return &Texture{tex: texture}
}
// ID returns imgui.TextureID of the texture.
func (t *Texture) ID() imgui.TextureID {
if t.tex != nil {
return t.tex.ID
}
return 0
}