image: Add image.Dipose and image.IsDisposed

This commit is contained in:
Hajime Hoshi 2016-02-07 01:27:55 +09:00
parent d1ff3701a6
commit e8c2e5f3d6
2 changed files with 17 additions and 9 deletions

View File

@ -61,10 +61,10 @@ func (c *graphicsContext) postUpdate() error {
func (c *graphicsContext) setSize(screenWidth, screenHeight, screenScale int) error {
if c.defaultRenderTarget != nil {
c.defaultRenderTarget.dispose()
c.defaultRenderTarget.Dispose()
}
if c.screen != nil {
c.screen.dispose()
c.screen.Dispose()
}
var err error

View File

@ -128,18 +128,26 @@ func (i *Image) At(x, y int) color.Color {
return color.RGBA{r, g, b, a}
}
func (i *Image) dispose() {
// Dispose disposes the image data. After disposing, the image becomes invalid.
// This is useful to save memory.
func (i *Image) Dispose() {
if i.isDisposed() {
panic("the image is already disposed")
}
useGLContext(func(c *opengl.Context) {
if i.framebuffer != nil {
i.framebuffer.Dispose(c)
}
if i.texture != nil {
i.texture.Dispose(c)
}
i.framebuffer.Dispose(c)
i.framebuffer = nil
i.texture.Dispose(c)
i.texture = nil
})
i.pixels = nil
}
// IsDisposed returns a boolean indicating wheather the image is disposed.
func (i *Image) IsDisposed() bool {
return i.texture == nil
}
// ReplacePixels replaces the pixels of the image with p.
//
// The given p must represent RGBA pre-multiplied alpha values. len(p) must equal to 4 * (image width) * (image height).