Add Game.Fps

This commit is contained in:
Hajime Hoshi 2013-06-23 22:38:41 +09:00
parent 92ac4b97ea
commit 97550279ee
4 changed files with 15 additions and 2 deletions

View File

@ -14,6 +14,7 @@ type TapInfo struct {
type Game interface { type Game interface {
ScreenWidth() int ScreenWidth() int
ScreenHeight() int ScreenHeight() int
Fps() int
Init(tf graphics.TextureFactory) Init(tf graphics.TextureFactory)
Update() Update()
Draw(g graphics.GraphicsContext, offscreen graphics.Texture) Draw(g graphics.GraphicsContext, offscreen graphics.Texture)
@ -34,7 +35,7 @@ func OpenGLRun(game Game, ui UI, screenScale int) {
}) })
go func() { go func() {
const frameTime = time.Second / 60 frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
tick := time.Tick(frameTime) tick := time.Tick(frameTime)
for { for {
<-tick <-tick

View File

@ -24,6 +24,10 @@ func (game *Rects) ScreenHeight() int {
return 240 return 240
} }
func (game *Rects) Fps() int {
return 60
}
func (game *Rects) Init(tf graphics.TextureFactory) { func (game *Rects) Init(tf graphics.TextureFactory) {
game.rectsTexture = tf.NewTexture(game.ScreenWidth(), game.ScreenHeight()) game.rectsTexture = tf.NewTexture(game.ScreenWidth(), game.ScreenHeight())
} }

View File

@ -27,6 +27,10 @@ func (game *Rotating) ScreenHeight() int {
return 240 return 240
} }
func (game *Rotating) Fps() int {
return 60
}
func (game *Rotating) Init(tf graphics.TextureFactory) { func (game *Rotating) Init(tf graphics.TextureFactory) {
file, err := os.Open("ebiten.png") file, err := os.Open("ebiten.png")
if err != nil { if err != nil {
@ -53,7 +57,7 @@ func (game *Rotating) Draw(g graphics.GraphicsContext, offscreen graphics.Textur
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()
tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height) tx, ty := float64(game.ebitenTexture.Width), float64(game.ebitenTexture.Height)
geometryMatrix.Translate(-tx/2, -ty/2) geometryMatrix.Translate(-tx/2, -ty/2)
geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / (60 * 10)) geometryMatrix.Rotate(float64(game.x) * 2 * math.Pi / float64(game.Fps() * 10))
geometryMatrix.Translate(tx/2, ty/2) geometryMatrix.Translate(tx/2, ty/2)
centerX := float64(game.ScreenWidth()) / 2 centerX := float64(game.ScreenWidth()) / 2
centerY := float64(game.ScreenHeight()) / 2 centerY := float64(game.ScreenHeight()) / 2

View File

@ -75,6 +75,10 @@ func (game *Sprites) ScreenHeight() int {
return 240 return 240
} }
func (game *Sprites) Fps() int {
return 60
}
func (game *Sprites) Init(tf graphics.TextureFactory) { func (game *Sprites) Init(tf graphics.TextureFactory) {
file, err := os.Open("ebiten.png") file, err := os.Open("ebiten.png")
if err != nil { if err != nil {