From de87c5d3aa9d5d416ee7c96fb86f0cda9736e139 Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Tue, 7 Jul 2020 18:19:34 +0200 Subject: [PATCH] Try casting to PhysicalResources first when setting resource provider. SetResourceProvider accepts Resources instead of factory method to it. --- allg5ui/renderer.go | 15 ++++++++++----- sdlui/renderer.go | 15 ++++++++++----- ui/renderer.go | 2 +- ui/rendererfactory.go | 2 +- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/allg5ui/renderer.go b/allg5ui/renderer.go index 046f7a1..efb716b 100644 --- a/allg5ui/renderer.go +++ b/allg5ui/renderer.go @@ -331,15 +331,20 @@ func (r *Renderer) SetMouseCursor(c ui.MouseCursor) { func (r *Renderer) SetPosition(p geom.PointF32) { r.disp.SetPosition(int(p.X), int(p.Y)) } -func (r *Renderer) SetResourceProvider(factory func() ui.Resources) { +func (r *Renderer) SetResourceProvider(res ui.Resources) { if r.res != nil { r.res.Destroy() } - res, err := ui.NewCopyResources("allg5ui", factory(), false) - if err != nil { - return + + if phys, ok := res.(ui.PhysicalResources); ok { + r.res = phys + } else { + copy, err := ui.NewCopyResources("allg5ui", res, false) + if err != nil { + return + } + r.res = copy } - r.res = res } func (r *Renderer) SetUnhandledEventHandler(handler func(allg5.Event)) { diff --git a/sdlui/renderer.go b/sdlui/renderer.go index 5d23c8e..301633f 100644 --- a/sdlui/renderer.go +++ b/sdlui/renderer.go @@ -473,15 +473,20 @@ func (r *Renderer) TextTexture(font ui.Font, color color.Color, text string) (ui func (r *Renderer) Resources() ui.Resources { return r.resources } -func (r *Renderer) SetResourceProvider(factory func() ui.Resources) { +func (r *Renderer) SetResourceProvider(resources ui.Resources) { if r.resources != nil { r.resources.Destroy() } - resources, err := ui.NewCopyResources("sdlui", factory(), false) - if err != nil { - return + + if physical, ok := resources.(ui.PhysicalResources); ok { + r.resources = physical + } else { + copy, err := ui.NewCopyResources("sdlui", resources, false) + if err != nil { + return + } + r.resources = copy } - r.resources = resources } // Texture diff --git a/ui/renderer.go b/ui/renderer.go index 570b07f..9cfce38 100644 --- a/ui/renderer.go +++ b/ui/renderer.go @@ -41,7 +41,7 @@ type Renderer interface { // Resources Resources() Resources - SetResourceProvider(factory func() Resources) + SetResourceProvider(resources Resources) } // TextTexture renders specified text to a new texture. diff --git a/ui/rendererfactory.go b/ui/rendererfactory.go index 6c3217a..07d338a 100644 --- a/ui/rendererfactory.go +++ b/ui/rendererfactory.go @@ -22,7 +22,7 @@ func NewRenderer(title string, width, height int, opts NewRendererOptions) (Rend if err != nil { return nil, err } - renderer.SetResourceProvider(func() Resources { return DefaultResources() }) + renderer.SetResourceProvider(DefaultResources()) return renderer, nil }