diff --git a/allg5ui/renderer.go b/allg5ui/renderer.go index efb716b..8f035a2 100644 --- a/allg5ui/renderer.go +++ b/allg5ui/renderer.go @@ -180,7 +180,7 @@ func (r *Renderer) CreateFontPath(path string, size int) (ui.Font, error) { return &font{f}, nil } -func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) { +func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (*texture, error) { im, err := source.CreateImage() if err != nil { return nil, err @@ -314,15 +314,23 @@ func (r *Renderer) RenderToDisplay() { r.disp.SetAsTarget() } -func (r *Renderer) Resources() ui.Resources { return r.res } - -func (r *Renderer) Size() geom.PointF32 { - return geom.PtF32(float32(r.disp.Width()), float32(r.disp.Height())) +func (r *Renderer) Resize(width, height int) { + r.disp.Resize(width, height) } -func (r *Renderer) SetIcon(texture ui.Texture) { - bmp := r.mustGetBitmap(texture) - r.disp.SetIcon(bmp) +func (r *Renderer) Resources() ui.Resources { return r.res } + +func (r *Renderer) Size() geom.Point { + return geom.Pt(r.disp.Width(), r.disp.Height()) +} + +func (r *Renderer) SetIcon(source ui.ImageSource) { + texture, err := r.createTexture(source, false) + if err != nil { + return + } + defer texture.Destroy() + r.disp.SetIcon(texture.bmp) } func (r *Renderer) SetMouseCursor(c ui.MouseCursor) { diff --git a/sdlui/renderer.go b/sdlui/renderer.go index 301633f..c703a8f 100644 --- a/sdlui/renderer.go +++ b/sdlui/renderer.go @@ -238,7 +238,7 @@ func (r *Renderer) CreateFontPath(path string, size int) (ui.Font, error) { return &Font{font}, nil } -func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) { +func (r *Renderer) createSurface(source ui.ImageSource) (*sdl.Surface, error) { m, err := source.CreateImage() if err != nil { return nil, err @@ -252,6 +252,14 @@ func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Tex if err != nil { return nil, err } + return surface, nil +} + +func (r *Renderer) createTexture(source ui.ImageSource, keepSource bool) (ui.Texture, error) { + surface, err := r.createSurface(source) + if err != nil { + return nil, err + } defer surface.Free() texture, err := r.renderer.CreateTextureFromSurface(surface) if err != nil { @@ -391,6 +399,10 @@ func (r *Renderer) RenderToDisplay() { r.renderer.SetRenderTarget(nil) } +func (r *Renderer) Resize(width, height int) { + r.window.SetSize(int32(width), int32(height)) +} + func (r *Renderer) SetDrawColor(c sdl.Color) { r.renderer.SetDrawColor(c.R, c.G, c.B, c.A) } @@ -399,14 +411,27 @@ func (r *Renderer) SetDrawColorGo(c color.Color) { r.SetDrawColor(ColorSDL(c)) } +func (r *Renderer) SetIcon(source ui.ImageSource) { + window := r.window + if window == nil { + return + } + surface, err := r.createSurface(source) + if err != nil { + return + } + defer surface.Free() + window.SetIcon(surface) +} + func (r *Renderer) SetMouseCursor(c ui.MouseCursor) { r.cursor = c } -func (r *Renderer) Size() geom.PointF32 { +func (r *Renderer) Size() geom.Point { w, h, err := r.renderer.GetOutputSize() if err != nil { - return geom.PtF32(geom.NaN32(), geom.NaN32()) + return geom.ZeroPt } - return geom.PtF32(float32(w), float32(h)) + return geom.Pt(int(w), int(h)) } func (r *Renderer) SystemCursor(id sdl.SystemCursor) *sdl.Cursor { diff --git a/ui/renderer.go b/ui/renderer.go index 9cfce38..0c2c5ff 100644 --- a/ui/renderer.go +++ b/ui/renderer.go @@ -32,8 +32,10 @@ type Renderer interface { Rectangle(r geom.RectangleF32, c color.Color, thickness float32) RenderTo(Texture) RenderToDisplay() + Resize(width, height int) + SetIcon(source ImageSource) SetMouseCursor(c MouseCursor) - Size() geom.PointF32 + Size() geom.Point Target() Texture Text(font Font, p geom.PointF32, color color.Color, text string) TextAlign(font Font, p geom.PointF32, color color.Color, text string, align HorizontalAlignment) diff --git a/ui/ui.go b/ui/ui.go index 737fcfd..576f8a5 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -46,7 +46,7 @@ func RunWait(r Renderer, s *Style, view Control, wait bool) error { ctx.Renderer().Refresh() for !ctx.HasQuit() { var size = r.Size() - var bounds = geom.RectF32(0, 0, size.X, size.Y) + var bounds = geom.RectF32(0, 0, float32(size.X), float32(size.Y)) overlays.Arrange(ctx, bounds, geom.ZeroPtF32, nil) overlays.Render(ctx) if ctx.HasQuit() {