160 lines
3.5 KiB
Go
160 lines
3.5 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"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 LayerViewRect(ctx *RenderCtx) rl.Rectangle {
|
|
|
|
z := ctx.Cam.Zoom
|
|
|
|
// get the viewport width in texture space
|
|
viewportWidth := float32(ctx.TargetWidth) / z
|
|
viewportHeight := float32(ctx.TargetHeight) / z
|
|
|
|
x := rl.Clamp(ctx.Cam.Target.X - viewportWidth/2, 0, float32(ctx.SourceWidth) - viewportWidth/2.0)
|
|
y := rl.Clamp(ctx.Cam.Target.Y - viewportHeight/2, 0, float32(ctx.SourceHeight) - viewportHeight/2.0)
|
|
|
|
return rl.Rectangle{
|
|
X: x,
|
|
Y: y,
|
|
Width: viewportWidth,
|
|
Height: viewportHeight,
|
|
}
|
|
}
|
|
|
|
func (s *Sketch) Draw(ctx *RenderCtx) {
|
|
// render onto all layer textures
|
|
for _, instance := range s.layerTools {
|
|
layer := instance.layer
|
|
rl.BeginTextureMode(*instance.texture)
|
|
rl.ClearBackground(rl.Blank)
|
|
layer.Draw(ctx)
|
|
rl.EndTextureMode()
|
|
}
|
|
// composite all layers to screen
|
|
|
|
//ClampCameraToLayer(ctx)
|
|
|
|
//view := LayerViewRect(ctx)
|
|
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),
|
|
}
|
|
|
|
fmt.Printf("src = %v, dst %v\n", src, dst)
|
|
|
|
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 DrawGrid(spacing int32, halfExtent int32) {
|
|
col := rl.Color{R: 220, G: 220, B: 220, A: 255}
|
|
|
|
for x := -halfExtent; x <= halfExtent; x += spacing {
|
|
rl.DrawLine(x, -halfExtent, x, halfExtent, col)
|
|
}
|
|
for y := -halfExtent; y <= halfExtent; y += spacing {
|
|
rl.DrawLine(-halfExtent, y, halfExtent, y, col)
|
|
}
|
|
}
|
|
|
|
func (tp *TestPattern) Draw(ctx *RenderCtx) {
|
|
|
|
rl.ClearBackground(rl.Black)
|
|
centerX := float32(ctx.SourceWidth)/2
|
|
centerY := float32(ctx.SourceHeight)/2
|
|
|
|
rl.DrawRectangleRec(rl.Rectangle { X: 0, Y: 0, Width: centerX, Height: centerY }, rl.Red)
|
|
rl.DrawRectangleRec(rl.Rectangle { X: centerX, Y: 0, Width: centerX, Height: centerY }, rl.Green)
|
|
rl.DrawRectangleRec(rl.Rectangle { X: 0, Y: centerY, Width: centerX, Height: centerY }, rl.Blue)
|
|
rl.DrawRectangleRec(rl.Rectangle { X: centerX, Y: centerY, Width: centerX, Height: centerY }, rl.White)
|
|
|
|
rl.DrawLine(0, 0, ctx.SourceWidth, ctx.SourceHeight, rl.Black)
|
|
|
|
rl.PushMatrix()
|
|
rl.Translatef(centerX, centerY, 0)
|
|
rl.SetLineWidth(4.0)
|
|
rl.DrawLine(-10000, 0, 10000, 0, rl.Red)
|
|
rl.DrawLine(0, -10000, 0, 10000, rl.Green)
|
|
rl.DrawRectangleLines(-50, -50, 100, 100, rl.Magenta)
|
|
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
|
|
}
|