mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 12:08:58 +01:00
shareable: Avoid unneeded extending images
This commit is contained in:
parent
d8ba49eaab
commit
ce4e00ff79
@ -48,8 +48,8 @@ type Node struct {
|
|||||||
y int
|
y int
|
||||||
width int
|
width int
|
||||||
height int
|
height int
|
||||||
|
|
||||||
used bool
|
used bool
|
||||||
|
|
||||||
parent *Node
|
parent *Node
|
||||||
child0 *Node
|
child0 *Node
|
||||||
child1 *Node
|
child1 *Node
|
||||||
@ -268,3 +268,33 @@ func (p *Page) Extend() bool {
|
|||||||
p.size = newSize
|
p.size = newSize
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (n *Node) clone() *Node {
|
||||||
|
if n == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
cloned := &Node{
|
||||||
|
x: n.x,
|
||||||
|
y: n.y,
|
||||||
|
width: n.width,
|
||||||
|
height: n.height,
|
||||||
|
used: n.used,
|
||||||
|
child0: n.child0.clone(),
|
||||||
|
child1: n.child1.clone(),
|
||||||
|
}
|
||||||
|
if cloned.child0 != nil {
|
||||||
|
cloned.child0.parent = cloned
|
||||||
|
}
|
||||||
|
if cloned.child1 != nil {
|
||||||
|
cloned.child1.parent = cloned
|
||||||
|
}
|
||||||
|
return cloned
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Page) Clone() *Page {
|
||||||
|
return &Page{
|
||||||
|
root: p.root.clone(),
|
||||||
|
size: p.size,
|
||||||
|
maxSize: p.maxSize,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -35,22 +35,39 @@ type backend struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
|
func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
|
||||||
|
// If the region is allocated without any extention, it's fine.
|
||||||
|
if n := b.page.Alloc(width, height); n != nil {
|
||||||
|
return n, true
|
||||||
|
}
|
||||||
|
|
||||||
|
// Simulate the extending the page and calculate the appropriate page size.
|
||||||
|
page := b.page.Clone()
|
||||||
|
nExtended := 0
|
||||||
for {
|
for {
|
||||||
if n := b.page.Alloc(width, height); n != nil {
|
if !page.Extend() {
|
||||||
return n, true
|
// The page can't be extended any more. Return as failure.
|
||||||
|
return nil, false
|
||||||
}
|
}
|
||||||
if !b.page.Extend() {
|
nExtended++
|
||||||
|
if n := page.Alloc(width, height); n != nil {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
s := b.page.Size()
|
|
||||||
newImg := restorable.NewImage(s, s, false)
|
|
||||||
newImg.DrawImage(b.restorable, 0, 0, s, s, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
|
||||||
|
|
||||||
b.restorable.Dispose()
|
|
||||||
b.restorable = newImg
|
|
||||||
}
|
}
|
||||||
return nil, false
|
|
||||||
|
for i := 0; i < nExtended; i++ {
|
||||||
|
b.page.Extend()
|
||||||
|
}
|
||||||
|
s := b.page.Size()
|
||||||
|
newImg := restorable.NewImage(s, s, false)
|
||||||
|
newImg.DrawImage(b.restorable, 0, 0, s, s, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
||||||
|
b.restorable.Dispose()
|
||||||
|
b.restorable = newImg
|
||||||
|
|
||||||
|
n := b.page.Alloc(width, height)
|
||||||
|
if n == nil {
|
||||||
|
panic("not reached")
|
||||||
|
}
|
||||||
|
return n, true
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
Loading…
Reference in New Issue
Block a user