Skip to content

Instantly share code, notes, and snippets.

@zephyrtronium
Created July 3, 2020 20:36
Show Gist options
  • Select an option

  • Save zephyrtronium/91628d0c6c58a8e95a717472444f0853 to your computer and use it in GitHub Desktop.

Select an option

Save zephyrtronium/91628d0c6c58a8e95a717472444f0853 to your computer and use it in GitHub Desktop.
gui renderer
// +build ignore
package main
import (
"context"
"encoding/json"
"image"
"log"
"os"
"golang.org/x/image/draw"
"gioui.org/app"
"gioui.org/f32"
"gioui.org/io/key"
"gioui.org/io/system"
"gioui.org/op"
"gioui.org/op/paint"
"github.com/zephyrtronium/xirho"
"github.com/zephyrtronium/xirho/encoding"
)
func run(r *xirho.R, aspect float64) {
log.Printf("rendering %+v\n", r)
w := app.NewWindow(app.Title("zeta xi"))
var ops op.Ops
var img *image.RGBA
size := image.Point{X: -1, Y: -1}
ctx, cancel := context.WithCancel(context.Background())
stop := make(chan bool, 1)
stop <- true
for ev := range w.Events() {
switch ev := ev.(type) {
case system.FrameEvent:
log.Printf("update: %+v, current bounds are %+v\n", ev, r.Hist.Bounds().Size())
cancel()
ctx, cancel = context.WithCancel(context.Background())
<-stop
if ev.Size != size {
var w, h int
if aspect <= 1 {
h = ev.Size.Y
w = int(float64(h) * aspect)
} else {
w = ev.Size.X
h = int(float64(w) / aspect)
}
b := r.Hist.Bounds().Size()
log.Printf("resize: old %dx%d, new %dx%d\n", b.X/2, b.Y/2, w, h)
r.Hist.Reset(w*2, h*2)
img = image.NewRGBA(image.Rect(0, 0, w, h))
size = ev.Size
}
log.Println("plotting after", r.Iters(), "iters")
draw.Draw(img, img.Bounds(), image.NewUniform(image.Black), image.Point{}, draw.Src)
draw.ApproxBiLinear.Scale(img, img.Bounds(), r.Hist, r.Hist.Bounds(), draw.Over, nil)
go render(ctx, r, stop)
paint.NewImageOp(img).Add(&ops)
sz := img.Bounds().Size()
paint.PaintOp{Rect: f32.Rect(0, 0, float32(sz.X), float32(sz.Y))}.Add(&ops)
ev.Frame(&ops)
case key.Event:
log.Println("keypress:", ev)
if ev.Name == key.NameEscape {
w.Close()
}
w.Invalidate()
case system.DestroyEvent:
cancel()
<-stop
}
}
cancel()
log.Fatalln("quit")
}
func render(ctx context.Context, r *xirho.R, stop chan bool) {
log.Println("begin render")
r.Render(ctx)
log.Println("stop render")
stop <- true
}
func main() {
f, err := os.Open("./img/discjulian.json")
if err != nil {
log.Fatalln(err)
}
r, aspect, err := encoding.Unmarshal(json.NewDecoder(f))
if err != nil {
log.Fatalln(err)
}
go run(r, aspect)
app.Main()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment