From e682bb27c0d57b4b89155a6c18c723849f7362cb Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Thu, 27 Jun 2019 22:54:46 -0700 Subject: [PATCH] Demo Running Level as Title Screen Wallpaper * Load SDL2 fonts from go-bindata storage so we don't have to ship external font files on disk. * Dedupe names of doodads so we don't show double on the front-end (go-bindata bundled doodads + those on local filesystem) * Use go-bindata for accessing wallpaper images. * Better flashed messages walking you through the Link Tool. * Stylize the title screen (MainScene) by rendering a live example level as the background wallpaper, with mobile doodads in motion. --- sdl/events.go | 2 +- sdl/text.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/sdl/events.go b/sdl/events.go index 7c90a1a..6c958da 100644 --- a/sdl/events.go +++ b/sdl/events.go @@ -13,7 +13,7 @@ var ( DebugWindowEvents = false DebugMouseEvents = false DebugClickEvents = false - DebugKeyEvents = true + DebugKeyEvents = false ) // Poll for events. diff --git a/sdl/text.go b/sdl/text.go index caac55d..efe59fd 100644 --- a/sdl/text.go +++ b/sdl/text.go @@ -3,6 +3,7 @@ package sdl import ( "fmt" "strings" + "sync" "git.kirsle.net/apps/doodle/lib/events" "git.kirsle.net/apps/doodle/lib/render" @@ -11,9 +12,29 @@ import ( ) // TODO: font filenames -var defaultFontFilename = "./fonts/DejaVuSans.ttf" +var defaultFontFilename = "DejaVuSans.ttf" -var fonts = map[string]*ttf.Font{} +// Font holds cached SDL_TTF structures for loaded fonts. They are created +// automatically when fonts are either preinstalled (InstallFont) or loaded for +// the first time as demanded by the DrawText method. +type Font struct { + Filename string + data []byte // raw binary data of font + ttf *ttf.Font +} + +var ( + fonts = map[string]*ttf.Font{} // keys like "DejaVuSans@14" by font size + installedFont = map[string][]byte{} // installed font files' binary handles + fontsMu sync.RWMutex +) + +// InstallFont preloads the font cache using TTF binary data in memory. +func InstallFont(filename string, binary []byte) { + fontsMu.Lock() + installedFont[filename] = binary + fontsMu.Unlock() +} // LoadFont loads and caches the font at a given size. func LoadFont(filename string, size int) (*ttf.Font, error) { @@ -27,10 +48,30 @@ func LoadFont(filename string, size int) (*ttf.Font, error) { return font, nil } - font, err := ttf.OpenFont(filename, size) - if err != nil { - return nil, err + // Do we have this font in memory? + var ( + font *ttf.Font + err error + ) + + if binary, ok := installedFont[filename]; ok { + var RWops *sdl.RWops + RWops, err = sdl.RWFromMem(binary) + if err != nil { + return nil, fmt.Errorf("LoadFont(%s): RWFromMem: %s", filename, err) + } + + font, err = ttf.OpenFontRW(RWops, 0, size) + } else { + font, err = ttf.OpenFont(filename, size) } + + // Error opening the font? + if err != nil { + return nil, fmt.Errorf("LoadFont(%s): %s", filename, err) + } + + // Cache this font name and size. fonts[keyName] = font return font, nil