mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Stop the game when the window is not active (#69)
This commit is contained in:
parent
038613aaf9
commit
a1214ff735
@ -41,6 +41,10 @@ pre {
|
||||
<li>Works on
|
||||
<ul>
|
||||
<li>Web browsers (powered by <a href="http://gopherjs.org/">GopherJS</a>)
|
||||
<ul>
|
||||
<li>Supported browsers: Chrome, Firefox, Safari on desktops</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>Mac OS X</li>
|
||||
<li>Linux (maybe)</li>
|
||||
<li>Windows (possibly)</li>
|
||||
@ -111,6 +115,7 @@ pre {
|
||||
<li>A lot of keyboard keys have been added. KeyMax and MouseButtonMax were removed.</li>
|
||||
</ul>
|
||||
</li>
|
||||
<li>The game is stopped when the window is not active.</li>
|
||||
</ul>
|
||||
</li>
|
||||
</ul>
|
||||
|
@ -21,6 +21,7 @@ import (
|
||||
glfw "github.com/go-gl/glfw3"
|
||||
"github.com/hajimehoshi/ebiten/internal/opengl"
|
||||
"runtime"
|
||||
"time"
|
||||
)
|
||||
|
||||
var current *ui
|
||||
@ -136,11 +137,19 @@ func Start(width, height, scale int, title string) (actualScale int, err error)
|
||||
return actualScale, nil
|
||||
}
|
||||
|
||||
func (u *ui) doEvents() {
|
||||
func (u *ui) pollEvents() {
|
||||
glfw.PollEvents()
|
||||
u.input.update(u.window, u.scale)
|
||||
}
|
||||
|
||||
func (u *ui) doEvents() {
|
||||
u.pollEvents()
|
||||
for current.window.GetAttribute(glfw.Focused) == 0 {
|
||||
time.Sleep(time.Second / 60)
|
||||
u.pollEvents()
|
||||
}
|
||||
}
|
||||
|
||||
func (u *ui) terminate() {
|
||||
glfw.Terminate()
|
||||
}
|
||||
|
@ -26,12 +26,24 @@ import (
|
||||
var canvas js.Object
|
||||
var context *opengl.Context
|
||||
|
||||
var windowIsFocused = true
|
||||
|
||||
func init() {
|
||||
// TODO: Check IE
|
||||
window := js.Global.Get("window")
|
||||
window.Set("onfocus", func() {
|
||||
windowIsFocused = true
|
||||
})
|
||||
window.Set("onblur", func() {
|
||||
windowIsFocused = false
|
||||
})
|
||||
}
|
||||
|
||||
func Use(f func(*opengl.Context)) {
|
||||
f(context)
|
||||
}
|
||||
|
||||
func DoEvents() {
|
||||
// TODO: requestAnimationFrame is not called when the window is not activated.
|
||||
func vsync() {
|
||||
ch := make(chan struct{})
|
||||
js.Global.Get("window").Call("requestAnimationFrame", func() {
|
||||
close(ch)
|
||||
@ -39,6 +51,13 @@ func DoEvents() {
|
||||
<-ch
|
||||
}
|
||||
|
||||
func DoEvents() {
|
||||
vsync()
|
||||
for !windowIsFocused {
|
||||
vsync()
|
||||
}
|
||||
}
|
||||
|
||||
func Terminate() {
|
||||
// Do nothing.
|
||||
}
|
||||
|
@ -5,6 +5,7 @@
|
||||
* A simple SNES-like 2D game library in Go
|
||||
* Works on
|
||||
* Web browsers (powered by [GopherJS](http://gopherjs.org/))
|
||||
* Support browsers: Chrome, Firefox, Safari on desktops
|
||||
* Mac OS X
|
||||
* Linux (maybe)
|
||||
* Windows (possibly)
|
||||
|
19
run.go
19
run.go
@ -47,8 +47,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
}
|
||||
|
||||
frames := 0
|
||||
t0 := time.Now().UnixNano()
|
||||
tt0 := t0
|
||||
t := time.Now().UnixNano()
|
||||
for {
|
||||
ui.DoEvents()
|
||||
if ui.IsClosed() {
|
||||
@ -74,19 +73,13 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
// Wait if the frame is too fast.
|
||||
now := time.Now().UnixNano()
|
||||
d := time.Duration(now - t0)
|
||||
if d < time.Second/90 {
|
||||
time.Sleep(time.Second/60 - d)
|
||||
}
|
||||
t0 = now
|
||||
|
||||
// Calc the current FPS.
|
||||
now := time.Now().UnixNano()
|
||||
frames++
|
||||
if time.Second <= time.Duration(now-tt0) {
|
||||
fps = frames
|
||||
tt0 = now
|
||||
if time.Second <= time.Duration(now-t) {
|
||||
fps = int(int64(frames) * int64(time.Second) / (now - t))
|
||||
// TODO: How to show the current FPS?
|
||||
t = now
|
||||
frames = 0
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user