feat: trying to optimize the damn triangles.

This commit is contained in:
Andrey Parhomenko 2024-01-11 04:45:28 +03:00
parent a35bf9b27a
commit 08c950a669
5 changed files with 32 additions and 9 deletions

View file

@ -11,6 +11,7 @@ type Player struct {
gg.Sprite gg.Sprite
MoveSpeed gg.Float MoveSpeed gg.Float
ScaleSpeed gg.Float ScaleSpeed gg.Float
Spawned bool
} }
func NewPlayer() *Player { func NewPlayer() *Player {
@ -38,6 +39,7 @@ func (p *Player) Start(c *Context) {
// Custom drawing function. // Custom drawing function.
func (p *Player) Draw(c *Context) { func (p *Player) Draw(c *Context) {
p.Sprite.Draw(c) p.Sprite.Draw(c)
return
t := p.Transform t := p.Transform
t.Scale.X *= 4. t.Scale.X *= 4.
t.Scale.Y *= 4. t.Scale.Y *= 4.
@ -51,6 +53,9 @@ func (p *Player) Draw(c *Context) {
} }
func (p *Player) Update(c *Context) { func (p *Player) Update(c *Context) {
if p.Spawned {
return
}
dt := c.DT() dt := c.DT()
cam := c.Camera cam := c.Camera
keys := c.Keys() keys := c.Keys()
@ -108,6 +113,11 @@ func (p *Player) Update(c *Context) {
case gg.Key0: case gg.Key0:
c.Del(p) c.Del(p)
case gg.KeyB: case gg.KeyB:
case gg.Key5:
pp := *p
counter++
p.Spawned = true
c.Spawn(&pp)
} }
} }

View file

@ -32,15 +32,15 @@ func NewTri() *Tri {
} }
func (t *Tri) Update(c *Context) { func (t *Tri) Update(c *Context) {
if t.Spawned {
return
}
dt := c.DT() dt := c.DT()
if t.ContainsPoint(c.AbsCursorPosition()) { if t.ContainsPoint(c.AbsCursorPosition()) {
t.Color = gg.Rgba(0, 1, 0, 1) t.Color = gg.Rgba(0, 1, 0, 1)
} else { } else {
t.Color = gg.Rgba(1, 0, 1, 1) t.Color = gg.Rgba(1, 0, 1, 1)
} }
if t.Spawned {
return
}
keys := c.Keys() keys := c.Keys()
@ -77,7 +77,6 @@ func (t *Tri) Update(c *Context) {
} }
tt := *t tt := *t
tt.Spawned = true tt.Spawned = true
tt.Visible = false
tt.Disconnect() tt.Disconnect()
if c.Spawn(&tt) == nil { if c.Spawn(&tt) == nil {
counter++ counter++

View file

@ -74,7 +74,9 @@ type Engine struct {
outerEvents, handleEvents EventChan outerEvents, handleEvents EventChan
wg sync.WaitGroup wg sync.WaitGroup
bufs [LayerBufSize]*Image //bufs [LayerBufSize]*Image
vertices []ebiten.Vertex
vindices []uint16
} }
type engine Engine type engine Engine
@ -296,6 +298,8 @@ func (e *engine) Update() error {
} }
func (e *engine) Draw(i *ebiten.Image) { func (e *engine) Draw(i *ebiten.Image) {
e.vindices = []uint16{}
e.vertices = []ebiten.Vertex{}
e.dt = time.Since(e.lastTime).Seconds() e.dt = time.Since(e.lastTime).Seconds()
eng := (*Engine)(e) eng := (*Engine)(e)
m := map[Layer][]Drawer{} m := map[Layer][]Drawer{}
@ -323,6 +327,9 @@ func (e *engine) Draw(i *ebiten.Image) {
drawer.Draw(c) 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. // Empty the buff to generate it again.
eng.Camera.buffered = false eng.Camera.buffered = false

View file

@ -13,6 +13,10 @@ type Sprite struct {
Visibility Visibility
} }
var (
//spritesOp DrawImageOptions
)
func (s *Sprite) Draw(c *Context) { func (s *Sprite) Draw(c *Context) {
// Nothing to draw. // Nothing to draw.
if s.Images[0] == nil { if s.Images[0] == nil {

View file

@ -104,14 +104,17 @@ func (r *DrawableTriangles) Draw(c *Context) {
} }
is := make([]uint16, len(r.Triangles) * 3) is := make([]uint16, len(r.Triangles) * 3)
start := len(c.Engine.vindices)
for i := 0 ; i < len(is) ; i++ { 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 return
} }