automated snapshot

This commit is contained in:
sumi
2026-01-10 16:03:24 -06:00
parent 90e36425ff
commit 0ce5df85aa
5 changed files with 145 additions and 88 deletions

View File

@@ -44,6 +44,8 @@ type LayerConfig struct {
func NewSketch(env *Env) Sketch {
fmt.Printf("Creating new sketch with env: %v\n", env)
// point at source center
// put source center at center of screen
var camera = TextureCam{
@@ -51,13 +53,15 @@ func NewSketch(env *Env) Sketch {
Zoom: 1.0,
}
return Sketch{
s := Sketch {
env: env,
layerTools: make(map[string]*LayerTools),
layerToolsOrdered: []*LayerTools{},
composite: rl.LoadRenderTexture(env.Offscreen.WidthInt32(), env.Offscreen.HeightInt32()),
cam: &camera,
}
return s
}
func (s *Sketch) AddLayer(name string, layer Layer) {
@@ -72,6 +76,8 @@ func (s *Sketch) AddLayer(name string, layer Layer) {
}
s.layerToolsOrdered = append(s.layerToolsOrdered, &layerTools)
s.layerTools[name] = &layerTools
}
func (s *Sketch) AddColorLayer(name string, c rl.Color) {
@@ -94,11 +100,11 @@ func (s *Sketch) RedrawLayers(env *Env, g *sg.Graphics) {
layer.Update(env, g)
// re-render to texture if dirty
if instance.layer.IsDirty() {
lg.Begin()
lg.BeginTexture(instance.texture)
lg.Begin()
layer.Draw(env, lg)
lg.EndTexture()
lg.End()
lg.EndTexture()
}
}
}
@@ -109,11 +115,6 @@ func (s *Sketch) Draw(env *Env) {
offscreen := env.Offscreen
s.RedrawLayers(env, offscreen)
// copy from full texture for compositing, with vertical flipping
src := offscreen.Bounds
src.Height = -src.Height
dst := offscreen.Bounds
/*
src := g.Rect {
X: 0, Y: 0,
@@ -127,14 +128,12 @@ func (s *Sketch) Draw(env *Env) {
}
*/
offscreen.Begin()
// calculate the viewable region of the offscreen buffer
viewport := s.CalcViewport(env)
// scale the offscreen buffer to the viewport maintaining aspect ratio
outputRect := offscreen.Bounds.ScaleTo(env.Viewport.Bounds)
fmt.Printf("outputRect = %v\n", outputRect)
//fmt.Printf("scaling %v to %v => outputRect = %v\n", offscreen.Bounds, env.Viewport.Bounds, outputRect)
x := float32(0)
y := float32(0)
@@ -142,19 +141,20 @@ func (s *Sketch) Draw(env *Env) {
h := outputRect.Height
output := sg.CreateGraphics(outputRect)
// render a checker pattern indicating transparency
output.Begin()
output.SetFill(true)
output.SetStroke(false)
checkSize := float32(outputRect.Width / 30.0)
grey := sg.RGBA(220, 220, 220, 255)
checkSize := float32(outputRect.Width / 50.0)
grey := rl.NewColor(220, 220, 220, 255)
cellX := 0
cellY := 0
for y < h {
x = 0
cellX = 0
for x < w {
c := sg.RGBA(rl.White.R, rl.White.G, rl.White.B, rl.White.A)
c := rl.White
if ((cellX + cellY) & 1) == 1 {
c = grey
}
@@ -167,9 +167,16 @@ func (s *Sketch) Draw(env *Env) {
y += checkSize
cellY++
}
output.End()
// render each layer onto the output graphics context
rl.GenTextureMipmaps(&s.composite.Texture)
rl.SetTextureFilter(s.composite.Texture, rl.FilterTrilinear)
// render each layer onto the composite
offscreen.BeginPremultiplyBlend()
offscreen.BeginTexture(s.composite)
offscreen.Begin()
offscreen.Clear()
//rl.BeginBlendMode(rl.BlendAlphaPremultiply)
@@ -197,35 +204,57 @@ func (s *Sketch) Draw(env *Env) {
g = uint8(float32(g) * (float32(config.a) / 255.0))
b = uint8(float32(b) * (float32(config.a) / 255.0))
tint := rl.NewColor(r, g, b, config.a)
offscreen.TransferTexture(instance.texture.Texture, src, dst, tint)
offscreen.TransferTexture(instance.texture.Texture, offscreen.Bounds, offscreen.Bounds, tint)
//rl.DrawTexturePro(instance.texture.Texture, src.ToRL(), dst.ToRL(), rl.Vector2{}, 0, tint)
}
}
offscreen.EndTexture()
offscreen.End()
offscreen.EndTexture()
offscreen.EndBlend()
rl.GenTextureMipmaps(&s.composite.Texture)
rl.SetTextureFilter(s.composite.Texture, rl.FilterTrilinear)
// minimap indexing for composite so that it scales down nicely
//rl.DrawTexturePro(s.composite.Texture, rl.Rectangle { X: viewport.X, Y: viewport.Y, Width: viewport.Width, Height: viewport.Height}, dst.ToRL(), rl.Vector2{}, 0, rl.White)
output.TransferTexture(s.composite.Texture, viewport, outputRect, rl.White)
//fmt.Printf("viewport -> %v\n", viewport)
output.SetFill(false)
output.SetStroke(true)
output.SetStrokeColor(rl.Gray)
output.DrawRect(outputRect)
// finally, transfer composite texture to output rect on screen
/**
rl.PushMatrix()
rl.BeginScissorMode(int32(offscreen.Bounds.X), int32(offscreen.Bounds.Y), int32(offscreen.Bounds.Width), int32(offscreen.Bounds.Height))
rl.Translatef(float32(offscreen.Bounds.UL().X), offscreen.Bounds.UL().Y, 0)
rl.DrawTexturePro(s.composite.Texture, viewport.ToRL(), outputRect.ToRL(), rl.Vector2{}, 0, rl.White)
rl.EndScissorMode()
rl.PopMatrix()
**/
output.End()
env.Window.Begin()
env.Window.TransferTexture(s.composite.Texture, viewport, outputRect, rl.White)
env.Window.SetFill(false)
env.Window.SetStroke(true)
env.Window.SetStrokeColor(rl.Gray)
env.Window.DrawRect(outputRect)
env.Window.End()
/*
env.Window.Begin()
env.Window.SetStroke(true)
env.Window.SetFill(false)
env.Window.SetStrokeWeight(5.0)
env.Window.SetStrokeColor(rl.Magenta)
env.Window.DrawRect(outputRect)
env.Window.End()
*/
}
// calculate the visible clip of the offscreen buffer based on the camera /zoom
func (s *Sketch) CalcViewport(env *Env) sg.Rect {
viewportWidth := rl.Clamp(env.Offscreen.Width()/s.cam.Zoom, 0, env.Offscreen.Width())
viewportHeight := rl.Clamp(env.Offscreen.Height()/s.cam.Zoom, 0, env.Offscreen.Width())
viewportHeight := rl.Clamp(env.Offscreen.Height()/s.cam.Zoom, 0, env.Offscreen.Height())
return sg.Rect{
X: rl.Clamp(s.cam.LookAt.X-viewportWidth/2.0, 0, env.Layout.Offscreen.Width-viewportWidth),
Y: rl.Clamp(s.cam.LookAt.Y-viewportHeight/2.0, 0, env.Layout.Offscreen.Height-viewportHeight),
Width: viewportWidth,
Height: -viewportHeight,
Height: viewportHeight,
}
}
@@ -325,7 +354,10 @@ func (cl *ColorLayer) Update(ctx *Env, g *sg.Graphics) {
}
func (cl *ColorLayer) Draw(ctx *Env, g *sg.Graphics) {
g.Background(cl.color)
g.SetFill(true)
g.SetFillColor(cl.color)
g.SetStroke(false)
g.DrawRect(g.Bounds)
//rl.ClearBackground(cl.color)
cl.dirty = false
}
@@ -353,10 +385,12 @@ func (il *ImageLayer) Update(ctx *Env, g *sg.Graphics) {
}
func (il *ImageLayer) Draw(env *Env, g *sg.Graphics) {
rl.PushMatrix()
rl.Translatef(
g.Width()/2.0-float32(il.texture.Width)/2.0,
g.Width()/2.0-float32(il.texture.Height)/2.0, 0)
g.Height()/2.0-float32(il.texture.Height)/2.0, 0)
g.DrawTexture(il.texture, sg.Origin, rl.White)
rl.PopMatrix()
}
func (il *ImageLayer) IsDirty() bool {