2013-06-22 19:01:26 +02:00
|
|
|
package rects
|
|
|
|
|
|
|
|
import (
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
2013-06-22 19:01:26 +02:00
|
|
|
"image/color"
|
2013-10-11 15:17:38 +02:00
|
|
|
"math"
|
2013-06-22 19:01:26 +02:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Rects struct {
|
2013-10-16 17:14:32 +02:00
|
|
|
rectTextureID graphics.RenderTargetID
|
2013-10-11 15:17:49 +02:00
|
|
|
rectTextureInited bool
|
2013-10-16 17:14:32 +02:00
|
|
|
offscreenID graphics.RenderTargetID
|
2013-10-11 15:17:49 +02:00
|
|
|
offscreenInited bool
|
|
|
|
rectBounds *graphics.Rect
|
|
|
|
rectColor *color.RGBA
|
2013-06-22 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
2013-10-16 16:06:35 +02:00
|
|
|
const (
|
2013-10-16 16:20:07 +02:00
|
|
|
rectTextureWidth = 16
|
2013-10-16 16:06:35 +02:00
|
|
|
rectTextureHeight = 16
|
2013-10-16 16:20:07 +02:00
|
|
|
offscreenWidth = 256
|
|
|
|
offscreenHeight = 240
|
2013-10-16 16:06:35 +02:00
|
|
|
)
|
|
|
|
|
2013-06-22 19:01:26 +02:00
|
|
|
func New() *Rects {
|
2013-10-09 16:34:11 +02:00
|
|
|
return &Rects{
|
2013-10-11 15:17:49 +02:00
|
|
|
rectTextureInited: false,
|
|
|
|
offscreenInited: false,
|
|
|
|
rectBounds: &graphics.Rect{},
|
|
|
|
rectColor: &color.RGBA{},
|
2013-10-09 16:34:11 +02:00
|
|
|
}
|
2013-06-22 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Rects) Init(tf graphics.TextureFactory) {
|
2013-10-16 17:14:32 +02:00
|
|
|
game.rectTextureID = tf.NewRenderTarget(rectTextureWidth, rectTextureHeight)
|
|
|
|
game.offscreenID = tf.NewRenderTarget(offscreenWidth, offscreenHeight)
|
2013-06-22 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
2013-07-05 14:02:17 +02:00
|
|
|
func (game *Rects) Update(context ebiten.GameContext) {
|
2013-10-11 15:17:38 +02:00
|
|
|
game.rectBounds.X = rand.Intn(context.ScreenWidth())
|
|
|
|
game.rectBounds.Y = rand.Intn(context.ScreenHeight())
|
|
|
|
game.rectBounds.Width =
|
|
|
|
rand.Intn(context.ScreenWidth() - game.rectBounds.X)
|
|
|
|
game.rectBounds.Height =
|
|
|
|
rand.Intn(context.ScreenHeight() - game.rectBounds.Y)
|
2013-10-09 16:34:11 +02:00
|
|
|
|
2013-10-16 16:06:35 +02:00
|
|
|
game.rectColor.R = uint8(rand.Intn(math.MaxUint8))
|
|
|
|
game.rectColor.G = uint8(rand.Intn(math.MaxUint8))
|
|
|
|
game.rectColor.B = uint8(rand.Intn(math.MaxUint8))
|
|
|
|
game.rectColor.A = uint8(rand.Intn(math.MaxUint8))
|
2013-06-22 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
2013-10-11 15:17:38 +02:00
|
|
|
func (game *Rects) rectGeometryMatrix() matrix.Geometry {
|
|
|
|
geometryMatrix := matrix.IdentityGeometry()
|
|
|
|
scaleX := float64(game.rectBounds.Width) /
|
2013-10-16 16:06:35 +02:00
|
|
|
float64(rectTextureWidth)
|
2013-10-11 15:17:38 +02:00
|
|
|
scaleY := float64(game.rectBounds.Height) /
|
2013-10-16 16:06:35 +02:00
|
|
|
float64(rectTextureHeight)
|
2013-10-11 15:17:38 +02:00
|
|
|
geometryMatrix.Scale(scaleX, scaleY)
|
|
|
|
geometryMatrix.Translate(
|
|
|
|
float64(game.rectBounds.X), float64(game.rectBounds.Y))
|
|
|
|
return geometryMatrix
|
|
|
|
}
|
|
|
|
|
|
|
|
func (game *Rects) rectColorMatrix() matrix.Color {
|
|
|
|
colorMatrix := matrix.IdentityColor()
|
|
|
|
colorMatrix.Elements[0][0] =
|
|
|
|
float64(game.rectColor.R) / float64(math.MaxUint8)
|
|
|
|
colorMatrix.Elements[1][1] =
|
|
|
|
float64(game.rectColor.G) / float64(math.MaxUint8)
|
|
|
|
colorMatrix.Elements[2][2] =
|
|
|
|
float64(game.rectColor.B) / float64(math.MaxUint8)
|
|
|
|
colorMatrix.Elements[3][3] =
|
|
|
|
float64(game.rectColor.A) / float64(math.MaxUint8)
|
|
|
|
return colorMatrix
|
|
|
|
}
|
|
|
|
|
2013-07-04 16:57:53 +02:00
|
|
|
func (game *Rects) Draw(g graphics.Context) {
|
2013-10-11 15:17:38 +02:00
|
|
|
if !game.rectTextureInited {
|
2013-10-16 17:14:32 +02:00
|
|
|
g.SetOffscreen(game.rectTextureID)
|
2013-10-11 20:20:13 +02:00
|
|
|
g.Fill(255, 255, 255)
|
2013-10-11 15:17:38 +02:00
|
|
|
game.rectTextureInited = true
|
|
|
|
}
|
2013-06-22 19:01:26 +02:00
|
|
|
|
2013-10-16 17:14:32 +02:00
|
|
|
g.SetOffscreen(game.offscreenID)
|
2013-10-11 15:17:49 +02:00
|
|
|
if !game.offscreenInited {
|
2013-10-11 20:20:13 +02:00
|
|
|
g.Fill(0, 0, 0)
|
2013-10-11 15:17:49 +02:00
|
|
|
game.offscreenInited = true
|
2013-10-11 15:17:38 +02:00
|
|
|
}
|
2013-10-16 17:14:32 +02:00
|
|
|
g.DrawTexture(g.TextureID(game.rectTextureID),
|
2013-10-11 15:17:38 +02:00
|
|
|
game.rectGeometryMatrix(),
|
|
|
|
game.rectColorMatrix())
|
2013-06-22 19:01:26 +02:00
|
|
|
|
2013-10-16 15:18:46 +02:00
|
|
|
g.ResetOffscreen()
|
2013-10-16 17:14:32 +02:00
|
|
|
g.DrawTexture(g.TextureID(game.offscreenID),
|
2013-06-22 19:01:26 +02:00
|
|
|
matrix.IdentityGeometry(),
|
|
|
|
matrix.IdentityColor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|