Make a start on font rendering
I was hopeful I could use ebiten/text, but font.Face doesn't seem set up for fixed-colour fonts.
This commit is contained in:
@@ -34,6 +34,7 @@ type AssetStore struct {
|
||||
entries entryMap
|
||||
|
||||
// These members are used to store things we've already loaded
|
||||
fonts map[string]*Font
|
||||
maps map[string]*Map
|
||||
menus map[string]*Menu
|
||||
objs map[string]*Object
|
||||
@@ -84,6 +85,7 @@ func (a *AssetStore) Refresh() error {
|
||||
|
||||
// Refresh
|
||||
a.entries = newEntryMap
|
||||
a.fonts = make(map[string]*Font)
|
||||
a.maps = make(map[string]*Map)
|
||||
a.menus = make(map[string]*Menu)
|
||||
a.objs = make(map[string]*Object)
|
||||
|
116
internal/assetstore/font.go
Normal file
116
internal/assetstore/font.go
Normal file
@@ -0,0 +1,116 @@
|
||||
package assetstore
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
|
||||
"code.ur.gs/lupine/ordoor/internal/fonts"
|
||||
)
|
||||
|
||||
type Font struct {
|
||||
Name string
|
||||
mapping map[rune]*Sprite
|
||||
}
|
||||
|
||||
func (a *AssetStore) Font(name string) (*Font, error) {
|
||||
name = canonical(name)
|
||||
|
||||
// FIXME: these fonts don't exist. For now, point at one that does.
|
||||
switch name {
|
||||
case "imfnt13", "imfnt14":
|
||||
name = "wh40k_12"
|
||||
}
|
||||
|
||||
if font, ok := a.fonts[name]; ok {
|
||||
return font, nil
|
||||
}
|
||||
log.Printf("Loading font %v", name)
|
||||
|
||||
filename, err := a.lookup(name, "fnt", "Fonts")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
raw, err := fonts.LoadFont(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
objFile, err := a.lookup(raw.ObjectFile, "", "Fonts")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
obj, err := a.ObjectByPath(objFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out := &Font{
|
||||
Name: name,
|
||||
mapping: make(map[rune]*Sprite, len(raw.Mapping)),
|
||||
}
|
||||
|
||||
for r, offset := range raw.Mapping {
|
||||
spr, err := obj.Sprite(offset)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out.mapping[r] = spr
|
||||
}
|
||||
|
||||
a.fonts[name] = out
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// FIXME: this violates the ebiten rules for fast drawing. We may need to do the
|
||||
// draw ourselves, with image.Paletted for each glyph to a single ebiten.Image
|
||||
//
|
||||
// FIXME: it'd be great if we didn't have to implement this all by ourselves;
|
||||
// golang.org/x/image/font and github.com/hajimehoshi/ebiten/text are *almost*
|
||||
// sufficient, but don't seem to like it when the glyphs are literal colours
|
||||
// instead of a mask.
|
||||
//
|
||||
// TODO: draw text in a bounding box, multiple lines, etc
|
||||
func (f *Font) DrawLine(text string) (*ebiten.Image, error) {
|
||||
sprites := make([]*Sprite, 0, len(text))
|
||||
width := 0
|
||||
height := 0
|
||||
|
||||
for _, r := range text {
|
||||
spr, ok := f.mapping[r]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Font %v does not specify rune %v", f.Name, r)
|
||||
}
|
||||
|
||||
width += spr.Rect.Dx()
|
||||
if y := spr.Rect.Dy(); y > height {
|
||||
height = y
|
||||
}
|
||||
|
||||
sprites = append(sprites, spr)
|
||||
}
|
||||
|
||||
img, err := ebiten.NewImage(width, height, ebiten.FilterDefault)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
xOff := 0
|
||||
for _, spr := range sprites {
|
||||
op := &ebiten.DrawImageOptions{}
|
||||
op.GeoM.Translate(float64(xOff), 0)
|
||||
|
||||
xOff += spr.Rect.Dx()
|
||||
|
||||
if err := img.DrawImage(spr.Image, op); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return img, nil
|
||||
}
|
@@ -8,8 +8,9 @@ import (
|
||||
|
||||
type Menu struct {
|
||||
assets *AssetStore
|
||||
obj *Object // TODO: handle multiple objects in the menu
|
||||
raw *menus.Menu
|
||||
fonts []*Font // TODO: place the fonts directly into the relevant records
|
||||
obj *Object // TODO: handle multiple objects in the menu
|
||||
raw *menus.Menu // TODO: remove raw
|
||||
|
||||
Name string
|
||||
}
|
||||
@@ -19,6 +20,11 @@ func (m *Menu) Records() []*menus.Record {
|
||||
return m.raw.Records
|
||||
}
|
||||
|
||||
// FIXME: don't expose this
|
||||
func (m *Menu) Font(idx int) *Font {
|
||||
return m.fonts[idx]
|
||||
}
|
||||
|
||||
func (m *Menu) Images(start, count int) ([]*ebiten.Image, error) {
|
||||
out := make([]*ebiten.Image, count)
|
||||
|
||||
@@ -70,6 +76,16 @@ func (a *AssetStore) Menu(name string) (*Menu, error) {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var fonts []*Font
|
||||
for _, fontName := range raw.FontNames {
|
||||
fnt, err := a.Font(fontName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
fonts = append(fonts, fnt)
|
||||
}
|
||||
|
||||
i18n, err := a.i18n()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -84,6 +100,7 @@ func (a *AssetStore) Menu(name string) (*Menu, error) {
|
||||
|
||||
menu := &Menu{
|
||||
assets: a,
|
||||
fonts: fonts,
|
||||
obj: obj,
|
||||
raw: raw,
|
||||
Name: name,
|
||||
|
@@ -10,17 +10,28 @@ import (
|
||||
"code.ur.gs/lupine/ordoor/internal/util/asciiscan"
|
||||
)
|
||||
|
||||
type Range struct {
|
||||
Min Point
|
||||
Max Point
|
||||
}
|
||||
|
||||
type Point struct {
|
||||
Rune rune
|
||||
Sprite int
|
||||
}
|
||||
|
||||
type Font struct {
|
||||
Name string
|
||||
// Contains the sprite data for the font. FIXME: load this?
|
||||
// Contains the sprite data for the font
|
||||
ObjectFile string
|
||||
|
||||
// Maps ASCII bytes to a sprite offset in the ObjectFile
|
||||
mapping map[int]int
|
||||
Ranges []Range
|
||||
Mapping map[rune]int
|
||||
}
|
||||
|
||||
func (f *Font) Entries() int {
|
||||
return len(f.mapping)
|
||||
return len(f.Mapping)
|
||||
}
|
||||
|
||||
// Returns the offsets required to display a given string, returning an error if
|
||||
@@ -30,7 +41,7 @@ func (f *Font) Indices(s string) ([]int, error) {
|
||||
|
||||
for i, b := range []byte(s) {
|
||||
|
||||
offset, ok := f.mapping[int(b)]
|
||||
offset, ok := f.Mapping[rune(b)]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("Unknown codepoint %v at offset %v in string %s", b, i, s)
|
||||
}
|
||||
@@ -58,7 +69,7 @@ func LoadFont(filename string) (*Font, error) {
|
||||
out := &Font{
|
||||
Name: filepath.Base(filename),
|
||||
ObjectFile: objFile,
|
||||
mapping: make(map[int]int),
|
||||
Mapping: make(map[rune]int),
|
||||
}
|
||||
|
||||
for {
|
||||
@@ -82,15 +93,32 @@ func LoadFont(filename string) (*Font, error) {
|
||||
cpEnd, _ := strconv.Atoi(fields[2])
|
||||
idxStart, _ := strconv.Atoi(fields[3])
|
||||
idxEnd, _ := strconv.Atoi(fields[4])
|
||||
size := idxEnd - idxStart
|
||||
cpSize := cpEnd - cpStart
|
||||
idxSize := idxEnd - idxStart
|
||||
|
||||
// FIXME: I'd love this to be an error but several .fnt files do it
|
||||
if cpEnd-cpStart != size {
|
||||
fmt.Printf("WARNING: %v has mismatched codepoints and indices: %q\n", filename, str)
|
||||
// Take the smallest range
|
||||
if cpSize != idxSize {
|
||||
fmt.Printf("WARNING: %v has mismatched codepoints (sz=%v) and indices (sz=%v): %q\n", filename, cpSize, idxSize, str)
|
||||
if cpSize < idxSize {
|
||||
idxEnd = idxStart + cpSize
|
||||
idxSize = cpSize
|
||||
} else {
|
||||
cpEnd = cpStart + idxSize
|
||||
cpSize = idxSize
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
for offset := 0; offset < size; offset++ {
|
||||
out.mapping[cpStart+offset] = idxStart + offset
|
||||
r := Range{
|
||||
Min: Point{Rune: rune(cpStart), Sprite: idxStart},
|
||||
Max: Point{Rune: rune(cpEnd), Sprite: idxEnd},
|
||||
}
|
||||
|
||||
out.Ranges = append(out.Ranges, r)
|
||||
|
||||
for offset := 0; offset <= cpSize; offset++ {
|
||||
out.Mapping[rune(cpStart+offset)] = idxStart + offset
|
||||
}
|
||||
case "v": // A single codepoint, 4 fields
|
||||
if len(fields) < 3 {
|
||||
@@ -99,8 +127,11 @@ func LoadFont(filename string) (*Font, error) {
|
||||
|
||||
cp, _ := strconv.Atoi(fields[1])
|
||||
idx, _ := strconv.Atoi(fields[2])
|
||||
pt := Point{Rune: rune(cp), Sprite: idx}
|
||||
|
||||
out.mapping[cp] = idx
|
||||
out.Ranges = append(out.Ranges, Range{Min: pt, Max: pt})
|
||||
|
||||
out.Mapping[rune(cp)] = idx
|
||||
default:
|
||||
return nil, parseErr
|
||||
}
|
||||
|
@@ -2,11 +2,14 @@ package menus
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"image/color"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"code.ur.gs/lupine/ordoor/internal/data"
|
||||
"code.ur.gs/lupine/ordoor/internal/util/asciiscan"
|
||||
)
|
||||
|
||||
@@ -93,6 +96,9 @@ type Menu struct {
|
||||
ObjectFiles []string
|
||||
FontNames []string
|
||||
|
||||
BackgroundColor color.Color
|
||||
HypertextColor color.Color
|
||||
|
||||
// FIXME: turn these into first-class data
|
||||
Properties map[string]string
|
||||
|
||||
@@ -146,7 +152,23 @@ func LoadMenu(filename string) (*Menu, error) {
|
||||
out.ObjectFiles = append(out.ObjectFiles, str)
|
||||
case 1: // List of properties
|
||||
k, v := asciiscan.ConsumeProperty(str)
|
||||
out.Properties[k] = v
|
||||
vInt, err := strconv.Atoi(v) // FIXME:
|
||||
switch k {
|
||||
case "BACKGROUND COLOR 0..255..-1 trans":
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out.BackgroundColor = data.ColorPalette[vInt]
|
||||
case "HYPERTEXT COLOR 0..255":
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
out.HypertextColor = data.ColorPalette[vInt]
|
||||
default:
|
||||
out.Properties[k] = v
|
||||
}
|
||||
case 2: // list of fonts
|
||||
// FIXME: do we need to do something cleverer here?
|
||||
if str == "NULL" {
|
||||
@@ -170,6 +192,8 @@ func LoadMenu(filename string) (*Menu, error) {
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Menu properties: %#+v", out.Properties)
|
||||
|
||||
return out, nil
|
||||
}
|
||||
|
||||
|
@@ -5,7 +5,6 @@ import (
|
||||
"log"
|
||||
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
|
||||
"code.ur.gs/lupine/ordoor/internal/menus"
|
||||
)
|
||||
@@ -28,7 +27,8 @@ type noninteractive struct {
|
||||
rect image.Rectangle
|
||||
|
||||
// Some non-interactives, e.g., overlays, are an image + text to be shown
|
||||
textImg *ebiten.Image
|
||||
textImg *ebiten.Image
|
||||
textOffset image.Point
|
||||
|
||||
clickImpl // Alright, alright, it turns out the bridge mission briefing is clickable
|
||||
hoverImpl
|
||||
@@ -102,14 +102,27 @@ func registerOverlay(d *Driver, r *menus.Record) error {
|
||||
}
|
||||
|
||||
if r.Text != "" {
|
||||
// FIXME: we should be rendering the text much more nicely than this
|
||||
textImg, err := ebiten.NewImage(sprite.Rect.Dx(), sprite.Rect.Dy(), ebiten.FilterDefault)
|
||||
// FIXME: is this always right? Seems to make sense for Main.mnu
|
||||
fnt := d.menu.Font(r.FontType/10 - 1)
|
||||
|
||||
textImg, err := fnt.DrawLine(r.Text)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ebitenutil.DebugPrint(textImg, r.Text)
|
||||
ni.textImg = textImg
|
||||
|
||||
// Centre the image
|
||||
xSlack := ni.rect.Dx() - textImg.Bounds().Dx()
|
||||
if xSlack > 0 {
|
||||
ni.textOffset.X = xSlack / 2
|
||||
}
|
||||
|
||||
ySlack := ni.rect.Dy() - textImg.Bounds().Dy()
|
||||
if ySlack > 0 {
|
||||
ni.textOffset.Y = ySlack / 2
|
||||
}
|
||||
|
||||
} else {
|
||||
log.Printf("Overlay without text detected: %#+v", r)
|
||||
}
|
||||
@@ -189,12 +202,16 @@ func (n *noninteractive) regions(tick int) []region {
|
||||
out := oneRegion(n.bounds().Min, n.frames.image(tick))
|
||||
|
||||
if n.textImg != nil {
|
||||
out = append(out, oneRegion(n.bounds().Min, n.textImg)...)
|
||||
out = append(out, oneRegion(n.textPos(), n.textImg)...)
|
||||
}
|
||||
|
||||
return out
|
||||
}
|
||||
|
||||
func (n *noninteractive) textPos() image.Point {
|
||||
return image.Pt(n.rect.Min.X+n.textOffset.X, n.rect.Min.Y+n.textOffset.Y)
|
||||
}
|
||||
|
||||
func (a *animationHover) regions(tick int) []region {
|
||||
if a.opening || a.closing {
|
||||
var anim animation
|
||||
|
Reference in New Issue
Block a user