From 98dfa2cce5d246b1ef431ad6c5f262bb709950b6 Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Fri, 8 Dec 2023 19:50:24 -0800 Subject: [PATCH] Image: add ReplaceFromImage and Destroy to cleanup textures --- image.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/image.go b/image.go index 6aff98b..2931c5b 100644 --- a/image.go +++ b/image.go @@ -81,6 +81,19 @@ func ImageFromFile(filename string) (*Image, error) { }, nil } +// ReplaceFromImage replaces the image with a new image. +func (w *Image) ReplaceFromImage(im image.Image) error { + // Free the old texture. + if w.texture != nil { + if err := w.texture.Free(); err != nil { + return err + } + w.texture = nil + } + w.Image = im + return nil +} + // OpenImage initializes an Image with a given file name. // // The file extension is important and should be a supported ImageType. @@ -188,3 +201,11 @@ func (w *Image) Present(e render.Engine, p render.Point) { // Call the BaseWidget Present in case we have subscribers. w.BaseWidget.Present(e, p) } + +// Destroy cleans up the image and releases textures. +func (w *Image) Destroy() { + if w.texture != nil { + w.texture.Free() + w.texture = nil + } +}