mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-26 03:38:55 +01:00
restorable: Refactoring: Make (*Image).Size() available even after disposing
This commit is contained in:
parent
6ba6cdc721
commit
ccacc4a0b8
@ -101,11 +101,6 @@ func (i *Image) Dispose() {
|
|||||||
theCommandQueue.Enqueue(c)
|
theCommandQueue.Enqueue(c)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Image) Size() (int, int) {
|
|
||||||
// i.image can be nil before initializing.
|
|
||||||
return i.width, i.height
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Image) InternalSize() (int, int) {
|
func (i *Image) InternalSize() (int, int) {
|
||||||
return i.internalWidth, i.internalHeight
|
return i.internalWidth, i.internalHeight
|
||||||
}
|
}
|
||||||
|
@ -75,6 +75,9 @@ type drawTrianglesHistoryItem struct {
|
|||||||
type Image struct {
|
type Image struct {
|
||||||
image *graphicscommand.Image
|
image *graphicscommand.Image
|
||||||
|
|
||||||
|
width int
|
||||||
|
height int
|
||||||
|
|
||||||
basePixels Pixels
|
basePixels Pixels
|
||||||
|
|
||||||
// drawTrianglesHistory is a set of draw-image commands.
|
// drawTrianglesHistory is a set of draw-image commands.
|
||||||
@ -100,6 +103,8 @@ func init() {
|
|||||||
const w, h = 16, 16
|
const w, h = 16, 16
|
||||||
emptyImage = &Image{
|
emptyImage = &Image{
|
||||||
image: graphicscommand.NewImage(w, h),
|
image: graphicscommand.NewImage(w, h),
|
||||||
|
width: w,
|
||||||
|
height: h,
|
||||||
priority: true,
|
priority: true,
|
||||||
}
|
}
|
||||||
pix := make([]byte, 4*w*h)
|
pix := make([]byte, 4*w*h)
|
||||||
@ -121,6 +126,8 @@ func init() {
|
|||||||
func NewImage(width, height int) *Image {
|
func NewImage(width, height int) *Image {
|
||||||
i := &Image{
|
i := &Image{
|
||||||
image: graphicscommand.NewImage(width, height),
|
image: graphicscommand.NewImage(width, height),
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
}
|
}
|
||||||
i.clear()
|
i.clear()
|
||||||
theImages.add(i)
|
theImages.add(i)
|
||||||
@ -172,6 +179,8 @@ func (i *Image) MakeVolatile() {
|
|||||||
func NewScreenFramebufferImage(width, height int) *Image {
|
func NewScreenFramebufferImage(width, height int) *Image {
|
||||||
i := &Image{
|
i := &Image{
|
||||||
image: graphicscommand.NewScreenFramebufferImage(width, height),
|
image: graphicscommand.NewScreenFramebufferImage(width, height),
|
||||||
|
width: width,
|
||||||
|
height: height,
|
||||||
screen: true,
|
screen: true,
|
||||||
}
|
}
|
||||||
i.clear()
|
i.clear()
|
||||||
@ -238,7 +247,8 @@ func (i *Image) BasePixelsForTesting() *Pixels {
|
|||||||
|
|
||||||
// Size returns the image's size.
|
// Size returns the image's size.
|
||||||
func (i *Image) Size() (int, int) {
|
func (i *Image) Size() (int, int) {
|
||||||
return i.image.Size()
|
// Do not acccess i.image since i.image can be nil after disposing.
|
||||||
|
return i.width, i.height
|
||||||
}
|
}
|
||||||
|
|
||||||
// internalSize returns the size of the internal texture.
|
// internalSize returns the size of the internal texture.
|
||||||
@ -290,7 +300,7 @@ func (i *Image) ClearPixels(x, y, width, height int) {
|
|||||||
//
|
//
|
||||||
// ReplacePixels for a part is forbidden if the image is rendered with DrawTriangles or Fill.
|
// ReplacePixels for a part is forbidden if the image is rendered with DrawTriangles or Fill.
|
||||||
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
|
func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
|
||||||
w, h := i.image.Size()
|
w, h := i.Size()
|
||||||
if width <= 0 || height <= 0 {
|
if width <= 0 || height <= 0 {
|
||||||
panic("restorable: width/height must be positive")
|
panic("restorable: width/height must be positive")
|
||||||
}
|
}
|
||||||
@ -397,7 +407,7 @@ func (i *Image) readPixelsFromGPUIfNeeded() {
|
|||||||
//
|
//
|
||||||
// Note that this must not be called until context is available.
|
// Note that this must not be called until context is available.
|
||||||
func (i *Image) At(x, y int) (byte, byte, byte, byte) {
|
func (i *Image) At(x, y int) (byte, byte, byte, byte) {
|
||||||
w, h := i.image.Size()
|
w, h := i.Size()
|
||||||
if x < 0 || y < 0 || w <= x || h <= y {
|
if x < 0 || y < 0 || w <= x || h <= y {
|
||||||
return 0, 0, 0, 0
|
return 0, 0, 0, 0
|
||||||
}
|
}
|
||||||
@ -472,20 +482,21 @@ func (i *Image) hasDependency() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Restore restores *graphicscommand.Image from the pixels using its state.
|
// Restore restores *graphicscommand.Image from the pixels using its state.
|
||||||
func (i *Image) restore(width, height int) {
|
func (i *Image) restore() {
|
||||||
|
w, h := i.Size()
|
||||||
// Do not dispose the image here. The image should be already disposed.
|
// Do not dispose the image here. The image should be already disposed.
|
||||||
|
|
||||||
if i.screen {
|
if i.screen {
|
||||||
// The screen image should also be recreated because framebuffer might
|
// The screen image should also be recreated because framebuffer might
|
||||||
// be changed.
|
// be changed.
|
||||||
i.image = graphicscommand.NewScreenFramebufferImage(width, height)
|
i.image = graphicscommand.NewScreenFramebufferImage(w, h)
|
||||||
i.basePixels = Pixels{}
|
i.basePixels = Pixels{}
|
||||||
i.drawTrianglesHistory = nil
|
i.drawTrianglesHistory = nil
|
||||||
i.stale = false
|
i.stale = false
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if i.volatile {
|
if i.volatile {
|
||||||
i.image = graphicscommand.NewImage(width, height)
|
i.image = graphicscommand.NewImage(w, h)
|
||||||
i.clear()
|
i.clear()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -493,7 +504,7 @@ func (i *Image) restore(width, height int) {
|
|||||||
panic("restorable: pixels must not be stale when restoring")
|
panic("restorable: pixels must not be stale when restoring")
|
||||||
}
|
}
|
||||||
|
|
||||||
gimg := graphicscommand.NewImage(width, height)
|
gimg := graphicscommand.NewImage(w, h)
|
||||||
// Clear the image explicitly.
|
// Clear the image explicitly.
|
||||||
if i != emptyImage {
|
if i != emptyImage {
|
||||||
// As clearImage uses emptyImage, clearImage cannot be called on emptyImage.
|
// As clearImage uses emptyImage, clearImage cannot be called on emptyImage.
|
||||||
@ -511,7 +522,7 @@ func (i *Image) restore(width, height int) {
|
|||||||
|
|
||||||
if len(i.drawTrianglesHistory) > 0 {
|
if len(i.drawTrianglesHistory) > 0 {
|
||||||
i.basePixels = Pixels{}
|
i.basePixels = Pixels{}
|
||||||
i.basePixels.AddOrReplace(gimg.Pixels(), 0, 0, width, height)
|
i.basePixels.AddOrReplace(gimg.Pixels(), 0, 0, w, h)
|
||||||
}
|
}
|
||||||
|
|
||||||
i.image = gimg
|
i.image = gimg
|
||||||
|
@ -154,11 +154,7 @@ func (i *images) restore() {
|
|||||||
|
|
||||||
// Dispose all the images ahead of restoring. A current texture ID and a new texture ID can be duplicated.
|
// Dispose all the images ahead of restoring. A current texture ID and a new texture ID can be duplicated.
|
||||||
// TODO: Write a test to confirm that ID duplication never happens.
|
// TODO: Write a test to confirm that ID duplication never happens.
|
||||||
sizes := map[*Image]struct{ w, h int }{}
|
|
||||||
for i := range i.images {
|
for i := range i.images {
|
||||||
// Keep the size before disposing i.image.
|
|
||||||
w, h := i.Size()
|
|
||||||
sizes[i] = struct{ w, h int }{w, h}
|
|
||||||
i.image.Dispose()
|
i.image.Dispose()
|
||||||
i.image = nil
|
i.image = nil
|
||||||
}
|
}
|
||||||
@ -215,8 +211,7 @@ func (i *images) restore() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, img := range sorted {
|
for _, img := range sorted {
|
||||||
s := sizes[img]
|
img.restore()
|
||||||
img.restore(s.w, s.h)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user