Introduce ImagePart again

This commit is contained in:
Hajime Hoshi 2014-12-27 17:37:31 +09:00
parent bf46451933
commit 325e05259a
6 changed files with 52 additions and 51 deletions

View File

@ -35,7 +35,7 @@ func DebugPrint(r *ebiten.Image, str string) {
} }
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) { func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) {
dsts, srcs := []image.Rectangle{}, []image.Rectangle{} parts := []ebiten.ImagePart{}
locationX, locationY := 0, 0 locationX, locationY := 0, 0
for _, c := range str { for _, c := range str {
if c == '\n' { if c == '\n' {
@ -49,8 +49,7 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
srcY := (code / xCharNum) * assets.TextImageCharHeight srcY := (code / xCharNum) * assets.TextImageCharHeight
dst := image.Rect(locationX, locationY, locationX+assets.TextImageCharWidth, locationY+assets.TextImageCharHeight) dst := image.Rect(locationX, locationY, locationX+assets.TextImageCharWidth, locationY+assets.TextImageCharHeight)
src := image.Rect(srcX, srcY, srcX+assets.TextImageCharWidth, srcY+assets.TextImageCharHeight) src := image.Rect(srcX, srcY, srcX+assets.TextImageCharWidth, srcY+assets.TextImageCharHeight)
dsts = append(dsts, dst) parts = append(parts, ebiten.ImagePart{Dst: dst, Src: src})
srcs = append(srcs, src)
locationX += assets.TextImageCharWidth locationX += assets.TextImageCharWidth
} }
cc := color.NRGBA64Model.Convert(c).(color.NRGBA64) cc := color.NRGBA64Model.Convert(c).(color.NRGBA64)
@ -59,10 +58,9 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
b := float64(cc.B) / math.MaxUint16 b := float64(cc.B) / math.MaxUint16
a := float64(cc.A) / math.MaxUint16 a := float64(cc.A) / math.MaxUint16
rt.DrawImage(d.textImage, &ebiten.DrawImageOptions{ rt.DrawImage(d.textImage, &ebiten.DrawImageOptions{
DstParts: dsts, Parts: parts,
SrcParts: srcs, GeoM: ebiten.TranslateGeo(float64(x+1), float64(y)),
GeoM: ebiten.TranslateGeo(float64(x+1), float64(y)), ColorM: ebiten.ScaleColor(r, g, b, a),
ColorM: ebiten.ScaleColor(r, g, b, a),
}) })
} }

View File

@ -34,7 +34,7 @@ func textWidth(str string) int {
func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c color.Color) { func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c color.Color) {
fontImageId := images.GetImage("font") fontImageId := images.GetImage("font")
dsts, srcs := []image.Rectangle{}, []image.Rectangle{} parts := []ebiten.ImagePart{}
locationX, locationY := 0, 0 locationX, locationY := 0, 0
for _, c := range str { for _, c := range str {
@ -46,8 +46,10 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
code := int(c) code := int(c)
x := (code % 16) * charWidth x := (code % 16) * charWidth
y := ((code - 32) / 16) * charHeight y := ((code - 32) / 16) * charHeight
dsts = append(dsts, image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight)) parts = append(parts, ebiten.ImagePart{
srcs = append(srcs, image.Rect(x, y, x+charWidth, y+charHeight)) Dst: image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight),
Src: image.Rect(x, y, x+charWidth, y+charHeight),
})
locationX += charWidth locationX += charWidth
} }
@ -61,10 +63,9 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
a := float64(c2.A) / max a := float64(c2.A) / max
clr := ebiten.ScaleColor(r, g, b, a) clr := ebiten.ScaleColor(r, g, b, a)
rt.DrawImage(fontImageId, &ebiten.DrawImageOptions{ rt.DrawImage(fontImageId, &ebiten.DrawImageOptions{
DstParts: dsts, Parts: parts,
SrcParts: srcs, GeoM: geo,
GeoM: geo, ColorM: clr,
ColorM: clr,
}) })
} }

View File

@ -138,7 +138,7 @@ const fieldBlockNumX = 10
const fieldBlockNumY = 20 const fieldBlockNumY = 20
func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int) { func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int) {
dsts, srcs := []image.Rectangle{}, []image.Rectangle{} parts := []ebiten.ImagePart{}
for i, blockCol := range blocks { for i, blockCol := range blocks {
for j, block := range blockCol { for j, block := range blockCol {
if block == BlockTypeNone { if block == BlockTypeNone {
@ -146,15 +146,16 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int)
} }
locationX := i*blockWidth + x locationX := i*blockWidth + x
locationY := j*blockHeight + y locationY := j*blockHeight + y
dsts = append(dsts, image.Rect(locationX, locationY, locationX+blockWidth, locationY+blockHeight))
srcX := (int(block) - 1) * blockWidth srcX := (int(block) - 1) * blockWidth
srcs = append(srcs, image.Rect(srcX, 0, srcX+blockWidth, blockHeight)) parts = append(parts, ebiten.ImagePart{
Dst: image.Rect(locationX, locationY, locationX+blockWidth, locationY+blockHeight),
Src: image.Rect(srcX, 0, srcX+blockWidth, blockHeight),
})
} }
} }
blocksImage := images.GetImage("blocks") blocksImage := images.GetImage("blocks")
r.DrawImage(blocksImage, &ebiten.DrawImageOptions{ r.DrawImage(blocksImage, &ebiten.DrawImageOptions{
SrcParts: srcs, Parts: parts,
DstParts: dsts,
}) })
} }

View File

@ -57,19 +57,20 @@ func drawTitleBackground(r *ebiten.Image, images *Images, c int) {
dy := (c / 4) % imageHeight dy := (c / 4) % imageHeight
backgroundImage := images.GetImage("background") backgroundImage := images.GetImage("background")
dsts, srcs := []image.Rectangle{}, []image.Rectangle{} parts := []ebiten.ImagePart{}
for j := -1; j < ScreenHeight/imageHeight+1; j++ { for j := -1; j < ScreenHeight/imageHeight+1; j++ {
for i := 0; i < ScreenWidth/imageWidth+1; i++ { for i := 0; i < ScreenWidth/imageWidth+1; i++ {
dstX := i*imageWidth + dx dstX := i*imageWidth + dx
dstY := j*imageHeight + dy dstY := j*imageHeight + dy
dsts = append(dsts, image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight)) parts = append(parts, ebiten.ImagePart{
srcs = append(srcs, image.Rect(0, 0, imageWidth, imageHeight)) Dst: image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight),
Src: image.Rect(0, 0, imageWidth, imageHeight),
})
} }
} }
r.DrawImage(backgroundImage, &ebiten.DrawImageOptions{ r.DrawImage(backgroundImage, &ebiten.DrawImageOptions{
SrcParts: srcs, Parts: parts,
DstParts: dsts,
}) })
} }

View File

@ -32,21 +32,22 @@ var (
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
dsts, srcs := []image.Rectangle{}, []image.Rectangle{} parts := []ebiten.ImagePart{}
w, h := gophersImage.Size() w, h := gophersImage.Size()
for i := 0; i < h; i++ { for i := 0; i < h; i++ {
width := w + i*3/4 width := w + i*3/4
x := ((h - i) * 3 / 4) / 2 x := ((h - i) * 3 / 4) / 2
dsts = append(dsts, image.Rect(x, i, x+width, i+1)) parts = append(parts, ebiten.ImagePart{
srcs = append(srcs, image.Rect(0, i, w, i+1)) Dst: image.Rect(x, i, x+width, i+1),
Src: image.Rect(0, i, w, i+1),
})
} }
maxWidth := float64(w) + float64(h)*0.75 maxWidth := float64(w) + float64(h)*0.75
geo := ebiten.TranslateGeo(-maxWidth/2, -float64(h)/2) geo := ebiten.TranslateGeo(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.TranslateGeo(screenWidth/2, screenHeight/2)) geo.Concat(ebiten.TranslateGeo(screenWidth/2, screenHeight/2))
screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{ screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{
DstParts: dsts, Parts: parts,
SrcParts: srcs, GeoM: geo,
GeoM: geo,
}) })
return nil return nil
} }

View File

@ -56,15 +56,14 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error
if options == nil { if options == nil {
options = &DrawImageOptions{} options = &DrawImageOptions{}
} }
dsts := options.DstParts parts := options.Parts
srcs := options.SrcParts if parts == nil {
if srcs == nil || dsts == nil {
w, h := img.size() w, h := img.size()
dsts = []image.Rectangle{ parts = []ImagePart{
image.Rect(0, 0, w, h), {
} Dst: image.Rect(0, 0, w, h),
srcs = []image.Rectangle{ Src: image.Rect(0, 0, w, h),
image.Rect(0, 0, w, h), },
} }
} }
geo := options.GeoM geo := options.GeoM
@ -74,7 +73,7 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error
return err return err
} }
w, h := img.texture.Size() w, h := img.texture.Size()
quads := textureQuads(dsts, srcs, w, h) quads := textureQuads(parts, w, h)
projectionMatrix := i.framebuffer.ProjectionMatrix() projectionMatrix := i.framebuffer.ProjectionMatrix()
shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr) shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr)
return nil return nil
@ -88,14 +87,10 @@ func v(y float64, height int) float32 {
return float32(y) / float32(internal.NextPowerOf2Int(height)) return float32(y) / float32(internal.NextPowerOf2Int(height))
} }
func textureQuads(dsts, srcs []image.Rectangle, width, height int) []shader.TextureQuad { func textureQuads(parts []ImagePart, width, height int) []shader.TextureQuad {
l := len(dsts) quads := make([]shader.TextureQuad, 0, len(parts))
if len(srcs) < l { for _, part := range parts {
l = len(srcs) dst, src := part.Dst, part.Src
}
quads := make([]shader.TextureQuad, 0, l)
for i := 0; i < l; i++ {
dst, src := dsts[i], srcs[i]
x1 := float32(dst.Min.X) x1 := float32(dst.Min.X)
x2 := float32(dst.Max.X) x2 := float32(dst.Max.X)
y1 := float32(dst.Min.Y) y1 := float32(dst.Min.Y)
@ -200,10 +195,14 @@ func (i *Image) At(x, y int) color.Color {
return color.RGBA{r, g, b, a} return color.RGBA{r, g, b, a}
} }
type ImagePart struct {
Dst image.Rectangle
Src image.Rectangle
}
// A DrawImageOptions presents options to render an image on an image. // A DrawImageOptions presents options to render an image on an image.
type DrawImageOptions struct { type DrawImageOptions struct {
DstParts []image.Rectangle Parts []ImagePart
SrcParts []image.Rectangle GeoM GeoM
GeoM GeoM ColorM ColorM
ColorM ColorM
} }