mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
Add image.DrawRect; Change Rects.Points -> Rects.Rect
This commit is contained in:
parent
f186d1b286
commit
887e2fc675
@ -16,10 +16,8 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten"
|
"github.com/hajimehoshi/ebiten"
|
||||||
"image"
|
|
||||||
"image/color"
|
"image/color"
|
||||||
"log"
|
"log"
|
||||||
"math/rand"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@ -27,53 +25,9 @@ const (
|
|||||||
screenHeight = 240
|
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 {
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
26
image.go
26
image.go
@ -95,32 +95,12 @@ func (i *Image) DrawImage(image *Image, options *DrawImageOptions) (err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// A Rects represents the set of rectangles.
|
// DrawRect draws a rectangle.
|
||||||
type Rects interface {
|
func (i *Image) DrawRect(x, y, width, height int, clr color.Color) error {
|
||||||
Len() int
|
return i.DrawRects(&rect{x, y, width, height, clr})
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// DrawRects draws rectangles on the image.
|
// DrawRects draws rectangles on the image.
|
||||||
//
|
|
||||||
// NOTE: This method is experimental.
|
|
||||||
func (i *Image) DrawRects(rects Rects) (err error) {
|
func (i *Image) DrawRects(rects Rects) (err error) {
|
||||||
ui.Use(func(c *opengl.Context) {
|
ui.Use(func(c *opengl.Context) {
|
||||||
err = i.framebuffer.DrawRects(c, &rectVertexQuads{rects})
|
err = i.framebuffer.DrawRects(c, &rectVertexQuads{rects})
|
||||||
|
61
shapes.go
Normal file
61
shapes.go
Normal file
@ -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)
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user