From 155be5a88dd23933bc0007269aecb16fad5f7165 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Fri, 16 Jan 2015 01:56:27 +0900 Subject: [PATCH] Add Rect.Color --- example/shapes/main.go | 18 ++++++++++++++---- image.go | 10 +++++++--- internal/graphics/framebuffer.go | 6 ++++-- internal/graphics/internal/shader/draw.go | 6 +++++- 4 files changed, 30 insertions(+), 10 deletions(-) diff --git a/example/shapes/main.go b/example/shapes/main.go index bbf5c08b3..f017ea3d3 100644 --- a/example/shapes/main.go +++ b/example/shapes/main.go @@ -28,6 +28,7 @@ const ( ) var rectsToDraw = make([]image.Rectangle, 100) +var colors = make([]color.NRGBA, 100) func min(a, b int) int { if a < b { @@ -48,22 +49,31 @@ func init() { x0, x1 := rand.Intn(screenWidth), rand.Intn(screenWidth) y0, y1 := rand.Intn(screenHeight), rand.Intn(screenHeight) rectsToDraw[i] = image.Rect(min(x0, x1), min(y0, y1), max(x0, x1), max(y0, y1)) + r, g, b, a := uint8(rand.Intn(0xff)), uint8(rand.Intn(0xff)), uint8(rand.Intn(0xff)), uint8(rand.Intn(0xff)) + colors[i] = color.NRGBA{r, g, b, a} } } -type rects []image.Rectangle +type rects struct { + rects []image.Rectangle + colors []color.NRGBA +} func (r rects) Len() int { - return len(r) + return len(r.rects) } func (r rects) Points(i int) (x0, y0, x1, y1 int) { - rect := &r[i] + rect := &r.rects[i] return rect.Min.X, rect.Min.Y, rect.Max.X, rect.Max.Y } +func (r rects) Color(i int) color.Color { + return r.colors[i] +} + func update(screen *ebiten.Image) error { - screen.DrawRects(color.NRGBA{0x80, 0x80, 0xff, 0x80}, rects(rectsToDraw)) + screen.DrawRects(&rects{rectsToDraw, colors}) return nil } diff --git a/image.go b/image.go index 14deb33f5..87383f5ef 100644 --- a/image.go +++ b/image.go @@ -98,6 +98,7 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) { type Rects interface { Len() int Points(i int) (x0, y0, x1, y1 int) + Color(i int) color.Color } type rectVertexQuads struct { @@ -113,13 +114,16 @@ func (l rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 float64) { return float64(ix0), float64(iy0), float64(ix1), float64(iy1) } +func (l rectVertexQuads) Color(i int) color.Color { + return l.Rects.Color(i) +} + // DrawRects draws rectangles on the image. // // NOTE: This method is experimental. -func (i *Image) DrawRects(clr color.Color, rects Rects) (err error) { - r, g, b, a := internal.RGBA(clr) +func (i *Image) DrawRects(rects Rects) (err error) { ui.Use(func(c *opengl.Context) { - err = i.framebuffer.DrawRects(c, r, g, b, a, &rectVertexQuads{rects}) + err = i.framebuffer.DrawRects(c, &rectVertexQuads{rects}) }) return } diff --git a/internal/graphics/framebuffer.go b/internal/graphics/framebuffer.go index ea39a6f44..5ccdf8a70 100644 --- a/internal/graphics/framebuffer.go +++ b/internal/graphics/framebuffer.go @@ -18,6 +18,7 @@ import ( "github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal/graphics/internal/shader" "github.com/hajimehoshi/ebiten/internal/opengl" + "image/color" ) func orthoProjectionMatrix(left, right, bottom, top int) *[4][4]float64 { @@ -116,12 +117,13 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu type VertexQuads interface { Len() int Vertex(i int) (x0, y0, x1, y1 float64) + Color(i int) color.Color } -func (f *Framebuffer) DrawRects(c *opengl.Context, r, g, b, a float64, quads VertexQuads) error { +func (f *Framebuffer) DrawRects(c *opengl.Context, quads VertexQuads) error { if err := f.setAsViewport(c); err != nil { return err } p := f.projectionMatrix() - return shader.DrawRects(c, p, r, g, b, a, quads) + return shader.DrawRects(c, p, quads) } diff --git a/internal/graphics/internal/shader/draw.go b/internal/graphics/internal/shader/draw.go index 0fc4e049b..b0aa14e5e 100644 --- a/internal/graphics/internal/shader/draw.go +++ b/internal/graphics/internal/shader/draw.go @@ -17,7 +17,9 @@ package shader import ( "errors" "fmt" + "github.com/hajimehoshi/ebiten/internal" "github.com/hajimehoshi/ebiten/internal/opengl" + "image/color" ) func glMatrix(m *[4][4]float64) []float32 { @@ -91,6 +93,7 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4 type VertexQuads interface { Len() int Vertex(i int) (x0, y0, x1, y1 float64) + Color(i int) color.Color } func max(a, b float32) float32 { @@ -100,7 +103,7 @@ func max(a, b float32) float32 { return a } -func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a float64, quads VertexQuads) error { +func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, quads VertexQuads) error { if !initialized { if err := initialize(c); err != nil { return err @@ -122,6 +125,7 @@ func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, r, g, b, a fl if x0 == x1 || y0 == y1 { continue } + r, g, b, a := internal.RGBA(quads.Color(i)) vertices = append(vertices, float32(x0), float32(y0), float32(r), float32(g), float32(b), float32(a), float32(x1), float32(y0), float32(r), float32(g), float32(b), float32(a),