Introduce ImageDrawOption

This commit is contained in:
Hajime Hoshi 2014-12-24 22:46:00 +09:00
parent aee86f7e9c
commit b39df50979
5 changed files with 61 additions and 16 deletions

View File

@ -1,2 +1 @@
Hello, World!
hogehoge

View File

@ -52,5 +52,11 @@ func DrawImagePartsColor(target *Image, dsts []image.Rectangle, img *Image, srcs
}
func DrawImagePartsGeometryColor(target *Image, dsts []image.Rectangle, img *Image, srcs []image.Rectangle, geo GeometryMatrix, color ColorMatrix) error {
return target.DrawImage(dsts, img, srcs, geo, color)
op := &ImageDrawOption{
DstParts: dsts,
SrcParts: srcs,
GeometryMatrix: &geo,
ColorMatrix: &color,
}
return target.DrawImage(img, op)
}

View File

@ -60,7 +60,12 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
b := float64(c2.B) / max
a := float64(c2.A) / max
clr := ebiten.ScaleColor(r, g, b, a)
rt.DrawImage(dsts, fontImageId, srcs, geo, clr)
rt.DrawImage(fontImageId, &ebiten.ImageDrawOption{
DstParts: dsts,
SrcParts: srcs,
GeometryMatrix: &geo,
ColorMatrix: &clr,
})
}
func drawTextWithShadow(rt *ebiten.Image, images *Images, str string, x, y, scale int, clr color.Color) {

View File

@ -16,7 +16,6 @@ package ebiten
import (
"github.com/hajimehoshi/ebiten/internal/opengl"
"image"
)
func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
@ -69,10 +68,11 @@ func (c *graphicsContext) postUpdate() error {
scale := float64(c.screenScale)
geo := ScaleGeometry(scale, scale)
clr := ColorMatrixI()
w, h := c.screen.size()
dsts := []image.Rectangle{image.Rect(0, 0, w, h)}
srcs := []image.Rectangle{image.Rect(0, 0, w, h)}
if err := c.defaultR.drawImage(dsts, c.screen, srcs, geo, clr); err != nil {
option := &ImageDrawOption{
GeometryMatrix: &geo,
ColorMatrix: &clr,
}
if err := c.defaultR.drawImage(c.screen, option); err != nil {
return err
}

View File

@ -52,14 +52,42 @@ func (i *innerImage) Fill(clr color.Color) error {
return nil
}
func (i *innerImage) drawImage(dsts []image.Rectangle, image *innerImage, srcs []image.Rectangle, geo GeometryMatrix, color ColorMatrix) error {
func (i *innerImage) drawImage(img *innerImage, option *ImageDrawOption) error {
if option == nil {
option = &ImageDrawOption{}
}
dsts := option.DstParts
if dsts == nil {
w, h := img.size()
dsts = []image.Rectangle{
image.Rect(0, 0, w, h),
}
}
srcs := option.SrcParts
if srcs == nil {
w, h := img.size()
srcs = []image.Rectangle{
image.Rect(0, 0, w, h),
}
}
geo := option.GeometryMatrix
if geo == nil {
i := GeometryMatrixI()
geo = &i
}
clr := option.ColorMatrix
if clr == nil {
i := ColorMatrixI()
clr = &i
}
if err := i.framebuffer.SetAsViewport(); err != nil {
return err
}
w, h := image.texture.Size()
w, h := img.texture.Size()
quads := textureQuads(dsts, srcs, w, h)
projectionMatrix := i.framebuffer.ProjectionMatrix()
shader.DrawTexture(image.texture.Native(), projectionMatrix, quads, &geo, &color)
shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr)
return nil
}
@ -134,14 +162,14 @@ func (i *Image) Fill(clr color.Color) (err error) {
// After determining parts to draw, this applies the geometry matrix geo and the color matrix color.
//
// If you want to draw a whole image simply, use DrawWholeImage.
func (i *Image) DrawImage(dsts []image.Rectangle, image *Image, srcs []image.Rectangle, geo GeometryMatrix, color ColorMatrix) (err error) {
return i.drawImage(dsts, image.inner, srcs, geo, color)
func (i *Image) DrawImage(image *Image, option *ImageDrawOption) (err error) {
return i.drawImage(image.inner, option)
}
func (i *Image) drawImage(dsts []image.Rectangle, image *innerImage, srcs []image.Rectangle, geo GeometryMatrix, color ColorMatrix) (err error) {
func (i *Image) drawImage(image *innerImage, option *ImageDrawOption) (err error) {
i.pixels = nil
i.syncer.Sync(func() {
err = i.inner.drawImage(dsts, image, srcs, geo, color)
err = i.inner.drawImage(image, option)
})
return
}
@ -176,3 +204,10 @@ func (i *Image) At(x, y int) color.Color {
r, g, b, a := i.pixels[idx], i.pixels[idx+1], i.pixels[idx+2], i.pixels[idx+3]
return color.RGBA{r, g, b, a}
}
type ImageDrawOption struct {
DstParts []image.Rectangle
SrcParts []image.Rectangle
GeometryMatrix *GeometryMatrix
ColorMatrix *ColorMatrix
}