ebiten/examples/blocks/blocks/font.go

68 lines
1.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 (
2023-11-15 14:16:55 +01:00
"bytes"
2016-02-15 17:13:04 +01:00
"image/color"
"log"
2016-02-15 17:13:04 +01:00
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
2023-11-15 14:16:55 +01:00
"github.com/hajimehoshi/ebiten/v2/text/v2"
)
const (
arcadeFontBaseSize = 8
)
var (
2023-11-15 14:16:55 +01:00
arcadeFaceSource *text.GoTextFaceSource
)
2023-11-15 14:16:55 +01:00
func init() {
s, err := text.NewGoTextFaceSource(bytes.NewReader(fonts.PressStart2P_ttf))
if err != nil {
log.Fatal(err)
}
2023-11-15 14:16:55 +01:00
arcadeFaceSource = s
}
var (
2023-11-15 14:16:55 +01:00
shadowColor = color.RGBA{0, 0, 0, 0x80}
2013-12-18 19:21:25 +01:00
)
2023-11-15 14:16:55 +01:00
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, primaryAlign, secondaryAlign text.Align) {
op := &text.DrawOptions{}
op.GeoM.Translate(float64(x)+1, float64(y)+1)
op.ColorScale.ScaleWithColor(shadowColor)
op.LineSpacing = arcadeFontBaseSize * float64(scale)
2023-11-15 14:16:55 +01:00
op.PrimaryAlign = primaryAlign
op.SecondaryAlign = secondaryAlign
text.Draw(rt, str, &text.GoTextFace{
Source: arcadeFaceSource,
Size: arcadeFontBaseSize * float64(scale),
}, op)
2014-12-29 08:41:58 +01:00
2023-11-15 14:16:55 +01:00
op.GeoM.Reset()
op.GeoM.Translate(float64(x), float64(y))
op.ColorScale.Reset()
op.ColorScale.ScaleWithColor(clr)
text.Draw(rt, str, &text.GoTextFace{
Source: arcadeFaceSource,
Size: arcadeFontBaseSize * float64(scale),
}, op)
2014-12-29 08:12:26 +01:00
}