2014-12-10 17:59:38 +01:00
|
|
|
/*
|
|
|
|
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 ebitenutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/hajimehoshi/ebiten"
|
2014-12-21 16:02:14 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal"
|
2014-12-10 17:59:38 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/assets"
|
|
|
|
"image/color"
|
|
|
|
)
|
|
|
|
|
|
|
|
type debugPrintState struct {
|
2014-12-22 02:36:42 +01:00
|
|
|
textImage *ebiten.Image
|
|
|
|
debugPrintRenderTarget *ebiten.Image
|
2014-12-10 17:59:38 +01:00
|
|
|
y int
|
|
|
|
}
|
|
|
|
|
|
|
|
var defaultDebugPrintState = new(debugPrintState)
|
|
|
|
|
2014-12-22 02:36:42 +01:00
|
|
|
func DebugPrint(r *ebiten.Image, str string) {
|
2014-12-20 12:54:14 +01:00
|
|
|
defaultDebugPrintState.DebugPrint(r, str)
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 02:36:42 +01:00
|
|
|
func (d *debugPrintState) drawText(rt *ebiten.Image, str string, x, y int, c color.Color) {
|
2014-12-20 18:42:55 +01:00
|
|
|
parts := []ebiten.ImagePart{}
|
2014-12-10 17:59:38 +01:00
|
|
|
locationX, locationY := 0, 0
|
|
|
|
for _, c := range str {
|
|
|
|
if c == '\n' {
|
|
|
|
locationX = 0
|
|
|
|
locationY += assets.TextImageCharHeight
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
code := int(c)
|
|
|
|
const xCharNum = assets.TextImageWidth / assets.TextImageCharWidth
|
2014-12-18 12:48:36 +01:00
|
|
|
srcX := float64(code%xCharNum) * assets.TextImageCharWidth
|
|
|
|
srcY := float64(code/xCharNum) * assets.TextImageCharHeight
|
2014-12-20 18:42:55 +01:00
|
|
|
parts = append(parts, ebiten.ImagePart{
|
2014-12-18 12:48:36 +01:00
|
|
|
Dst: ebiten.Rect{float64(locationX), float64(locationY), assets.TextImageCharWidth, assets.TextImageCharHeight},
|
2014-12-18 12:04:40 +01:00
|
|
|
Src: ebiten.Rect{srcX, srcY, assets.TextImageCharWidth, assets.TextImageCharHeight},
|
2014-12-10 17:59:38 +01:00
|
|
|
})
|
|
|
|
locationX += assets.TextImageCharWidth
|
|
|
|
}
|
2014-12-21 16:02:14 +01:00
|
|
|
geo := ebiten.TranslateGeometry(float64(x)+1, float64(y))
|
|
|
|
r, g, b, a := internal.RGBA(c)
|
|
|
|
clr := ebiten.ScaleColor(r, g, b, a)
|
2014-12-22 02:36:42 +01:00
|
|
|
rt.DrawImage(d.textImage, parts, geo, clr)
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|
|
|
|
|
2014-12-22 02:36:42 +01:00
|
|
|
func (d *debugPrintState) DebugPrint(r *ebiten.Image, str string) {
|
|
|
|
if d.textImage == nil {
|
2014-12-10 17:59:38 +01:00
|
|
|
img, err := assets.TextImage()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2014-12-22 02:36:42 +01:00
|
|
|
d.textImage, err = ebiten.NewImageFromImage(img, ebiten.FilterNearest)
|
2014-12-10 17:59:38 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-17 14:04:33 +01:00
|
|
|
if d.debugPrintRenderTarget == nil {
|
2014-12-10 17:59:38 +01:00
|
|
|
width, height := 256, 256
|
|
|
|
var err error
|
2014-12-22 02:36:42 +01:00
|
|
|
d.debugPrintRenderTarget, err = ebiten.NewImage(width, height, ebiten.FilterNearest)
|
2014-12-10 17:59:38 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
2014-12-21 16:02:14 +01:00
|
|
|
d.drawText(r, str, 1, d.y+1, color.NRGBA{0x00, 0x00, 0x00, 0x80})
|
|
|
|
d.drawText(r, str, 0, d.y, color.NRGBA{0xff, 0xff, 0xff, 0xff})
|
2014-12-10 17:59:38 +01:00
|
|
|
}
|