From 8bd7ce5c207497a2d439931d70cd5b280ac59e98 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 18 Mar 2023 11:01:46 +0900 Subject: [PATCH] vector: add anti-alias options to the utility functions Closes #2606 --- examples/audio/main.go | 4 ++-- examples/blocks/blocks/gamescene.go | 2 +- examples/cursor/main.go | 2 +- examples/piano/main.go | 4 ++-- examples/raycasting/main.go | 8 ++++---- examples/shapes/main.go | 14 +++++++------- examples/snake/main.go | 4 ++-- examples/stars/main.go | 2 +- examples/text/main.go | 6 +++--- vector/util.go | 24 ++++++++++++------------ 10 files changed, 35 insertions(+), 35 deletions(-) diff --git a/examples/audio/main.go b/examples/audio/main.go index 276a8bb5e..e446d6d7b 100644 --- a/examples/audio/main.go +++ b/examples/audio/main.go @@ -336,13 +336,13 @@ func (p *Player) seekBarIfNeeded() error { func (p *Player) draw(screen *ebiten.Image) { // Draw the bar. 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. c := p.current cx := float32(x) + float32(w)*float32(p.current)/float32(p.total) 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. m := (c / time.Minute) % 100 diff --git a/examples/blocks/blocks/gamescene.go b/examples/blocks/blocks/gamescene.go index fab944cd3..70bd0c5ba 100644 --- a/examples/blocks/blocks/gamescene.go +++ b/examples/blocks/blocks/gamescene.go @@ -108,7 +108,7 @@ func init() { } 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} diff --git a/examples/cursor/main.go b/examples/cursor/main.go index 48e1cd9ed..3fa95d74d 100644 --- a/examples/cursor/main.go +++ b/examples/cursor/main.go @@ -60,7 +60,7 @@ func (g *Game) Update() error { func (g *Game) Draw(screen *ebiten.Image) { 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() { diff --git a/examples/piano/main.go b/examples/piano/main.go index 5e23c6990..b70d96171 100644 --- a/examples/piano/main.go +++ b/examples/piano/main.go @@ -147,7 +147,7 @@ func init() { for i, k := range whiteKeys { x := i*keyWidth + 36 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) } @@ -158,7 +158,7 @@ func init() { } x := i*keyWidth + 24 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) } } diff --git a/examples/raycasting/main.go b/examples/raycasting/main.go index 36b69af76..ef195145e 100644 --- a/examples/raycasting/main.go +++ b/examples/raycasting/main.go @@ -247,7 +247,7 @@ func (g *Game) Draw(screen *ebiten.Image) { if g.showRays { // Draw 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 for _, obj := range g.objects { 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 - vector.DrawFilledRect(screen, float32(g.px)-2, float32(g.py)-2, 4, 4, color.Black) - 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)-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}, true) if g.showRays { ebitenutil.DebugPrintAt(screen, "R: hide rays", padding, 0) diff --git a/examples/shapes/main.go b/examples/shapes/main.go index 8f17c2488..cfdac6e51 100644 --- a/examples/shapes/main.go +++ b/examples/shapes/main.go @@ -41,15 +41,15 @@ func (g *Game) Update() error { func (g *Game) Draw(screen *ebiten.Image) { cf := float32(g.count) - vector.StrokeLine(screen, 100, 100, 300, 100, 1, color.RGBA{0xff, 0xff, 0xff, 0xff}) - vector.StrokeLine(screen, 50, 150, 50, 350, 1, color.RGBA{0xff, 0xff, 0x00, 0xff}) - vector.StrokeLine(screen, 50, 100+cf, 200+cf, 250, 4, color.RGBA{0x00, 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}, true) + 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.StrokeRect(screen, 300-cf, 50, 120, 120, 10+cf/4, color.RGBA{0x00, 0x80, 0x00, 0xff}) + 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}, true) - vector.DrawFilledCircle(screen, 400, 400, 100, color.RGBA{0x80, 0x00, 0x80, 0x80}) - vector.StrokeCircle(screen, 400, 400, 10+cf, 10+cf/2, color.RGBA{0xff, 0x80, 0xff, 0xff}) + 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}, true) ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS())) } diff --git a/examples/snake/main.go b/examples/snake/main.go index c9171a014..37b5c5d89 100644 --- a/examples/snake/main.go +++ b/examples/snake/main.go @@ -172,9 +172,9 @@ func (g *Game) Update() error { func (g *Game) Draw(screen *ebiten.Image) { 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 { ebitenutil.DebugPrint(screen, fmt.Sprintf("Press up/down/left/right to start")) diff --git a/examples/stars/main.go b/examples/stars/main.go index 3e9823a68..9b79921d6 100644 --- a/examples/stars/main.go +++ b/examples/stars/main.go @@ -63,7 +63,7 @@ func (s *Star) Draw(screen *ebiten.Image) { G: uint8(0xdd * s.brightness / 0xff), B: uint8(0xff * s.brightness / 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 { diff --git a/examples/text/main.go b/examples/text/main.go index 631a4a4a0..377aad6f5 100644 --- a/examples/text/main.go +++ b/examples/text/main.go @@ -93,13 +93,13 @@ func (g *Game) Draw(screen *ebiten.Image) { { const x, y = 20, 40 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) } { const x, y = 20, 140 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) } { @@ -114,7 +114,7 @@ func (g *Game) Draw(screen *ebiten.Image) { const x, y = 160, 240 const lineHeight = 80 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) } { diff --git a/vector/util.go b/vector/util.go index d2d46778e..0465181a8 100644 --- a/vector/util.go +++ b/vector/util.go @@ -37,7 +37,7 @@ func init() { 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() for i := range vs { vs[i].SrcX = 1 @@ -50,12 +50,12 @@ func drawVerticesForUtil(dst *ebiten.Image, vs []ebiten.Vertex, is []uint16, clr op := &ebiten.DrawTrianglesOptions{} op.ColorScaleMode = ebiten.ColorScaleModePremultipliedAlpha - op.AntiAlias = true + op.AntiAlias = antialias dst.DrawTriangles(vs, is, whiteSubImage, op) } // 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 path.MoveTo(x0, y0) path.LineTo(x1, y1) @@ -63,11 +63,11 @@ func StrokeLine(dst *ebiten.Image, x0, y0, x1, y1 float32, strokeWidth float32, strokeOp.Width = strokeWidth 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. -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 path.MoveTo(x, y) 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) 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. // // 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 path.MoveTo(x, y) path.LineTo(x, y+height) @@ -94,22 +94,22 @@ func StrokeRect(dst *ebiten.Image, x, y, width, height float32, strokeWidth floa strokeOp.MiterLimit = 10 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. -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 path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise) 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. // // 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 path.Arc(cx, cy, r, 0, 2*math.Pi, Clockwise) path.Close() @@ -118,5 +118,5 @@ func StrokeCircle(dst *ebiten.Image, cx, cy, r float32, strokeWidth float32, clr strokeOp.Width = strokeWidth vs, is := path.AppendVerticesAndIndicesForStroke(nil, nil, strokeOp) - drawVerticesForUtil(dst, vs, is, clr) + drawVerticesForUtil(dst, vs, is, clr, antialias) }