automated snapshot

This commit is contained in:
sumi
2025-12-22 17:50:36 -06:00
parent 96a4a34ae0
commit aea3c71cf9
3 changed files with 53 additions and 23 deletions

View File

@@ -54,8 +54,8 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
// render onto all layer textures
for _, instance := range s.layerToolsOrdered {
instance.layer.Update(ctx)
layer := instance.layer
if instance.layer.IsDirty() {
layer := instance.layer
rl.BeginTextureMode(*instance.texture)
layer.Draw(ctx)
rl.EndTextureMode()
@@ -64,12 +64,7 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
// composite all layers to screen
screen := rl.Rectangle {
X: 0,
Y: 0,
Width: float32(ctx.TargetWidth),
Height: float32(ctx.TargetHeight),
}
outputRect := s.calcOutputRectKeepingAspectRatio(ctx)
// copy from full texture for compositing, with vertical flipping
src := rl.Rectangle {
@@ -92,7 +87,40 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
}
rl.EndTextureMode()
rl.DrawTexturePro(s.composite.Texture, viewport, screen, rl.Vector2{}, 0, rl.White)
rl.DrawTexturePro(s.composite.Texture, viewport, outputRect, rl.Vector2{}, 0, rl.White)
outlineRect := outputRect.ToInt32()
rl.DrawRectangleLines(outlineRect.X, outlineRect.Y, outlineRect.Width, outlineRect.Height, rl.Gray)
}
func (s *Sketch) calcOutputRectKeepingAspectRatio(ctx *RenderCtx) rl.Rectangle {
sourceAspect := float32(ctx.SourceWidth) / float32(ctx.SourceHeight)
targetAspect := ctx.TargetBounds.Width / ctx.TargetBounds.Height
outputWidth := ctx.TargetBounds.Width
outputHeight := ctx.TargetBounds.Height
if sourceAspect < targetAspect {
// source is relatively taller than the target
// so we set the output height to the target height
// and calculate the width based on source aspect and center
outputWidth = outputHeight * sourceAspect
} else {
// source is relatively wider than the target
// so we set the output width to the target width
// and calculate the height based on source aspect and center
outputHeight = outputWidth / sourceAspect
}
// output width and height are correct -- center within TargetBounds
x := ctx.TargetBounds.X + ctx.TargetBounds.Width / 2.0 - outputWidth / 2.0
y := ctx.TargetBounds.Y + ctx.TargetBounds.Height / 2.0 - outputHeight / 2.0
return rl.Rectangle {
X: x, Y: y,
Width: outputWidth,
Height: outputHeight,
}
}
func (s *Sketch) CalcViewport(ctx *RenderCtx) rl.Rectangle {
@@ -111,6 +139,7 @@ func (s *Sketch) Update(ctx *RenderCtx) {
if rl.IsMouseButtonDown(rl.MouseRightButton) {
// get mouse delta from last frame
delta := rl.GetMouseDelta()
sourceScale := float32(ctx.SourceWidth) / ctx.TargetBounds.Width
// compute the amount to move scaled by the camera zoom
delta = rl.Vector2Scale(delta, -sourceScale/s.cam.Zoom)
delta.Y = -delta.Y
@@ -233,8 +262,7 @@ func (p Ports) Eval(t float64) map[string]float64 {
/** RenderCtx **/
type RenderCtx struct {
TargetWidth int32
TargetHeight int32
TargetBounds rl.Rectangle
SourceWidth int32
SourceHeight int32
Time float64