ebiten/example/blocks/blocks/font.go

114 lines
2.8 KiB
Go
Raw Normal View History

// Copyright 2014 Hajime Hoshi
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2014-12-09 15:16:04 +01:00
2014-12-08 17:35:35 +01:00
package blocks
2013-12-18 19:21:25 +01:00
import (
2014-12-09 14:09:22 +01:00
"github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/ebitenutil"
2013-12-18 19:21:25 +01:00
"image/color"
"math"
2015-01-04 16:42:20 +01:00
"strings"
2013-12-18 19:21:25 +01:00
)
var imageFont *ebiten.Image
2013-12-18 19:21:25 +01:00
func init() {
var err error
imageFont, _, err = ebitenutil.NewImageFromFile("images/blocks/font.png", ebiten.FilterNearest)
if err != nil {
panic(err)
}
2013-12-18 19:21:25 +01:00
}
const charWidth = 8
const charHeight = 8
func textWidth(str string) int {
2014-12-29 09:44:06 +01:00
// TODO: Take care about '\n'
2013-12-18 19:21:25 +01:00
return charWidth * len(str)
}
2015-01-04 16:42:20 +01:00
type fontImageParts string
2015-01-03 15:20:17 +01:00
2015-01-04 16:42:20 +01:00
func (f fontImageParts) Len() int {
return len(f)
}
func (f fontImageParts) Dst(i int) (x0, y0, x1, y1 int) {
x := i - strings.LastIndex(string(f)[:i], "\n") - 1
y := strings.Count(string(f)[:i], "\n")
x *= charWidth
y *= charHeight
if x < 0 {
return 0, 0, 0, 0
2015-01-03 15:20:17 +01:00
}
2015-01-04 16:42:20 +01:00
return x, y, x + charWidth, y + charHeight
}
func (f fontImageParts) Src(i int) (x0, y0, x1, y1 int) {
code := int(f[i])
if code == '\n' {
return 0, 0, 0, 0
2013-12-18 19:21:25 +01:00
}
2015-01-04 16:42:20 +01:00
x := (code % 16) * charWidth
y := ((code - 32) / 16) * charHeight
return x, y, x + charWidth, y + charHeight
}
2013-12-18 19:21:25 +01:00
2015-01-04 16:42:20 +01:00
func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) error {
2015-01-03 10:35:44 +01:00
options := &ebiten.DrawImageOptions{
2015-01-04 16:42:20 +01:00
ImageParts: fontImageParts(str),
2015-01-03 10:35:44 +01:00
}
options.GeoM.Scale(float64(scale), float64(scale))
options.GeoM.Translate(float64(ox), float64(oy))
2015-01-04 16:42:20 +01:00
ur, ug, ub, ua := c.RGBA()
const max = math.MaxUint16
2015-01-04 16:42:20 +01:00
r := float64(ur) / max
g := float64(ug) / max
b := float64(ub) / max
a := float64(ua) / max
if 0 < a {
r /= a
g /= a
b /= a
}
2015-01-03 10:35:44 +01:00
options.ColorM.Scale(r, g, b, a)
return rt.DrawImage(imageFont, options)
2013-12-18 19:21:25 +01:00
}
2014-12-29 06:27:43 +01:00
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error {
if err := drawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil {
return err
}
if err := drawText(rt, str, x, y, scale, clr); err != nil {
return err
}
return nil
2013-12-18 19:21:25 +01:00
}
2014-12-29 08:12:26 +01:00
2014-12-29 08:41:58 +01:00
func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error {
w := textWidth(str) * scale
x += (width - w) / 2
return drawTextWithShadow(rt, str, x, y, scale, clr)
}
2014-12-29 08:12:26 +01:00
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error {
w := textWidth(str) * scale
x += width - w
return drawTextWithShadow(rt, str, x, y, scale, clr)
}