Files
sumi/main.go
2025-12-22 22:33:57 -06:00

239 lines
6.3 KiB
Go

package main
import (
// "fmt"
"log"
"math/rand"
"os"
"time"
gui "github.com/gen2brain/raylib-go/raygui"
rl "github.com/gen2brain/raylib-go/raylib"
//"github.com/ojrac/opensimplex-go"
)
const (
snapshotsDir = "snapshots"
)
func main() {
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)
storage, err := NewStorage(snapshotsDir)
if err != nil {
log.Printf("Error loading storage: %v\n", err)
os.Exit(1)
}
rl.SetConfigFlags(rl.FlagMsaa4xHint)
rl.InitWindow(targetWidth, targetHeight, "sumi sierpinski arrow")
// layout_name: controls initialization
var ControlScrollPaneScrollView rl.Rectangle
var ControlScrollPaneScrollOffset rl.Vector2
//var ControlScrollPaneBoundsOffset rl.Vector2
BlendModeToggleGroupActive := int32(0)
Slider003Value := float32(0.0)
RTogglePressed := false
GTogglePressed := false
BTogglePressed := false
Slider008Value := float32(0.0)
Slider009Value := float32(0.0)
Slider010Value := float32(0.0)
ATogglePressed := false
SaturationTogglePressed := false
SaturationSliderValue := float32(0.0)
KSliderValue := float32(0.0)
KTogglePressed := false
// end layout vars
rl.SetTargetFPS(60)
t0 := time.Now()
/*
*/
imageField := NewImageField("/home/d/Dropbox/art/passage/data/david.png")
field :=
TranslateField{
x: -float32(sourceWidth / 2.0),
y: -float32(sourceHeight / 2.0),
field: &ScaleField{
scale: 4.0,
field: &imageField,
},
}
rng := rand.New(rand.NewSource(0))
sketch := NewSketch(sourceWidth, sourceHeight)
contourLayer := NewContourLayer(&sketch, rng, &field)
//sketch.CreateLayer("testPattern", &TestPattern{}, int32(sourceWidth), int32(sourceHeight))
//sketch.CreateLayer("actors", &contourLayer, int32(sourceWidth), int32(sourceHeight))
//sketch.CreateLayer("field", &FieldLayer{field: &field, dirty: true}, int32(sourceWidth), int32(sourceHeight))
sketch.CreateLayer("contours", &contourLayer)
ports := MakePorts()
ports["sierpinskiArrowAngle"] = Sine {
Amp: 120,
Bias: 100,
Freq: 0.1,
}
for !rl.WindowShouldClose() {
// 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{
TargetBounds: targetBounds,
SourceWidth: int32(sourceWidth),
SourceHeight: int32(sourceHeight),
Time: t,
Ports: ports.Eval(t),
}
sketch.Update(renderCtx)
/**
* MAIN DRAWING
*/
rl.BeginDrawing()
rl.ClearBackground(rl.GetColor(uint(gui.GetStyle(gui.DEFAULT, gui.BACKGROUND_COLOR))))
sketch.Draw(renderCtx)
//gui.LoadStyle("styles/dark.rgs")
gui.SetStyle(gui.DEFAULT, gui.BACKGROUND_COLOR, 0x181818FF)
gui.SetStyle(gui.DEFAULT, gui.BASE_COLOR_NORMAL, 0x2A2A2AFF)
gui.SetStyle(gui.DEFAULT, gui.BASE_COLOR_FOCUSED, 0x3A3A3AFF)
gui.SetStyle(gui.DEFAULT, gui.BASE_COLOR_PRESSED, 0x4A4A4AFF)
gui.SetStyle(gui.DEFAULT, gui.TEXT_COLOR_NORMAL, 0xE0E0E0FF)
gui.SetStyle(gui.DEFAULT, gui.TEXT_COLOR_FOCUSED, 0xFFFFFFFF)
gui.SetStyle(gui.DEFAULT, gui.BORDER_COLOR_NORMAL, 0x404040FF)
controlPanelWidth := float32(targetWidth) / 4.0
controlPanelHeight := float32(targetHeight)
// add gui layout controls
// raygui: controls drawing
gui.ScrollPanel(
rl.Rectangle{
X: 10,
Y: 10,
Width: controlPanelWidth - 20,
Height: controlPanelHeight - 20,
},
"",
rl.Rectangle{X: 10, Y: 10, Width: controlPanelWidth, Height: controlPanelHeight },
&ControlScrollPaneScrollOffset,
&ControlScrollPaneScrollView,
)
gui.GroupBox(rl.Rectangle{X: 20, Y: 20, Width: controlPanelWidth - 50, Height: 60 }, "Blending")
BlendModeToggleGroupActive = gui.ToggleGroup(rl.Rectangle{X: 30, Y: 40, Width: (controlPanelWidth - 70) / 3.0, Height: 24}, "ONE;TWO;THREE", BlendModeToggleGroupActive)
gui.Label(rl.Rectangle{X: 20, Y: 90, Width: 120, Height: 24}, "Contour Layer")
ATogglePressed = gui.LabelButton(rl.Rectangle{X: 40, Y: 136, Width: 16, Height: 16}, "A")
Slider003Value = gui.Slider(rl.Rectangle{X: 64, Y: 136, Width: 264, Height: 16}, "", "", Slider003Value, 0, 255)
RTogglePressed = gui.LabelButton(rl.Rectangle{X: 40, Y: 160, Width: 16, Height: 16}, "R")
Slider008Value = gui.Slider(rl.Rectangle{X: 64, Y: 160, Width: 264, Height: 16}, "", "", Slider008Value, 0, 100)
GTogglePressed = gui.LabelButton(rl.Rectangle{X: 40, Y: 184, Width: 16, Height: 16}, "G")
Slider009Value = gui.Slider(rl.Rectangle{X: 64, Y: 184, Width: 264, Height: 16}, "", "", Slider009Value, 0, 100)
BTogglePressed = gui.LabelButton(rl.Rectangle{X: 40, Y: 208, Width: 16, Height: 16}, "B")
Slider010Value = gui.Slider(rl.Rectangle{X: 64, Y: 208, Width: 264, Height: 16}, "", "", Slider010Value, 0, 100)
SaturationTogglePressed = gui.LabelButton(rl.Rectangle{X: 40, Y: 232, Width: 16, Height: 16}, "S")
gui.Slider(rl.Rectangle{X: 64, Y: 232, Width: 264, Height: 16}, "", "", SaturationSliderValue, 0, 100)
gui.Slider(rl.Rectangle{X: 64, Y: 256, Width: 264, Height: 16}, "", "", KSliderValue, 0, 100)
KTogglePressed = gui.LabelButton(rl.Rectangle{X: 40, Y: 256, Width: 16, Height: 16}, "K")
// NOTE: LabelButton returns "pressed this frame" bool, so these are edge-triggered.
_ = RTogglePressed
_ = GTogglePressed
_ = BTogglePressed
_ = ATogglePressed
_ = SaturationTogglePressed
_ = KTogglePressed
rl.EndDrawing()
if rl.IsKeyDown(rl.KeySpace) {
capture := sketch.Capture()
if _, err := storage.Save(capture); err != nil {
log.Printf("Error saving snapshot: %v\n", err)
}
}
for ch := rl.GetCharPressed(); ch != 0; ch = rl.GetCharPressed() {
c := rune(ch)
if c == 'c' {
sketch.ResetCamera()
} else if c >= '1' && c <= '9' {
zoom := 1 << int(ch-'0')
sketch.cam.Zoom = float32(zoom)
}
}
//rl.EndMode2D()
// HUD
}
rl.CloseWindow()
}
type FieldLayer struct {
field Field
dirty bool
}
func (s *FieldLayer) Update(ctx *RenderCtx) {
;
}
func (s *FieldLayer) Draw(ctx *RenderCtx) {
for x := range ctx.SourceWidth {
for y := range ctx.SourceHeight {
v := s.field.Get(float32(x), float32(y))
clr := GrayCurve(v, 1.0)
rl.DrawPixel(x, y, clr)
}
}
s.dirty = false
}
func (s *FieldLayer) IsDirty() bool {
return s.dirty
}