From 08c950a66931630cea76df831ff1c803c9578bb4 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 11 Jan 2024 04:45:28 +0300 Subject: [PATCH] feat: trying to optimize the damn triangles. --- cmd/test/player.go | 10 ++++++++++ cmd/test/trianlge.go | 7 +++---- engine.go | 9 ++++++++- sprite.go | 4 ++++ triangle.go | 11 +++++++---- 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/cmd/test/player.go b/cmd/test/player.go index 38fb26b..7df7c27 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -11,6 +11,7 @@ type Player struct { gg.Sprite MoveSpeed gg.Float ScaleSpeed gg.Float + Spawned bool } func NewPlayer() *Player { @@ -38,6 +39,7 @@ func (p *Player) Start(c *Context) { // Custom drawing function. func (p *Player) Draw(c *Context) { p.Sprite.Draw(c) + return t := p.Transform t.Scale.X *= 4. t.Scale.Y *= 4. @@ -51,6 +53,9 @@ func (p *Player) Draw(c *Context) { } func (p *Player) Update(c *Context) { + if p.Spawned { + return + } dt := c.DT() cam := c.Camera keys := c.Keys() @@ -108,6 +113,11 @@ func (p *Player) Update(c *Context) { case gg.Key0: c.Del(p) case gg.KeyB: + case gg.Key5: + pp := *p + counter++ + p.Spawned = true + c.Spawn(&pp) } } diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 8dcf030..72df453 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -32,15 +32,15 @@ func NewTri() *Tri { } func (t *Tri) Update(c *Context) { - if t.Spawned { - return - } dt := c.DT() if t.ContainsPoint(c.AbsCursorPosition()) { t.Color = gg.Rgba(0, 1, 0, 1) } else { t.Color = gg.Rgba(1, 0, 1, 1) } + if t.Spawned { + return + } keys := c.Keys() @@ -77,7 +77,6 @@ func (t *Tri) Update(c *Context) { } tt := *t tt.Spawned = true - tt.Visible = false tt.Disconnect() if c.Spawn(&tt) == nil { counter++ diff --git a/engine.go b/engine.go index aa8ef8b..372bcd7 100644 --- a/engine.go +++ b/engine.go @@ -74,7 +74,9 @@ type Engine struct { outerEvents, handleEvents EventChan wg sync.WaitGroup - bufs [LayerBufSize]*Image + //bufs [LayerBufSize]*Image + vertices []ebiten.Vertex + vindices []uint16 } type engine Engine @@ -296,6 +298,8 @@ func (e *engine) Update() error { } func (e *engine) Draw(i *ebiten.Image) { + e.vindices = []uint16{} + e.vertices = []ebiten.Vertex{} e.dt = time.Since(e.lastTime).Seconds() eng := (*Engine)(e) m := map[Layer][]Drawer{} @@ -323,6 +327,9 @@ func (e *engine) Draw(i *ebiten.Image) { drawer.Draw(c) } } + img := NewImage(1, 1) + img.Set(0, 0, Rgba(1, 1, 1, 1)) + i.DrawTriangles(e.vertices, e.vindices, img, &ebiten.DrawTrianglesOptions{}) // Empty the buff to generate it again. eng.Camera.buffered = false diff --git a/sprite.go b/sprite.go index c747263..7039ac3 100644 --- a/sprite.go +++ b/sprite.go @@ -13,6 +13,10 @@ type Sprite struct { Visibility } +var ( + //spritesOp DrawImageOptions +) + func (s *Sprite) Draw(c *Context) { // Nothing to draw. if s.Images[0] == nil { diff --git a/triangle.go b/triangle.go index 53d8fc9..aa47417 100644 --- a/triangle.go +++ b/triangle.go @@ -104,14 +104,17 @@ func (r *DrawableTriangles) Draw(c *Context) { } is := make([]uint16, len(r.Triangles) * 3) + start := len(c.Engine.vindices) for i := 0 ; i < len(is) ; i++ { - is[i] = uint16(i) + is[i] = uint16(start + i) } + + c.Engine.vertices = append(c.Engine.vertices, vs...) + c.Engine.vindices = append(c.Engine.vindices, is...) - img := NewImage(1, 1) - img.Set(0, 0, r.Color) - c.DrawTriangles(vs, is, img, &r.DrawTrianglesOptions) + return + //c.DrawTriangles(vs, is, img, &r.DrawTrianglesOptions) return }