From 325e05259ae9b31f6e181bb28b5f7fcff65de1f6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 27 Dec 2014 17:37:31 +0900 Subject: [PATCH] Introduce ImagePart again --- ebitenutil/debugprint.go | 12 ++++----- example/blocks/blocks/font.go | 15 ++++++----- example/blocks/blocks/piece.go | 11 ++++---- example/blocks/blocks/titlescene.go | 11 ++++---- example/perspective/main.go | 13 ++++----- image.go | 41 ++++++++++++++--------------- 6 files changed, 52 insertions(+), 51 deletions(-) diff --git a/ebitenutil/debugprint.go b/ebitenutil/debugprint.go index b87c4630d..779450310 100644 --- a/ebitenutil/debugprint.go +++ b/ebitenutil/debugprint.go @@ -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) { - dsts, srcs := []image.Rectangle{}, []image.Rectangle{} + parts := []ebiten.ImagePart{} locationX, locationY := 0, 0 for _, c := range str { 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 dst := image.Rect(locationX, locationY, locationX+assets.TextImageCharWidth, locationY+assets.TextImageCharHeight) src := image.Rect(srcX, srcY, srcX+assets.TextImageCharWidth, srcY+assets.TextImageCharHeight) - dsts = append(dsts, dst) - srcs = append(srcs, src) + parts = append(parts, ebiten.ImagePart{Dst: dst, Src: src}) locationX += assets.TextImageCharWidth } 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 a := float64(cc.A) / math.MaxUint16 rt.DrawImage(d.textImage, &ebiten.DrawImageOptions{ - DstParts: dsts, - SrcParts: srcs, - GeoM: ebiten.TranslateGeo(float64(x+1), float64(y)), - ColorM: ebiten.ScaleColor(r, g, b, a), + Parts: parts, + GeoM: ebiten.TranslateGeo(float64(x+1), float64(y)), + ColorM: ebiten.ScaleColor(r, g, b, a), }) } diff --git a/example/blocks/blocks/font.go b/example/blocks/blocks/font.go index ac9bca1e8..ccbc47a67 100644 --- a/example/blocks/blocks/font.go +++ b/example/blocks/blocks/font.go @@ -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) { fontImageId := images.GetImage("font") - dsts, srcs := []image.Rectangle{}, []image.Rectangle{} + parts := []ebiten.ImagePart{} locationX, locationY := 0, 0 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) x := (code % 16) * charWidth y := ((code - 32) / 16) * charHeight - dsts = append(dsts, image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight)) - srcs = append(srcs, image.Rect(x, y, x+charWidth, y+charHeight)) + parts = append(parts, ebiten.ImagePart{ + Dst: image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight), + Src: image.Rect(x, y, x+charWidth, y+charHeight), + }) 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 clr := ebiten.ScaleColor(r, g, b, a) rt.DrawImage(fontImageId, &ebiten.DrawImageOptions{ - DstParts: dsts, - SrcParts: srcs, - GeoM: geo, - ColorM: clr, + Parts: parts, + GeoM: geo, + ColorM: clr, }) } diff --git a/example/blocks/blocks/piece.go b/example/blocks/blocks/piece.go index 3bbff3f7d..05ca823ac 100644 --- a/example/blocks/blocks/piece.go +++ b/example/blocks/blocks/piece.go @@ -138,7 +138,7 @@ const fieldBlockNumX = 10 const fieldBlockNumY = 20 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 j, block := range blockCol { if block == BlockTypeNone { @@ -146,15 +146,16 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int) } locationX := i*blockWidth + x locationY := j*blockHeight + y - dsts = append(dsts, image.Rect(locationX, locationY, locationX+blockWidth, locationY+blockHeight)) 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") r.DrawImage(blocksImage, &ebiten.DrawImageOptions{ - SrcParts: srcs, - DstParts: dsts, + Parts: parts, }) } diff --git a/example/blocks/blocks/titlescene.go b/example/blocks/blocks/titlescene.go index 1ffd0433b..341ef4f82 100644 --- a/example/blocks/blocks/titlescene.go +++ b/example/blocks/blocks/titlescene.go @@ -57,19 +57,20 @@ func drawTitleBackground(r *ebiten.Image, images *Images, c int) { dy := (c / 4) % imageHeight backgroundImage := images.GetImage("background") - dsts, srcs := []image.Rectangle{}, []image.Rectangle{} + parts := []ebiten.ImagePart{} for j := -1; j < ScreenHeight/imageHeight+1; j++ { for i := 0; i < ScreenWidth/imageWidth+1; i++ { dstX := i*imageWidth + dx dstY := j*imageHeight + dy - dsts = append(dsts, image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight)) - srcs = append(srcs, image.Rect(0, 0, imageWidth, imageHeight)) + parts = append(parts, ebiten.ImagePart{ + Dst: image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight), + Src: image.Rect(0, 0, imageWidth, imageHeight), + }) } } r.DrawImage(backgroundImage, &ebiten.DrawImageOptions{ - SrcParts: srcs, - DstParts: dsts, + Parts: parts, }) } diff --git a/example/perspective/main.go b/example/perspective/main.go index f097cf6fb..69ce4694b 100644 --- a/example/perspective/main.go +++ b/example/perspective/main.go @@ -32,21 +32,22 @@ var ( ) func update(screen *ebiten.Image) error { - dsts, srcs := []image.Rectangle{}, []image.Rectangle{} + parts := []ebiten.ImagePart{} w, h := gophersImage.Size() for i := 0; i < h; i++ { width := w + i*3/4 x := ((h - i) * 3 / 4) / 2 - dsts = append(dsts, image.Rect(x, i, x+width, i+1)) - srcs = append(srcs, image.Rect(0, i, w, i+1)) + parts = append(parts, ebiten.ImagePart{ + Dst: image.Rect(x, i, x+width, i+1), + Src: image.Rect(0, i, w, i+1), + }) } maxWidth := float64(w) + float64(h)*0.75 geo := ebiten.TranslateGeo(-maxWidth/2, -float64(h)/2) geo.Concat(ebiten.TranslateGeo(screenWidth/2, screenHeight/2)) screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{ - DstParts: dsts, - SrcParts: srcs, - GeoM: geo, + Parts: parts, + GeoM: geo, }) return nil } diff --git a/image.go b/image.go index 3b4298b3a..8b31cf191 100644 --- a/image.go +++ b/image.go @@ -56,15 +56,14 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error if options == nil { options = &DrawImageOptions{} } - dsts := options.DstParts - srcs := options.SrcParts - if srcs == nil || dsts == nil { + parts := options.Parts + if parts == nil { w, h := img.size() - dsts = []image.Rectangle{ - image.Rect(0, 0, w, h), - } - srcs = []image.Rectangle{ - image.Rect(0, 0, w, h), + parts = []ImagePart{ + { + Dst: image.Rect(0, 0, w, h), + Src: image.Rect(0, 0, w, h), + }, } } geo := options.GeoM @@ -74,7 +73,7 @@ func (i *innerImage) drawImage(img *innerImage, options *DrawImageOptions) error return err } w, h := img.texture.Size() - quads := textureQuads(dsts, srcs, w, h) + quads := textureQuads(parts, w, h) projectionMatrix := i.framebuffer.ProjectionMatrix() shader.DrawTexture(img.texture.Native(), projectionMatrix, quads, geo, clr) return nil @@ -88,14 +87,10 @@ func v(y float64, height int) float32 { return float32(y) / float32(internal.NextPowerOf2Int(height)) } -func textureQuads(dsts, srcs []image.Rectangle, width, height int) []shader.TextureQuad { - l := len(dsts) - if len(srcs) < l { - l = len(srcs) - } - quads := make([]shader.TextureQuad, 0, l) - for i := 0; i < l; i++ { - dst, src := dsts[i], srcs[i] +func textureQuads(parts []ImagePart, width, height int) []shader.TextureQuad { + quads := make([]shader.TextureQuad, 0, len(parts)) + for _, part := range parts { + dst, src := part.Dst, part.Src x1 := float32(dst.Min.X) x2 := float32(dst.Max.X) 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} } +type ImagePart struct { + Dst image.Rectangle + Src image.Rectangle +} + // A DrawImageOptions presents options to render an image on an image. type DrawImageOptions struct { - DstParts []image.Rectangle - SrcParts []image.Rectangle - GeoM GeoM - ColorM ColorM + Parts []ImagePart + GeoM GeoM + ColorM ColorM }