SetWindowIcon for SDL2 and Text.Update()

master
Noah 2021-12-30 16:39:48 -08:00
parent 16fbb28ace
commit 9e640ab5c3
2 changed files with 67 additions and 0 deletions

View File

@ -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

33
text.go
View File

@ -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
}