ebiten/example/game/testpattern/testpattern.go

79 lines
1.8 KiB
Go
Raw Normal View History

2013-10-18 19:58:00 +02:00
package testpattern
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
"image"
_ "image/png"
"math"
"os"
)
type TestPattern struct {
2013-10-21 15:18:10 +02:00
textureId graphics.TextureId
2013-10-18 19:58:00 +02:00
textureWidth int
textureHeight int
geos []matrix.Geometry
}
func New() *TestPattern {
return &TestPattern{}
}
2013-10-20 08:51:26 +02:00
func (game *TestPattern) InitTextures(tf graphics.TextureFactory) {
2013-10-18 19:58:00 +02:00
file, err := os.Open("images/test_pattern.png")
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.textureId, err = tf.CreateTextureFromImage(img); err != nil {
2013-10-18 19:58:00 +02:00
panic(err)
}
size := img.Bounds().Size()
game.textureWidth = size.X
game.textureHeight = size.Y
}
2013-11-29 20:24:52 +01:00
func (game *TestPattern) Update() {
2013-10-18 19:58:00 +02:00
geo := matrix.IdentityGeometry()
geo.Translate(13, 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Translate(float64(13+game.textureWidth), 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Translate(float64(13+game.textureWidth*2), 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Translate(float64(13+game.textureWidth*3), 13)
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Scale(2, 2)
geo.Translate(13, float64(13+game.textureHeight))
game.geos = append(game.geos, geo)
geo = matrix.IdentityGeometry()
geo.Rotate(math.Pi)
geo.Scale(2, 2)
geo.Translate(float64(game.textureWidth*2),
float64(game.textureHeight*2))
geo.Translate(float64(13+game.textureWidth*2),
float64(13+game.textureHeight))
game.geos = append(game.geos, geo)
}
func (game *TestPattern) Draw(g graphics.Canvas) {
2013-10-18 19:58:00 +02:00
for _, geo := range game.geos {
g.DrawTexture(game.textureId, geo, matrix.IdentityColor())
}
}