mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 11:18:54 +01:00
graphics: Add imageForPixels
This commit is contained in:
parent
8d074ecc20
commit
b3aa567f38
@ -51,6 +51,7 @@ func newImageImpl(width, height int, filter Filter, volatile bool) (*imageImpl,
|
||||
filter: filter,
|
||||
volatile: volatile,
|
||||
}
|
||||
i.pixels.image = i.image
|
||||
i.pixels.resetWithPixels(make([]uint8, width*height*4))
|
||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||
return i, nil
|
||||
@ -83,6 +84,7 @@ func newImageImplFromImage(source image.Image, filter Filter) (*imageImpl, error
|
||||
height: h,
|
||||
filter: filter,
|
||||
}
|
||||
i.pixels.image = i.image
|
||||
i.pixels.resetWithPixels(pixels)
|
||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||
return i, nil
|
||||
@ -100,6 +102,7 @@ func newScreenImageImpl(width, height int) (*imageImpl, error) {
|
||||
volatile: true,
|
||||
screen: true,
|
||||
}
|
||||
i.pixels.image = i.image
|
||||
i.pixels.resetWithPixels(make([]uint8, width*height*4))
|
||||
runtime.SetFinalizer(i, (*imageImpl).Dispose)
|
||||
return i, nil
|
||||
@ -192,7 +195,7 @@ func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color {
|
||||
return color.Transparent
|
||||
}
|
||||
idx := 4*x + 4*y*i.width
|
||||
clr, err := i.pixels.at(i.image, idx, context)
|
||||
clr, err := i.pixels.at(idx, context)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
@ -205,7 +208,7 @@ func (i *imageImpl) flushPixelsIfInconsistent(context *opengl.Context) error {
|
||||
if i.disposed {
|
||||
return nil
|
||||
}
|
||||
if err := i.pixels.flushIfInconsistent(i.image, context); err != nil {
|
||||
if err := i.pixels.flushIfInconsistent(context); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
@ -217,7 +220,7 @@ func (i *imageImpl) flushPixelsIfNeeded(target *Image, context *opengl.Context)
|
||||
if i.disposed {
|
||||
return nil
|
||||
}
|
||||
if err := i.pixels.flushIfNeeded(i.image, target, context); err != nil {
|
||||
if err := i.pixels.flushIfNeeded(target, context); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
17
pixels.go
17
pixels.go
@ -30,9 +30,14 @@ type drawImageHistoryItem struct {
|
||||
mode opengl.CompositeMode
|
||||
}
|
||||
|
||||
type imageForPixels interface {
|
||||
Pixels(context *opengl.Context) ([]uint8, error)
|
||||
}
|
||||
|
||||
// basePixels and baseColor are exclusive.
|
||||
|
||||
type pixels struct {
|
||||
image imageForPixels
|
||||
inconsistent bool
|
||||
basePixels []uint8
|
||||
baseColor color.Color
|
||||
@ -80,11 +85,11 @@ func (p *pixels) appendDrawImageHistory(item *drawImageHistoryItem) {
|
||||
p.drawImageHistory = append(p.drawImageHistory, item)
|
||||
}
|
||||
|
||||
func (p *pixels) at(image *graphics.Image, idx int, context *opengl.Context) (color.Color, error) {
|
||||
func (p *pixels) at(idx int, context *opengl.Context) (color.Color, error) {
|
||||
if p.inconsistent || p.basePixels == nil || p.drawImageHistory != nil {
|
||||
p.inconsistent = false
|
||||
var err error
|
||||
p.basePixels, err = image.Pixels(context)
|
||||
p.basePixels, err = p.image.Pixels(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -104,13 +109,13 @@ func (p *pixels) hasHistoryWith(target *Image) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (p *pixels) flushIfInconsistent(image *graphics.Image, context *opengl.Context) error {
|
||||
func (p *pixels) flushIfInconsistent(context *opengl.Context) error {
|
||||
if !p.inconsistent {
|
||||
return nil
|
||||
}
|
||||
p.inconsistent = false
|
||||
var err error
|
||||
p.basePixels, err = image.Pixels(context)
|
||||
p.basePixels, err = p.image.Pixels(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -119,7 +124,7 @@ func (p *pixels) flushIfInconsistent(image *graphics.Image, context *opengl.Cont
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *pixels) flushIfNeeded(image *graphics.Image, target *Image, context *opengl.Context) error {
|
||||
func (p *pixels) flushIfNeeded(target *Image, context *opengl.Context) error {
|
||||
if p.drawImageHistory == nil {
|
||||
return nil
|
||||
}
|
||||
@ -132,7 +137,7 @@ func (p *pixels) flushIfNeeded(image *graphics.Image, target *Image, context *op
|
||||
}
|
||||
p.inconsistent = false
|
||||
var err error
|
||||
p.basePixels, err = image.Pixels(context)
|
||||
p.basePixels, err = p.image.Pixels(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user