From 6e76188bd8e37aad127e0bf212bbba93e4ffe068 Mon Sep 17 00:00:00 2001 From: surdeus Date: Mon, 8 Jan 2024 02:53:00 +0300 Subject: [PATCH] feat: use values not pointers for matrices. --- engine.go | 8 +++----- polygon.go | 4 ++-- rect.go | 15 ++++++--------- sprite.go | 17 +++++++---------- transform.go | 38 ++++++++++++-------------------------- triangle.go | 3 +-- vector.go | 6 +++--- 7 files changed, 34 insertions(+), 57 deletions(-) diff --git a/engine.go b/engine.go index 5dd0389..94f321c 100644 --- a/engine.go +++ b/engine.go @@ -206,9 +206,7 @@ func (e *Engine) CursorPosition() Vector { } func (e *Engine) AbsCursorPosition() Vector { - m := &Matrix{} - m.Concat(e.Camera.AbsMatrix()) - return e.CursorPosition().Apply(m) + return e.CursorPosition().Apply(e.Camera.AbsMatrix()) } func (e *engine) Update() error { @@ -278,8 +276,8 @@ func (e *engine) Update() error { if !realPos.Eq(eng.cursorPos) { absM := eng.Camera.AbsMatrix() - absPrevPos :=eng.cursorPos.Apply(&absM) - absPos := realPos.Apply(&absM) + absPrevPos :=eng.cursorPos.Apply(absM) + absPos := realPos.Apply(absM) events = append(events, &MouseMove{ Real: realPos.Sub(eng.cursorPos), diff --git a/polygon.go b/polygon.go index d10a0e9..3decfb5 100644 --- a/polygon.go +++ b/polygon.go @@ -9,7 +9,7 @@ type Polygon struct { Triangles } -func (p *Polygon) ContainsPoint(pnt Point) bool { +func (p Polygon) ContainsPoint(pnt Point) bool { return p.MakeTriangles().ContainsPoint(pnt) } @@ -22,7 +22,7 @@ type DrawablePolygon struct { Colority } -func (p *Polygon) MakeTriangles() Triangles { +func (p Polygon) MakeTriangles() Triangles { m := p.Matrix() ret := make(Triangles, len(p.Triangles)) for i, t := range p.Triangles { diff --git a/rect.go b/rect.go index 06dd36e..59887fa 100644 --- a/rect.go +++ b/rect.go @@ -79,11 +79,9 @@ func (r *DrawableRectangle) Draw(c *Context) { img := NewImage(1, 1) img.Set(0, 0, r.Color) - - opts := &ebiten.DrawImageOptions{ - GeoM: *m, - } - c.DrawImage(img, opts) + c.DrawImage(img, &ebiten.DrawImageOptions{ + GeoM: m, + }) return } @@ -98,11 +96,10 @@ func (r *DrawableRectangle) Draw(c *Context) { w, h := r.Images[0].Size() // Drawing with shader. - opts := &ebiten.DrawRectShaderOptions{ - GeoM: *m, + c.DrawRectShader(w, h, r.Shader, &ebiten.DrawRectShaderOptions{ + GeoM: m, Images: r.Images, Uniforms: r.Uniforms, - } - c.DrawRectShader(w, h, r.Shader, opts) + }) } diff --git a/sprite.go b/sprite.go index c91c078..cc5d887 100644 --- a/sprite.go +++ b/sprite.go @@ -19,29 +19,26 @@ func (s *Sprite) Draw(c *Context) { } t := s.Rectangle().Transform - m := &Matrix{} - tm := t.Matrix() - m.Concat(*tm) + m := t.Matrix() if !s.Floating { m.Concat(c.Camera.RealMatrix()) } // Drawing without shader. if s.Shader == nil { - opts := &ebiten.DrawImageOptions{} - opts.GeoM = *m - c.DrawImage(s.Images[0], opts) + c.DrawImage(s.Images[0], &ebiten.DrawImageOptions{ + GeoM: m, + }) return } w, h := s.Images[0].Size() // Drawing with shader. - opts := &ebiten.DrawRectShaderOptions{ + c.DrawRectShader(w, h, s.Shader, &ebiten.DrawRectShaderOptions{ Images: s.Images, Uniforms: s.Uniforms, - GeoM: *m, - } - c.DrawRectShader(w, h, s.Shader, opts) + GeoM: m, + }) } // Return the rectangle that contains the sprite. diff --git a/transform.go b/transform.go index a408d75..ca6d468 100644 --- a/transform.go +++ b/transform.go @@ -6,7 +6,7 @@ import ( ) type Transformer interface { - GetTransform() *Transform + GetTransform() Transform } // The structure represents basic transformation @@ -25,7 +25,7 @@ type Transform struct { Parent Transformer } -func (t *Transform) GetTransform() *Transform { +func (t Transform) GetTransform() Transform { return t } @@ -40,20 +40,6 @@ func T() Transform { 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) { if t.Parent == nil { t.Position = absPosition @@ -65,9 +51,9 @@ func (t *Transform) SetAbsPosition(absPosition Vector) { } // Get the absolute representation of the transform. -func (t *Transform) Abs() Transform { +func (t Transform) Abs() Transform { if t.Parent == nil { - return *t + return t } ret := T() @@ -78,21 +64,21 @@ func (t *Transform) Abs() Transform { return ret } -func (t *Transform) AbsPosition() Vector { +func (t Transform) AbsPosition() Vector { if t.Parent == nil { return t.Position } return t.Position.Apply(t.ParentMatrix()) } -func (t *Transform) AbsScale() Vector { +func (t Transform) AbsScale() Vector { if t.Parent == nil { return t.Scale } return V2(1) } -func (t *Transform) AbsRotation() Float { +func (t Transform) AbsRotation() Float { if t.Parent == nil { return t.Rotation } @@ -125,8 +111,8 @@ func (t *Transform) Disconnect() { *t = t.Abs() } -func (t *Transform) ParentMatrix() *Matrix { - g := &Matrix{} +func (t Transform) ParentMatrix() Matrix { + g := Matrix{} if t.Parent == nil { return g } @@ -143,8 +129,8 @@ func (t *Transform) ParentMatrix() *Matrix { // Returns the GeoM with corresponding // to the transfrom transformation. -func (t *Transform)Matrix() *Matrix { - g := &Matrix{} +func (t Transform)Matrix() Matrix { + g := Matrix{} // Scale first. 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) m := t.ParentMatrix() - g.Concat(*m) + g.Concat(m) return g } diff --git a/triangle.go b/triangle.go index 655b4a0..53d8fc9 100644 --- a/triangle.go +++ b/triangle.go @@ -90,7 +90,6 @@ func (ts Triangles) ContainsPoint(p Point) bool { func (r *DrawableTriangles) Draw(c *Context) { m := c.Camera.RealMatrix() - cm := &m // Draw solid color if no shader. if r.Shader == nil { @@ -99,7 +98,7 @@ func (r *DrawableTriangles) Draw(c *Context) { buf.Color = r.Color for i := range r.Triangles { 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() } } diff --git a/vector.go b/vector.go index abf0949..5ad417e 100644 --- a/vector.go +++ b/vector.go @@ -45,9 +45,9 @@ func (v Vector) Eq(o Vector) bool { } // 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) - return V(x, y) + return Vector{x, y} } // 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. func (v Vector) Rotate(a Float) Vector { - m := &Matrix{} + m := Matrix{} m.Rotate(a) return v.Apply(m) }