mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 12:32:05 +01:00
restorable: Move 'putting random-colored dot' to restorable
This commit is contained in:
parent
810dc33b4a
commit
24e20306f2
@ -72,6 +72,20 @@ type Image struct {
|
|||||||
screen bool
|
screen bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type rand struct {
|
||||||
|
x, y, z, w uint32
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rand) next() uint32 {
|
||||||
|
// Xorshift: http://en.wikipedia.org/wiki/Xorshift
|
||||||
|
t := r.x ^ (r.x << 11)
|
||||||
|
r.x, r.y, r.z = r.y, r.z, r.w
|
||||||
|
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
|
||||||
|
return r.w
|
||||||
|
}
|
||||||
|
|
||||||
|
var theRand = &rand{12345678, 4185243, 776511, 45411}
|
||||||
|
|
||||||
// NewImage creates an empty image with the given size.
|
// NewImage creates an empty image with the given size.
|
||||||
func NewImage(width, height int, volatile bool) *Image {
|
func NewImage(width, height int, volatile bool) *Image {
|
||||||
i := &Image{
|
i := &Image{
|
||||||
@ -80,6 +94,17 @@ func NewImage(width, height int, volatile bool) *Image {
|
|||||||
}
|
}
|
||||||
theImages.add(i)
|
theImages.add(i)
|
||||||
runtime.SetFinalizer(i, (*Image).Dispose)
|
runtime.SetFinalizer(i, (*Image).Dispose)
|
||||||
|
|
||||||
|
// Put a random color pixel on newImg to make tests reliable
|
||||||
|
// (e.g. shareable_test.TestEnsureNotShared. This test might pass
|
||||||
|
// without this ReplacePixels even when the next DrawImage has a bug).
|
||||||
|
// This avoids to use remaining GPU memory state unexpectedly.
|
||||||
|
//
|
||||||
|
// TODO: Is it better clearing the image? How about the cost?
|
||||||
|
v := theRand.next()
|
||||||
|
r, g, b := uint8(v>>24), uint8(v>>16), uint8(v>>8)
|
||||||
|
i.ReplacePixels([]byte{r, g, b, 0xff}, 0, 0, 1, 1)
|
||||||
|
|
||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,20 +87,6 @@ type Image struct {
|
|||||||
node *packing.Node
|
node *packing.Node
|
||||||
}
|
}
|
||||||
|
|
||||||
type rand struct {
|
|
||||||
x, y, z, w uint32
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rand) next() uint32 {
|
|
||||||
// Xorshift: http://en.wikipedia.org/wiki/Xorshift
|
|
||||||
t := r.x ^ (r.x << 11)
|
|
||||||
r.x, r.y, r.z = r.y, r.z, r.w
|
|
||||||
r.w = (r.w ^ (r.w >> 19)) ^ (t ^ (t >> 8))
|
|
||||||
return r.w
|
|
||||||
}
|
|
||||||
|
|
||||||
var theRand = &rand{12345678, 4185243, 776511, 45411}
|
|
||||||
|
|
||||||
func (s *Image) ensureNotShared() {
|
func (s *Image) ensureNotShared() {
|
||||||
if s.node == nil {
|
if s.node == nil {
|
||||||
return
|
return
|
||||||
@ -108,16 +94,6 @@ func (s *Image) ensureNotShared() {
|
|||||||
|
|
||||||
x, y, w, h := s.region()
|
x, y, w, h := s.region()
|
||||||
newImg := restorable.NewImage(w, h, false)
|
newImg := restorable.NewImage(w, h, false)
|
||||||
|
|
||||||
// Put a random color pixel on newImg to make tests reliable.
|
|
||||||
//
|
|
||||||
// Of course this function works without this ReplacePixels,
|
|
||||||
// but the tests might pass without this even if there is a bug
|
|
||||||
// in DrawImage because of previous GPU memory state.
|
|
||||||
v := theRand.next()
|
|
||||||
r, g, b := uint8(v>>24), uint8(v>>16), uint8(v>>8)
|
|
||||||
newImg.ReplacePixels([]byte{r, g, b, 0xff}, 0, 0, 1, 1)
|
|
||||||
|
|
||||||
newImg.DrawImage(s.backend.restorable, x, y, x+w, y+h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
newImg.DrawImage(s.backend.restorable, x, y, x+w, y+h, nil, nil, opengl.CompositeModeCopy, graphics.FilterNearest)
|
||||||
|
|
||||||
s.dispose()
|
s.dispose()
|
||||||
|
Loading…
Reference in New Issue
Block a user