diff --git a/contour_layer.go b/contour_layer.go index 1bd0ffb..a2b2ef0 100644 --- a/contour_layer.go +++ b/contour_layer.go @@ -1,6 +1,7 @@ package main import ( + "fmt" "math" "math/rand" "github.com/gen2brain/raylib-go/raylib" @@ -8,21 +9,27 @@ import ( type ContourLayer struct { field Field + maxActors uint32 actors []*Actor + actorIndex uint32 rng *rand.Rand } func NewContourLayer(sketch *Sketch, rng *rand.Rand, field Field) *ContourLayer { - actors := make([]*Actor, 0) + maxActors := 200000 + + actors := make([]*Actor, maxActors) layer := ContourLayer { rng: rng, field: field, actors: actors, + maxActors: uint32(maxActors), + actorIndex: 0, } - layer.AddActors(1, sketch.sourceWidth, sketch.sourceHeight) + //layer.AddActors(1, sketch.sourceWidth, sketch.sourceHeight) return &layer } @@ -38,18 +45,22 @@ func (s *ContourLayer) AddActors(n, sourceWidth, sourceHeight int32) { stepSize: 1, color: rl.NewColor(11, 35, 176, 50), } - s.actors = append(s.actors, newActor) + s.actors[s.actorIndex] = newActor + s.actorIndex = (s.actorIndex + 1) % s.maxActors } } func (s *ContourLayer) Update(ctx *RenderCtx) { s.AddActors(100, ctx.SourceWidth, ctx.SourceHeight) + fmt.Printf("num actors = %d\n", len(s.actors)) } func (s *ContourLayer) Draw(ctx *RenderCtx) { rl.BeginBlendMode(rl.BlendAdditive) for _, actor := range s.actors { - actor.Draw() + if actor != nil { + actor.Draw() + } } rl.EndBlendMode() } diff --git a/sketch.go b/sketch.go index a2dd3d2..7de9540 100644 --- a/sketch.go +++ b/sketch.go @@ -90,22 +90,28 @@ func (s *Sketch) AddColorLayer(name string, c rl.Color) { s.AddLayer(name, colorLayer) } -func (s *Sketch) Draw(ctx *RenderCtx) { +func (s *Sketch) Redraw(ctx *RenderCtx) { // render onto all layer textures for _, instance := range s.layerToolsOrdered { layer := instance.layer - layer.Update(ctx) - if instance.layer.IsDirty() { - rl.BeginTextureMode(instance.texture) - rl.PushMatrix() - layer.Draw(ctx) - rl.PopMatrix() - rl.EndTextureMode() + // ignore this layer entirely unless it's visible + if instance.config.visible { + layer.Update(ctx) + // re-render to texture if dirty + if instance.layer.IsDirty() { + rl.BeginTextureMode(instance.texture) + rl.PushMatrix() + layer.Draw(ctx) + rl.PopMatrix() + rl.EndTextureMode() + } } } +} - // composite all layers to screen +func (s *Sketch) Draw(ctx *RenderCtx) { + s.Redraw(ctx) // copy from full texture for compositing, with vertical flipping src := rl.Rectangle { @@ -122,7 +128,6 @@ func (s *Sketch) Draw(ctx *RenderCtx) { viewport := s.CalcViewport(ctx) rl.BeginTextureMode(s.composite) - //rl.ClearBackground(rl.Black) for _, instance := range s.layerToolsOrdered { config := instance.config if config.visible {