clock: Refactoring: Use clock.FPS everywhere

This commit is contained in:
Hajime Hoshi 2017-07-14 03:19:50 +09:00
parent 8dda875c30
commit 59110ba5ec
4 changed files with 14 additions and 16 deletions

View File

@ -42,8 +42,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/loop"
)
const FPS = 60
type players struct {
players map[*Player]struct{}
sync.RWMutex
@ -265,7 +263,7 @@ func (c *Context) loop() {
c.m.Unlock()
c.frames++
clock.Inc()
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / FPS
bytesPerFrame := c.sampleRate * bytesPerSample * channelNum / clock.FPS
l := (c.frames * int64(bytesPerFrame)) - c.writtenBytes
l &= mask
c.writtenBytes += l

View File

@ -18,6 +18,8 @@ import (
"github.com/hajimehoshi/ebiten/internal/sync"
)
const FPS = 60
var (
m sync.Mutex
valid bool

View File

@ -29,7 +29,6 @@ func CurrentFPS() float64 {
type runContext struct {
running bool
fps int
currentFPS float64
runningSlowly bool
frames int64
@ -103,13 +102,11 @@ func (g *loopGraphicsContext) Invalidate() {
g.graphicsContext.Invalidate()
}
func Run(g GraphicsContext, width, height int, scale float64, title string, fps int) (err error) {
func Run(g GraphicsContext, width, height int, scale float64, title string) (err error) {
if currentRunContext != nil {
return errors.New("loop: The game is already running")
}
currentRunContext = &runContext{
fps: fps,
}
currentRunContext = &runContext{}
currentRunContext.startRunning()
defer currentRunContext.endRunning()
@ -146,14 +143,14 @@ func (c *runContext) updateCount(now int64) int {
}
c.lastClockFrame = f
} else {
count = int(t * int64(c.fps) / int64(time.Second))
count = int(t * int64(clock.FPS) / int64(time.Second))
}
// Stabilize FPS.
if count == 0 && (int64(time.Second)/int64(c.fps)/2) < t {
if count == 0 && (int64(time.Second)/int64(clock.FPS)/2) < t {
count = 1
}
if count == 2 && (int64(time.Second)/int64(c.fps)*3/2) > t {
if count == 2 && (int64(time.Second)/int64(clock.FPS)*3/2) > t {
count = 1
}
@ -164,7 +161,7 @@ func (c *runContext) updateCount(now int64) int {
if sync {
c.lastUpdated = now
} else {
c.lastUpdated += int64(count) * int64(time.Second) / int64(c.fps)
c.lastUpdated += int64(count) * int64(time.Second) / int64(clock.FPS)
}
c.frames += int64(count)

9
run.go
View File

@ -17,12 +17,13 @@ package ebiten
import (
"sync/atomic"
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/loop"
"github.com/hajimehoshi/ebiten/internal/ui"
)
// FPS represents how many times game updating happens in a second.
const FPS = 60
// FPS represents how many times game updating happens in a second (60).
const FPS = clock.FPS
// CurrentFPS returns the current number of frames per second of rendering.
//
@ -81,7 +82,7 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
go func() {
g := newGraphicsContext(f)
theGraphicsContext.Store(g)
if err := loop.Run(g, width, height, scale, title, FPS); err != nil {
if err := loop.Run(g, width, height, scale, title); err != nil {
ch <- err
}
close(ch)
@ -105,7 +106,7 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
go func() {
g := newGraphicsContext(f)
theGraphicsContext.Store(g)
if err := loop.Run(g, width, height, scale, title, FPS); err != nil {
if err := loop.Run(g, width, height, scale, title); err != nil {
ch <- err
}
close(ch)