ebiten/example/game/rotating/rotating.go

79 lines
2.5 KiB
Go
Raw Normal View History

2013-07-01 15:25:41 +02:00
// Copyright 2013 Hajime Hoshi
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
2013-06-22 16:16:01 +02:00
package rotating
2013-06-21 04:02:38 +02:00
import (
2013-07-04 16:57:53 +02:00
"github.com/hajimehoshi/go.ebiten"
2013-06-21 04:02:38 +02:00
"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-10-09 16:34:11 +02:00
ebitenTexture graphics.Texture
x int
geometryMatrix matrix.Geometry
2013-06-21 04:02:38 +02:00
}
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) Init(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-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-07-05 14:02:17 +02:00
func (game *Rotating) Update(context ebiten.GameContext) {
2013-06-21 04:02:38 +02:00
game.x++
2013-10-09 16:34:11 +02:00
game.geometryMatrix = matrix.IdentityGeometry()
tx, ty := float64(game.ebitenTexture.Width()), float64(game.ebitenTexture.Height())
game.geometryMatrix.Translate(-tx/2, -ty/2)
game.geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / float64(ebiten.FPS*10))
game.geometryMatrix.Translate(tx/2, ty/2)
centerX := float64(context.ScreenWidth()) / 2
centerY := float64(context.ScreenHeight()) / 2
game.geometryMatrix.Translate(centerX-tx/2, centerY-ty/2)
2013-06-21 04:02:38 +02:00
}
2013-07-04 16:57:53 +02:00
func (game *Rotating) Draw(g graphics.Context) {
2013-06-21 04:02:38 +02:00
g.Fill(&color.RGBA{R: 128, G: 128, B: 255, A: 255})
2013-07-12 18:36:01 +02:00
g.DrawTexture(game.ebitenTexture.ID(),
2013-10-09 16:34:11 +02:00
game.geometryMatrix,
2013-06-21 04:02:38 +02:00
matrix.IdentityColor())
}