Add DrawImage* functions (#40)

This commit is contained in:
Hajime Hoshi 2014-12-24 03:05:45 +09:00
parent a0cff8961b
commit 6cb8dac860
14 changed files with 86 additions and 51 deletions

60
drawimage.go Normal file
View File

@ -0,0 +1,60 @@
/*
Copyright 2014 Hajime Hoshi
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// +build ignore
package ebiten
import (
"image"
)
func DrawImage(target *Image, img *Image, x, y int) error {
return DrawImageColor(target, img, x, y, ColorMatrixI())
}
func DrawImageGeometry(target *Image, img *Image, geo GeometryMatrix) error {
return DrawImageGeometryColor(target, img, geo, ColorMatrixI())
}
func DrawImageColor(target *Image, img *Image, x, y int, color ColorMatrix) error {
geo := TranslateGeometry(float64(x), float64(y))
return DrawImageGeometryColor(target, img, geo, color)
}
func DrawImageGeometryColor(target *Image, img *Image, geo GeometryMatrix, color ColorMatrix) error {
w, h := img.Size()
dsts := []image.Rectangle{image.Rect(0, 0, w, h)}
srcs := []image.Rectangle{image.Rect(0, 0, w, h)}
return DrawImagePartsGeometryColor(target, dsts, img, srcs, geo, color)
}
func DrawImageParts(target *Image, dsts []image.Rectangle, img *Image, srcs []image.Rectangle, x, y int) error {
return DrawImagePartsColor(target, dsts, img, srcs, x, y, ColorMatrixI())
}
func DrawImagePartsGeometry(target *Image, dsts []image.Rectangle, img *Image, srcs []image.Rectangle, geo GeometryMatrix) error {
return DrawImagePartsGeometryColor(target, dsts, img, srcs, geo, ColorMatrixI())
}
func DrawImagePartsColor(target *Image, dsts []image.Rectangle, img *Image, srcs []image.Rectangle, x, y int, color ColorMatrix) error {
geo := TranslateGeometry(float64(x), float64(y))
return DrawImagePartsGeometryColor(target, dsts, img, srcs, geo, color)
}
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)
}

View File

@ -55,14 +55,13 @@ func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c col
srcs = append(srcs, src) srcs = append(srcs, src)
locationX += assets.TextImageCharWidth locationX += assets.TextImageCharWidth
} }
geo := ebiten.TranslateGeometry(float64(x)+1, float64(y))
cc := color.NRGBA64Model.Convert(c).(color.NRGBA64) cc := color.NRGBA64Model.Convert(c).(color.NRGBA64)
r := float64(cc.R) / math.MaxUint16 r := float64(cc.R) / math.MaxUint16
g := float64(cc.G) / math.MaxUint16 g := float64(cc.G) / math.MaxUint16
b := float64(cc.B) / math.MaxUint16 b := float64(cc.B) / math.MaxUint16
a := float64(cc.A) / math.MaxUint16 a := float64(cc.A) / math.MaxUint16
clr := ebiten.ScaleColor(r, g, b, a) clr := ebiten.ScaleColor(r, g, b, a)
rt.DrawImage(dsts, d.textImage, srcs, geo, clr) ebiten.DrawImagePartsColor(rt, dsts, d.textImage, srcs, x+1, y, clr)
} }
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) { func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {

View File

@ -51,18 +51,15 @@ func update(screen *ebiten.Image) error {
return err return err
} }
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
geo := ebiten.TranslateGeometry(15+float64(i)*(diff), 20)
clr := ebiten.ScaleColor(1.0, 1.0, 1.0, 0.5) clr := ebiten.ScaleColor(1.0, 1.0, 1.0, 0.5)
if err := ebiten.DrawWholeImage(tmpRenderTarget, ebitenImage, geo, clr); err != nil { if err := ebiten.DrawImageColor(tmpRenderTarget, ebitenImage, 15+int(float64(i)*diff), 20, clr); err != nil {
return err return err
} }
} }
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}) screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
geo := ebiten.TranslateGeometry(0, float64(i)*(diff)) if err := ebiten.DrawImage(screen, tmpRenderTarget, 0, int(float64(i)*diff)); err != nil {
clr := ebiten.ColorMatrixI()
if err := ebiten.DrawWholeImage(screen, tmpRenderTarget, geo, clr); err != nil {
return err return err
} }
} }

View File

@ -112,11 +112,11 @@ func (f *Field) flushLine(j int) bool {
return true return true
} }
func (f *Field) Draw(r *ebiten.Image, images *Images, geo ebiten.GeometryMatrix) { func (f *Field) Draw(r *ebiten.Image, images *Images, x, y int) {
blocks := make([][]BlockType, len(f.blocks)) blocks := make([][]BlockType, len(f.blocks))
for i, blockCol := range f.blocks { for i, blockCol := range f.blocks {
blocks[i] = make([]BlockType, len(blockCol)) blocks[i] = make([]BlockType, len(blockCol))
copy(blocks[i], blockCol[:]) copy(blocks[i], blockCol[:])
} }
drawBlocks(r, images, blocks, geo) drawBlocks(r, images, blocks, x, y)
} }

View File

@ -117,11 +117,9 @@ func (s *GameScene) Draw(r *ebiten.Image, images *Images) {
geoMat := ebiten.ScaleGeometry(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h)) geoMat := ebiten.ScaleGeometry(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number? geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
colorMat := ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5) colorMat := ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5)
ebiten.DrawWholeImage(r, field, geoMat, colorMat) ebiten.DrawImageGeometryColor(r, field, geoMat, colorMat)
geoMat = ebiten.GeometryMatrixI() s.field.Draw(r, images, 20, 20)
geoMat.Concat(ebiten.TranslateGeometry(20, 20))
s.field.Draw(r, images, geoMat)
if s.currentPiece != nil { if s.currentPiece != nil {
s.currentPiece.Draw(r, images, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle) s.currentPiece.Draw(r, images, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle)

View File

@ -139,7 +139,7 @@ const blockHeight = 10
const fieldBlockNumX = 10 const fieldBlockNumX = 10
const fieldBlockNumY = 20 const fieldBlockNumY = 20
func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, geo ebiten.GeometryMatrix) { func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int) {
dsts, srcs := []image.Rectangle{}, []image.Rectangle{} dsts, srcs := []image.Rectangle{}, []image.Rectangle{}
for i, blockCol := range blocks { for i, blockCol := range blocks {
for j, block := range blockCol { for j, block := range blockCol {
@ -154,7 +154,7 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, geo ebite
} }
} }
blocksImage := images.GetImage("blocks") blocksImage := images.GetImage("blocks")
r.DrawImage(dsts, blocksImage, srcs, geo, ebiten.ColorMatrixI()) ebiten.DrawImageParts(r, dsts, blocksImage, srcs, x, y)
} }
func (p *Piece) InitialPosition() (int, int) { func (p *Piece) InitialPosition() (int, int) {
@ -226,10 +226,7 @@ func (p *Piece) Draw(r *ebiten.Image, images *Images, fieldX, fieldY int, pieceX
} }
} }
geoMat := ebiten.GeometryMatrixI()
x := fieldX + pieceX*blockWidth x := fieldX + pieceX*blockWidth
y := fieldY + pieceY*blockHeight y := fieldY + pieceY*blockHeight
geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y))) drawBlocks(r, images, blocks, x, y)
drawBlocks(r, images, blocks, geoMat)
} }

View File

@ -73,12 +73,11 @@ func (s *SceneManager) Draw(r *ebiten.Image, images *Images) {
to.Clear() to.Clear()
s.next.Draw(to, images) s.next.Draw(to, images)
color := ebiten.ColorMatrixI() ebiten.DrawImage(r, from, 0, 0)
ebiten.DrawWholeImage(r, from, ebiten.GeometryMatrixI(), color)
alpha := float64(s.transitionCount) / float64(transitionMaxCount) alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color.Elements[3][3] = alpha color := ebiten.ScaleColor(1, 1, 1, alpha)
ebiten.DrawWholeImage(r, to, ebiten.GeometryMatrixI(), color) ebiten.DrawImageColor(r, to, 0, 0, color)
} }
func (s *SceneManager) GoTo(scene Scene) { func (s *SceneManager) GoTo(scene Scene) {

View File

@ -68,10 +68,7 @@ func drawTitleBackground(r *ebiten.Image, images *Images, c int) {
dx := (-c / 4) % imageWidth dx := (-c / 4) % imageWidth
dy := (c / 4) % imageHeight dy := (c / 4) % imageHeight
geo := ebiten.GeometryMatrixI() ebiten.DrawImageParts(r, dsts, backgroundImage, srcs, dx, dy)
geo.Concat(ebiten.TranslateGeometry(float64(dx), float64(dy)))
clr := ebiten.ColorMatrixI()
r.DrawImage(dsts, backgroundImage, srcs, geo, clr)
} }
func drawLogo(r *ebiten.Image, images *Images, str string) { func drawLogo(r *ebiten.Image, images *Images, str string) {

View File

@ -58,9 +58,7 @@ func Update(screen *ebiten.Image) error {
geo.Concat(ebiten.ScaleGeometry(scaleX, scaleY)) geo.Concat(ebiten.ScaleGeometry(scaleX, scaleY))
geo.Concat(ebiten.RotateGeometry(float64(count%720) * 2 * math.Pi / 720)) geo.Concat(ebiten.RotateGeometry(float64(count%720) * 2 * math.Pi / 720))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
//clr := ebiten.RotateHue(float64(count%180) * 2 * math.Pi / 180) if err := ebiten.DrawImageGeometry(screen, gophersImage, geo); err != nil {
clr := ebiten.ColorMatrixI()
if err := ebiten.DrawWholeImage(screen, gophersImage, geo, clr); err != nil {
return err return err
} }
return nil return nil

View File

@ -37,10 +37,10 @@ var (
func Update(screen *ebiten.Image) error { func Update(screen *ebiten.Image) error {
geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio) geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio)
ebiten.DrawWholeImage(gophersRenderTarget, gophersImage, geo, ebiten.ColorMatrixI()) ebiten.DrawImageGeometry(gophersRenderTarget, gophersImage, geo)
geo = ebiten.ScaleGeometry(mosaicRatio/2.0, mosaicRatio/2.0) geo = ebiten.ScaleGeometry(mosaicRatio/2.0, mosaicRatio/2.0)
ebiten.DrawWholeImage(screen, gophersRenderTarget, geo, ebiten.ColorMatrixI()) ebiten.DrawImageGeometry(screen, gophersRenderTarget, geo)
return nil return nil
} }

View File

@ -44,14 +44,13 @@ func Update(screen *ebiten.Image) error {
mx, my := ebiten.CursorPosition() mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
geo := ebiten.TranslateGeometry(float64(mx), float64(my))
clr := ebiten.ScaleColor(1.0, 0.25, 0.25, 1.0) clr := ebiten.ScaleColor(1.0, 0.25, 0.25, 1.0)
theta := 2.0 * math.Pi * float64(count%60) / 60.0 theta := 2.0 * math.Pi * float64(count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta)) clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawWholeImage(canvasRenderTarget, brushRenderTarget, geo, clr) ebiten.DrawImageColor(canvasRenderTarget, brushRenderTarget, mx, my, clr)
} }
ebiten.DrawWholeImage(screen, canvasRenderTarget, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI()) ebiten.DrawImage(screen, canvasRenderTarget, 0, 0)
ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my)) ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my))
return nil return nil

View File

@ -46,7 +46,7 @@ func Update(screen *ebiten.Image) error {
geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2) geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.ScaleGeometry(0.4, 0.4)) geo.Concat(ebiten.ScaleGeometry(0.4, 0.4))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
screen.DrawImage(dsts, gophersImage, srcs, geo, ebiten.ColorMatrixI()) ebiten.DrawImagePartsGeometry(screen, dsts, gophersImage, srcs, geo)
return nil return nil
} }

View File

@ -18,17 +18,8 @@ package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
"image"
) )
// DrawWholeImage draws the whole image.
func DrawWholeImage(target *Image, img *Image, geo GeometryMatrix, color ColorMatrix) error {
w, h := img.Size()
dsts := []image.Rectangle{image.Rect(0, 0, w, h)}
srcs := []image.Rectangle{image.Rect(0, 0, w, h)}
return target.DrawImage(dsts, img, srcs, geo, color)
}
// Filter represents the type of filter to be used when an image is maginified or minified. // Filter represents the type of filter to be used when an image is maginified or minified.
type Filter opengl.Filter type Filter opengl.Filter

View File

@ -104,9 +104,9 @@ func TestComposition(t *testing.T) {
t.Fatal(err) t.Fatal(err)
return return
} }
DrawWholeImage(img2, img1, GeometryMatrixI(), ColorMatrixI()) DrawImageGeometryColor(img2, img1, GeometryMatrixI(), ColorMatrixI())
DrawWholeImage(img3, img2, GeometryMatrixI(), ColorMatrixI()) DrawImageGeometryColor(img3, img2, GeometryMatrixI(), ColorMatrixI())
DrawWholeImage(img_12_3, img3, GeometryMatrixI(), ColorMatrixI()) DrawImageGeometryColor(img_12_3, img3, GeometryMatrixI(), ColorMatrixI())
img2.Fill(img2Color) img2.Fill(img2Color)
img3.Fill(img3Color) img3.Fill(img3Color)
@ -115,9 +115,9 @@ func TestComposition(t *testing.T) {
t.Fatal(err) t.Fatal(err)
return return
} }
DrawWholeImage(img3, img2, GeometryMatrixI(), ColorMatrixI()) DrawImageGeometryColor(img3, img2, GeometryMatrixI(), ColorMatrixI())
DrawWholeImage(img3, img1, GeometryMatrixI(), ColorMatrixI()) DrawImageGeometryColor(img3, img1, GeometryMatrixI(), ColorMatrixI())
DrawWholeImage(img_1_23, img3, GeometryMatrixI(), ColorMatrixI()) DrawImageGeometryColor(img_1_23, img3, GeometryMatrixI(), ColorMatrixI())
for j := 0; j < h; j++ { for j := 0; j < h; j++ {
for i := 0; i < w; i++ { for i := 0; i < w; i++ {