Add image.DrawRect / DrawRects (leaving some bugs)

This commit is contained in:
Hajime Hoshi 2015-01-17 15:46:56 +09:00
parent 5ff2b8697f
commit 7835f5cb2e
3 changed files with 36 additions and 0 deletions

View File

@ -26,6 +26,7 @@ const (
) )
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
screen.DrawRect(0, 0, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
screen.FillRect(10, 10, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80}) screen.FillRect(10, 10, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
screen.FillRect(20, 20, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80}) screen.FillRect(20, 20, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80})
screen.DrawLine(130, 10, 140, 150, color.NRGBA{0xff, 0x80, 0x80, 0x80}) screen.DrawLine(130, 10, 140, 150, color.NRGBA{0xff, 0x80, 0x80, 0x80})

View File

@ -108,6 +108,14 @@ func (i *Image) DrawLines(lines Lines) (err error) {
return return
} }
func (i *Image) DrawRect(x, y, width, height int, clr color.Color) error {
return i.DrawLines(&rectsAsLines{&rect{x, y, width, height, clr}})
}
func (i *Image) DrawRects(rects Rects) error {
return i.DrawLines(&rectsAsLines{rects})
}
// FillRect draws a filled rectangle. // FillRect draws a filled rectangle.
func (i *Image) FillRect(x, y, width, height int, clr color.Color) error { func (i *Image) FillRect(x, y, width, height int, clr color.Color) error {
return i.FillRects(&rect{x, y, width, height, clr}) return i.FillRects(&rect{x, y, width, height, clr})

View File

@ -43,6 +43,33 @@ func (l *line) Color(i int) color.Color {
return l.color return l.color
} }
type rectsAsLines struct {
Rects
}
func (r *rectsAsLines) Len() int {
return r.Rects.Len() * 4
}
func (r *rectsAsLines) Points(i int) (x0, y0, x1, y1 int) {
x, y, w, h := r.Rects.Rect(i / 4)
switch i % 4 {
case 0:
return x, y, x + w, y
case 1:
return x, y, x + 1, y + h
case 2:
return x, y + h, x + w, y + h
case 3:
return x + w, y, x + w, y + h
}
panic("not reach")
}
func (r *rectsAsLines) Color(i int) color.Color {
return r.Rects.Color(i / 4)
}
// A Rects represents the set of rectangles. // A Rects represents the set of rectangles.
type Rects interface { type Rects interface {
Len() int Len() int