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