Remove innerImage

This commit is contained in:
Hajime Hoshi 2015-01-12 19:54:25 +09:00
parent 21455a146d
commit 01dbc515b5
4 changed files with 60 additions and 90 deletions

View File

@ -70,37 +70,49 @@ func IsGamepadButtonPressed(id int, button GamepadButton) bool {
// NewImage returns an empty image.
func NewImage(width, height int, filter Filter) (*Image, error) {
var innerImage *innerImage
var img *Image
var err error
ui.Use(func(c *opengl.Context) {
var texture *graphics.Texture
var framebuffer *graphics.Framebuffer
texture, err = graphics.NewTexture(c, width, height, glFilter(c, filter))
if err != nil {
return
}
innerImage, err = newInnerImage(c, texture)
innerImage.Clear(c)
framebuffer, err = graphics.NewFramebufferFromTexture(c, texture)
if err != nil {
return
}
img = &Image{framebuffer: framebuffer, texture: texture}
})
if err != nil {
return nil, err
}
return &Image{inner: innerImage}, nil
if err := img.Clear(); err != nil {
return nil, err
}
return img, nil
}
// NewImageFromImage creates a new image with the given image (img).
func NewImageFromImage(img image.Image, filter Filter) (*Image, error) {
var innerImage *innerImage
var eimg *Image
var err error
ui.Use(func(c *opengl.Context) {
var texture *graphics.Texture
var framebuffer *graphics.Framebuffer
texture, err = graphics.NewTextureFromImage(c, img, glFilter(c, filter))
if err != nil {
return
}
innerImage, err = newInnerImage(c, texture)
framebuffer, err = graphics.NewFramebufferFromTexture(c, texture)
if err != nil {
return
}
eimg = &Image{framebuffer: framebuffer, texture: texture}
})
if err != nil {
return nil, err
}
return &Image{inner: innerImage}, nil
return eimg, nil
}

View File

@ -29,14 +29,14 @@ func newGraphicsContext(c *opengl.Context, screenWidth, screenHeight, screenScal
if err != nil {
return nil, err
}
screen, err := newInnerImage(c, texture)
screenF, err := graphics.NewFramebufferFromTexture(c, texture)
if err != nil {
return nil, err
}
screen := &Image{framebuffer: screenF, texture: texture}
return &graphicsContext{
glContext: c,
defaultR: &innerImage{f, nil},
defaultR: &Image{framebuffer: f, texture: nil},
screen: screen,
screenScale: screenScale,
}, nil
@ -44,8 +44,8 @@ func newGraphicsContext(c *opengl.Context, screenWidth, screenHeight, screenScal
type graphicsContext struct {
glContext *opengl.Context
screen *innerImage
defaultR *innerImage
screen *Image
defaultR *Image
screenScale int
}
@ -59,18 +59,18 @@ func (c *graphicsContext) dispose() {
}
func (c *graphicsContext) preUpdate() error {
return c.screen.Clear(c.glContext)
return c.screen.Clear()
}
func (c *graphicsContext) postUpdate() error {
if err := c.defaultR.Clear(c.glContext); err != nil {
if err := c.defaultR.Clear(); err != nil {
return err
}
scale := float64(c.screenScale)
options := &DrawImageOptions{}
options.GeoM.Scale(scale, scale)
if err := c.defaultR.drawImage(c.glContext, c.screen, options); err != nil {
if err := c.defaultR.DrawImage(c.screen, options); err != nil {
return err
}
return nil

View File

@ -24,79 +24,31 @@ import (
"image/color"
)
type innerImage struct {
framebuffer *graphics.Framebuffer
texture *graphics.Texture
}
func newInnerImage(c *opengl.Context, texture *graphics.Texture) (*innerImage, error) {
framebuffer, err := graphics.NewFramebufferFromTexture(c, texture)
if err != nil {
return nil, err
}
return &innerImage{framebuffer, texture}, nil
}
func (i *innerImage) size() (width, height int) {
return i.framebuffer.Size()
}
func (i *innerImage) Clear(c *opengl.Context) error {
return i.Fill(c, color.Transparent)
}
func (i *innerImage) Fill(c *opengl.Context, clr color.Color) error {
r, g, b, a := internal.RGBA(clr)
return i.framebuffer.Fill(c, r, g, b, a)
}
func (i *innerImage) drawImage(c *opengl.Context, img *innerImage, options *DrawImageOptions) error {
if options == nil {
options = &DrawImageOptions{}
}
parts := options.ImageParts
if parts == nil {
// Check options.Parts for backward-compatibility.
dparts := options.Parts
if dparts != nil {
parts = imageParts(dparts)
} else {
w, h := img.size()
parts = &wholeImage{w, h}
}
}
w, h := img.size()
quads := &textureQuads{parts: parts, width: w, height: h}
return i.framebuffer.DrawTexture(c, img.texture, quads, &options.GeoM, &options.ColorM)
}
// Image represents an image.
// The pixel format is alpha-premultiplied.
// Image implements image.Image.
type Image struct {
inner *innerImage
pixels []uint8
framebuffer *graphics.Framebuffer
texture *graphics.Texture
pixels []uint8
}
// Size returns the size of the image.
func (i *Image) Size() (width, height int) {
return i.inner.size()
return i.framebuffer.Size()
}
// Clear resets the pixels of the image into 0.
func (i *Image) Clear() (err error) {
i.pixels = nil
ui.Use(func(c *opengl.Context) {
err = i.inner.Clear(c)
})
return
return i.Fill(color.Transparent)
}
// Fill fills the image with a solid color.
func (i *Image) Fill(clr color.Color) (err error) {
i.pixels = nil
r, g, b, a := internal.RGBA(clr)
ui.Use(func(c *opengl.Context) {
err = i.inner.Fill(c, clr)
err = i.framebuffer.Fill(c, r, g, b, a)
})
return
}
@ -119,20 +71,32 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
if i == image {
return errors.New("Image.DrawImage: image should be different from the receiver")
}
return i.drawImage(image.inner, options)
}
func (i *Image) drawImage(image *innerImage, option *DrawImageOptions) (err error) {
i.pixels = nil
if options == nil {
options = &DrawImageOptions{}
}
parts := options.ImageParts
if parts == nil {
// Check options.Parts for backward-compatibility.
dparts := options.Parts
if dparts != nil {
parts = imageParts(dparts)
} else {
w, h := image.Size()
parts = &wholeImage{w, h}
}
}
w, h := image.Size()
quads := &textureQuads{parts: parts, width: w, height: h}
ui.Use(func(c *opengl.Context) {
err = i.inner.drawImage(c, image, option)
err = i.framebuffer.DrawTexture(c, image.texture, quads, &options.GeoM, &options.ColorM)
})
return
}
// Bounds returns the bounds of the image.
func (i *Image) Bounds() image.Rectangle {
w, h := i.inner.size()
w, h := i.Size()
return image.Rect(0, 0, w, h)
}
@ -148,13 +112,13 @@ func (i *Image) At(x, y int) color.Color {
if i.pixels == nil {
ui.Use(func(c *opengl.Context) {
var err error
i.pixels, err = i.inner.texture.Pixels(c)
i.pixels, err = i.texture.Pixels(c)
if err != nil {
panic(err)
}
})
}
w, _ := i.inner.size()
w, _ := i.Size()
w = internal.NextPowerOf2Int(w)
idx := 4*x + 4*y*w
r, g, b, a := i.pixels[idx], i.pixels[idx+1], i.pixels[idx+2], i.pixels[idx+3]

18
run.go
View File

@ -60,22 +60,16 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
if ui.IsClosed() {
return nil
}
ui.Use(func(*opengl.Context) {
err = graphicsContext.preUpdate()
})
if err != nil {
if err := graphicsContext.preUpdate(); err != nil {
return err
}
if err := f(&Image{inner: graphicsContext.screen}); err != nil {
if err := f(graphicsContext.screen); err != nil {
return err
}
ui.Use(func(*opengl.Context) {
err = graphicsContext.postUpdate()
if err != nil {
return
}
ui.SwapBuffers()
})
if err := graphicsContext.postUpdate(); err != nil {
return err
}
ui.SwapBuffers()
if err != nil {
return err
}