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

@@ -36,7 +36,7 @@ func (s *ContourLayer) AddActors(n, sourceWidth, sourceHeight int32) {
position: rl.Vector2{X: float32(x), Y: float32(y)},
field: s.field,
stepSize: 1,
color: rl.NewColor(11, 35, 176, 20),
color: rl.NewColor(11, 35, 176, 50),
}
s.actors = append(s.actors, newActor)
}

26
main.go
View File

@@ -12,16 +12,16 @@ import (
)
const (
targetWidth = 1000
targetHeight = 1000
sourceScale = 2
snapshotsDir = "snapshots"
)
func main() {
sourceWidth := int32(sourceScale * targetWidth)
sourceHeight := int32(sourceScale * targetHeight)
var sourceWidth int32 = 8000
var sourceHeight int32 = 6400
var targetWidth int32 = int32(2000)
var targetHeight int32 = int32(1200)
os.MkdirAll(snapshotsDir, 0755)
log := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile)
@@ -47,7 +47,7 @@ func main() {
x: -float32(sourceWidth / 2.0),
y: -float32(sourceHeight / 2.0),
field: &ScaleField{
scale: 1.0,
scale: 4.0,
field: &imageField,
},
}
@@ -74,10 +74,16 @@ func main() {
// begin drawing
t := time.Since(t0).Seconds()
targetBounds := rl.Rectangle {
X: float32(targetWidth) / 4.0,
Y: 0,
Width: float32(targetWidth) * 3.0 / 4.0,
Height: float32(targetHeight),
}
// set up RenderCtx
renderCtx := &RenderCtx{
TargetWidth: int32(targetWidth),
TargetHeight: int32(targetHeight),
TargetBounds: targetBounds,
SourceWidth: int32(sourceWidth),
SourceHeight: int32(sourceHeight),
Time: t,
@@ -91,11 +97,7 @@ func main() {
*/
rl.BeginDrawing()
rl.ClearBackground(rl.Blank)
sketch.Draw(renderCtx)
rl.DrawText("Mouse right button drag to move, mouse wheel to zoom", 10, 10, 20, rl.White)
rl.EndDrawing()
if rl.IsKeyDown(rl.KeySpace) {

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