ebiten/example/blocks/font.go
2014-12-09 23:16:04 +09:00

70 lines
1.9 KiB
Go

/*
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.
*/
package blocks
import (
"github.com/hajimehoshi/ebiten"
"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)
}
func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) {
fontTextureId := textures.GetTexture("font")
parts := []ebiten.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, ebiten.TexturePart{
LocationX: locationX,
LocationY: locationY,
Source: ebiten.Rect{x, y, charWidth, charHeight},
})
locationX += charWidth
}
geoMat := ebiten.GeometryMatrixI()
geoMat.Scale(float64(scale), float64(scale))
geoMat.Translate(float64(x), float64(y))
clrMat := ebiten.ColorMatrixI()
clrMat.Scale(clr)
context.Texture(fontTextureId).Draw(parts, geoMat, clrMat)
}
func drawTextWithShadow(context ebiten.GraphicsContext, textures *Textures, 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)
}