Add Rect.Color

This commit is contained in:
Hajime Hoshi 2015-01-16 01:56:27 +09:00
parent 037f35b0db
commit 155be5a88d
4 changed files with 30 additions and 10 deletions

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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),