Separated the lazy text rendering method.
This commit is contained in:
parent
58aa819a4e
commit
0a73529306
36
fonts.go
36
fonts.go
@ -3,21 +3,49 @@ package tins2020
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/veandco/go-sdl2/sdl"
|
||||||
"github.com/veandco/go-sdl2/ttf"
|
"github.com/veandco/go-sdl2/ttf"
|
||||||
"opslag.de/schobers/fs/vfs"
|
"opslag.de/schobers/fs/vfs"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
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)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer surface.Free()
|
||||||
|
texture, err := NewTextureFromSurface(renderer, surface)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return texture, 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 {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer texture.Destroy()
|
||||||
|
texture.Copy(renderer, texture.RectOffset(pos))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
type Fonts struct {
|
type Fonts struct {
|
||||||
dir vfs.CopyDir
|
dir vfs.CopyDir
|
||||||
fonts map[string]*ttf.Font
|
fonts map[string]*Font
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fonts) Init(dir vfs.CopyDir) {
|
func (f *Fonts) Init(dir vfs.CopyDir) {
|
||||||
f.dir = dir
|
f.dir = dir
|
||||||
f.fonts = map[string]*ttf.Font{}
|
f.fonts = map[string]*Font{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Fonts) Font(name string) *ttf.Font { return f.fonts[name] }
|
func (f *Fonts) Font(name string) *Font { return f.fonts[name] }
|
||||||
|
|
||||||
type FontDescriptor struct {
|
type FontDescriptor struct {
|
||||||
Name string
|
Name string
|
||||||
@ -47,7 +75,7 @@ func (f *Fonts) Load(name, path string, size int) error {
|
|||||||
if font, ok := f.fonts[name]; ok {
|
if font, ok := f.fonts[name]; ok {
|
||||||
font.Close()
|
font.Close()
|
||||||
}
|
}
|
||||||
f.fonts[name] = font
|
f.fonts[name] = &Font{font}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,15 +37,5 @@ func (f *FPS) Render(ctx *Context) {
|
|||||||
f.ticks[f.slot]++
|
f.ticks[f.slot]++
|
||||||
|
|
||||||
font := ctx.Fonts.Font("debug")
|
font := ctx.Fonts.Font("debug")
|
||||||
surface, err := font.RenderUTF8Solid(fmt.Sprintf("FPS: %d", f.total), sdl.Color{R: 255, G: 255, B: 255, A: 255})
|
font.RenderCopy(ctx.Renderer, fmt.Sprintf("FPS: %d", f.total), Pt(5, 5), sdl.Color{R: 255, G: 255, B: 255, A: 255})
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer surface.Free()
|
|
||||||
texture, err := NewTextureFromSurface(ctx.Renderer, surface)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer texture.Destroy()
|
|
||||||
texture.Copy(ctx.Renderer, texture.RectOffset(Pt(5, 5)))
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user