Skip to content

Commit

Permalink
ebiten: bug fix: DrawImage/DrawRectShader unexpectedly modified the g…
Browse files Browse the repository at this point in the history
…iven options

Closes #2733
  • Loading branch information
hajimehoshi committed Aug 26, 2023
1 parent dec08b9 commit 3678b20
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
12 changes: 7 additions & 5 deletions image.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,10 +240,11 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
}
filter := builtinshader.Filter(options.Filter)

geoM := options.GeoM
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 {
options.GeoM.Translate(float64(offsetX), float64(offsetY))
geoM.Translate(float64(offsetX), float64(offsetY))
}
a, b, c, d, tx, ty := options.GeoM.elements32()
a, b, c, d, tx, ty := geoM.elements32()

bounds := img.Bounds()
sx0, sy0 := img.adjustPosition(bounds.Min.X, bounds.Min.Y)
Expand All @@ -269,7 +270,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
})
}

i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, false, canSkipMipmap(options.GeoM, filter), false)
i.image.DrawTriangles(srcs, vs, is, blend, i.adjustedRegion(), img.adjustedRegion(), [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, i.tmpUniforms, false, canSkipMipmap(geoM, filter), false)
}

// Vertex represents a vertex passed to DrawTriangles.
Expand Down Expand Up @@ -792,10 +793,11 @@ func (i *Image) DrawRectShader(width, height int, shader *Shader, options *DrawR
}
}

geoM := options.GeoM
if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 {
options.GeoM.Translate(float64(offsetX), float64(offsetY))
geoM.Translate(float64(offsetX), float64(offsetY))
}
a, b, c, d, tx, ty := options.GeoM.elements32()
a, b, c, d, tx, ty := geoM.elements32()
cr, cg, cb, ca := options.ColorScale.elements()
vs := i.ensureTmpVertices(4 * graphics.VertexFloatCount)
graphics.QuadVertices(vs, float32(sx), float32(sy), float32(sx+width), float32(sy+height), a, b, c, d, tx, ty, cr, cg, cb, ca)
Expand Down
28 changes: 28 additions & 0 deletions image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4203,3 +4203,31 @@ func TestImageDrawTrianglesShaderWithTooBigIndex(t *testing.T) {
}
dst.DrawTrianglesShader(vs, is, shader, nil)
}

// Issue #2733
func TestImageGeoMAfterDraw(t *testing.T) {
src := ebiten.NewImage(1, 1)
dst := ebiten.NewImageWithOptions(image.Rect(-1, -1, 0, 0), nil)
op0 := &ebiten.DrawImageOptions{}
dst.DrawImage(src, op0)
if x, y := op0.GeoM.Apply(0, 0); x != 0 || y != 0 {
t.Errorf("got: (%0.2f, %0.2f), want: (0, 0)", x, y)
}

s, err := ebiten.NewShader([]byte(`//kage:unit pixels
package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
return vec4(1)
}
`))
if err != nil {
t.Fatal(err)
}
op1 := &ebiten.DrawRectShaderOptions{}
dst.DrawRectShader(1, 1, s, op1)
if x, y := op1.GeoM.Apply(0, 0); x != 0 || y != 0 {
t.Errorf("got: (%0.2f, %0.2f), want: (0, 0)", x, y)
}
}

0 comments on commit 3678b20

Please sign in to comment.