forked from AllenDang/giu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTexture.go
52 lines (44 loc) · 1.08 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
package giu
import (
"errors"
"github.com/faiface/mainthread"
"image"
"runtime"
"github.com/AllenDang/imgui-go"
)
type Texture struct {
id imgui.TextureID
}
type loadImageResult struct {
id imgui.TextureID
err error
}
// Create new texture from rgba.
// Note: this function has to be invokded in a go routine.
// If call this in mainthread will result in stuck.
func NewTextureFromRgba(rgba *image.RGBA) (*Texture, error) {
Update()
result := mainthread.CallVal(func() interface{} {
texId, err := Context.renderer.LoadImage(rgba)
return &loadImageResult{id: texId, err: err}
})
if tid, ok := result.(*loadImageResult); ok {
texture := Texture{id: tid.id}
if tid.err == nil {
// Set finalizer
runtime.SetFinalizer(&texture, (*Texture).release)
}
return &texture, tid.err
}
return nil, errors.New("Unknown error occurred")
}
// 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)
})
}