diff --git a/ebitenutil/debugprint.go b/ebitenutil/debugprint.go index 0193e7bfe..80e04f819 100644 --- a/ebitenutil/debugprint.go +++ b/ebitenutil/debugprint.go @@ -55,7 +55,7 @@ func (d *debugPrintState) drawText(gr ebiten.GraphicsContext, str string, x, y i locationX += assets.TextImageCharWidth } geom := ebiten.GeometryMatrixI() - geom.Translate(float64(x)+1, float64(y)) + geom.Concat(ebiten.TranslateGeometry(float64(x)+1, float64(y))) clrm := ebiten.ColorMatrixI() clrm.Scale(clr) gr.Texture(d.textTexture).Draw(parts, geom, clrm) diff --git a/example/blocks/blocks/font.go b/example/blocks/blocks/font.go index 9b0bad1c5..8ab92e84a 100644 --- a/example/blocks/blocks/font.go +++ b/example/blocks/blocks/font.go @@ -56,8 +56,8 @@ func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x, } geoMat := ebiten.GeometryMatrixI() - geoMat.Scale(float64(scale), float64(scale)) - geoMat.Translate(float64(x), float64(y)) + geoMat.Concat(ebiten.ScaleGeometry(float64(scale), float64(scale))) + geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y))) clrMat := ebiten.ColorMatrixI() clrMat.Scale(clr) context.Texture(fontTextureId).Draw(parts, geoMat, clrMat) diff --git a/example/blocks/blocks/gamescene.go b/example/blocks/blocks/gamescene.go index d5cd8c840..982b9e295 100644 --- a/example/blocks/blocks/gamescene.go +++ b/example/blocks/blocks/gamescene.go @@ -116,16 +116,14 @@ func (s *GameScene) Draw(context ebiten.GraphicsContext, textures *Textures) { field := textures.GetTexture("empty") geoMat := ebiten.GeometryMatrixI() - geoMat.Scale( - float64(fieldWidth)/float64(emptyWidth), - float64(fieldHeight)/float64(emptyHeight)) - geoMat.Translate(20, 20) // magic number? + geoMat.Concat(ebiten.ScaleGeometry(float64(fieldWidth)/float64(emptyWidth), float64(fieldHeight)/float64(emptyHeight))) + geoMat.Concat(ebiten.TranslateGeometry(20, 20)) // TODO: magic number? colorMat := ebiten.ColorMatrixI() colorMat.Scale(color.RGBA{0, 0, 0, 0x80}) ebiten.DrawWhole(context.Texture(field), emptyWidth, emptyHeight, geoMat, colorMat) geoMat = ebiten.GeometryMatrixI() - geoMat.Translate(20, 20) + geoMat.Concat(ebiten.TranslateGeometry(20, 20)) s.field.Draw(context, textures, geoMat) if s.currentPiece != nil { diff --git a/example/blocks/blocks/piece.go b/example/blocks/blocks/piece.go index 9a2e62eb1..b82761398 100644 --- a/example/blocks/blocks/piece.go +++ b/example/blocks/blocks/piece.go @@ -234,7 +234,7 @@ func (p *Piece) Draw(context ebiten.GraphicsContext, textures *Textures, fieldX, geoMat := ebiten.GeometryMatrixI() x := fieldX + pieceX*blockWidth y := fieldY + pieceY*blockHeight - geoMat.Translate(float64(x), float64(y)) + geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y))) drawBlocks(context, textures, blocks, geoMat) } diff --git a/example/blocks/blocks/titlescene.go b/example/blocks/blocks/titlescene.go index c57c11955..c6aa98b96 100644 --- a/example/blocks/blocks/titlescene.go +++ b/example/blocks/blocks/titlescene.go @@ -69,7 +69,7 @@ func drawTitleBackground(context ebiten.GraphicsContext, textures *Textures, c i dx := (-c / 4) % textureWidth dy := (c / 4) % textureHeight geo := ebiten.GeometryMatrixI() - geo.Translate(float64(dx), float64(dy)) + geo.Concat(ebiten.TranslateGeometry(float64(dx), float64(dy))) clr := ebiten.ColorMatrixI() context.Texture(backgroundTextureId).Draw(parts, geo, clr) } diff --git a/example/paint/main.go b/example/paint/main.go index c9d171563..198c989f2 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -55,7 +55,7 @@ func (g *Game) Draw(gr ebiten.GraphicsContext) error { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { gr.PushRenderTarget(g.canvasRenderTarget) geo := ebiten.GeometryMatrixI() - geo.Translate(float64(mx), float64(my)) + geo.Concat(ebiten.TranslateGeometry(float64(mx), float64(my))) clr := ebiten.ColorMatrixI() clr.Scale(color.RGBA{0xff, 0xff, 0x00, 0xff}) theta := 2.0 * math.Pi * float64(g.count%60) / 60.0 diff --git a/geometrymatrix.go b/geometrymatrix.go index 5568ef388..fc831a1a5 100644 --- a/geometrymatrix.go +++ b/geometrymatrix.go @@ -63,30 +63,33 @@ func (g *GeometryMatrix) setElement(i, j int, element float64) { g.Elements[i][j] = element } -// Translate translates the geometry matrix by (tx, ty). -func (g *GeometryMatrix) Translate(tx, ty float64) { - g.Elements[0][2] += tx - g.Elements[1][2] += ty +// ScaleGeometry returns a matrix that scales a geometry matrix by (x, y). +func ScaleGeometry(x, y float64) GeometryMatrix { + return GeometryMatrix{ + [2][3]float64{ + {x, 0, 0}, + {0, y, 0}, + }, + } } -// Scale scales the geometry matrix by (x, y). -func (g *GeometryMatrix) Scale(x, y float64) { - g.Elements[0][0] *= x - g.Elements[0][1] *= x - g.Elements[0][2] *= x - g.Elements[1][0] *= y - g.Elements[1][1] *= y - g.Elements[1][2] *= y +// TranslateGeometry returns a matrix taht translates a geometry matrix by (tx, ty). +func TranslateGeometry(tx, ty float64) GeometryMatrix { + return GeometryMatrix{ + [2][3]float64{ + {1, 0, tx}, + {0, 1, ty}, + }, + } } -// Rotate rotates the geometry matrix by theta. -func (g *GeometryMatrix) Rotate(theta float64) { +// RotateGeometry returns a matrix that rotates a geometry matrix by theta. +func RotateGeometry(theta float64) GeometryMatrix { sin, cos := math.Sincos(theta) - rotate := GeometryMatrix{ + return GeometryMatrix{ [2][3]float64{ {cos, -sin, 0}, {sin, cos, 0}, }, } - g.Concat(rotate) } diff --git a/graphicscontext.go b/graphicscontext.go index 44aed9554..46f09c009 100644 --- a/graphicscontext.go +++ b/graphicscontext.go @@ -101,7 +101,7 @@ func (c *graphicsContext) postUpdate() { scale := float64(c.screenScale) geo := GeometryMatrixI() - geo.Scale(scale, scale) + geo.Concat(ScaleGeometry(scale, scale)) DrawWhole(c.RenderTarget(c.screenID), c.screenWidth, c.screenHeight, geo, ColorMatrixI()) gl.Flush()