From 887e2fc675f718b17d943c4a21982ded0dbd5099 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 17 Jan 2015 03:46:00 +0900 Subject: [PATCH] Add image.DrawRect; Change Rects.Points -> Rects.Rect --- example/shapes/main.go | 50 ++-------------------------------- image.go | 26 +++--------------- shapes.go | 61 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 71 deletions(-) create mode 100644 shapes.go diff --git a/example/shapes/main.go b/example/shapes/main.go index f017ea3d3..dbec15df1 100644 --- a/example/shapes/main.go +++ b/example/shapes/main.go @@ -16,10 +16,8 @@ package main import ( "github.com/hajimehoshi/ebiten" - "image" "image/color" "log" - "math/rand" ) const ( @@ -27,53 +25,9 @@ const ( screenHeight = 240 ) -var rectsToDraw = make([]image.Rectangle, 100) -var colors = make([]color.NRGBA, 100) - -func min(a, b int) int { - if a < b { - return a - } - return b -} - -func max(a, b int) int { - if a < b { - return b - } - return a -} - -func init() { - for i, _ := range rectsToDraw { - 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 struct { - rects []image.Rectangle - colors []color.NRGBA -} - -func (r rects) Len() int { - return len(r.rects) -} - -func (r rects) Points(i int) (x0, y0, x1, y1 int) { - 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(&rects{rectsToDraw, colors}) + screen.DrawRect(10, 10, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80}) + screen.DrawRect(20, 20, 100, 100, color.NRGBA{0x80, 0x80, 0xff, 0x80}) return nil } diff --git a/image.go b/image.go index 871a5922f..37b4c3168 100644 --- a/image.go +++ b/image.go @@ -95,32 +95,12 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) { return } -// A Rects represents the set of rectangles. -type Rects interface { - Len() int - Points(i int) (x0, y0, x1, y1 int) - Color(i int) color.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) { - return l.Rects.Points(i) -} - -func (l rectVertexQuads) Color(i int) color.Color { - return l.Rects.Color(i) +// DrawRect draws a rectangle. +func (i *Image) DrawRect(x, y, width, height int, clr color.Color) error { + return i.DrawRects(&rect{x, y, width, height, clr}) } // DrawRects draws rectangles on the image. -// -// NOTE: This method is experimental. func (i *Image) DrawRects(rects Rects) (err error) { ui.Use(func(c *opengl.Context) { err = i.framebuffer.DrawRects(c, &rectVertexQuads{rects}) diff --git a/shapes.go b/shapes.go new file mode 100644 index 000000000..be2bc3870 --- /dev/null +++ b/shapes.go @@ -0,0 +1,61 @@ +// Copyright 2014 Hajime Hoshi +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package ebiten + +import ( + "image/color" +) + +// A Rects represents the set of rectangles. +type Rects interface { + Len() int + Rect(i int) (x, y, width, height int) + Color(i int) color.Color +} + +type rect struct { + x, y int + width, height int + color color.Color +} + +func (r *rect) Len() int { + return 1 +} + +func (r *rect) Rect(i int) (x, y, width, height int) { + return r.x, r.y, r.width, r.height +} + +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) +}