Added support for (horizontally) aligment on text rendering.
This commit is contained in:
parent
69c8ae72f3
commit
8e2b80395d
28
fonts.go
28
fonts.go
@ -8,12 +8,20 @@ import (
|
||||
"opslag.de/schobers/fs/vfs"
|
||||
)
|
||||
|
||||
type TextAlignment int
|
||||
|
||||
const (
|
||||
TextAlignmentLeft TextAlignment = iota
|
||||
TextAlignmentCenter
|
||||
TextAlignmentRight
|
||||
)
|
||||
|
||||
type Font struct {
|
||||
*ttf.Font
|
||||
}
|
||||
|
||||
func (f *Font) Render(renderer *sdl.Renderer, text string, pos Point, color sdl.Color) (*Texture, error) {
|
||||
surface, err := f.RenderUTF8Solid(text, color)
|
||||
surface, err := f.RenderUTF8Blended(text, color)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@ -25,6 +33,24 @@ func (f *Font) Render(renderer *sdl.Renderer, text string, pos Point, color sdl.
|
||||
return texture, nil
|
||||
}
|
||||
|
||||
func (f *Font) RenderCopyAlign(renderer *sdl.Renderer, text string, pos Point, color sdl.Color, align TextAlignment) error {
|
||||
texture, err := f.Render(renderer, text, pos, color)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer texture.Destroy()
|
||||
rect := texture.Rect()
|
||||
switch align {
|
||||
case TextAlignmentLeft:
|
||||
texture.Copy(renderer, &sdl.Rect{X: pos.X, Y: pos.Y, W: rect.W, H: rect.H})
|
||||
case TextAlignmentCenter:
|
||||
texture.Copy(renderer, &sdl.Rect{X: pos.X - (rect.W / 2), Y: pos.Y, W: rect.W, H: rect.H})
|
||||
case TextAlignmentRight:
|
||||
texture.Copy(renderer, &sdl.Rect{X: pos.X - rect.W, Y: pos.Y, W: rect.W, H: rect.H})
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Font) RenderCopy(renderer *sdl.Renderer, text string, pos Point, color sdl.Color) error {
|
||||
texture, err := f.Render(renderer, text, pos, color)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user