From 0f6732bcf99786a13a8ba4a54d055d4eb19fa551 Mon Sep 17 00:00:00 2001 From: surdeus Date: Thu, 11 Jan 2024 06:39:57 +0300 Subject: [PATCH] feat: implemented layering for DrawableTriangles. --- cmd/test/debug.go | 9 +++--- cmd/test/player.go | 20 ++---------- cmd/test/trianlge.go | 6 ++-- engine.go | 75 ++++++++++++++++++++++++++++++++++---------- object.go | 4 +-- polygon.go | 8 ++--- rect.go | 31 +++++++----------- sprite.go | 11 ++++--- triangle.go | 60 ++++++++--------------------------- 9 files changed, 103 insertions(+), 121 deletions(-) diff --git a/cmd/test/debug.go b/cmd/test/debug.go index dd8d16e..f869971 100644 --- a/cmd/test/debug.go +++ b/cmd/test/debug.go @@ -11,17 +11,17 @@ type Debug struct { gg.Object } -func (d *Debug) Draw(c *Context) { +func (d *Debug) Draw(c *Context) []gg.EVertex { e := c.Engine keyStrs := []string{} keyStrs = append(keyStrs, fmt.Sprintf( "counter: %d", counter, )) keyStrs = append(keyStrs, fmt.Sprintf( - "tps: %d", int(c.TPS()), + "tps: %d", int(c.Tps()), )) keyStrs = append(keyStrs, fmt.Sprintf( - "fps: %d", int(c.FPS()), + "fps: %d", int(c.Fps()), )) keyStrs = append(keyStrs, fmt.Sprintf( "relPlayerPos: %v", player.Position, @@ -74,7 +74,8 @@ func (d *Debug) Draw(c *Context) { e.DebugPrint(c.Image, strings.Join(keyStrs, "\n")) - + + return nil } func (d *Debug) IsVisible() bool { return true } diff --git a/cmd/test/player.go b/cmd/test/player.go index 7df7c27..25b59c1 100644 --- a/cmd/test/player.go +++ b/cmd/test/player.go @@ -36,27 +36,11 @@ func NewPlayer() *Player { 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. - - r := &gg.DrawableRectangle{} - r.Color = gg.Rgba(0, 0, 1, 1) - r.Rectangle = gg.Rectangle{ - Transform: t, - } - r.Draw(c) -} - func (p *Player) Update(c *Context) { if p.Spawned { return } - dt := c.DT() + dt := c.Dt() cam := c.Camera keys := c.Keys() @@ -140,7 +124,7 @@ func (p *Player) Update(c *Context) { c.Camera.Position = pos.Sub(ec.Abs) case *gg.WheelChange: c.Camera.Scale = c.Camera.Scale.Add(gg.V2( - ec.Offset.Y * c.DT() * p.ScaleSpeed * 40, + ec.Offset.Y * c.Dt() * p.ScaleSpeed * 40, )) }} } diff --git a/cmd/test/trianlge.go b/cmd/test/trianlge.go index 72df453..323611d 100644 --- a/cmd/test/trianlge.go +++ b/cmd/test/trianlge.go @@ -32,7 +32,7 @@ func NewTri() *Tri { } func (t *Tri) Update(c *Context) { - dt := c.DT() + dt := c.Dt() if t.ContainsPoint(c.AbsCursorPosition()) { t.Color = gg.Rgba(0, 1, 0, 1) } else { @@ -53,12 +53,12 @@ func (t *Tri) Update(c *Context) { case gg.KeyM: absPos := t.AbsPosition() t.SetAbsPosition( - absPos.Add(gg.V(0, 100*c.DT()*d)), + absPos.Add(gg.V(0, 100*c.Dt()*d)), ) case gg.KeyN: absPos := t.AbsPosition() t.SetAbsPosition( - absPos.Add(gg.V(100*c.DT()*d, 0)), + absPos.Add(gg.V(100*c.Dt()*d, 0)), ) case gg.KeyV: t.Rotation += d * gg.Pi * 0.3 * dt diff --git a/engine.go b/engine.go index 372bcd7..5b1c82c 100644 --- a/engine.go +++ b/engine.go @@ -11,7 +11,7 @@ import ( ) const ( - LayerBufSize = 0 + MaxVertices = 1 << 16 ) type GraphicsLibrary = ebiten.GraphicsLibrary @@ -64,7 +64,7 @@ type Engine struct { // The same delta time for all frames // and all objects. lastTime time.Time - dt Float + dt, fdt Float // Temporary stuff keys, prevKeys []Key @@ -75,8 +75,8 @@ type Engine struct { wg sync.WaitGroup //bufs [LayerBufSize]*Image - vertices []ebiten.Vertex - vindices []uint16 + vertices map[Layer] []ebiten.Vertex + //vindices []uint16 } type engine Engine @@ -297,9 +297,22 @@ func (e *engine) Update() error { return nil } -func (e *engine) Draw(i *ebiten.Image) { - e.vindices = []uint16{} - e.vertices = []ebiten.Vertex{} +var ( + fullPageIndexes = func() [MaxVertices]uint16 { + ret := [MaxVertices]uint16{} + for i:=0 ; i