From 9e640ab5c3da8598530bc678e76f8ec39b29372e Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 30 Dec 2021 16:39:48 -0800 Subject: [PATCH] SetWindowIcon for SDL2 and Text.Update() --- sdl/sdl.go | 34 ++++++++++++++++++++++++++++++++++ text.go | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) diff --git a/sdl/sdl.go b/sdl/sdl.go index baba09e..9cca831 100644 --- a/sdl/sdl.go +++ b/sdl/sdl.go @@ -2,13 +2,16 @@ package sdl import ( + "bytes" "fmt" + "image" "time" "git.kirsle.net/go/render" "git.kirsle.net/go/render/event" "github.com/veandco/go-sdl2/sdl" "github.com/veandco/go-sdl2/ttf" + "golang.org/x/image/bmp" ) // Renderer manages the SDL state. @@ -86,6 +89,37 @@ func (r *Renderer) Setup() error { return nil } +// SetWindowIcon sets an icon for the SDL2 window. +func (r *Renderer) SetWindowIcon(icon image.Image) error { + var ( + fh = bytes.NewBuffer([]byte{}) // bitmap icon buffer + ) + + // Encode icon image to bitmap + err := bmp.Encode(fh, icon) + if err != nil { + return err + } + + // Create an SDL2 RWOps from the bitmap data in memory. + rw, err := sdl.RWFromMem(fh.Bytes()) + if err != nil { + return err + } + + // Surface from RWOps + surface, err := sdl.LoadBMPRW(rw, true) + if err != nil { + return err + } + defer surface.Free() + + // Set the app window icon. + r.window.SetIcon(surface) + + return nil +} + // SetTitle sets the SDL window title. func (r *Renderer) SetTitle(title string) { r.title = title diff --git a/text.go b/text.go index 4b67810..cb09985 100644 --- a/text.go +++ b/text.go @@ -23,3 +23,36 @@ func (t Text) String() string { func (t Text) IsZero() bool { return t.Text == "" && t.Size == 0 && t.Color == Invisible && t.Padding == 0 && t.PadX == 0 && t.PadY == 0 && t.Stroke == Invisible && t.Shadow == Invisible } + +// Update properties on Text and return the updated copy. +// Only non-zerovalues will cause an update. +func (t Text) Update(other Text) Text { + if other.Text != "" { + t.Text = other.Text + } + if other.Size != 0 { + t.Size = other.Size + } + if !other.Color.IsZero() { + t.Color = other.Color + } + if other.Padding > 0 { + t.Padding = other.Padding + } + if other.PadX > 0 { + t.PadX = other.PadX + } + if other.PadY > 0 { + t.PadY = other.PadY + } + if !other.Stroke.IsZero() { + t.Stroke = other.Stroke + } + if !other.Stroke.IsZero() { + t.Shadow = other.Shadow + } + if other.FontFilename != "" { + t.FontFilename = other.FontFilename + } + return t +}