ebiten/example/game/monochrome/monochrome.go

103 lines
2.2 KiB
Go
Raw Normal View History

2013-06-23 16:43:44 +02:00
package monochrome
import (
2013-10-14 04:34:58 +02:00
"github.com/hajimehoshi/go-ebiten"
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2013-06-23 16:43:44 +02:00
"image"
_ "image/png"
"os"
)
type Monochrome struct {
2013-10-09 16:34:11 +02:00
ebitenTexture graphics.Texture
ch chan bool
colorMatrix matrix.Color
geometryMatrix matrix.Geometry
2013-06-23 16:43:44 +02:00
}
func New() *Monochrome {
2013-06-23 17:43:19 +02:00
return &Monochrome{
2013-10-09 16:34:11 +02:00
ch: make(chan bool),
colorMatrix: matrix.IdentityColor(),
geometryMatrix: matrix.IdentityGeometry(),
2013-06-23 17:43:19 +02:00
}
2013-06-23 16:43:44 +02:00
}
func (game *Monochrome) Init(tf graphics.TextureFactory) {
2013-07-02 18:27:04 +02:00
file, err := os.Open("images/ebiten.png")
2013-06-23 16:43:44 +02:00
if err != nil {
panic(err)
}
defer file.Close()
img, _, err := image.Decode(file)
if err != nil {
panic(err)
}
if game.ebitenTexture, err = tf.NewTextureFromImage(img); err != nil {
panic(err)
}
2013-06-23 17:43:19 +02:00
go game.update()
}
func mean(a, b matrix.Color, k float64) matrix.Color {
dim := a.Dim()
result := matrix.Color{}
2013-06-23 17:43:41 +02:00
for i := 0; i < dim-1; i++ {
2013-06-23 17:43:19 +02:00
for j := 0; j < dim; j++ {
result.Elements[i][j] =
2013-06-23 17:43:41 +02:00
a.Elements[i][j]*(1-k) +
b.Elements[i][j]*k
2013-06-23 17:43:19 +02:00
}
}
return result
}
func (game *Monochrome) update() {
colorI := matrix.IdentityColor()
colorMonochrome := matrix.Monochrome()
for {
2013-10-06 11:44:15 +02:00
for i := 0; i < ebiten.FPS; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
2013-10-06 11:44:15 +02:00
rate := float64(i) / float64(ebiten.FPS)
2013-06-23 17:43:19 +02:00
game.colorMatrix = mean(colorI, colorMonochrome, rate)
game.ch <- true
}
2013-10-06 11:44:15 +02:00
for i := 0; i < ebiten.FPS; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
game.colorMatrix = colorMonochrome
game.ch <- true
}
2013-10-06 11:44:15 +02:00
for i := 0; i < ebiten.FPS; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
2013-10-06 11:44:15 +02:00
rate := float64(i) / float64(ebiten.FPS)
2013-06-23 17:43:19 +02:00
game.colorMatrix = mean(colorMonochrome, colorI, rate)
game.ch <- true
}
2013-10-06 11:44:15 +02:00
for i := 0; i < ebiten.FPS; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
game.colorMatrix = colorI
game.ch <- true
}
}
2013-06-23 16:43:44 +02:00
}
2013-07-05 14:02:17 +02:00
func (game *Monochrome) Update(context ebiten.GameContext) {
2013-06-23 17:43:19 +02:00
game.ch <- true
<-game.ch
2013-10-09 16:34:11 +02:00
game.geometryMatrix = matrix.IdentityGeometry()
tx := context.ScreenWidth()/2 - game.ebitenTexture.Width()/2
ty := context.ScreenHeight()/2 - game.ebitenTexture.Height()/2
game.geometryMatrix.Translate(float64(tx), float64(ty))
2013-06-23 16:43:44 +02:00
}
2013-07-04 16:57:53 +02:00
func (game *Monochrome) Draw(g graphics.Context) {
2013-10-11 20:20:13 +02:00
g.Fill(128, 128, 255)
2013-06-23 16:43:44 +02:00
2013-07-12 18:36:01 +02:00
g.DrawTexture(game.ebitenTexture.ID(),
2013-10-09 16:34:11 +02:00
game.geometryMatrix, game.colorMatrix)
2013-06-23 16:43:44 +02:00
}