Browse Source

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.
master
Noah Petherbridge 3 years ago
parent
commit
e682bb27c0
  1. 2
      sdl/events.go
  2. 49
      sdl/text.go

2
sdl/events.go

@ -13,7 +13,7 @@ var (
DebugWindowEvents = false
DebugMouseEvents = false
DebugClickEvents = false
DebugKeyEvents = true
DebugKeyEvents = false
)
// Poll for events.

49
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"
// 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
)
var fonts = map[string]*ttf.Font{}
// 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)
// 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, err
return nil, fmt.Errorf("LoadFont(%s): %s", filename, err)
}
// Cache this font name and size.
fonts[keyName] = font
return font, nil

Loading…
Cancel
Save