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

View File

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

View File

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

9
run.go
View File

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