mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Add RotateMatrix and TranslateMatrix
This commit is contained in:
parent
6ed9102f01
commit
bc91c2210b
@ -117,10 +117,13 @@ func (game *DemoGame) Update() {
|
||||
func (game *DemoGame) Draw(g graphics.GraphicsContext, offscreen graphics.TextureID) {
|
||||
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
|
||||
geometryMatrix := graphics.IdentityGeometryMatrix()
|
||||
geometryMatrix.SetTx(float64(game.x))
|
||||
geometryMatrix.SetTy(float64(game.x))
|
||||
tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height)
|
||||
geometryMatrix = geometryMatrix.Concat(graphics.TranslateMatrix(-tx/2, -ty/2))
|
||||
geometryMatrix = geometryMatrix.Concat(graphics.RotateMatrix(float64(game.x) / 60))
|
||||
geometryMatrix = geometryMatrix.Concat(graphics.TranslateMatrix(tx/2, ty/2))
|
||||
geometryMatrix = geometryMatrix.Concat(graphics.TranslateMatrix(100, 100))
|
||||
g.DrawTexture(game.ebitenTexture.ID,
|
||||
0, 0, game.ebitenTexture.Width, game.ebitenTexture.Height,
|
||||
0, 0, int(tx), int(ty),
|
||||
geometryMatrix,
|
||||
graphics.IdentityColorMatrix())
|
||||
}
|
||||
@ -133,4 +136,5 @@ func main() {
|
||||
currentUI.Init()
|
||||
|
||||
ebiten.OpenGLRun(game, currentUI)
|
||||
|
||||
}
|
||||
|
@ -14,6 +14,10 @@ func IdentityColorMatrix() *ColorMatrix {
|
||||
return &ColorMatrix{*IdentityAffineMatrix(colorMatrixDimension)}
|
||||
}
|
||||
|
||||
func (matrix *ColorMatrix) Concat(other *ColorMatrix) *ColorMatrix {
|
||||
return &ColorMatrix{*matrix.AffineMatrix.Concat(&other.AffineMatrix)}
|
||||
}
|
||||
|
||||
func (matrix *ColorMatrix) Clone() *ColorMatrix {
|
||||
return &ColorMatrix{*(matrix.AffineMatrix.Clone())}
|
||||
}
|
||||
|
@ -1,5 +1,9 @@
|
||||
package graphics
|
||||
|
||||
import (
|
||||
"math"
|
||||
)
|
||||
|
||||
type GeometryMatrix struct {
|
||||
AffineMatrix
|
||||
}
|
||||
@ -14,6 +18,27 @@ func IdentityGeometryMatrix() *GeometryMatrix {
|
||||
return &GeometryMatrix{*IdentityAffineMatrix(geometryMatrixDimension)}
|
||||
}
|
||||
|
||||
func TranslateMatrix(tx, ty float64) *GeometryMatrix {
|
||||
matrix := IdentityGeometryMatrix()
|
||||
matrix.SetTx(tx)
|
||||
matrix.SetTy(ty)
|
||||
return matrix
|
||||
}
|
||||
|
||||
func RotateMatrix(theta float64) *GeometryMatrix {
|
||||
matrix := NewGeometryMatrix()
|
||||
cos, sin := math.Cos(theta), math.Sin(theta)
|
||||
matrix.SetA(cos)
|
||||
matrix.SetB(-sin)
|
||||
matrix.SetC(sin)
|
||||
matrix.SetD(cos)
|
||||
return matrix
|
||||
}
|
||||
|
||||
func (matrix *GeometryMatrix) Concat(other *GeometryMatrix) *GeometryMatrix {
|
||||
return &GeometryMatrix{*matrix.AffineMatrix.Concat(&other.AffineMatrix)}
|
||||
}
|
||||
|
||||
func (matrix *GeometryMatrix) Clone() *GeometryMatrix {
|
||||
return &GeometryMatrix{*(matrix.AffineMatrix.Clone())}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user