ebiten/example/blocks/font.go

54 lines
1.3 KiB
Go
Raw Normal View History

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"
2013-12-18 19:21:25 +01:00
"image/color"
)
func init() {
2014-12-08 17:35:35 +01:00
texturePaths["font"] = "images/blocks/font.png"
2013-12-18 19:21:25 +01:00
}
const charWidth = 8
const charHeight = 8
func textWidth(str string) int {
return charWidth * len(str)
}
2014-12-09 14:09:22 +01:00
func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) {
2014-05-03 08:25:41 +02:00
fontTextureId := textures.GetTexture("font")
2014-12-09 14:09:22 +01:00
parts := []ebiten.TexturePart{}
2013-12-18 19:21:25 +01:00
locationX := 0
locationY := 0
for _, c := range str {
if c == '\n' {
locationX = 0
locationY += charHeight
continue
}
code := int(c)
x := (code % 16) * charWidth
y := ((code - 32) / 16) * charHeight
2014-12-09 14:09:22 +01:00
parts = append(parts, ebiten.TexturePart{
2013-12-18 19:21:25 +01:00
LocationX: locationX,
LocationY: locationY,
2014-12-09 14:09:22 +01:00
Source: ebiten.Rect{x, y, charWidth, charHeight},
2013-12-18 19:21:25 +01:00
})
locationX += charWidth
}
2014-12-09 14:09:22 +01:00
geoMat := ebiten.GeometryMatrixI()
2013-12-18 19:21:25 +01:00
geoMat.Scale(float64(scale), float64(scale))
geoMat.Translate(float64(x), float64(y))
2014-12-09 14:09:22 +01:00
clrMat := ebiten.ColorMatrixI()
2013-12-18 19:21:25 +01:00
clrMat.Scale(clr)
2014-05-03 06:58:18 +02:00
context.Texture(fontTextureId).Draw(parts, geoMat, clrMat)
2013-12-18 19:21:25 +01:00
}
2014-12-09 14:09:22 +01:00
func drawTextWithShadow(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) {
2014-05-03 08:25:41 +02:00
drawText(context, textures, str, x+1, y+1, scale, color.RGBA{0, 0, 0, 0x80})
drawText(context, textures, str, x, y, scale, clr)
2013-12-18 19:21:25 +01:00
}