mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-09-21 13:52:22 +02:00
100 lines
2.6 KiB
Go
100 lines
2.6 KiB
Go
|
// Copyright 2013 Hajime Hoshi
|
||
|
//
|
||
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
// of this software and associated documentation files (the "Software"), to deal
|
||
|
// in the Software without restriction, including without limitation the rights
|
||
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
// copies of the Software, and to permit persons to whom the Software is
|
||
|
// furnished to do so, subject to the following conditions:
|
||
|
//
|
||
|
// The above copyright notice and this permission notice shall be included in
|
||
|
// all copies or substantial portions of the Software.
|
||
|
//
|
||
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||
|
// SOFTWARE.
|
||
|
|
||
|
package text
|
||
|
|
||
|
import (
|
||
|
"github.com/hajimehoshi/go.ebiten/graphics"
|
||
|
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
||
|
"image"
|
||
|
"image/color"
|
||
|
"os"
|
||
|
)
|
||
|
|
||
|
type Text struct {
|
||
|
textTexture graphics.Texture
|
||
|
}
|
||
|
|
||
|
func New() *Text {
|
||
|
return &Text{}
|
||
|
}
|
||
|
|
||
|
func (game *Text) ScreenWidth() int {
|
||
|
return 256
|
||
|
}
|
||
|
|
||
|
func (game *Text) ScreenHeight() int {
|
||
|
return 240
|
||
|
}
|
||
|
|
||
|
func (game *Text) Fps() int {
|
||
|
return 60
|
||
|
}
|
||
|
|
||
|
func (game *Text) Init(tf graphics.TextureFactory) {
|
||
|
file, err := os.Open("images/text.png")
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
img, _, err := image.Decode(file)
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
if game.textTexture, err = tf.NewTextureFromImage(img); err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (game *Text) Update() {
|
||
|
}
|
||
|
|
||
|
func (game *Text) Draw(g graphics.Context, offscreen graphics.Texture) {
|
||
|
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
|
||
|
game.drawText(g, "Hello, World!", 10, 10)
|
||
|
}
|
||
|
|
||
|
func (game *Text) drawText(g graphics.Context, text string, x, y int) {
|
||
|
const letterWidth = 6
|
||
|
const letterHeight = 16
|
||
|
|
||
|
parts := []graphics.TexturePart{}
|
||
|
textX := 0
|
||
|
for _, c := range text {
|
||
|
code := int(c)
|
||
|
x := (code % 32) * letterWidth
|
||
|
y := (code / 32) * letterHeight
|
||
|
source := graphics.Rect{x, y, letterWidth, letterHeight}
|
||
|
parts = append(parts, graphics.TexturePart{
|
||
|
LocationX: textX,
|
||
|
LocationY: 0,
|
||
|
Source: source,
|
||
|
})
|
||
|
textX += letterWidth
|
||
|
}
|
||
|
|
||
|
geometryMatrix := matrix.IdentityGeometry()
|
||
|
geometryMatrix.Translate(float64(x), float64(y))
|
||
|
colorMatrix := matrix.IdentityColor()
|
||
|
g.DrawTextureParts(game.textTexture.ID, parts,
|
||
|
geometryMatrix, colorMatrix)
|
||
|
}
|