forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Texture.go
58 lines (47 loc) · 1.24 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
package giu
import (
"fmt"
"image"
"runtime"
"github.com/HACKERALERT/imgui-go"
"github.com/HACKERALERT/mainthread"
)
type Texture struct {
id imgui.TextureID
}
type loadImageResult struct {
id imgui.TextureID
err error
}
// NewTextureFromRgba creates a new texture from image.Image and, when it is done, calls loadCallback(loadedTexture).
func NewTextureFromRgba(rgba image.Image, loadCallback func(*Texture)) {
go func() {
Update()
result := mainthread.CallVal(func() interface{} {
texID, err := Context.renderer.LoadImage(ImageToRgba(rgba))
return &loadImageResult{id: texID, err: err}
})
tid, ok := result.(*loadImageResult)
switch {
case !ok:
panic("giu: NewTextureFromRgba: unexpected error occurred")
case tid.err != nil:
panic(fmt.Sprintf("giu: NewTextureFromRgba: error loading texture: %v", tid.err))
}
texture := Texture{id: tid.id}
// Set finalizer
runtime.SetFinalizer(&texture, (*Texture).release)
// execute callback
loadCallback(&texture)
}()
}
// ToTexture converts imgui.TextureID to Texture.
func ToTexture(textureID imgui.TextureID) *Texture {
return &Texture{id: textureID}
}
func (t *Texture) release() {
Update()
mainthread.Call(func() {
Context.renderer.ReleaseImage(t.id)
})
}