vector: add anti-alias options to the utility functions

Closes #2606
This commit is contained in:
Hajime Hoshi 2023-03-18 11:01:46 +09:00
parent f66ed646bd
commit 8bd7ce5c20
10 changed files with 35 additions and 35 deletions

View File

@ -336,13 +336,13 @@ func (p *Player) seekBarIfNeeded() error {
func (p *Player) draw(screen *ebiten.Image) { func (p *Player) draw(screen *ebiten.Image) {
// Draw the bar. // Draw the bar.
x, y, w, h := playerBarRect() x, y, w, h := playerBarRect()
vector.DrawFilledRect(screen, float32(x), float32(y), float32(w), float32(h), playerBarColor) vector.DrawFilledRect(screen, float32(x), float32(y), float32(w), float32(h), playerBarColor, true)
// Draw the cursor on the bar. // Draw the cursor on the bar.
c := p.current c := p.current
cx := float32(x) + float32(w)*float32(p.current)/float32(p.total) cx := float32(x) + float32(w)*float32(p.current)/float32(p.total)
cy := float32(y) + float32(h)/2 cy := float32(y) + float32(h)/2
vector.DrawFilledCircle(screen, cx, cy, 12, playerCurrentColor) vector.DrawFilledCircle(screen, cx, cy, 12, playerCurrentColor, true)
// Compose the curren time text. // Compose the curren time text.
m := (c / time.Minute) % 100 m := (c / time.Minute) % 100

View File

@ -108,7 +108,7 @@ func init() {
} }
func drawWindow(r *ebiten.Image, x, y, width, height int) { func drawWindow(r *ebiten.Image, x, y, width, height int) {
vector.DrawFilledRect(r, float32(x), float32(y), float32(width), float32(height), color.RGBA{0, 0, 0, 0xc0}) vector.DrawFilledRect(r, float32(x), float32(y), float32(width), float32(height), color.RGBA{0, 0, 0, 0xc0}, false)
} }
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff} var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}

View File

@ -60,7 +60,7 @@ func (g *Game) Update() error {
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
for r, c := range g.gridColors { for r, c := range g.gridColors {
vector.DrawFilledRect(screen, float32(r.Min.X), float32(r.Min.Y), float32(r.Dx()), float32(r.Dy()), c) vector.DrawFilledRect(screen, float32(r.Min.X), float32(r.Min.Y), float32(r.Dx()), float32(r.Dy()), c, false)
} }
switch ebiten.CursorShape() { switch ebiten.CursorShape() {

View File

@ -147,7 +147,7 @@ func init() {
for i, k := range whiteKeys { for i, k := range whiteKeys {
x := i*keyWidth + 36 x := i*keyWidth + 36
height := 112 height := 112
vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.White) vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.White, false)
text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.Black) text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.Black)
} }
@ -158,7 +158,7 @@ func init() {
} }
x := i*keyWidth + 24 x := i*keyWidth + 24
height := 64 height := 64
vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.Black) vector.DrawFilledRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.Black, false)
text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.White) text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.White)
} }
} }

View File

@ -247,7 +247,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
if g.showRays { if g.showRays {
// Draw rays // Draw rays
for _, r := range rays { for _, r := range rays {
vector.StrokeLine(screen, float32(r.X1), float32(r.Y1), float32(r.X2), float32(r.Y2), 1, color.RGBA{255, 255, 0, 150}) vector.StrokeLine(screen, float32(r.X1), float32(r.Y1), float32(r.X2), float32(r.Y2), 1, color.RGBA{255, 255, 0, 150}, true)
} }
} }
@ -259,13 +259,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
// Draw walls // Draw walls
for _, obj := range g.objects { for _, obj := range g.objects {
for _, w := range obj.walls { for _, w := range obj.walls {
vector.StrokeLine(screen, float32(w.X1), float32(w.Y1), float32(w.X2), float32(w.Y2), 1, color.RGBA{255, 0, 0, 255}) vector.StrokeLine(screen, float32(w.X1), float32(w.Y1), float32(w.X2), float32(w.Y2), 1, color.RGBA{255, 0, 0, 255}, true)
} }
} }
// Draw player as a rect // Draw player as a rect
vector.DrawFilledRect(screen, float32(g.px)-2, float32(g.py)-2, 4, 4, color.Black) vector.DrawFilledRect(screen, float32(g.px)-2, float32(g.py)-2, 4, 4, color.Black, true)
vector.DrawFilledRect(screen, float32(g.px)-1, float32(g.py)-1, 2, 2, color.RGBA{255, 100, 100, 255}) vector.DrawFilledRect(screen, float32(g.px)-1, float32(g.py)-1, 2, 2, color.RGBA{255, 100, 100, 255}, true)
if g.showRays { if g.showRays {
ebitenutil.DebugPrintAt(screen, "R: hide rays", padding, 0) ebitenutil.DebugPrintAt(screen, "R: hide rays", padding, 0)

View File

@ -41,15 +41,15 @@ func (g *Game) Update() error {
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
cf := float32(g.count) cf := float32(g.count)
vector.StrokeLine(screen, 100, 100, 300, 100, 1, color.RGBA{0xff, 0xff, 0xff, 0xff}) vector.StrokeLine(screen, 100, 100, 300, 100, 1, color.RGBA{0xff, 0xff, 0xff, 0xff}, true)
vector.StrokeLine(screen, 50, 150, 50, 350, 1, color.RGBA{0xff, 0xff, 0x00, 0xff}) vector.StrokeLine(screen, 50, 150, 50, 350, 1, color.RGBA{0xff, 0xff, 0x00, 0xff}, true)
vector.StrokeLine(screen, 50, 100+cf, 200+cf, 250, 4, color.RGBA{0x00, 0xff, 0xff, 0xff}) vector.StrokeLine(screen, 50, 100+cf, 200+cf, 250, 4, color.RGBA{0x00, 0xff, 0xff, 0xff}, true)
vector.DrawFilledRect(screen, 50+cf, 50+cf, 100+cf, 100+cf, color.RGBA{0x80, 0x80, 0x80, 0xc0}) vector.DrawFilledRect(screen, 50+cf, 50+cf, 100+cf, 100+cf, color.RGBA{0x80, 0x80, 0x80, 0xc0}, true)
vector.StrokeRect(screen, 300-cf, 50, 120, 120, 10+cf/4, color.RGBA{0x00, 0x80, 0x00, 0xff}) vector.StrokeRect(screen, 300-cf, 50, 120, 120, 10+cf/4, color.RGBA{0x00, 0x80, 0x00, 0xff}, true)
vector.DrawFilledCircle(screen, 400, 400, 100, color.RGBA{0x80, 0x00, 0x80, 0x80}) vector.DrawFilledCircle(screen, 400, 400, 100, color.RGBA{0x80, 0x00, 0x80, 0x80}, true)
vector.StrokeCircle(screen, 400, 400, 10+cf, 10+cf/2, color.RGBA{0xff, 0x80, 0xff, 0xff}) vector.StrokeCircle(screen, 400, 400, 10+cf, 10+cf/2, color.RGBA{0xff, 0x80, 0xff, 0xff}, true)
ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS())) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS()))
} }

View File

@ -172,9 +172,9 @@ func (g *Game) Update() error {
func (g *Game) Draw(screen *ebiten.Image) { func (g *Game) Draw(screen *ebiten.Image) {
for _, v := range g.snakeBody { for _, v := range g.snakeBody {
vector.DrawFilledRect(screen, float32(v.X*gridSize), float32(v.Y*gridSize), gridSize, gridSize, color.RGBA{0x80, 0xa0, 0xc0, 0xff}) vector.DrawFilledRect(screen, float32(v.X*gridSize), float32(v.Y*gridSize), gridSize, gridSize, color.RGBA{0x80, 0xa0, 0xc0, 0xff}, false)
} }
vector.DrawFilledRect(screen, float32(g.apple.X*gridSize), float32(g.apple.Y*gridSize), gridSize, gridSize, color.RGBA{0xFF, 0x00, 0x00, 0xff}) vector.DrawFilledRect(screen, float32(g.apple.X*gridSize), float32(g.apple.Y*gridSize), gridSize, gridSize, color.RGBA{0xFF, 0x00, 0x00, 0xff}, false)
if g.moveDirection == dirNone { if g.moveDirection == dirNone {
ebitenutil.DebugPrint(screen, fmt.Sprintf("Press up/down/left/right to start")) ebitenutil.DebugPrint(screen, fmt.Sprintf("Press up/down/left/right to start"))

View File

@ -63,7 +63,7 @@ func (s *Star) Draw(screen *ebiten.Image) {
G: uint8(0xdd * s.brightness / 0xff), G: uint8(0xdd * s.brightness / 0xff),
B: uint8(0xff * s.brightness / 0xff), B: uint8(0xff * s.brightness / 0xff),
A: 0xff} A: 0xff}
vector.StrokeLine(screen, s.fromx/scale, s.fromy/scale, s.tox/scale, s.toy/scale, 1, c) vector.StrokeLine(screen, s.fromx/scale, s.fromy/scale, s.tox/scale, s.toy/scale, 1, c, true)
} }
type Game struct { type Game struct {

View File

@ -93,13 +93,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
{ {
const x, y = 20, 40 const x, y = 20, 40
b := text.BoundString(mplusNormalFont, sampleText) b := text.BoundString(mplusNormalFont, sampleText)
vector.DrawFilledRect(screen, float32(b.Min.X+x), float32(b.Min.Y+y), float32(b.Dx()), float32(b.Dy()), gray) vector.DrawFilledRect(screen, float32(b.Min.X+x), float32(b.Min.Y+y), float32(b.Dx()), float32(b.Dy()), gray, false)
text.Draw(screen, sampleText, mplusNormalFont, x, y, color.White) text.Draw(screen, sampleText, mplusNormalFont, x, y, color.White)
} }
{ {
const x, y = 20, 140 const x, y = 20, 140
b := text.BoundString(mplusBigFont, sampleText) b := text.BoundString(mplusBigFont, sampleText)
vector.DrawFilledRect(screen, float32(b.Min.X+x), float32(b.Min.Y+y), float32(b.Dx()), float32(b.Dy()), gray) vector.DrawFilledRect(screen, float32(b.Min.X+x), float32(b.Min.Y+y), float32(b.Dx()), float32(b.Dy()), gray, false)
text.Draw(screen, sampleText, mplusBigFont, x, y, color.White) text.Draw(screen, sampleText, mplusBigFont, x, y, color.White)
} }
{ {
@ -114,7 +114,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
const x, y = 160, 240 const x, y = 160, 240
const lineHeight = 80 const lineHeight = 80
b := text.BoundString(text.FaceWithLineHeight(mplusBigFont, lineHeight), sampleText) b := text.BoundString(text.FaceWithLineHeight(mplusBigFont, lineHeight), sampleText)
vector.DrawFilledRect(screen, float32(b.Min.X+x), float32(b.Min.Y+y), float32(b.Dx()), float32(b.Dy()), gray) vector.DrawFilledRect(screen, float32(b.Min.X+x), float32(b.Min.Y+y), float32(b.Dx()), float32(b.Dy()), gray, false)
text.Draw(screen, sampleText, text.FaceWithLineHeight(mplusBigFont, lineHeight), x, y, color.White) text.Draw(screen, sampleText, text.FaceWithLineHeight(mplusBigFont, lineHeight), x, y, color.White)
} }
{ {

View File

@ -37,7 +37,7 @@ func init() {
whiteImage.WritePixels(pix) whiteImage.WritePixels(pix)
} }
func drawVerticesForUtil(dst *ebiten.Image, vs []ebiten.Vertex, is []uint16, clr color.Color) { func drawVerticesForUtil(dst *ebiten.Image, vs []ebiten.Vertex, is []uint16, clr color.Color, antialias bool) {
r, g, b, a := clr.RGBA() r, g, b, a := clr.RGBA()
for i := range vs { for i := range vs {
vs[i].SrcX = 1 vs[i].SrcX = 1
@ -50,12 +50,12 @@ func drawVerticesForUtil(dst *ebiten.Image, vs []ebiten.Vertex, is []uint16, clr
op := &ebiten.DrawTrianglesOptions{} op := &ebiten.DrawTrianglesOptions{}
op.ColorScaleMode = ebiten.ColorScaleModePremultipliedAlpha op.ColorScaleMode = ebiten.ColorScaleModePremultipliedAlpha
op.AntiAlias = true op.AntiAlias = antialias
dst.DrawTriangles(vs, is, whiteSubImage, op) dst.DrawTriangles(vs, is, whiteSubImage, op)
} }
// StrokeLine strokes a line (x0, y0)-(x1, y1) with the specified width and color. // StrokeLine strokes a line (x0, y0)-(x1, y1) with the specified width and color.
func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32, clr color.Color) { func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32, clr color.Color, antialias bool) {
var path Path var path Path
path.MoveTo(x0, y0) path.MoveTo(x0, y0)
path.LineTo(x1, y1) path.LineTo(x1, y1)
@ -63,11 +63,11 @@ func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32,
strokeOp.Width = strokeWidth strokeOp.Width = strokeWidth
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp) vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr) drawVerticesForUtil(dst, vs, is, clr, antialias)
} }
// DrawFilledRect fills a rectangle with the specified width and color. // DrawFilledRect fills a rectangle with the specified width and color.
func DrawFilledRect(dst *ebiten.Image, x, y, width, height float32, clr color.Color) { func DrawFilledRect(dst *ebiten.Image, x, y, width, height float32, clr color.Color, antialias bool) {
var path Path var path Path
path.MoveTo(x, y) path.MoveTo(x, y)
path.LineTo(x, y+height) path.LineTo(x, y+height)
@ -75,13 +75,13 @@ func DrawFilledRect(dst *ebiten.Image, x, y, width, height float32, clr color.Co
path.LineTo(x+width, y) path.LineTo(x+width, y)
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil) vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
drawVerticesForUtil(dst, vs, is, clr) drawVerticesForUtil(dst, vs, is, clr, antialias)
} }
// StrokeRect strokes a rectangle with the specified width and color. // StrokeRect strokes a rectangle with the specified width and color.
// //
// clr has be to be a solid (non-transparent) color. // clr has be to be a solid (non-transparent) color.
func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth float32, clr color.Color) { func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth float32, clr color.Color, antialias bool) {
var path Path var path Path
path.MoveTo(x, y) path.MoveTo(x, y)
path.LineTo(x, y+height) path.LineTo(x, y+height)
@ -94,22 +94,22 @@ func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth floa
strokeOp.MiterLimit = 10 strokeOp.MiterLimit = 10
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp) vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr) drawVerticesForUtil(dst, vs, is, clr, antialias)
} }
// DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color. // DrawFilledCircle fills a circle with the specified center position (cx, cy), the radius (r), width and color.
func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color) { func DrawFilledCircle(dst *ebiten.Image, cx, cy, r float32, clr color.Color, antialias bool) {
var path Path var path Path
path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise) path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise)
vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil) vs, is := path.AppendVerticesAndIndicesForFilling(nil, nil)
drawVerticesForUtil(dst, vs, is, clr) drawVerticesForUtil(dst, vs, is, clr, antialias)
} }
// StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color. // StrokeCircle strokes a circle with the specified center position (cx, cy), the radius (r), width and color.
// //
// clr has be to be a solid (non-transparent) color. // clr has be to be a solid (non-transparent) color.
func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr color.Color) { func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr color.Color, antialias bool) {
var path Path var path Path
path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise) path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise)
path.Close() path.Close()
@ -118,5 +118,5 @@ func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr
strokeOp.Width = strokeWidth strokeOp.Width = strokeWidth
vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp) vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp)
drawVerticesForUtil(dst, vs, is, clr) drawVerticesForUtil(dst, vs, is, clr, antialias)
} }