feat: use values not pointers for matrices.

This commit is contained in:
Andrey Parhomenko 2024-01-08 02:53:00 +03:00
parent e602e54ded
commit 6e76188bd8
7 changed files with 34 additions and 57 deletions

View file

@ -206,9 +206,7 @@ func (e *Engine) CursorPosition() Vector {
} }
func (e *Engine) AbsCursorPosition() Vector { func (e *Engine) AbsCursorPosition() Vector {
m := &Matrix{} return e.CursorPosition().Apply(e.Camera.AbsMatrix())
m.Concat(e.Camera.AbsMatrix())
return e.CursorPosition().Apply(m)
} }
func (e *engine) Update() error { func (e *engine) Update() error {
@ -278,8 +276,8 @@ func (e *engine) Update() error {
if !realPos.Eq(eng.cursorPos) { if !realPos.Eq(eng.cursorPos) {
absM := eng.Camera.AbsMatrix() absM := eng.Camera.AbsMatrix()
absPrevPos :=eng.cursorPos.Apply(&absM) absPrevPos :=eng.cursorPos.Apply(absM)
absPos := realPos.Apply(&absM) absPos := realPos.Apply(absM)
events = append(events, &MouseMove{ events = append(events, &MouseMove{
Real: realPos.Sub(eng.cursorPos), Real: realPos.Sub(eng.cursorPos),

View file

@ -9,7 +9,7 @@ type Polygon struct {
Triangles Triangles
} }
func (p *Polygon) ContainsPoint(pnt Point) bool { func (p Polygon) ContainsPoint(pnt Point) bool {
return p.MakeTriangles().ContainsPoint(pnt) return p.MakeTriangles().ContainsPoint(pnt)
} }
@ -22,7 +22,7 @@ type DrawablePolygon struct {
Colority Colority
} }
func (p *Polygon) MakeTriangles() Triangles { func (p Polygon) MakeTriangles() Triangles {
m := p.Matrix() m := p.Matrix()
ret := make(Triangles, len(p.Triangles)) ret := make(Triangles, len(p.Triangles))
for i, t := range p.Triangles { for i, t := range p.Triangles {

15
rect.go
View file

@ -79,11 +79,9 @@ func (r *DrawableRectangle) Draw(c *Context) {
img := NewImage(1, 1) img := NewImage(1, 1)
img.Set(0, 0, r.Color) img.Set(0, 0, r.Color)
c.DrawImage(img, &ebiten.DrawImageOptions{
opts := &ebiten.DrawImageOptions{ GeoM: m,
GeoM: *m, })
}
c.DrawImage(img, opts)
return return
} }
@ -98,11 +96,10 @@ func (r *DrawableRectangle) Draw(c *Context) {
w, h := r.Images[0].Size() w, h := r.Images[0].Size()
// Drawing with shader. // Drawing with shader.
opts := &ebiten.DrawRectShaderOptions{ c.DrawRectShader(w, h, r.Shader, &ebiten.DrawRectShaderOptions{
GeoM: *m, GeoM: m,
Images: r.Images, Images: r.Images,
Uniforms: r.Uniforms, Uniforms: r.Uniforms,
} })
c.DrawRectShader(w, h, r.Shader, opts)
} }

View file

@ -19,29 +19,26 @@ func (s *Sprite) Draw(c *Context) {
} }
t := s.Rectangle().Transform t := s.Rectangle().Transform
m := &Matrix{} m := t.Matrix()
tm := t.Matrix()
m.Concat(*tm)
if !s.Floating { if !s.Floating {
m.Concat(c.Camera.RealMatrix()) m.Concat(c.Camera.RealMatrix())
} }
// Drawing without shader. // Drawing without shader.
if s.Shader == nil { if s.Shader == nil {
opts := &ebiten.DrawImageOptions{} c.DrawImage(s.Images[0], &ebiten.DrawImageOptions{
opts.GeoM = *m GeoM: m,
c.DrawImage(s.Images[0], opts) })
return return
} }
w, h := s.Images[0].Size() w, h := s.Images[0].Size()
// Drawing with shader. // Drawing with shader.
opts := &ebiten.DrawRectShaderOptions{ c.DrawRectShader(w, h, s.Shader, &ebiten.DrawRectShaderOptions{
Images: s.Images, Images: s.Images,
Uniforms: s.Uniforms, Uniforms: s.Uniforms,
GeoM: *m, GeoM: m,
} })
c.DrawRectShader(w, h, s.Shader, opts)
} }
// Return the rectangle that contains the sprite. // Return the rectangle that contains the sprite.

View file

@ -6,7 +6,7 @@ import (
) )
type Transformer interface { type Transformer interface {
GetTransform() *Transform GetTransform() Transform
} }
// The structure represents basic transformation // The structure represents basic transformation
@ -25,7 +25,7 @@ type Transform struct {
Parent Transformer Parent Transformer
} }
func (t *Transform) GetTransform() *Transform { func (t Transform) GetTransform() Transform {
return t return t
} }
@ -40,20 +40,6 @@ func T() Transform {
return ret return ret
} }
func (t Transform) ScaledToXY(x, y Float) Transform {
return t.ScaledToX(x).ScaledToY(y)
}
func (t Transform) ScaledToX(x Float) Transform {
t.Scale.X = x
return t
}
func (t Transform) ScaledToY(y Float) Transform {
t.Scale.Y = y
return t
}
func (t *Transform) SetAbsPosition(absPosition Vector) { func (t *Transform) SetAbsPosition(absPosition Vector) {
if t.Parent == nil { if t.Parent == nil {
t.Position = absPosition t.Position = absPosition
@ -65,9 +51,9 @@ func (t *Transform) SetAbsPosition(absPosition Vector) {
} }
// Get the absolute representation of the transform. // Get the absolute representation of the transform.
func (t *Transform) Abs() Transform { func (t Transform) Abs() Transform {
if t.Parent == nil { if t.Parent == nil {
return *t return t
} }
ret := T() ret := T()
@ -78,21 +64,21 @@ func (t *Transform) Abs() Transform {
return ret return ret
} }
func (t *Transform) AbsPosition() Vector { func (t Transform) AbsPosition() Vector {
if t.Parent == nil { if t.Parent == nil {
return t.Position return t.Position
} }
return t.Position.Apply(t.ParentMatrix()) return t.Position.Apply(t.ParentMatrix())
} }
func (t *Transform) AbsScale() Vector { func (t Transform) AbsScale() Vector {
if t.Parent == nil { if t.Parent == nil {
return t.Scale return t.Scale
} }
return V2(1) return V2(1)
} }
func (t *Transform) AbsRotation() Float { func (t Transform) AbsRotation() Float {
if t.Parent == nil { if t.Parent == nil {
return t.Rotation return t.Rotation
} }
@ -125,8 +111,8 @@ func (t *Transform) Disconnect() {
*t = t.Abs() *t = t.Abs()
} }
func (t *Transform) ParentMatrix() *Matrix { func (t Transform) ParentMatrix() Matrix {
g := &Matrix{} g := Matrix{}
if t.Parent == nil { if t.Parent == nil {
return g return g
} }
@ -143,8 +129,8 @@ func (t *Transform) ParentMatrix() *Matrix {
// Returns the GeoM with corresponding // Returns the GeoM with corresponding
// to the transfrom transformation. // to the transfrom transformation.
func (t *Transform)Matrix() *Matrix { func (t Transform)Matrix() Matrix {
g := &Matrix{} g := Matrix{}
// Scale first. // Scale first.
g.Scale(t.Scale.X, t.Scale.Y) g.Scale(t.Scale.X, t.Scale.Y)
@ -160,7 +146,7 @@ func (t *Transform)Matrix() *Matrix {
g.Translate(t.Position.X, t.Position.Y) g.Translate(t.Position.X, t.Position.Y)
m := t.ParentMatrix() m := t.ParentMatrix()
g.Concat(*m) g.Concat(m)
return g return g
} }

View file

@ -90,7 +90,6 @@ func (ts Triangles) ContainsPoint(p Point) bool {
func (r *DrawableTriangles) Draw(c *Context) { func (r *DrawableTriangles) Draw(c *Context) {
m := c.Camera.RealMatrix() m := c.Camera.RealMatrix()
cm := &m
// Draw solid color if no shader. // Draw solid color if no shader.
if r.Shader == nil { if r.Shader == nil {
@ -99,7 +98,7 @@ func (r *DrawableTriangles) Draw(c *Context) {
buf.Color = r.Color buf.Color = r.Color
for i := range r.Triangles { for i := range r.Triangles {
for j := range r.Triangles[i] { for j := range r.Triangles[i] {
buf.Dst = r.Triangles[i][j].Apply(cm) buf.Dst = r.Triangles[i][j].Apply(m)
vs[i*3 + j] = buf.Ebiten() vs[i*3 + j] = buf.Ebiten()
} }
} }

View file

@ -45,9 +45,9 @@ func (v Vector) Eq(o Vector) bool {
} }
// Returns the vector with the matrix applied // Returns the vector with the matrix applied
func (v Vector) Apply(m *Matrix) Vector { func (v Vector) Apply(m Matrix) Vector {
x, y := m.Apply(v.X, v.Y) x, y := m.Apply(v.X, v.Y)
return V(x, y) return Vector{x, y}
} }
// Adds the vector to other one returning the result. // Adds the vector to other one returning the result.
@ -80,7 +80,7 @@ func (v Vector) Neg() Vector {
// Returns the vector rotated by "a" angle in radians. // Returns the vector rotated by "a" angle in radians.
func (v Vector) Rotate(a Float) Vector { func (v Vector) Rotate(a Float) Vector {
m := &Matrix{} m := Matrix{}
m.Rotate(a) m.Rotate(a)
return v.Apply(m) return v.Apply(m)
} }