Skip to content

Commit

Permalink
Merge pull request #354 from gucio321/texture-releasing
Browse files Browse the repository at this point in the history
backend/texture: change default texture impl
  • Loading branch information
gucio321 authored Oct 20, 2024
2 parents 1d3efee + 07c7063 commit 34d1e6e
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions backend/texture.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ import (
_ "image/png"
"os"
"path/filepath"
"runtime"

"github.com/AllenDang/cimgui-go/imgui"
)

// Texture implements a simple texture loader. It wraps backend's methods to allow creating textures easily.
// IMPORTANT: as the texture is mainly handled by C OpenGL, it is not covered by Garbae Collector (GC).
//
// Remember to call (*Texture).Release when you no longer need it.
type Texture struct {
ID imgui.TextureID
Width int
Expand All @@ -28,13 +31,16 @@ func NewTextureFromRgba(rgba *image.RGBA) *Texture {
Height: rgba.Bounds().Dy(),
}

// Set finalizer
runtime.SetFinalizer(&texture, (*Texture).release)
// I leav it for documentation here:
// GC runs in a separated thread so this may not work correctly (will crash opengl)
// runtime.SetFinalizer(&texture, (*Texture).release)

return &texture
}

func (t *Texture) release() {
// Release tells OpenGL that this texture is no longer needed.
// ATTENTION: This will not be automatically handled by GC so remember to do it manually if you have many textures!
func (t *Texture) Release() {
textureManager.DeleteTexture(t.ID)
}

Expand Down

0 comments on commit 34d1e6e

Please sign in to comment.