Skip to content

Commit

Permalink
vector: add StrokeLine, FillRect, and StrokeRect
Browse files Browse the repository at this point in the history
  • Loading branch information
hajimehoshi committed Oct 21, 2022
1 parent dbfacb2 commit 6f7b1a8
Show file tree
Hide file tree
Showing 12 changed files with 147 additions and 178 deletions.
23 changes: 6 additions & 17 deletions ebitenutil/shapes.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,30 +35,19 @@ func init() {
// DrawLine draws a line segment on the given destination dst.
//
// DrawLine is intended to be used mainly for debugging or prototyping purpose.
//
// Deprecated: as of v2.5. Use vector.StrokeLine instead.
func DrawLine(dst *ebiten.Image, x1, y1, x2, y2 float64, clr color.Color) {
length := math.Hypot(x2-x1, y2-y1)

op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(length, 1)
op.GeoM.Rotate(math.Atan2(y2-y1, x2-x1))
op.GeoM.Translate(x1, y1)
op.ColorM.ScaleWithColor(clr)
// Filter must be 'nearest' filter (default).
// Linear filtering would make edges blurred.
dst.DrawImage(whiteSubImage, op)
vector.StrokeLine(dst, float32(x1), float32(y1), float32(x2), float32(y2), 1, clr)
}

// DrawRect draws a rectangle on the given destination dst.
//
// DrawRect is intended to be used mainly for debugging or prototyping purpose.
//
// Deprecated: as of v2.5. Use vector.FillRect instead.
func DrawRect(dst *ebiten.Image, x, y, width, height float64, clr color.Color) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(width, height)
op.GeoM.Translate(x, y)
op.ColorM.ScaleWithColor(clr)
// Filter must be 'nearest' filter (default).
// Linear filtering would make edges blurred.
dst.DrawImage(whiteSubImage, op)
vector.FillRect(dst, float32(x), float32(y), float32(width), float32(height), clr)
}

// DrawCircle draws a circle on given destination dst.
Expand Down
3 changes: 2 additions & 1 deletion examples/audio/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
raudio "github.com/hajimehoshi/ebiten/v2/examples/resources/audio"
riaudio "github.com/hajimehoshi/ebiten/v2/examples/resources/images/audio"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

const (
Expand Down Expand Up @@ -338,7 +339,7 @@ func (p *Player) seekBarIfNeeded() error {
func (p *Player) draw(screen *ebiten.Image) {
// Draw the bar.
x, y, w, h := playerBarRect()
ebitenutil.DrawRect(screen, float64(x), float64(y), float64(w), float64(h), playerBarColor)
vector.FillRect(screen, float32(x), float32(y), float32(w), float32(h), playerBarColor)

// Draw the cursor on the bar.
c := p.current
Expand Down
4 changes: 2 additions & 2 deletions examples/blocks/blocks/gamescene.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"time"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

var (
Expand Down Expand Up @@ -107,7 +107,7 @@ func init() {
}

func drawWindow(r *ebiten.Image, x, y, width, height int) {
ebitenutil.DrawRect(r, float64(x), float64(y), float64(width), float64(height), color.RGBA{0, 0, 0, 0xc0})
vector.FillRect(r, float32(x), float32(y), float32(width), float32(height), color.RGBA{0, 0, 0, 0xc0})
}

var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}
Expand Down
3 changes: 2 additions & 1 deletion examples/cursor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

const (
Expand All @@ -50,7 +51,7 @@ func (g *Game) Update() error {

func (g *Game) Draw(screen *ebiten.Image) {
for r, c := range g.gridColors {
ebitenutil.DrawRect(screen, float64(r.Min.X), float64(r.Min.Y), float64(r.Dx()), float64(r.Dy()), c)
vector.FillRect(screen, float32(r.Min.X), float32(r.Min.Y), float32(r.Dx()), float32(r.Dy()), c)
}

switch ebiten.CursorShape() {
Expand Down
5 changes: 3 additions & 2 deletions examples/piano/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/examples/resources/fonts"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/text"
"github.com/hajimehoshi/ebiten/v2/vector"
)

var (
Expand Down Expand Up @@ -149,7 +150,7 @@ func init() {
for i, k := range whiteKeys {
x := i*keyWidth + 36
height := 112
ebitenutil.DrawRect(pianoImage, float64(x), float64(y), float64(keyWidth-1), float64(height), color.White)
vector.FillRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.White)
text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.Black)
}

Expand All @@ -160,7 +161,7 @@ func init() {
}
x := i*keyWidth + 24
height := 64
ebitenutil.DrawRect(pianoImage, float64(x), float64(y), float64(keyWidth-1), float64(height), color.Black)
vector.FillRect(pianoImage, float32(x), float32(y), float32(keyWidth-1), float32(height), color.Black)
text.Draw(pianoImage, k, arcadeFont, x+8, y+height-8, color.White)
}
}
Expand Down
9 changes: 5 additions & 4 deletions examples/raycasting/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/examples/resources/images"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

const (
Expand Down Expand Up @@ -249,7 +250,7 @@ func (g *Game) Draw(screen *ebiten.Image) {
if g.showRays {
// Draw rays
for _, r := range rays {
ebitenutil.DrawLine(screen, r.X1, r.Y1, r.X2, r.Y2, 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})
}
}

Expand All @@ -261,13 +262,13 @@ func (g *Game) Draw(screen *ebiten.Image) {
// Draw walls
for _, obj := range g.objects {
for _, w := range obj.walls {
ebitenutil.DrawLine(screen, w.X1, w.Y1, w.X2, w.Y2, 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})
}
}

// Draw player as a rect
ebitenutil.DrawRect(screen, float64(g.px)-2, float64(g.py)-2, 4, 4, color.Black)
ebitenutil.DrawRect(screen, float64(g.px)-1, float64(g.py)-1, 2, 2, color.RGBA{255, 100, 100, 255})
vector.FillRect(screen, float32(g.px)-2, float32(g.py)-2, 4, 4, color.Black)
vector.FillRect(screen, float32(g.px)-1, float32(g.py)-1, 2, 2, color.RGBA{255, 100, 100, 255})

if g.showRays {
ebitenutil.DebugPrintAt(screen, "R: hide rays", padding, 0)
Expand Down
141 changes: 7 additions & 134 deletions examples/shapes/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,164 +19,37 @@ package main

import (
"fmt"
"image"
"image/color"
"log"
"math"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

const (
screenWidth = 640
screenHeight = 480
)

var (
whiteImage = ebiten.NewImage(3, 3)
)

func init() {
whiteImage.Fill(color.White)
}

type Game struct {
count int
}

func line(x0, y0, x1, y1 float32, clr color.RGBA) ([]ebiten.Vertex, []uint16) {
const width = 1

theta := math.Atan2(float64(y1-y0), float64(x1-x0))
theta += math.Pi / 2
dx := float32(math.Cos(theta))
dy := float32(math.Sin(theta))

r := float32(clr.R) / 0xff
g := float32(clr.G) / 0xff
b := float32(clr.B) / 0xff
a := float32(clr.A) / 0xff

return []ebiten.Vertex{
{
DstX: x0 - width*dx/2,
DstY: y0 - width*dy/2,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
{
DstX: x0 + width*dx/2,
DstY: y0 + width*dy/2,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
{
DstX: x1 - width*dx/2,
DstY: y1 - width*dy/2,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
{
DstX: x1 + width*dx/2,
DstY: y1 + width*dy/2,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
}, []uint16{0, 1, 2, 1, 2, 3}
}

func rect(x, y, w, h float32, clr color.RGBA) ([]ebiten.Vertex, []uint16) {
r := float32(clr.R) / 0xff
g := float32(clr.G) / 0xff
b := float32(clr.B) / 0xff
a := float32(clr.A) / 0xff
x0 := x
y0 := y
x1 := x + w
y1 := y + h

return []ebiten.Vertex{
{
DstX: x0,
DstY: y0,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
{
DstX: x1,
DstY: y0,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
{
DstX: x0,
DstY: y1,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
{
DstX: x1,
DstY: y1,
SrcX: 1,
SrcY: 1,
ColorR: r,
ColorG: g,
ColorB: b,
ColorA: a,
},
}, []uint16{0, 1, 2, 1, 2, 3}
}

func (g *Game) Update() error {
g.count++
g.count %= 240
return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
src := whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image)

cf := float64(g.count)
v, i := line(100, 100, 300, 100, color.RGBA{0xff, 0xff, 0xff, 0xff})
screen.DrawTriangles(v, i, src, nil)
v, i = line(50, 150, 50, 350, color.RGBA{0xff, 0xff, 0x00, 0xff})
screen.DrawTriangles(v, i, src, nil)
v, i = line(50, 100+float32(cf), 200+float32(cf), 250, color.RGBA{0x00, 0xff, 0xff, 0xff})
screen.DrawTriangles(v, i, src, nil)
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+float32(cf), 200+float32(cf), 250, 4, color.RGBA{0x00, 0xff, 0xff, 0xff})

v, i = rect(50+float32(cf), 50+float32(cf), 100+float32(cf), 100+float32(cf), color.RGBA{0x80, 0x80, 0x80, 0x80})
screen.DrawTriangles(v, i, src, nil)
v, i = rect(300-float32(cf), 50, 120, 120, color.RGBA{0x00, 0x80, 0x00, 0x80})
screen.DrawTriangles(v, i, src, nil)
vector.FillRect(screen, 50+float32(cf), 50+float32(cf), 100+float32(cf), 100+float32(cf), color.RGBA{0x80, 0x80, 0x80, 0xff})
vector.StrokeRect(screen, 300-float32(cf), 50, 120, 120, 10, color.RGBA{0x00, 0x80, 0x00, 0xff})

ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS()))
}
Expand Down
5 changes: 3 additions & 2 deletions examples/snake/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

const (
Expand Down Expand Up @@ -174,9 +175,9 @@ func (g *Game) Update() error {

func (g *Game) Draw(screen *ebiten.Image) {
for _, v := range g.snakeBody {
ebitenutil.DrawRect(screen, float64(v.X*gridSize), float64(v.Y*gridSize), gridSize, gridSize, color.RGBA{0x80, 0xa0, 0xc0, 0xff})
vector.FillRect(screen, float32(v.X*gridSize), float32(v.Y*gridSize), gridSize, gridSize, color.RGBA{0x80, 0xa0, 0xc0, 0xff})
}
ebitenutil.DrawRect(screen, float64(g.apple.X*gridSize), float64(g.apple.Y*gridSize), gridSize, gridSize, color.RGBA{0xFF, 0x00, 0x00, 0xff})
vector.FillRect(screen, float32(g.apple.X*gridSize), float32(g.apple.Y*gridSize), gridSize, gridSize, color.RGBA{0xFF, 0x00, 0x00, 0xff})

if g.moveDirection == dirNone {
ebitenutil.DebugPrint(screen, fmt.Sprintf("Press up/down/left/right to start"))
Expand Down
19 changes: 10 additions & 9 deletions examples/stars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (
"time"

"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/vector"
)

const (
Expand All @@ -35,18 +35,18 @@ const (
)

type Star struct {
fromx, fromy, tox, toy, brightness float64
fromx, fromy, tox, toy, brightness float32
}

func (s *Star) Init() {
s.tox = rand.Float64() * screenWidth * scale
s.tox = rand.Float32() * screenWidth * scale
s.fromx = s.tox
s.toy = rand.Float64() * screenHeight * scale
s.toy = rand.Float32() * screenHeight * scale
s.fromy = s.toy
s.brightness = rand.Float64() * 0xff
s.brightness = rand.Float32() * 0xff
}

func (s *Star) Update(x, y float64) {
func (s *Star) Update(x, y float32) {
s.fromx = s.tox
s.fromy = s.toy
s.tox += (s.tox - x) / 32
Expand All @@ -61,11 +61,12 @@ func (s *Star) Update(x, y float64) {
}

func (s *Star) Draw(screen *ebiten.Image) {
c := color.RGBA{R: uint8(0xbb * s.brightness / 0xff),
c := color.RGBA{
R: uint8(0xbb * s.brightness / 0xff),
G: uint8(0xdd * s.brightness / 0xff),
B: uint8(0xff * s.brightness / 0xff),
A: 0xff}
ebitenutil.DrawLine(screen, s.fromx/scale, s.fromy/scale, s.tox/scale, s.toy/scale, c)
vector.StrokeLine(screen, s.fromx/scale, s.fromy/scale, s.tox/scale, s.toy/scale, 1, c)
}

type Game struct {
Expand All @@ -83,7 +84,7 @@ func NewGame() *Game {
func (g *Game) Update() error {
x, y := ebiten.CursorPosition()
for i := 0; i < starsCount; i++ {
g.stars[i].Update(float64(x*scale), float64(y*scale))
g.stars[i].Update(float32(x*scale), float32(y*scale))
}
return nil
}
Expand Down
Loading

0 comments on commit 6f7b1a8

Please sign in to comment.