2013-06-22 19:01:26 +02:00
|
|
|
package rects
|
|
|
|
|
|
|
|
import (
|
2013-10-14 04:34:58 +02:00
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics"
|
|
|
|
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
|
2013-12-02 16:15:01 +01:00
|
|
|
"github.com/hajimehoshi/go-ebiten/ui"
|
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-12-01 13:42:41 +01:00
|
|
|
rectTextureId graphics.RenderTargetId
|
|
|
|
rectTextureInited bool
|
|
|
|
offscreenId graphics.RenderTargetId
|
|
|
|
offscreenInited bool
|
|
|
|
rectBounds *graphics.Rect
|
|
|
|
rectColor *color.RGBA
|
2013-12-02 16:15:01 +01:00
|
|
|
screenSizeUpdatedCh chan ui.ScreenSizeUpdatedEvent
|
2013-11-29 20:24:52 +01:00
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
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-12-01 13:42:41 +01:00
|
|
|
rectTextureInited: false,
|
|
|
|
offscreenInited: false,
|
|
|
|
rectBounds: &graphics.Rect{},
|
|
|
|
rectColor: &color.RGBA{},
|
2013-12-02 16:15:01 +01:00
|
|
|
screenSizeUpdatedCh: make(chan ui.ScreenSizeUpdatedEvent),
|
2013-10-09 16:34:11 +02:00
|
|
|
}
|
2013-06-22 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
2013-12-02 16:15:01 +01:00
|
|
|
func (game *Rects) OnScreenSizeUpdated(e ui.ScreenSizeUpdatedEvent) {
|
2013-12-01 13:23:03 +01:00
|
|
|
go func() {
|
|
|
|
e := e
|
|
|
|
game.screenSizeUpdatedCh <- e
|
|
|
|
}()
|
2013-11-29 20:24:52 +01:00
|
|
|
}
|
|
|
|
|
2013-10-20 08:51:26 +02:00
|
|
|
func (game *Rects) InitTextures(tf graphics.TextureFactory) {
|
2013-10-20 18:13:09 +02:00
|
|
|
var err error
|
2013-12-03 15:24:34 +01:00
|
|
|
game.rectTextureId, err = tf.CreateRenderTarget("rect", rectTextureWidth, rectTextureHeight)
|
2013-10-20 18:13:09 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-12-03 15:24:34 +01:00
|
|
|
game.offscreenId, err = tf.CreateRenderTarget("offscreen", offscreenWidth, offscreenHeight)
|
2013-10-20 18:13:09 +02:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2013-06-22 19:01:26 +02:00
|
|
|
}
|
|
|
|
|
2013-11-29 15:18:44 +01:00
|
|
|
func min(a, b int) int {
|
|
|
|
if a < b {
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
func abs(a int) int {
|
|
|
|
if a < 0 {
|
|
|
|
return -a
|
|
|
|
}
|
|
|
|
return a
|
|
|
|
}
|
|
|
|
|
2013-11-29 20:24:52 +01:00
|
|
|
func (game *Rects) Update() {
|
|
|
|
events:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case e := <-game.screenSizeUpdatedCh:
|
|
|
|
game.screenWidth, game.screenHeight = e.Width, e.Height
|
|
|
|
default:
|
|
|
|
break events
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if game.screenWidth == 0 || game.screenHeight == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
x1 := rand.Intn(game.screenWidth)
|
|
|
|
x2 := rand.Intn(game.screenWidth)
|
|
|
|
y1 := rand.Intn(game.screenHeight)
|
|
|
|
y2 := rand.Intn(game.screenHeight)
|
2013-11-29 15:18:44 +01:00
|
|
|
game.rectBounds.X = min(x1, x2)
|
|
|
|
game.rectBounds.Y = min(y1, y2)
|
|
|
|
game.rectBounds.Width = abs(x1 - x2)
|
|
|
|
game.rectBounds.Height = abs(y1 - y2)
|
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-11-29 14:06:22 +01:00
|
|
|
func (game *Rects) Draw(g graphics.Canvas) {
|
2013-10-11 15:17:38 +02:00
|
|
|
if !game.rectTextureInited {
|
2013-10-21 15:18:10 +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-21 15:18:10 +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-11-29 14:45:18 +01:00
|
|
|
g.DrawRenderTarget(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-11-29 14:45:18 +01:00
|
|
|
g.DrawRenderTarget(game.offscreenId,
|
2013-06-22 19:01:26 +02:00
|
|
|
matrix.IdentityGeometry(),
|
|
|
|
matrix.IdentityColor())
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
}
|