ebitenutil: Bug fix: line should use linear filter

This commit is contained in:
Hajime Hoshi 2017-12-03 23:11:39 +09:00
parent d832f1e3cb
commit 95906e9630

View File

@ -21,11 +21,16 @@ import (
"github.com/hajimehoshi/ebiten"
)
var emptyImage *ebiten.Image
var (
emptyNearestImage *ebiten.Image
emptyLinearImage *ebiten.Image
)
func init() {
emptyImage, _ = ebiten.NewImage(16, 16, ebiten.FilterNearest)
_ = emptyImage.Fill(color.White)
emptyNearestImage, _ = ebiten.NewImage(16, 16, ebiten.FilterNearest)
_ = emptyNearestImage.Fill(color.White)
emptyLinearImage, _ = ebiten.NewImage(16, 16, ebiten.FilterLinear)
_ = emptyLinearImage.Fill(color.White)
}
func colorScale(clr color.Color) (rf, gf, bf, af float64) {
@ -45,7 +50,7 @@ func colorScale(clr color.Color) (rf, gf, bf, af float64) {
//
// DrawLine is intended to be used mainly for debugging or prototyping purpose.
func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
ew, eh := emptyImage.Size()
ew, eh := emptyLinearImage.Size()
length := math.Hypot(x2-x1, y2-y1)
op := &ebiten.DrawImageOptions{}
@ -53,18 +58,18 @@ func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
op.GeoM.Translate(x1, y1)
op.ColorM.Scale(colorScale(clr))
_ = dst.DrawImage(emptyImage, op)
_ = dst.DrawImage(emptyLinearImage, op)
}
// DrawRect draws a rectangle on the given destination dst.
//
// DrawRect is intended to be used mainly for debugging or prototyping purpose.
func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
ew, eh := emptyImage.Size()
ew, eh := emptyNearestImage.Size()
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(width/float64(ew), height/float64(eh))
op.GeoM.Translate(x, y)
op.ColorM.Scale(colorScale(clr))
_ = dst.DrawImage(emptyImage, op)
_ = dst.DrawImage(emptyNearestImage, op)
}