From 7835f5cb2eca258532a4ae62c35b042be2f75001 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 17 Jan 2015 15:46:56 +0900 Subject: [PATCH] Add image.DrawRect / DrawRects (leaving some bugs) --- example/shapes/main.go | 1 + image.go | 8 ++++++++ shapes.go | 27 +++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/example/shapes/main.go b/example/shapes/main.go index c5b6dfeb4..c54bb52d6 100644 --- a/example/shapes/main.go +++ b/example/shapes/main.go @@ -26,6 +26,7 @@ const ( ) 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(20, 20, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80}) screen.DrawLine(130, 10, 140, 150, color.NRGBA{0xff, 0x80, 0x80, 0x80}) diff --git a/image.go b/image.go index 8bb6db065..00b8b07e4 100644 --- a/image.go +++ b/image.go @@ -108,6 +108,14 @@ func (i *Image) DrawLines(lines Lines) (err error) { 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. func (i *Image) FillRect(x, y, width, height int, clr color.Color) error { return i.FillRects(&rect{x, y, width, height, clr}) diff --git a/shapes.go b/shapes.go index 2d5392644..4304d5976 100644 --- a/shapes.go +++ b/shapes.go @@ -43,6 +43,33 @@ func (l *line) Color(i int) color.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. type Rects interface { Len() int