Files
sumi/sketch.go
2025-12-20 01:51:28 -06:00

107 lines
2.0 KiB
Go

package main
import (
"github.com/gen2brain/raylib-go/raylib"
)
type Sketch struct {
layerTools map[string]LayerTools
}
func NewSketch() Sketch {
return Sketch {
layerTools: make(map[string]LayerTools),
}
}
func (s *Sketch) CreateLayer(name string, layer Layer, sourceWidth int32, sourceHeight int32) {
texture := rl.LoadRenderTexture(sourceWidth, sourceHeight)
s.layerTools[name] = LayerTools {
name: name,
texture: &texture,
layer: layer,
}
}
func (s *Sketch) Draw(ctx *RenderCtx) {
// render onto all layer textures
for _, instance := range s.layerTools {
layer := instance.layer
rl.BeginTextureMode(*instance.texture)
layer.Draw(ctx)
rl.EndTextureMode()
}
// composite all layers to screen
src := rl.Rectangle {
X: 0, Y: 0,
Width: float32(ctx.SourceWidth),
Height: -float32(ctx.SourceHeight),
}
dst := rl.Rectangle {
X: 0, Y: 0,
Width: float32(ctx.TargetWidth),
Height: float32(ctx.TargetHeight),
}
for _, instance := range s.layerTools {
rl.DrawTexturePro(instance.texture.Texture, src, dst, rl.Vector2{}, 0, rl.White)
}
}
type LayerTools struct {
name string
layer Layer
texture *rl.RenderTexture2D
}
/** Layer **/
type Layer interface {
Draw(ctx *RenderCtx)
}
type TestPattern struct { }
func (tp *TestPattern) Draw(ctx *RenderCtx) {
rl.DrawRectangle(0, 0, int32(ctx.SourceWidth), int32(ctx.SourceHeight), rl.Magenta)
rl.PushMatrix()
rl.Translatef(float32(ctx.SourceWidth)/2.0, float32(ctx.SourceHeight)/2.0, 0.0)
rl.DrawRectangle(-100, -100, 200, 200, rl.Green)
rl.PopMatrix()
}
/** Ports **/
type Ports map[string]Signal
func MakePorts() Ports {
return make(Ports)
}
/**
* materialize current value for all ports
**/
func (p Ports) Eval(t float64) map[string]float64 {
out := make(map[string]float64, len(p))
for name, sig := range p {
out[name] = sig.Eval(t)
}
return out
}
/** RenderCtx **/
type RenderCtx struct {
TargetWidth int32
TargetHeight int32
SourceWidth int32
SourceHeight int32
Time float64
Ports map[string]float64
Cam rl.Camera2D
}