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 {
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),

View file

@ -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 {

15
rect.go
View file

@ -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)
})
}

View file

@ -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.

View file

@ -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
}

View file

@ -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()
}
}

View file

@ -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)
}