Split rendering of buffer & blitting it to the display.

This commit is contained in:
Sander Schobers 2021-01-12 20:35:07 +01:00
parent 480e864b53
commit dbeacc3794

View File

@ -1,6 +1,10 @@
package ui package ui
import "opslag.de/schobers/geom" import (
"errors"
"opslag.de/schobers/geom"
)
type RenderBufferFn func(ctx Context, size geom.PointF32) type RenderBufferFn func(ctx Context, size geom.PointF32)
@ -9,6 +13,8 @@ type Buffer struct {
size geom.PointF32 size geom.PointF32
} }
var ErrNewBufferSize = errors.New("buffer has been resized")
func (b *Buffer) Update(ctx Context, size geom.PointF32) error { func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
if b.texture != nil { if b.texture != nil {
if size == b.size { if size == b.size {
@ -24,19 +30,30 @@ func (b *Buffer) Update(ctx Context, size geom.PointF32) error {
} }
b.texture = texture b.texture = texture
b.size = size b.size = size
return nil return ErrNewBufferSize
} }
func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) { func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) {
if b.texture == nil { if b.texture == nil {
return return
} }
b.RenderContent(ctx, fn)
b.RenderToDisplay(ctx, pos)
}
func (b *Buffer) RenderContent(ctx Context, fn RenderBufferFn) {
renderer := ctx.Renderer() renderer := ctx.Renderer()
currTarget := renderer.Target() currTarget := renderer.Target()
renderer.RenderTo(b.texture) renderer.RenderTo(b.texture)
fn(ctx, b.size) fn(ctx, b.size)
renderer.RenderTo(currTarget) renderer.RenderTo(currTarget)
renderer.DrawTexturePoint(b.texture, pos) }
func (b *Buffer) RenderToDisplay(ctx Context, pos geom.PointF32) {
if b.texture == nil {
return
}
ctx.Renderer().DrawTexturePoint(b.texture, pos)
} }
type BufferControl struct { type BufferControl struct {