more code cleanup
This commit is contained in:
212
sketch.go
212
sketch.go
@@ -8,7 +8,7 @@ import (
|
||||
)
|
||||
|
||||
type Sketch struct {
|
||||
graphics *sg.Graphics
|
||||
env *Env
|
||||
cam *TextureCam
|
||||
composite rl.RenderTexture2D
|
||||
layerTools map[string]*LayerTools
|
||||
@@ -42,26 +42,26 @@ type LayerConfig struct {
|
||||
kValue float32
|
||||
}
|
||||
|
||||
func NewSketch(g *sg.Graphics) Sketch {
|
||||
func NewSketch(env *Env) Sketch {
|
||||
|
||||
// point at source center
|
||||
// put source center at center of screen
|
||||
var camera = TextureCam{
|
||||
LookAt: rl.Vector2{X: float32(g.Layout.Graphics.Width) / 2.0, Y: float32(g.Layout.Graphics.Height) / 2.0},
|
||||
LookAt: rl.Vector2{X: env.Offscreen.Width() / 2.0, Y: env.Offscreen.Height() / 2.0},
|
||||
Zoom: 1.0,
|
||||
}
|
||||
|
||||
return Sketch{
|
||||
graphics: g,
|
||||
env: env,
|
||||
layerTools: make(map[string]*LayerTools),
|
||||
layerToolsOrdered: []*LayerTools{},
|
||||
composite: rl.LoadRenderTexture(int32(g.Layout.Graphics.Width), int32(g.Layout.Graphics.Height)),
|
||||
composite: rl.LoadRenderTexture(env.Offscreen.WidthInt32(), env.Offscreen.HeightInt32()),
|
||||
cam: &camera,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sketch) AddLayer(name string, layer Layer) {
|
||||
texture := rl.LoadRenderTexture(int32(s.graphics.Layout.Graphics.Width), int32(s.graphics.Layout.Graphics.Height))
|
||||
texture := rl.LoadRenderTexture(s.env.Offscreen.WidthInt32(), s.env.Offscreen.HeightInt32())
|
||||
config := NewLayerConfig()
|
||||
layerTools :=
|
||||
LayerTools{
|
||||
@@ -82,87 +82,101 @@ func (s *Sketch) AddColorLayer(name string, c rl.Color) {
|
||||
s.AddLayer(name, colorLayer)
|
||||
}
|
||||
|
||||
func (s *Sketch) Redraw(ctx *Env) {
|
||||
g := ctx.Graphics
|
||||
func (s *Sketch) RedrawLayers(env *Env, g *sg.Graphics) {
|
||||
// render onto all layer textures
|
||||
for _, instance := range s.layerToolsOrdered {
|
||||
layer := instance.layer
|
||||
// ignore this layer entirely unless it's visible
|
||||
if instance.config.visible {
|
||||
layer.Update(ctx)
|
||||
w := float32(instance.texture.Texture.Width)
|
||||
h := float32(instance.texture.Texture.Height)
|
||||
lg := sg.CreateGraphics(sg.Rect { X: 0, Y: 0, Width: w, Height: h })
|
||||
layer.Update(env, g)
|
||||
// re-render to texture if dirty
|
||||
if instance.layer.IsDirty() {
|
||||
g.PushMatrix()
|
||||
g.BeginTexture(instance.texture)
|
||||
layer.Draw(ctx)
|
||||
g.PopMatrix()
|
||||
g.EndTexture()
|
||||
lg.Begin()
|
||||
lg.BeginTexture(instance.texture)
|
||||
layer.Draw(env, lg)
|
||||
lg.EndTexture()
|
||||
lg.End()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sketch) Draw(ctx *Env) {
|
||||
func (s *Sketch) Draw(env *Env) {
|
||||
|
||||
g := ctx.Graphics
|
||||
s.Redraw(ctx)
|
||||
offscreen := env.Offscreen
|
||||
s.RedrawLayers(env, offscreen)
|
||||
|
||||
// copy from full texture for compositing, with vertical flipping
|
||||
src := g.Layout.Graphics
|
||||
src := offscreen.Bounds
|
||||
src.Height = -src.Height
|
||||
dst := g.Layout.Graphics
|
||||
dst := offscreen.Bounds
|
||||
|
||||
/*
|
||||
src := g.Rect {
|
||||
X: 0, Y: 0,
|
||||
Width: float32(ctx.SourceWidth),
|
||||
Height: -float32(ctx.SourceHeight),
|
||||
}
|
||||
dst := g.Rect{
|
||||
X: 0, Y: 0,
|
||||
Width: float32(ctx.SourceWidth),
|
||||
Height: float32(ctx.SourceHeight),
|
||||
}
|
||||
src := g.Rect {
|
||||
X: 0, Y: 0,
|
||||
Width: float32(ctx.SourceWidth),
|
||||
Height: -float32(ctx.SourceHeight),
|
||||
}
|
||||
dst := g.Rect{
|
||||
X: 0, Y: 0,
|
||||
Width: float32(ctx.SourceWidth),
|
||||
Height: float32(ctx.SourceHeight),
|
||||
}
|
||||
*/
|
||||
|
||||
viewport := s.CalcViewport(ctx)
|
||||
offscreen.Begin()
|
||||
|
||||
outputRect := g.Layout.Graphics.ScaleTo(g.Layout.Viewport)
|
||||
// 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)
|
||||
|
||||
x := float32(0)
|
||||
y := float32(0)
|
||||
w := outputRect.Width
|
||||
h := outputRect.Height
|
||||
g.PushMatrix()
|
||||
g.Translate(outputRect.UL())
|
||||
g.BeginClip(outputRect)
|
||||
checkSize := float32(25.0)
|
||||
grey := rl.NewColor(220, 220, 220, 255)
|
||||
|
||||
output := sg.CreateGraphics(outputRect)
|
||||
|
||||
output.Begin()
|
||||
output.SetFill(true)
|
||||
output.SetStroke(false)
|
||||
checkSize := float32(outputRect.Width / 30.0)
|
||||
grey := sg.RGBA(220, 220, 220, 255)
|
||||
cellX := 0
|
||||
cellY := 0
|
||||
for y < h {
|
||||
x = 0
|
||||
cellX = 0
|
||||
for x < w {
|
||||
c := rl.White
|
||||
c := sg.RGBA(rl.White.R, rl.White.G, rl.White.B, rl.White.A)
|
||||
if ((cellX + cellY) & 1) == 1 {
|
||||
c = grey
|
||||
}
|
||||
rl.DrawRectangle(int32(x), int32(y), int32(checkSize), int32(checkSize), c)
|
||||
output.SetFillColor(c)
|
||||
output.DrawRect(sg.Rect { X: x, Y: y, Width: checkSize, Height: checkSize })
|
||||
//rl.DrawRectangle(int32(x), int32(y), int32(checkSize), int32(checkSize), c)
|
||||
x += checkSize
|
||||
cellX++
|
||||
}
|
||||
y += checkSize
|
||||
cellY++
|
||||
}
|
||||
g.EndClip()
|
||||
g.PopMatrix()
|
||||
|
||||
rl.BeginBlendMode(rl.BlendAlphaPremultiply)
|
||||
// render each layer onto the output graphics context
|
||||
offscreen.BeginTexture(s.composite)
|
||||
offscreen.Clear()
|
||||
|
||||
//rl.BeginBlendMode(rl.BlendAlphaPremultiply)
|
||||
//rl.BeginBlendMode(rl.BlendAlpha)
|
||||
rl.BeginTextureMode(s.composite)
|
||||
rl.ClearBackground(rl.Blank)
|
||||
//rl.BeginTextureMode(s.composite)
|
||||
|
||||
//rl.ClearBackground(rl.Blank)
|
||||
//rl.ClearBackground(rl.Black)
|
||||
for _, instance := range s.layerToolsOrdered {
|
||||
config := instance.config
|
||||
@@ -183,39 +197,44 @@ func (s *Sketch) Draw(ctx *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)
|
||||
rl.DrawTexturePro(instance.texture.Texture, src.ToRL(), dst.ToRL(), rl.Vector2{}, 0, tint)
|
||||
offscreen.TransferTexture(instance.texture.Texture, src, dst, tint)
|
||||
//rl.DrawTexturePro(instance.texture.Texture, src.ToRL(), dst.ToRL(), rl.Vector2{}, 0, tint)
|
||||
}
|
||||
}
|
||||
rl.EndTextureMode()
|
||||
rl.EndBlendMode()
|
||||
offscreen.EndTexture()
|
||||
offscreen.End()
|
||||
|
||||
rl.GenTextureMipmaps(&s.composite.Texture)
|
||||
rl.SetTextureFilter(s.composite.Texture, rl.FilterTrilinear)
|
||||
|
||||
rl.DrawTexturePro(s.composite.Texture, viewport.ToRL(), outputRect.ToRL(), rl.Vector2{}, 0, rl.White)
|
||||
output.TransferTexture(s.composite.Texture, viewport, outputRect, rl.White)
|
||||
|
||||
outputRectRL := outputRect.ToRL()
|
||||
outlineRect := (&outputRectRL).ToInt32()
|
||||
rl.DrawRectangleLines(outlineRect.X, outlineRect.Y, outlineRect.Width, outlineRect.Height, rl.Gray)
|
||||
output.SetFill(false)
|
||||
output.SetStroke(true)
|
||||
output.SetStrokeColor(rl.Gray)
|
||||
output.DrawRect(outputRect)
|
||||
|
||||
output.End()
|
||||
}
|
||||
|
||||
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))
|
||||
// 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())
|
||||
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),
|
||||
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,
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Sketch) Update(ctx *Env) {
|
||||
func (s *Sketch) Update(env *Env) {
|
||||
|
||||
if rl.IsMouseButtonDown(rl.MouseRightButton) {
|
||||
// get mouse delta from last frame
|
||||
delta := rl.GetMouseDelta()
|
||||
sourceScale := float32(ctx.Graphics.Layout.Graphics.Width) / float32(ctx.Graphics.Layout.Viewport.Width)
|
||||
sourceScale := env.Offscreen.Width() / env.Viewport.Width()
|
||||
// compute the amount to move scaled by the camera zoom
|
||||
delta = rl.Vector2Scale(delta, -sourceScale/s.cam.Zoom)
|
||||
delta.Y = -delta.Y
|
||||
@@ -223,8 +242,8 @@ func (s *Sketch) Update(ctx *Env) {
|
||||
}
|
||||
|
||||
// clamp LookAt to be somewhere on the texture
|
||||
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))
|
||||
s.cam.LookAt.X = rl.Clamp(s.cam.LookAt.X, 0, env.Offscreen.Width())
|
||||
s.cam.LookAt.Y = rl.Clamp(s.cam.LookAt.Y, 0, env.Offscreen.Height())
|
||||
|
||||
// Zoom based on mouse wheel
|
||||
wheel := rl.GetMouseWheelMove()
|
||||
@@ -241,8 +260,12 @@ func (s *Sketch) Update(ctx *Env) {
|
||||
s.cam.Zoom = rl.Clamp(s.cam.Zoom, 1, math.MaxInt64)
|
||||
}
|
||||
|
||||
func (s *Sketch) ResetCamera() {
|
||||
s.cam.LookAt = rl.Vector2{X: float32(s.graphics.Layout.Graphics.Width) / 2.0, Y: float32(s.graphics.Layout.Graphics.Height) / 2.0}
|
||||
func (s *Sketch) ResetCamera(env *Env) {
|
||||
s.cam.LookAt =
|
||||
rl.Vector2 {
|
||||
X: env.Offscreen.Width() / 2.0,
|
||||
Y: env.Offscreen.Height() / 2.0,
|
||||
}
|
||||
s.cam.Zoom = 1.0
|
||||
}
|
||||
|
||||
@@ -253,7 +276,7 @@ type SketchCapture struct {
|
||||
layerToolsOrdered []*LayerTools
|
||||
}
|
||||
|
||||
func (s *Sketch) Capture() *SketchCapture {
|
||||
func (s *Sketch) Capture(env *Env) *SketchCapture {
|
||||
composite := rl.LoadImageFromTexture(s.composite.Texture)
|
||||
rl.ImageFlipVertical(composite)
|
||||
for _, layerTool := range s.layerToolsOrdered {
|
||||
@@ -261,8 +284,8 @@ func (s *Sketch) Capture() *SketchCapture {
|
||||
rl.ImageFlipVertical(layerTool.capture)
|
||||
}
|
||||
return &SketchCapture{
|
||||
width: s.graphics.GetGraphicsWidth(),
|
||||
height: s.graphics.GetGraphicsHeight(),
|
||||
width: env.Offscreen.WidthInt32(),
|
||||
height: env.Offscreen.HeightInt32(),
|
||||
compositeImage: composite,
|
||||
layerTools: s.layerTools,
|
||||
layerToolsOrdered: s.layerToolsOrdered,
|
||||
@@ -287,8 +310,8 @@ func NewLayerConfig() LayerConfig {
|
||||
/** Layer **/
|
||||
|
||||
type Layer interface {
|
||||
Update(ctx *Env)
|
||||
Draw(ctx *Env)
|
||||
Update(ctx *Env, g *sg.Graphics)
|
||||
Draw(ctx *Env, g *sg.Graphics)
|
||||
IsDirty() bool
|
||||
}
|
||||
|
||||
@@ -297,12 +320,13 @@ type ColorLayer struct {
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (cl *ColorLayer) Update(ctx *Env) {
|
||||
func (cl *ColorLayer) Update(ctx *Env, g *sg.Graphics) {
|
||||
|
||||
}
|
||||
|
||||
func (cl *ColorLayer) Draw(ctx *Env) {
|
||||
rl.ClearBackground(cl.color)
|
||||
func (cl *ColorLayer) Draw(ctx *Env, g *sg.Graphics) {
|
||||
g.Background(cl.color)
|
||||
//rl.ClearBackground(cl.color)
|
||||
cl.dirty = false
|
||||
}
|
||||
|
||||
@@ -324,55 +348,21 @@ func NewImageLayer(path string) *ImageLayer {
|
||||
}
|
||||
}
|
||||
|
||||
func (il *ImageLayer) Update(ctx *Env) {
|
||||
func (il *ImageLayer) Update(ctx *Env, g *sg.Graphics) {
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
func (il *ImageLayer) Draw(env *Env, g *sg.Graphics) {
|
||||
rl.Translatef(
|
||||
g.Width()/2.0-float32(il.texture.Width)/2.0,
|
||||
g.Width()/2.0-float32(il.texture.Height)/2.0, 0)
|
||||
g.DrawTexture(il.texture, sg.Origin, rl.White)
|
||||
}
|
||||
|
||||
func (il *ImageLayer) IsDirty() bool {
|
||||
return il.dirty
|
||||
}
|
||||
|
||||
type TestPattern struct {
|
||||
dirty bool
|
||||
}
|
||||
|
||||
func (tp *TestPattern) Update(ctx *Env) {
|
||||
|
||||
}
|
||||
|
||||
func (tp *TestPattern) Draw(ctx *Env) {
|
||||
|
||||
rl.ClearBackground(rl.Black)
|
||||
centerX := float32(ctx.GetGraphicsWidth()) / 2
|
||||
centerY := float32(ctx.GetGraphicsHeight()) / 2
|
||||
|
||||
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.Graphics.GetGraphicsWidth(), ctx.Graphics.GetGraphicsHeight(), rl.Black)
|
||||
|
||||
rl.PushMatrix()
|
||||
rl.Translatef(centerX, centerY, 0)
|
||||
rl.SetLineWidth(4.0)
|
||||
rl.DrawLine(-10000, 0, 10000, 0, rl.Red)
|
||||
rl.DrawLine(0, -10000, 0, 10000, rl.Green)
|
||||
rl.DrawRectangleLines(-50, -50, 100, 100, rl.Magenta)
|
||||
rl.PopMatrix()
|
||||
|
||||
tp.dirty = false
|
||||
}
|
||||
|
||||
func (tp *TestPattern) IsDirty() bool {
|
||||
return tp.dirty
|
||||
}
|
||||
|
||||
/** Ports **/
|
||||
|
||||
type Ports map[string]Signal
|
||||
|
||||
Reference in New Issue
Block a user