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

View file

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

View file

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

View file

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

View file

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