package ui

import "opslag.de/schobers/geom"

type Buffer struct {
	im Image
}

func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
	if b.im != nil {
		if b.im.Width() == size.X && b.im.Height() == size.Y {
			return nil
		}
		b.im.Destroy()
		b.im = nil
	}
	im, err := ctx.Renderer().CreateImageSize(size.X, size.Y)
	if err != nil {
		return err
	}
	b.im = im
	return nil
}

func (b *Buffer) Render(ctx Context, pos geom.PointF32, render func()) {
	renderer := ctx.Renderer()
	currTarget := renderer.Target()
	renderer.RenderTo(b.im)
	render()
	renderer.RenderTo(currTarget)
	renderer.DrawImage(b.im, pos)
}