ebiten/example/game/rotating/rotating.go

70 lines
1.4 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 (
"github.com/hajimehoshi/go.ebiten/graphics"
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
"image"
"image/color"
_ "image/png"
2013-06-22 19:01:26 +02:00
"math"
2013-06-21 04:02:38 +02:00
"os"
)
2013-06-22 19:01:26 +02:00
type Rotating struct {
2013-06-21 04:02:38 +02:00
ebitenTexture graphics.Texture
x int
}
2013-06-22 19:01:26 +02:00
func New() *Rotating {
return &Rotating{}
2013-06-21 04:02:38 +02:00
}
2013-06-22 19:01:26 +02:00
func (game *Rotating) ScreenWidth() int {
2013-06-21 16:16:52 +02:00
return 256
}
2013-06-22 19:01:26 +02:00
func (game *Rotating) ScreenHeight() int {
2013-06-21 16:16:52 +02:00
return 240
}
2013-06-23 15:38:41 +02:00
func (game *Rotating) Fps() int {
return 60
}
2013-06-22 19:01:26 +02:00
func (game *Rotating) Init(tf graphics.TextureFactory) {
2013-06-21 04:02:38 +02:00
file, err := os.Open("ebiten.png")
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
2013-06-23 09:06:32 +02:00
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
2013-06-21 04:02:38 +02:00
}
2013-06-22 19:01:26 +02:00
func (game *Rotating) Update() {
2013-06-21 04:02:38 +02:00
game.x++
}
2013-06-22 19:01:26 +02:00
func (game *Rotating) Draw(g graphics.GraphicsContext, offscreen graphics.Texture) {
2013-06-21 04:02:38 +02:00
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
geometryMatrix := matrix.IdentityGeometry()
tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height)
geometryMatrix.Translate(-tx/2, -ty/2)
2013-06-23 17:43:41 +02:00
geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / float64(game.Fps()*10))
2013-06-21 04:02:38 +02:00
geometryMatrix.Translate(tx/2, ty/2)
2013-06-21 16:16:52 +02:00
centerX := float64(game.ScreenWidth()) / 2
centerY := float64(game.ScreenHeight()) / 2
2013-06-21 04:16:19 +02:00
geometryMatrix.Translate(centerX-tx/2, centerY-ty/2)
2013-06-21 16:16:52 +02:00
g.DrawTexture(game.ebitenTexture.ID,
2013-06-21 04:02:38 +02:00
geometryMatrix,
matrix.IdentityColor())
}