Refactored DrawTexture on Renderer to favor rendering using destination rectangle instead of a point.

This commit is contained in:
Sander Schobers 2020-05-17 11:12:45 +02:00
parent f20397c684
commit c78c4052d0
8 changed files with 63 additions and 29 deletions

View File

@ -220,21 +220,38 @@ func (r *Renderer) DefaultTarget() ui.Texture {
func (r *Renderer) Display() *allg5.Display { return r.disp } func (r *Renderer) Display() *allg5.Display { return r.disp }
func (r *Renderer) DrawTexture(texture ui.Texture, p geom.PointF32) { func (r *Renderer) DrawTexture(texture ui.Texture, p geom.RectangleF32) {
bmp := r.mustGetBitmap(texture) r.DrawTextureOptions(texture, p, ui.DrawOptions{})
x, y := snap(p)
bmp.Draw(x, y)
} }
func (r *Renderer) DrawTextureOptions(texture ui.Texture, p geom.PointF32, opts ui.DrawOptions) { func (r *Renderer) DrawTextureOptions(texture ui.Texture, p geom.RectangleF32, opts ui.DrawOptions) {
bmp := r.mustGetBitmap(texture) bmp := r.mustGetBitmap(texture)
x, y := snap(p.Min)
var o allg5.DrawOptions var o allg5.DrawOptions
if opts.Tint != nil { if opts.Tint != nil {
tint := newColor(opts.Tint) tint := newColor(opts.Tint)
o.Tint = &tint o.Tint = &tint
} }
if opts.Scale != nil { w, h := p.Dx(), p.Dy()
o.Scale = &allg5.Scale{Horizontal: opts.Scale.X, Vertical: opts.Scale.Y} bmpW, bmpH := float32(bmp.Width()), float32(bmp.Height())
if w != bmpW || h != bmpH {
o.Scale = &allg5.Scale{Horizontal: w / bmpW, Vertical: h / bmpH}
}
bmp.DrawOptions(x, y, o)
}
func (r *Renderer) DrawTexturePoint(texture ui.Texture, p geom.PointF32) {
bmp := r.mustGetBitmap(texture)
x, y := snap(p)
bmp.Draw(x, y)
}
func (r *Renderer) DrawTexturePointOptions(texture ui.Texture, p geom.PointF32, opts ui.DrawOptions) {
bmp := r.mustGetBitmap(texture)
var o allg5.DrawOptions
if opts.Tint != nil {
tint := newColor(opts.Tint)
o.Tint = &tint
} }
x, y := snap(p) x, y := snap(p)
bmp.DrawOptions(x, y, o) bmp.DrawOptions(x, y, o)

View File

@ -285,28 +285,43 @@ func (r *Renderer) CreateTextureTarget(w, h float32) (ui.Texture, error) {
func (r *Renderer) DefaultTarget() ui.Texture { return r } func (r *Renderer) DefaultTarget() ui.Texture { return r }
func (r *Renderer) DrawTexture(t ui.Texture, p geom.PointF32) { func (r *Renderer) drawTexture(t sdlTexture, src, dst sdl.Rect, opts ui.DrawOptions) {
r.DrawTextureOptions(t, p, ui.DrawOptions{}) if opts.Tint != nil {
t.SetColor(opts.Tint)
}
r.renderer.Copy(t.Native(), &src, &dst)
} }
func (r *Renderer) DrawTextureOptions(t ui.Texture, p geom.PointF32, opts ui.DrawOptions) { func (r *Renderer) DrawTexture(t ui.Texture, dst geom.RectangleF32) {
r.DrawTextureOptions(t, dst, ui.DrawOptions{})
}
func (r *Renderer) DrawTextureOptions(t ui.Texture, dst geom.RectangleF32, opts ui.DrawOptions) {
texture, ok := t.(sdlTexture) texture, ok := t.(sdlTexture)
if !ok { if !ok {
return return
} }
if opts.Tint != nil {
texture.SetColor(opts.Tint)
}
width, height, err := texture.Size() width, height, err := texture.Size()
if err != nil { if err != nil {
return return
} }
dst := RectPtr(int32(p.X), int32(p.Y), width, height) r.drawTexture(texture, Rect(0, 0, width, height), RectAbs(int32(dst.Min.X), int32(dst.Min.Y), int32(dst.Max.X), int32(dst.Max.Y)), opts)
if opts.Scale != nil { }
dst.W = int32(float32(width) * opts.Scale.X)
dst.H = int32(float32(height) * opts.Scale.Y) func (r *Renderer) DrawTexturePoint(t ui.Texture, dst geom.PointF32) {
r.DrawTexturePointOptions(t, dst, ui.DrawOptions{})
}
func (r *Renderer) DrawTexturePointOptions(t ui.Texture, dst geom.PointF32, opts ui.DrawOptions) {
texture, ok := t.(sdlTexture)
if !ok {
return
} }
r.renderer.Copy(texture.Native(), RectPtr(0, 0, width, height), dst) width, height, err := texture.Size()
if err != nil {
return
}
r.drawTexture(texture, Rect(0, 0, width, height), Rect(int32(dst.X), int32(dst.Y), width, height), opts)
} }
func (r *Renderer) FillRectangle(rect geom.RectangleF32, c color.Color) { func (r *Renderer) FillRectangle(rect geom.RectangleF32, c color.Color) {
@ -401,7 +416,7 @@ func (r *Renderer) Text(font ui.Font, p geom.PointF32, color color.Color, text s
} }
defer texture.Destroy() defer texture.Destroy()
r.DrawTexture(&Texture{texture}, p) r.DrawTexturePoint(&Texture{texture}, p)
} }
func (r *Renderer) TextAlign(font ui.Font, p geom.PointF32, color color.Color, text string, align ui.HorizontalAlignment) { func (r *Renderer) TextAlign(font ui.Font, p geom.PointF32, color color.Color, text string, align ui.HorizontalAlignment) {

View File

@ -36,7 +36,7 @@ func (b *Buffer) Render(ctx Context, pos geom.PointF32, fn RenderBufferFn) {
renderer.RenderTo(b.texture) renderer.RenderTo(b.texture)
fn(ctx, b.size) fn(ctx, b.size)
renderer.RenderTo(currTarget) renderer.RenderTo(currTarget)
renderer.DrawTexture(b.texture, pos) renderer.DrawTexturePoint(b.texture, pos)
} }
type BufferControl struct { type BufferControl struct {

View File

@ -181,8 +181,8 @@ func (b *Button) Render(ctx Context) {
if scaled == nil { // let the renderer scale if scaled == nil { // let the renderer scale
scaled = icon scaled = icon
} }
scale, iconWidth := ScaleToHeight(SizeOfTexture(scaled), boundsH) _, iconWidth := ScaleToHeight(SizeOfTexture(scaled), boundsH)
ctx.Renderer().DrawTextureOptions(scaled, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: textColor, Scale: scale}) ctx.Renderer().DrawTextureOptions(scaled, geom.RectRelF32(pos.X, pos.Y, iconWidth, boundsH), DrawOptions{Tint: textColor})
pos.X += iconWidth + pad pos.X += iconWidth + pad
} }
if len(b.Text) != 0 { if len(b.Text) != 0 {

View File

@ -123,8 +123,9 @@ func (c *Checkbox) Render(ctx Context) {
if scaledIcon == nil { // let the renderer scale if scaledIcon == nil { // let the renderer scale
scaledIcon = icon scaledIcon = icon
} }
scale, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon), boundsH) _, iconWidth := ScaleToHeight(SizeOfTexture(scaledIcon), boundsH)
ctx.Renderer().DrawTextureOptions(scaledIcon, geom.PtF32(pos.X, pos.Y), DrawOptions{Tint: iconColor, Scale: scale}) rect := geom.RectRelF32(pos.X, pos.Y, iconWidth, boundsH)
ctx.Renderer().DrawTextureOptions(scaledIcon, rect, DrawOptions{Tint: iconColor})
pos.X += iconWidth + pad pos.X += iconWidth + pad
} }
if len(c.Text) != 0 { if len(c.Text) != 0 {

View File

@ -7,8 +7,7 @@ import (
) )
type DrawOptions struct { type DrawOptions struct {
Tint color.Color Tint color.Color
Scale *geom.PointF32
} }
func ScaleToHeight(size geom.PointF32, height float32) (*geom.PointF32, float32) { func ScaleToHeight(size geom.PointF32, height float32) (*geom.PointF32, float32) {

View File

@ -23,8 +23,10 @@ type Renderer interface {
CreateTexturePath(path string, source bool) (Texture, error) CreateTexturePath(path string, source bool) (Texture, error)
CreateTextureTarget(w, h float32) (Texture, error) CreateTextureTarget(w, h float32) (Texture, error)
DefaultTarget() Texture DefaultTarget() Texture
DrawTexture(t Texture, p geom.PointF32) DrawTexture(t Texture, p geom.RectangleF32)
DrawTextureOptions(t Texture, p geom.PointF32, opts DrawOptions) DrawTextureOptions(t Texture, p geom.RectangleF32, opts DrawOptions)
DrawTexturePoint(t Texture, p geom.PointF32)
DrawTexturePointOptions(t Texture, p geom.PointF32, opts DrawOptions)
FillRectangle(r geom.RectangleF32, c color.Color) FillRectangle(r geom.RectangleF32, c color.Color)
Rectangle(r geom.RectangleF32, c color.Color, thickness float32) Rectangle(r geom.RectangleF32, c color.Color, thickness float32)
RenderTo(Texture) RenderTo(Texture)

View File

@ -179,5 +179,5 @@ func (h *sliderHandle) Render(ctx Context) {
if h.IsOver() { if h.IsOver() {
color = ctx.Style().Palette.PrimaryLight color = ctx.Style().Palette.PrimaryLight
} }
ctx.Renderer().DrawTextureOptions(h.texture(ctx), h.Bounds().Min, DrawOptions{Tint: color}) ctx.Renderer().DrawTexturePointOptions(h.texture(ctx), h.Bounds().Min, DrawOptions{Tint: color})
} }