ebiten/example/game/monochrome/monochrome.go

130 lines
2.7 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"
)
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-23 16:43:44 +02:00
type Monochrome struct {
2013-11-29 20:24:52 +01:00
ebitenTextureId graphics.TextureId
ch chan bool
colorMatrix matrix.Color
geometryMatrix matrix.Geometry
screenSizeUpdatedCh chan ebiten.ScreenSizeUpdatedEvent
screenWidth int
screenHeight int
2013-06-23 16:43:44 +02:00
}
func New() *Monochrome {
2013-06-23 17:43:19 +02:00
return &Monochrome{
2013-11-29 20:24:52 +01:00
ch: make(chan bool),
colorMatrix: matrix.IdentityColor(),
geometryMatrix: matrix.IdentityGeometry(),
screenSizeUpdatedCh: make(chan ebiten.ScreenSizeUpdatedEvent),
2013-06-23 17:43:19 +02:00
}
2013-06-23 16:43:44 +02:00
}
2013-12-01 13:23:03 +01:00
func (game *Monochrome) OnScreenSizeUpdated(e ebiten.ScreenSizeUpdatedEvent) {
go func() {
e := e
game.screenSizeUpdatedCh <- e
}()
2013-11-29 20:24:52 +01:00
}
2013-10-20 08:51:26 +02:00
func (game *Monochrome) InitTextures(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)
}
2013-11-28 18:38:18 +01:00
if game.ebitenTextureId, err = tf.CreateTextureFromImage(img); err != nil {
2013-06-23 16:43:44 +02:00
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() {
2013-11-23 11:51:24 +01:00
const fps = 60
2013-06-23 17:43:19 +02:00
colorI := matrix.IdentityColor()
colorMonochrome := matrix.Monochrome()
for {
2013-11-23 11:51:24 +01:00
for i := 0; i < fps; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
2013-11-23 11:51:24 +01:00
rate := float64(i) / float64(fps)
2013-06-23 17:43:19 +02:00
game.colorMatrix = mean(colorI, colorMonochrome, rate)
game.ch <- true
}
2013-11-23 11:51:24 +01:00
for i := 0; i < fps; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
game.colorMatrix = colorMonochrome
game.ch <- true
}
2013-11-23 11:51:24 +01:00
for i := 0; i < fps; i++ {
2013-06-23 17:43:19 +02:00
<-game.ch
2013-11-23 11:51:24 +01:00
rate := float64(i) / float64(fps)
2013-06-23 17:43:19 +02:00
game.colorMatrix = mean(colorMonochrome, colorI, rate)
game.ch <- true
}
2013-11-23 11:51:24 +01:00
for i := 0; i < 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-11-29 20:24:52 +01:00
func (game *Monochrome) Update() {
events:
for {
select {
case e := <-game.screenSizeUpdatedCh:
game.screenWidth, game.screenHeight = e.Width, e.Height
default:
break events
}
}
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()
2013-11-29 20:24:52 +01:00
tx := game.screenWidth/2 - ebitenTextureWidth/2
ty := game.screenHeight/2 - ebitenTextureHeight/2
2013-10-09 16:34:11 +02:00
game.geometryMatrix.Translate(float64(tx), float64(ty))
2013-06-23 16:43:44 +02:00
}
func (game *Monochrome) Draw(g graphics.Canvas) {
2013-10-11 20:20:13 +02:00
g.Fill(128, 128, 255)
2013-06-23 16:43:44 +02:00
2013-10-21 15:18:10 +02:00
g.DrawTexture(game.ebitenTextureId,
2013-10-09 16:34:11 +02:00
game.geometryMatrix, game.colorMatrix)
2013-06-23 16:43:44 +02:00
}