Remove DrawImage.+ function (#41)

This commit is contained in:
Hajime Hoshi 2014-12-24 23:06:51 +09:00
parent b39df50979
commit 494d07387d
13 changed files with 64 additions and 91 deletions

View File

@ -1,62 +0,0 @@
// 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.
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 {
op := &ImageDrawOption{
DstParts: dsts,
SrcParts: srcs,
GeometryMatrix: &geo,
ColorMatrix: &color,
}
return target.DrawImage(img, op)
}

View File

@ -59,7 +59,11 @@ 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
clr := ebiten.ScaleColor(r, g, b, a)
ebiten.DrawImagePartsColor(rt, dsts, d.textImage, srcs, x+1, y, clr)
op := ebiten.At(x+1, y)
op.DstParts = dsts
op.SrcParts = srcs
op.ColorMatrix = &clr
rt.DrawImage(d.textImage, op)
}
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {

View File

@ -50,14 +50,17 @@ func update(screen *ebiten.Image) error {
}
for i := 0; i < 10; i++ {
clr := ebiten.ScaleColor(1.0, 1.0, 1.0, 0.5)
if err := ebiten.DrawImageColor(tmpRenderTarget, ebitenImage, 15+int(float64(i)*diff), 20, clr); err != nil {
op := ebiten.At(15+int(float64(i)*diff), 20)
op.ColorMatrix = &clr
if err := tmpRenderTarget.DrawImage(ebitenImage, op); err != nil {
return err
}
}
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
for i := 0; i < 10; i++ {
if err := ebiten.DrawImage(screen, tmpRenderTarget, 0, int(float64(i)*diff)); err != nil {
op := ebiten.At(0, int(float64(i)*diff))
if err := screen.DrawImage(tmpRenderTarget, op); err != nil {
return err
}
}

View File

@ -112,10 +112,13 @@ func (s *GameScene) Draw(r *ebiten.Image, images *Images) {
field := images.GetImage("empty")
w, h := field.Size()
geoMat := ebiten.ScaleGeometry(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
colorMat := ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5)
ebiten.DrawImageGeometryColor(r, field, geoMat, colorMat)
geo := ebiten.ScaleGeometry(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h))
geo.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number?
clr := ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5)
r.DrawImage(field, &ebiten.ImageDrawOption{
GeometryMatrix: &geo,
ColorMatrix: &clr,
})
s.field.Draw(r, images, 20, 20)

View File

@ -152,7 +152,10 @@ func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int)
}
}
blocksImage := images.GetImage("blocks")
ebiten.DrawImageParts(r, dsts, blocksImage, srcs, x, y)
op := ebiten.At(x, y)
op.SrcParts = srcs
op.DstParts = dsts
r.DrawImage(blocksImage, op)
}
func (p *Piece) InitialPosition() (int, int) {

View File

@ -71,11 +71,14 @@ func (s *SceneManager) Draw(r *ebiten.Image, images *Images) {
to.Clear()
s.next.Draw(to, images)
ebiten.DrawImage(r, from, 0, 0)
r.DrawImage(from, nil)
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color := ebiten.ScaleColor(1, 1, 1, alpha)
ebiten.DrawImageColor(r, to, 0, 0, color)
clr := ebiten.ScaleColor(1, 1, 1, alpha)
op := &ebiten.ImageDrawOption{
ColorMatrix: &clr,
}
r.DrawImage(to, op)
}
func (s *SceneManager) GoTo(scene Scene) {

View File

@ -66,7 +66,10 @@ func drawTitleBackground(r *ebiten.Image, images *Images, c int) {
dx := (-c / 4) % imageWidth
dy := (c / 4) % imageHeight
ebiten.DrawImageParts(r, dsts, backgroundImage, srcs, dx, dy)
op := ebiten.At(dx, dy)
op.SrcParts = srcs
op.DstParts = dsts
r.DrawImage(backgroundImage, op)
}
func drawLogo(r *ebiten.Image, images *Images, str string) {

View File

@ -35,10 +35,14 @@ var (
func update(screen *ebiten.Image) error {
geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio)
ebiten.DrawImageGeometry(gophersRenderTarget, gophersImage, geo)
gophersRenderTarget.DrawImage(gophersImage, &ebiten.ImageDrawOption{
GeometryMatrix: &geo,
})
geo = ebiten.ScaleGeometry(mosaicRatio, mosaicRatio)
ebiten.DrawImageGeometry(screen, gophersRenderTarget, geo)
screen.DrawImage(gophersRenderTarget, &ebiten.ImageDrawOption{
GeometryMatrix: &geo,
})
return nil
}

View File

@ -45,10 +45,12 @@ func Update(screen *ebiten.Image) error {
clr := ebiten.ScaleColor(1.0, 0.25, 0.25, 1.0)
theta := 2.0 * math.Pi * float64(count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawImageColor(canvasRenderTarget, brushRenderTarget, mx, my, clr)
op := ebiten.At(mx, my)
op.ColorMatrix = &clr
canvasRenderTarget.DrawImage(brushRenderTarget, op)
}
ebiten.DrawImage(screen, canvasRenderTarget, 0, 0)
screen.DrawImage(canvasRenderTarget, nil)
ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my))
return nil

View File

@ -43,7 +43,11 @@ func update(screen *ebiten.Image) error {
maxWidth := float64(w) + float64(h)*0.75
geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
ebiten.DrawImagePartsGeometry(screen, dsts, gophersImage, srcs, geo)
screen.DrawImage(gophersImage, &ebiten.ImageDrawOption{
DstParts: dsts,
SrcParts: srcs,
GeometryMatrix: &geo,
})
return nil
}

View File

@ -56,7 +56,9 @@ func update(screen *ebiten.Image) error {
geo.Concat(ebiten.ScaleGeometry(scaleX, scaleY))
geo.Concat(ebiten.RotateGeometry(float64(count%720) * 2 * math.Pi / 720))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
if err := ebiten.DrawImageGeometry(screen, gophersImage, geo); err != nil {
if err := screen.DrawImage(gophersImage, &ebiten.ImageDrawOption{
GeometryMatrix: &geo,
}); err != nil {
return err
}
return nil

View File

@ -57,15 +57,12 @@ func (i *innerImage) drawImage(img *innerImage, option *ImageDrawOption) error {
option = &ImageDrawOption{}
}
dsts := option.DstParts
if dsts == nil {
srcs := option.SrcParts
if srcs == nil || 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),
}
@ -211,3 +208,10 @@ type ImageDrawOption struct {
GeometryMatrix *GeometryMatrix
ColorMatrix *ColorMatrix
}
func At(x, y int) *ImageDrawOption {
geo := TranslateGeometry(float64(x), float64(y))
return &ImageDrawOption{
GeometryMatrix: &geo,
}
}

View File

@ -102,9 +102,9 @@ func TestComposition(t *testing.T) {
t.Fatal(err)
return
}
DrawImage(img2, img1, 0, 0)
DrawImage(img3, img2, 0, 0)
DrawImage(img_12_3, img3, 0, 0)
img2.DrawImage(img1, nil)
img3.DrawImage(img2, nil)
img_12_3.DrawImage(img3, nil)
img2.Fill(img2Color)
img3.Fill(img3Color)
@ -113,9 +113,9 @@ func TestComposition(t *testing.T) {
t.Fatal(err)
return
}
DrawImage(img3, img2, 0, 0)
DrawImage(img3, img1, 0, 0)
DrawImage(img_1_23, img3, 0, 0)
img3.DrawImage(img2, nil)
img3.DrawImage(img1, nil)
img_1_23.DrawImage(img3, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {