ebiten/example/game/rotating/rotating.go

91 lines
1.9 KiB
Go
Raw Normal View History

2013-06-22 16:16:01 +02:00
package rotating
2013-06-21 04:02:38 +02:00
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"github.com/hajimehoshi/go-ebiten/ui"
2013-06-21 04:02:38 +02:00
"image"
_ "image/png"
2013-06-22 19:01:26 +02:00
"math"
2013-06-21 04:02:38 +02:00
"os"
)
2013-10-16 16:20:07 +02:00
const (
2013-10-17 17:45:12 +02:00
ebitenTextureWidth = 57
2013-10-16 16:20:07 +02:00
ebitenTextureHeight = 26
)
2013-06-22 19:01:26 +02:00
type Rotating struct {
2013-12-01 13:42:41 +01:00
ebitenTextureId graphics.TextureId
x int
geometryMatrix matrix.Geometry
screenSizeUpdatedCh chan ui.ScreenSizeUpdatedEvent
2013-11-29 20:24:52 +01:00
screenWidth int
screenHeight int
2013-06-21 04:02:38 +02:00
}
2013-06-22 19:01:26 +02:00
func New() *Rotating {
2013-11-29 20:24:52 +01:00
return &Rotating{
screenSizeUpdatedCh: make(chan ui.ScreenSizeUpdatedEvent),
2013-11-29 20:24:52 +01:00
}
}
func (game *Rotating) OnScreenSizeUpdated(e ui.ScreenSizeUpdatedEvent) {
2013-12-01 13:23:03 +01:00
go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
2013-06-21 04:02:38 +02:00
}
2013-10-20 08:51:26 +02:00
func (game *Rotating) InitTextures(tf graphics.TextureFactory) {
2013-07-02 18:27:04 +02:00
file, err := os.Open("images/ebiten.png")
2013-06-21 04:02:38 +02:00
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
2013-11-28 18:38:18 +01:00
if game.ebitenTextureId, err = tf.CreateTextureFromImage(img); err != nil {
2013-06-23 09:06:32 +02:00
panic(err)
}
2013-06-21 04:02:38 +02:00
}
2013-11-29 20:24:52 +01:00
func (game *Rotating) 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
}
2013-11-23 11:51:24 +01:00
const fps = 60
2013-06-21 04:02:38 +02:00
game.x++
2013-10-09 16:34:11 +02:00
game.geometryMatrix = matrix.IdentityGeometry()
2013-10-16 16:20:07 +02:00
tx, ty := float64(ebitenTextureWidth), float64(ebitenTextureHeight)
2013-10-09 16:34:11 +02:00
game.geometryMatrix.Translate(-tx/2, -ty/2)
2013-11-23 11:51:24 +01:00
game.geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / float64(fps*10))
2013-10-09 16:34:11 +02:00
game.geometryMatrix.Translate(tx/2, ty/2)
2013-11-29 20:24:52 +01:00
centerX := float64(game.screenWidth) / 2
centerY := float64(game.screenHeight) / 2
2013-10-09 16:34:11 +02:00
game.geometryMatrix.Translate(centerX-tx/2, centerY-ty/2)
2013-06-21 04:02:38 +02:00
}
func (game *Rotating) Draw(g graphics.Canvas) {
2013-10-11 20:20:13 +02:00
g.Fill(128, 128, 255)
2013-10-21 15:18:10 +02:00
g.DrawTexture(game.ebitenTextureId,
2013-10-09 16:34:11 +02:00
game.geometryMatrix,
2013-06-21 04:02:38 +02:00
matrix.IdentityColor())
}