ebiten/example/blocks/font.go

65 lines
1.4 KiB
Go
Raw Normal View History

2013-12-18 19:21:25 +01:00
package blocks
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"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-05-03 08:25:41 +02:00
func drawText(
context graphics.Context,
2014-05-11 12:44:36 +02:00
textures Textures,
2014-05-03 08:25:41 +02:00
str string,
x, y, scale int,
clr color.Color) {
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
}
geoMat := matrix.IdentityGeometry()
geoMat.Scale(float64(scale), float64(scale))
geoMat.Translate(float64(x), float64(y))
clrMat := matrix.IdentityColor()
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-05-03 08:25:41 +02:00
func drawTextWithShadow(
context graphics.Context,
2014-05-11 12:44:36 +02:00
textures Textures,
2014-05-03 08:25:41 +02:00
str string,
x, y, scale int,
clr color.Color) {
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
}