Add FPS const

This commit is contained in:
Hajime Hoshi 2016-03-13 04:48:13 +09:00
parent 305016f636
commit b1f856a0e0
10 changed files with 28 additions and 17 deletions

View File

@ -50,8 +50,7 @@ func init() {
} }
func (r *recorder) delay() int { func (r *recorder) delay() int {
// Assume that the FPS is 60. delay := 100 * r.skips / ebiten.FPS
delay := 100 * r.skips / 60
if delay < 2 { if delay < 2 {
return 2 return 2
} }

View File

@ -36,7 +36,7 @@ var (
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
count++ count++
count %= 600 count %= ebiten.FPS * 10
diff := float64(count) * 0.2 diff := float64(count) * 0.2
switch { switch {
case 480 < count: case 480 < count:

View File

@ -69,7 +69,7 @@ func (s *GamepadScene) Update(state *GameState) error {
if state.Input.gamepadConfig.Scan(0, b) { if state.Input.gamepadConfig.Scan(0, b) {
s.currentIndex++ s.currentIndex++
if s.currentIndex == len(gamepadStdButtons) { if s.currentIndex == len(gamepadStdButtons) {
s.countAfterSetting = 60 s.countAfterSetting = ebiten.FPS
} }
} }
return nil return nil

View File

@ -260,7 +260,7 @@ func (s *GameScene) Update(state *GameState) error {
s.currentFrame++ s.currentFrame++
const maxLandingCount = 60 const maxLandingCount = ebiten.FPS
if s.currentPiece == nil { if s.currentPiece == nil {
s.initCurrentPiece(s.choosePiece()) s.initCurrentPiece(s.choosePiece())
} }
@ -303,8 +303,9 @@ func (s *GameScene) Update(state *GameState) error {
y := s.currentPieceY y := s.currentPieceY
angle := s.currentPieceAngle angle := s.currentPieceAngle
s.currentPieceYCarry += 2*s.level() + 1 s.currentPieceYCarry += 2*s.level() + 1
for 60 <= s.currentPieceYCarry { const maxCarry = 60
s.currentPieceYCarry -= 60 for maxCarry <= s.currentPieceYCarry {
s.currentPieceYCarry -= maxCarry
s.currentPieceY = s.field.DropPiece(piece, s.currentPieceX, s.currentPieceY, angle) s.currentPieceY = s.field.DropPiece(piece, s.currentPieceX, s.currentPieceY, angle)
moved = y != s.currentPieceY moved = y != s.currentPieceY
} }

View File

@ -47,7 +47,7 @@ func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(mx), float64(my)) op.GeoM.Translate(float64(mx), float64(my))
op.ColorM.Scale(1.0, 0.50, 0.125, 1.0) op.ColorM.Scale(1.0, 0.50, 0.125, 1.0)
theta := 2.0 * math.Pi * float64(count%60) / 60.0 theta := 2.0 * math.Pi * float64(count%60) / ebiten.FPS
op.ColorM.RotateHue(theta) op.ColorM.RotateHue(theta)
if err := canvasImage.DrawImage(brushImage, op); err != nil { if err := canvasImage.DrawImage(brushImage, op); err != nil {
return err return err

View File

@ -94,7 +94,7 @@ func toBytes(l, r []int16) []byte {
} }
func addNote() error { func addNote() error {
size := sampleRate / 60 size := sampleRate / ebiten.FPS
notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2} notes := []float64{freqC, freqD, freqE, freqF, freqG, freqA * 2, freqB * 2}
defer func() { defer func() {

View File

@ -19,6 +19,8 @@ import (
"io" "io"
"sync" "sync"
"time" "time"
"github.com/hajimehoshi/ebiten"
) )
type mixedPlayersStream struct { type mixedPlayersStream struct {
@ -43,8 +45,7 @@ func (s *mixedPlayersStream) Read(b []byte) (int, error) {
s.context.Lock() s.context.Lock()
defer s.context.Unlock() defer s.context.Unlock()
// TODO: 60 (FPS) is a magic number bytesPerFrame := s.context.sampleRate * bytesPerSample * channelNum / ebiten.FPS
bytesPerFrame := s.context.sampleRate * bytesPerSample * channelNum / 60
x := s.context.frames*bytesPerFrame + len(b) x := s.context.frames*bytesPerFrame + len(b)
if x <= s.writtenBytes { if x <= s.writtenBytes {
return 0, nil return 0, nil

View File

@ -21,6 +21,7 @@ import (
"runtime" "runtime"
"github.com/gopherjs/gopherjs/js" "github.com/gopherjs/gopherjs/js"
"github.com/hajimehoshi/ebiten"
) )
type player struct { type player struct {
@ -75,7 +76,7 @@ func max64(a, b int64) int64 {
} }
func (p *player) proceed() error { func (p *player) proceed() error {
bufferSize := p.sampleRate * bytesPerSample * channelNum / 60 bufferSize := p.sampleRate * bytesPerSample * channelNum / ebiten.FPS
c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate)) c := int64(p.context.Get("currentTime").Float() * float64(p.sampleRate))
if p.positionInSamples < c { if p.positionInSamples < c {
p.positionInSamples = c p.positionInSamples = c

View File

@ -154,6 +154,7 @@ func (u *userInterface) doEvents() error {
return err return err
} }
for u.window.GetAttrib(glfw.Focused) == 0 { for u.window.GetAttrib(glfw.Focused) == 0 {
// Wait for an arbitrary period to avoid busy loop.
time.Sleep(time.Second / 60) time.Sleep(time.Second / 60)
if err := u.pollEvents(); err != nil { if err := u.pollEvents(); err != nil {
return err return err

18
run.go
View File

@ -29,12 +29,20 @@ var runContext = &struct {
isRunningSlowly bool isRunningSlowly bool
}{} }{}
// CurrentFPS returns the current number of frames per second. // FPS represents how many times game updating happens in a second.
const FPS = 60
// CurrentFPS returns the current number of frames per second of rendering.
//
// This value represents how many times rendering happens in 1/60 second and
// NOT how many times logical game updating (a passed function to Run) happens.
// Note that logical game updating is assured to happen 60 times in a second
// as long as the screen is active.
func CurrentFPS() float64 { func CurrentFPS() float64 {
return runContext.fps return runContext.fps
} }
// IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS. // IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS of rendering.
// The game screen is not updated when IsRunningSlowly is true. // The game screen is not updated when IsRunningSlowly is true.
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true. // It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
func IsRunningSlowly() bool { func IsRunningSlowly() bool {
@ -103,10 +111,10 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
now := ui.Now() now := ui.Now()
// If beforeForUpdate is too old, we assume that screen is not shown. // If beforeForUpdate is too old, we assume that screen is not shown.
runContext.isRunningSlowly = false runContext.isRunningSlowly = false
if int64(5*time.Second/60) < now-beforeForUpdate { if int64(5*time.Second/FPS) < now-beforeForUpdate {
beforeForUpdate = now beforeForUpdate = now
} else { } else {
c := float64(now-beforeForUpdate) * 60 / float64(time.Second) c := float64(now-beforeForUpdate) * FPS / float64(time.Second)
runContext.isRunningSlowly = c >= 2.5 runContext.isRunningSlowly = c >= 2.5
for i := 0; i < int(c); i++ { for i := 0; i < int(c); i++ {
if err := ui.DoEvents(); err != nil { if err := ui.DoEvents(); err != nil {
@ -119,7 +127,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
return err return err
} }
} }
beforeForUpdate += int64(c) * int64(time.Second/60) beforeForUpdate += int64(c) * int64(time.Second/FPS)
ui.SwapBuffers() ui.SwapBuffers()
} }