mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
ui: Add runContext struct
This commit is contained in:
parent
75ef9a6d47
commit
30d4437e97
146
run.go
146
run.go
@ -20,49 +20,29 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
var runContext = &struct {
|
type runContext struct {
|
||||||
running bool
|
running bool
|
||||||
fps float64
|
fps float64
|
||||||
newScreenWidth int
|
newScreenWidth int
|
||||||
newScreenHeight int
|
newScreenHeight int
|
||||||
newScreenScale int
|
newScreenScale int
|
||||||
isRunningSlowly bool
|
isRunningSlowly bool
|
||||||
}{}
|
|
||||||
|
|
||||||
// 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 {
|
|
||||||
return runContext.fps
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS of rendering.
|
var currentRunContext = &runContext{}
|
||||||
// The game screen is not updated when IsRunningSlowly is true.
|
|
||||||
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
|
func (c *runContext) CurrentFPS() float64 {
|
||||||
func IsRunningSlowly() bool {
|
return c.fps
|
||||||
return runContext.isRunningSlowly
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs the game.
|
func (c *runContext) IsRunningSlowly() bool {
|
||||||
// f is a function which is called at every frame.
|
return c.isRunningSlowly
|
||||||
// The argument (*Image) is the render target that represents the screen.
|
}
|
||||||
//
|
|
||||||
// This function must be called from the main thread.
|
func (c *runContext) Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||||
// Note that ebiten bounds the main goroutine to the main OS thread by runtime.LockOSThread.
|
c.running = true
|
||||||
//
|
|
||||||
// The given function f is guaranteed to be called 60 times a second
|
|
||||||
// even if a rendering frame is skipped.
|
|
||||||
// f is not called when the screen is not shown.
|
|
||||||
func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|
||||||
runContext.running = true
|
|
||||||
defer func() {
|
defer func() {
|
||||||
runContext.running = false
|
c.running = false
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
|
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
|
||||||
@ -82,26 +62,26 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
beforeForFPS := now
|
beforeForFPS := now
|
||||||
for {
|
for {
|
||||||
// TODO: setSize should be called after swapping buffers?
|
// TODO: setSize should be called after swapping buffers?
|
||||||
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight || 0 < runContext.newScreenScale {
|
if 0 < c.newScreenWidth || 0 < c.newScreenHeight || 0 < c.newScreenScale {
|
||||||
changed := false
|
changed := false
|
||||||
if 0 < runContext.newScreenWidth || 0 < runContext.newScreenHeight {
|
if 0 < c.newScreenWidth || 0 < c.newScreenHeight {
|
||||||
c := ui.CurrentUI().SetScreenSize(runContext.newScreenWidth, runContext.newScreenHeight)
|
c := ui.CurrentUI().SetScreenSize(c.newScreenWidth, c.newScreenHeight)
|
||||||
changed = changed || c
|
changed = changed || c
|
||||||
}
|
}
|
||||||
if 0 < runContext.newScreenScale {
|
if 0 < c.newScreenScale {
|
||||||
c := ui.CurrentUI().SetScreenScale(runContext.newScreenScale)
|
c := ui.CurrentUI().SetScreenScale(c.newScreenScale)
|
||||||
changed = changed || c
|
changed = changed || c
|
||||||
}
|
}
|
||||||
if changed {
|
if changed {
|
||||||
w, h := runContext.newScreenWidth, runContext.newScreenHeight
|
w, h := c.newScreenWidth, c.newScreenHeight
|
||||||
if err := graphicsContext.setSize(w, h, ui.CurrentUI().ActualScreenScale()); err != nil {
|
if err := graphicsContext.setSize(w, h, ui.CurrentUI().ActualScreenScale()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
runContext.newScreenWidth = 0
|
c.newScreenWidth = 0
|
||||||
runContext.newScreenHeight = 0
|
c.newScreenHeight = 0
|
||||||
runContext.newScreenScale = 0
|
c.newScreenScale = 0
|
||||||
|
|
||||||
if err := ui.CurrentUI().DoEvents(); err != nil {
|
if err := ui.CurrentUI().DoEvents(); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -111,13 +91,13 @@ 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
|
c.isRunningSlowly = false
|
||||||
if int64(5*time.Second/FPS) < now-beforeForUpdate {
|
if int64(5*time.Second/FPS) < now-beforeForUpdate {
|
||||||
beforeForUpdate = now
|
beforeForUpdate = now
|
||||||
} else {
|
} else {
|
||||||
c := float64(now-beforeForUpdate) * FPS / float64(time.Second)
|
t := float64(now-beforeForUpdate) * FPS / float64(time.Second)
|
||||||
runContext.isRunningSlowly = c >= 2.5
|
c.isRunningSlowly = t >= 2.5
|
||||||
for i := 0; i < int(c); i++ {
|
for i := 0; i < int(t); i++ {
|
||||||
if err := ui.CurrentUI().DoEvents(); err != nil {
|
if err := ui.CurrentUI().DoEvents(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -128,7 +108,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
beforeForUpdate += int64(c) * int64(time.Second/FPS)
|
beforeForUpdate += int64(t) * int64(time.Second/FPS)
|
||||||
ui.CurrentUI().SwapBuffers()
|
ui.CurrentUI().SwapBuffers()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -136,35 +116,77 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
now = ui.Now()
|
now = ui.Now()
|
||||||
frames++
|
frames++
|
||||||
if time.Second <= time.Duration(now-beforeForFPS) {
|
if time.Second <= time.Duration(now-beforeForFPS) {
|
||||||
runContext.fps = float64(frames) * float64(time.Second) / float64(now-beforeForFPS)
|
c.fps = float64(frames) * float64(time.Second) / float64(now-beforeForFPS)
|
||||||
beforeForFPS = now
|
beforeForFPS = now
|
||||||
frames = 0
|
frames = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *runContext) SetScreenSize(width, height int) {
|
||||||
|
if !c.running {
|
||||||
|
panic("ebiten: SetScreenSize must be called during Run")
|
||||||
|
}
|
||||||
|
if width <= 0 || height <= 0 {
|
||||||
|
panic("ebiten: width and height must be positive")
|
||||||
|
}
|
||||||
|
c.newScreenWidth = width
|
||||||
|
c.newScreenHeight = height
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *runContext) SetScreenScale(scale int) {
|
||||||
|
if !c.running {
|
||||||
|
panic("ebiten: SetScreenScale must be called during Run")
|
||||||
|
}
|
||||||
|
if scale <= 0 {
|
||||||
|
panic("ebiten: scale must be positive")
|
||||||
|
}
|
||||||
|
c.newScreenScale = scale
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 {
|
||||||
|
return currentRunContext.CurrentFPS()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
||||||
|
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
|
||||||
|
func IsRunningSlowly() bool {
|
||||||
|
return currentRunContext.IsRunningSlowly()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Run runs the game.
|
||||||
|
// f is a function which is called at every frame.
|
||||||
|
// The argument (*Image) is the render target that represents the screen.
|
||||||
|
//
|
||||||
|
// This function must be called from the main thread.
|
||||||
|
// Note that ebiten bounds the main goroutine to the main OS thread by runtime.LockOSThread.
|
||||||
|
//
|
||||||
|
// The given function f is guaranteed to be called 60 times a second
|
||||||
|
// even if a rendering frame is skipped.
|
||||||
|
// f is not called when the screen is not shown.
|
||||||
|
func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||||
|
return currentRunContext.Run(f, width, height, scale, title)
|
||||||
|
}
|
||||||
|
|
||||||
// SetScreenSize changes the (logical) size of the screen.
|
// SetScreenSize changes the (logical) size of the screen.
|
||||||
// This doesn't affect the current scale of the screen.
|
// This doesn't affect the current scale of the screen.
|
||||||
func SetScreenSize(width, height int) {
|
func SetScreenSize(width, height int) {
|
||||||
if !runContext.running {
|
currentRunContext.SetScreenSize(width, height)
|
||||||
panic("SetScreenSize must be called during Run")
|
|
||||||
}
|
|
||||||
if width <= 0 || height <= 0 {
|
|
||||||
panic("width and height must be positive")
|
|
||||||
}
|
|
||||||
runContext.newScreenWidth = width
|
|
||||||
runContext.newScreenHeight = height
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetScreenSize changes the scale of the screen.
|
// SetScreenSize changes the scale of the screen.
|
||||||
func SetScreenScale(scale int) {
|
func SetScreenScale(scale int) {
|
||||||
if !runContext.running {
|
currentRunContext.SetScreenScale(scale)
|
||||||
panic("SetScreenScale must be called during Run")
|
|
||||||
}
|
|
||||||
if scale <= 0 {
|
|
||||||
panic("scale must be positive")
|
|
||||||
}
|
|
||||||
runContext.newScreenScale = scale
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ScreenScale returns the current screen scale.
|
// ScreenScale returns the current screen scale.
|
||||||
|
Loading…
Reference in New Issue
Block a user