refactoring progress

This commit is contained in:
2026-01-08 11:54:35 -06:00
parent bbad63c60a
commit b3d9d0340a
7 changed files with 312 additions and 155 deletions

121
sketch.go
View File

@@ -2,14 +2,13 @@ package main
import (
"fmt"
g "github.com/d2fn/sumi/internal/graphics"
sg "github.com/d2fn/sumi/internal/graphics"
"github.com/gen2brain/raylib-go/raylib"
"math"
)
type Sketch struct {
sourceWidth int32
sourceHeight int32
graphics *sg.Graphics
cam *TextureCam
composite rl.RenderTexture2D
layerTools map[string]*LayerTools
@@ -21,15 +20,6 @@ type TextureCam struct {
Zoom float32
}
/** RenderCtx **/
type RenderCtx struct {
TargetBounds g.Rect
SourceWidth int32
SourceHeight int32
Time float64
Ports map[string]float64
}
type LayerTools struct {
name string
layer Layer
@@ -52,27 +42,26 @@ type LayerConfig struct {
kValue float32
}
func NewSketch(sourceWidth, sourceHeight int32) Sketch {
func NewSketch(g *sg.Graphics) Sketch {
// point at source center
// put source center at center of screen
var camera = TextureCam{
LookAt: rl.Vector2{X: float32(sourceWidth) / 2.0, Y: float32(sourceHeight) / 2.0},
LookAt: rl.Vector2{X: float32(g.Layout.Graphics.Width) / 2.0, Y: float32(g.Layout.Graphics.Height) / 2.0},
Zoom: 1.0,
}
return Sketch{
sourceWidth: sourceWidth,
sourceHeight: sourceHeight,
graphics: g,
layerTools: make(map[string]*LayerTools),
layerToolsOrdered: []*LayerTools{},
composite: rl.LoadRenderTexture(sourceWidth, sourceHeight),
composite: rl.LoadRenderTexture(int32(g.Layout.Graphics.Width), int32(g.Layout.Graphics.Height)),
cam: &camera,
}
}
func (s *Sketch) AddLayer(name string, layer Layer) {
texture := rl.LoadRenderTexture(s.sourceWidth, s.sourceHeight)
texture := rl.LoadRenderTexture(int32(s.graphics.Layout.Graphics.Width), int32(s.graphics.Layout.Graphics.Height))
config := NewLayerConfig()
layerTools :=
LayerTools{
@@ -93,7 +82,8 @@ func (s *Sketch) AddColorLayer(name string, c rl.Color) {
s.AddLayer(name, colorLayer)
}
func (s *Sketch) Redraw(ctx *RenderCtx) {
func (s *Sketch) Redraw(ctx *Env) {
g := ctx.Graphics
// render onto all layer textures
for _, instance := range s.layerToolsOrdered {
layer := instance.layer
@@ -102,46 +92,51 @@ func (s *Sketch) Redraw(ctx *RenderCtx) {
layer.Update(ctx)
// re-render to texture if dirty
if instance.layer.IsDirty() {
rl.BeginTextureMode(instance.texture)
rl.PushMatrix()
g.PushMatrix()
g.BeginTexture(instance.texture)
layer.Draw(ctx)
rl.PopMatrix()
rl.EndTextureMode()
g.PopMatrix()
g.EndTexture()
}
}
}
}
func (s *Sketch) Draw(ctx *RenderCtx) {
func (s *Sketch) Draw(ctx *Env) {
g := ctx.Graphics
s.Redraw(ctx)
// copy from full texture for compositing, with vertical flipping
src := g.Layout.Graphics
src.Height = -src.Height
dst := g.Layout.Graphics
/*
src := g.Rect {
X: 0, Y: 0,
Width: float32(ctx.SourceWidth),
Height: -float32(ctx.SourceHeight),
}
dst := g.Rect {
dst := g.Rect{
X: 0, Y: 0,
Width: float32(ctx.SourceWidth),
Height: float32(ctx.SourceHeight),
}
*/
viewport := s.CalcViewport(ctx)
sourceRect := g.Rect{X: 0, Y: 0, Width: float32(ctx.SourceWidth), Height: float32(ctx.SourceHeight)}
targetRect := g.Rect{X: float32(ctx.TargetBounds.X), Y: float32(ctx.TargetBounds.Y), Width: float32(ctx.TargetBounds.Width), Height: float32(ctx.TargetBounds.Height)}
outputRect := sourceRect.ScaleTo(targetRect)
outputRect := g.Layout.Graphics.ScaleTo(g.Layout.Viewport)
fmt.Printf("outputRect = %v\n", outputRect)
x := float32(0)
y := float32(0)
w := outputRect.Width
h := outputRect.Height
rl.PushMatrix()
rl.Translatef(outputRect.X, outputRect.Y, 0)
rl.BeginScissorMode(int32(outputRect.X), int32(outputRect.Y), int32(outputRect.Width), int32(outputRect.Height))
g.PushMatrix()
g.Translate(outputRect.UL())
g.BeginClip(outputRect)
checkSize := float32(25.0)
grey := rl.NewColor(220, 220, 220, 255)
cellX := 0
@@ -161,8 +156,8 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
y += checkSize
cellY++
}
rl.EndScissorMode()
rl.PopMatrix()
g.EndClip()
g.PopMatrix()
rl.BeginBlendMode(rl.BlendAlphaPremultiply)
//rl.BeginBlendMode(rl.BlendAlpha)
@@ -188,7 +183,7 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
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)
rl.DrawTexturePro(instance.texture.Texture, src, dst, rl.Vector2{}, 0, tint)
rl.DrawTexturePro(instance.texture.Texture, src.ToRL(), dst.ToRL(), rl.Vector2{}, 0, tint)
}
}
rl.EndTextureMode()
@@ -197,29 +192,30 @@ func (s *Sketch) Draw(ctx *RenderCtx) {
rl.GenTextureMipmaps(&s.composite.Texture)
rl.SetTextureFilter(s.composite.Texture, rl.FilterTrilinear)
rl.DrawTexturePro(s.composite.Texture, viewport, *outputRect.ToRL(), rl.Vector2{}, 0, rl.White)
rl.DrawTexturePro(s.composite.Texture, viewport.ToRL(), outputRect.ToRL(), rl.Vector2{}, 0, rl.White)
outlineRect := outputRect.ToRL().ToInt32()
outputRectRL := outputRect.ToRL()
outlineRect := (&outputRectRL).ToInt32()
rl.DrawRectangleLines(outlineRect.X, outlineRect.Y, outlineRect.Width, outlineRect.Height, rl.Gray)
}
func (s *Sketch) CalcViewport(ctx *RenderCtx) g.Rect {
viewportWidth := rl.Clamp(float32(ctx.SourceWidth)/s.cam.Zoom, 0, float32(ctx.SourceWidth))
viewportHeight := rl.Clamp(float32(ctx.SourceHeight)/s.cam.Zoom, 0, float32(ctx.SourceHeight))
return g.Rect {
X: rl.Clamp(s.cam.LookAt.X-viewportWidth/2.0, 0, float32(ctx.SourceWidth)-viewportWidth),
Y: rl.Clamp(s.cam.LookAt.Y-viewportHeight/2.0, 0, float32(ctx.SourceHeight)-viewportHeight),
func (s *Sketch) CalcViewport(ctx *Env) sg.Rect {
viewportWidth := rl.Clamp(float32(ctx.Graphics.Layout.Graphics.Width)/s.cam.Zoom, 0, float32(ctx.Graphics.Layout.Graphics.Width))
viewportHeight := rl.Clamp(float32(ctx.Graphics.Layout.Graphics.Height)/s.cam.Zoom, 0, float32(ctx.Graphics.Layout.Graphics.Height))
return sg.Rect{
X: rl.Clamp(s.cam.LookAt.X-viewportWidth/2.0, 0, float32(ctx.Graphics.Layout.Graphics.Width)-viewportWidth),
Y: rl.Clamp(s.cam.LookAt.Y-viewportHeight/2.0, 0, float32(ctx.Graphics.Layout.Graphics.Height)-viewportHeight),
Width: float32(viewportWidth),
Height: -float32(viewportHeight),
}
}
func (s *Sketch) Update(ctx *RenderCtx) {
func (s *Sketch) Update(ctx *Env) {
if rl.IsMouseButtonDown(rl.MouseRightButton) {
// get mouse delta from last frame
delta := rl.GetMouseDelta()
sourceScale := float32(ctx.SourceWidth) / float32(ctx.TargetBounds.Width)
sourceScale := float32(ctx.Graphics.Layout.Graphics.Width) / float32(ctx.Graphics.Layout.Viewport.Width)
// compute the amount to move scaled by the camera zoom
delta = rl.Vector2Scale(delta, -sourceScale/s.cam.Zoom)
delta.Y = -delta.Y
@@ -227,8 +223,8 @@ func (s *Sketch) Update(ctx *RenderCtx) {
}
// clamp LookAt to be somewhere on the texture
s.cam.LookAt.X = rl.Clamp(s.cam.LookAt.X, 0, float32(ctx.SourceWidth-1))
s.cam.LookAt.Y = rl.Clamp(s.cam.LookAt.Y, 0, float32(ctx.SourceHeight-1))
s.cam.LookAt.X = rl.Clamp(s.cam.LookAt.X, 0, float32(ctx.Graphics.Layout.Graphics.Width))
s.cam.LookAt.Y = rl.Clamp(s.cam.LookAt.Y, 0, float32(ctx.Graphics.Layout.Graphics.Height))
// Zoom based on mouse wheel
wheel := rl.GetMouseWheelMove()
@@ -246,12 +242,12 @@ func (s *Sketch) Update(ctx *RenderCtx) {
}
func (s *Sketch) ResetCamera() {
s.cam.LookAt = rl.Vector2{X: float32(s.sourceWidth) / 2.0, Y: float32(s.sourceHeight) / 2.0}
s.cam.LookAt = rl.Vector2{X: float32(s.graphics.Layout.Graphics.Width) / 2.0, Y: float32(s.graphics.Layout.Graphics.Height) / 2.0}
s.cam.Zoom = 1.0
}
type SketchCapture struct {
width, height uint32
width, height int32
compositeImage *rl.Image
layerTools map[string]*LayerTools
layerToolsOrdered []*LayerTools
@@ -265,7 +261,8 @@ func (s *Sketch) Capture() *SketchCapture {
rl.ImageFlipVertical(layerTool.capture)
}
return &SketchCapture{
width: uint32(s.sourceWidth), height: uint32(s.sourceHeight),
width: s.graphics.GetGraphicsWidth(),
height: s.graphics.GetGraphicsHeight(),
compositeImage: composite,
layerTools: s.layerTools,
layerToolsOrdered: s.layerToolsOrdered,
@@ -290,8 +287,8 @@ func NewLayerConfig() LayerConfig {
/** Layer **/
type Layer interface {
Update(ctx *RenderCtx)
Draw(ctx *RenderCtx)
Update(ctx *Env)
Draw(ctx *Env)
IsDirty() bool
}
@@ -300,11 +297,11 @@ type ColorLayer struct {
dirty bool
}
func (cl *ColorLayer) Update(ctx *RenderCtx) {
func (cl *ColorLayer) Update(ctx *Env) {
}
func (cl *ColorLayer) Draw(ctx *RenderCtx) {
func (cl *ColorLayer) Draw(ctx *Env) {
rl.ClearBackground(cl.color)
cl.dirty = false
}
@@ -327,12 +324,12 @@ func NewImageLayer(path string) *ImageLayer {
}
}
func (il *ImageLayer) Update(ctx *RenderCtx) {
func (il *ImageLayer) Update(ctx *Env) {
}
func (il *ImageLayer) Draw(ctx *RenderCtx) {
rl.Translatef(float32(ctx.SourceWidth)/2.0-float32(il.texture.Width)/2.0, float32(ctx.SourceHeight)/2.0-float32(il.texture.Height)/2.0, 0)
func (il *ImageLayer) Draw(ctx *Env) {
rl.Translatef(float32(ctx.GetGraphicsWidth())/2.0-float32(il.texture.Width)/2.0, float32(ctx.GetGraphicsHeight())/2.0-float32(il.texture.Height)/2.0, 0)
rl.DrawTexture(il.texture, 0, 0, rl.White)
}
@@ -344,22 +341,22 @@ type TestPattern struct {
dirty bool
}
func (tp *TestPattern) Update(ctx *RenderCtx) {
func (tp *TestPattern) Update(ctx *Env) {
}
func (tp *TestPattern) Draw(ctx *RenderCtx) {
func (tp *TestPattern) Draw(ctx *Env) {
rl.ClearBackground(rl.Black)
centerX := float32(ctx.SourceWidth) / 2
centerY := float32(ctx.SourceHeight) / 2
centerX := float32(ctx.GetGraphicsWidth()) / 2
centerY := float32(ctx.GetGraphicsHeight()) / 2
rl.DrawRectangleRec(*g.Rect{X: 0, Y: 0, Width: centerX, Height: centerY}.ToRL(), rl.Red)
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.DrawLine(0, 0, ctx.Graphics.GetGraphicsWidth(), ctx.Graphics.GetGraphicsHeight(), rl.Black)
rl.PushMatrix()
rl.Translatef(centerX, centerY, 0)