ebiten/example/blocks/font.go

55 lines
1.4 KiB
Go
Raw Normal View History

2013-12-18 19:21:25 +01:00
package blocks
import (
2014-12-05 14:16:58 +01:00
"github.com/hajimehoshi/ebiten/graphics"
"github.com/hajimehoshi/ebiten/graphics/matrix"
2013-12-18 19:21:25 +01:00
"image/color"
)
func init() {
texturePaths["font"] = "images/blocks/font.png"
}
const charWidth = 8
const charHeight = 8
func textWidth(str string) int {
return charWidth * len(str)
}
2014-12-06 17:09:59 +01:00
func drawText(context graphics.Context, textures *Textures, str string, x, y, scale int, clr color.Color) {
2014-05-03 08:25:41 +02:00
fontTextureId := textures.GetTexture("font")
2013-12-18 19:21:25 +01:00
parts := []graphics.TexturePart{}
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
parts = append(parts, graphics.TexturePart{
LocationX: locationX,
LocationY: locationY,
Source: graphics.Rect{x, y, charWidth, charHeight},
})
locationX += charWidth
}
2014-12-06 15:03:17 +01:00
geoMat := matrix.GeometryI()
2013-12-18 19:21:25 +01:00
geoMat.Scale(float64(scale), float64(scale))
geoMat.Translate(float64(x), float64(y))
2014-12-06 15:03:17 +01:00
clrMat := matrix.ColorI()
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-06 17:09:59 +01:00
func drawTextWithShadow(context graphics.Context, 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
}