diff --git a/image.go b/image.go index 37b4c3168..4db945810 100644 --- a/image.go +++ b/image.go @@ -103,7 +103,7 @@ func (i *Image) DrawRect(x, y, width, height int, clr color.Color) error { // DrawRects draws rectangles on the image. func (i *Image) DrawRects(rects Rects) (err error) { ui.Use(func(c *opengl.Context) { - err = i.framebuffer.DrawRects(c, &rectVertexQuads{rects}) + err = i.framebuffer.DrawRects(c, rects) }) return } diff --git a/internal/graphics/framebuffer.go b/internal/graphics/framebuffer.go index 39777acee..9707a53d0 100644 --- a/internal/graphics/framebuffer.go +++ b/internal/graphics/framebuffer.go @@ -114,16 +114,16 @@ func (f *Framebuffer) DrawTexture(c *opengl.Context, t *Texture, quads TextureQu return shader.DrawTexture(c, t.native, p, quads, geo, clr) } -type VertexQuads interface { +type Rects interface { Len() int - Vertex(i int) (x0, y0, x1, y1 int) + Rect(i int) (x, y, width, height int) Color(i int) color.Color } -func (f *Framebuffer) DrawRects(c *opengl.Context, quads VertexQuads) error { +func (f *Framebuffer) DrawRects(c *opengl.Context, rects Rects) error { if err := f.setAsViewport(c); err != nil { return err } p := f.projectionMatrix() - return shader.DrawRects(c, p, quads) + return shader.DrawRects(c, p, rects) } diff --git a/internal/graphics/internal/shader/draw.go b/internal/graphics/internal/shader/draw.go index 3fcc3cbd0..7c6bb77f1 100644 --- a/internal/graphics/internal/shader/draw.go +++ b/internal/graphics/internal/shader/draw.go @@ -89,20 +89,13 @@ func DrawTexture(c *opengl.Context, texture opengl.Texture, projectionMatrix *[4 return nil } -type VertexQuads interface { +type Rects interface { Len() int - Vertex(i int) (x0, y0, x1, y1 int) + Rect(i int) (x, y, width, height int) Color(i int) color.Color } -func max(a, b float32) float32 { - if a < b { - return b - } - return a -} - -func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, quads VertexQuads) error { +func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, rects Rects) error { if !initialized { if err := initialize(c); err != nil { return err @@ -110,7 +103,7 @@ func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, quads VertexQ initialized = true } - if quads.Len() == 0 { + if rects.Len() == 0 { return nil } @@ -119,12 +112,13 @@ func DrawRects(c *opengl.Context, projectionMatrix *[4][4]float64, quads VertexQ vertices := vertices[0:0] num := 0 - for i := 0; i < quads.Len(); i++ { - x0, y0, x1, y1 := quads.Vertex(i) - if x0 == x1 || y0 == y1 { + for i := 0; i < rects.Len(); i++ { + x, y, w, h := rects.Rect(i) + if w == 0 || h == 0 { continue } - r, g, b, a := quads.Color(i).RGBA() + x0, y0, x1, y1 := x, y, x+w, y+h + r, g, b, a := rects.Color(i).RGBA() vertices = append(vertices, int16(x0), int16(y0), int16(r), int16(g), int16(b), int16(a), int16(x1), int16(y0), int16(r), int16(g), int16(b), int16(a), diff --git a/shapes.go b/shapes.go index be2bc3870..1b98eef27 100644 --- a/shapes.go +++ b/shapes.go @@ -42,20 +42,3 @@ func (r *rect) Rect(i int) (x, y, width, height int) { func (r *rect) Color(i int) color.Color { return r.color } - -type rectVertexQuads struct { - Rects -} - -func (l *rectVertexQuads) Len() int { - return l.Rects.Len() -} - -func (l *rectVertexQuads) Vertex(i int) (x0, y0, x1, y1 int) { - x, y, width, height := l.Rects.Rect(i) - return x, y, x + width, y + height -} - -func (l *rectVertexQuads) Color(i int) color.Color { - return l.Rects.Color(i) -}