loop: Remove IsRunning

This commit is contained in:
Hajime Hoshi 2016-09-03 21:14:06 +09:00
parent cd885ce7c5
commit 071b65f173
3 changed files with 29 additions and 19 deletions

View File

@ -27,14 +27,6 @@ func CurrentFPS() float64 {
return currentRunContext.getCurrentFPS()
}
func IsRunning() bool {
// TODO: Need lock?
if currentRunContext == nil {
return false
}
return currentRunContext.isRunning()
}
type runContext struct {
running bool
fps int

View File

@ -22,6 +22,7 @@ package ui
import (
"errors"
"runtime"
"sync"
"time"
"github.com/go-gl/glfw/v3.2/glfw"
@ -34,7 +35,9 @@ type userInterface struct {
height int
scale float64
funcs chan func()
running bool
sizeChanged bool
m sync.Mutex
}
var currentUI *userInterface
@ -74,6 +77,10 @@ func initialize() error {
func RunMainThreadLoop(ch <-chan error) error {
// TODO: Check this is done on the main thread.
currentUI.setRunning(true)
defer func() {
currentUI.setRunning(false)
}()
for {
select {
case f := <-currentUI.funcs:
@ -85,6 +92,18 @@ func RunMainThreadLoop(ch <-chan error) error {
}
}
func (u *userInterface) isRunning() bool {
u.m.Lock()
defer u.m.Unlock()
return u.running
}
func (u *userInterface) setRunning(running bool) {
u.m.Lock()
defer u.m.Unlock()
u.running = running
}
func (u *userInterface) runOnMainThread(f func() error) error {
if u.funcs == nil {
// already closed
@ -102,6 +121,9 @@ func (u *userInterface) runOnMainThread(f func() error) error {
func SetScreenSize(width, height int) (bool, error) {
u := currentUI
if !u.isRunning() {
return false, errors.New("ui: Run is not called yet")
}
r := false
if err := u.runOnMainThread(func() error {
var err error
@ -118,6 +140,9 @@ func SetScreenSize(width, height int) (bool, error) {
func SetScreenScale(scale float64) (bool, error) {
u := currentUI
if !u.isRunning() {
return false, errors.New("ui: Run is not called yet")
}
r := false
if err := u.runOnMainThread(func() error {
var err error
@ -134,6 +159,9 @@ func SetScreenScale(scale float64) (bool, error) {
func ScreenScale() float64 {
u := currentUI
if !u.isRunning() {
return 0
}
s := 0.0
_ = u.runOnMainThread(func() error {
s = u.scale

12
run.go
View File

@ -15,7 +15,6 @@
package ebiten
import (
"math"
"sync/atomic"
"github.com/hajimehoshi/ebiten/internal/loop"
@ -116,9 +115,6 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
//
// This function is concurrent-safe.
func SetScreenSize(width, height int) {
if !loop.IsRunning() {
panic("ebiten: Run is not called yet")
}
if width <= 0 || height <= 0 {
panic("ebiten: width and height must be positive")
}
@ -131,9 +127,6 @@ func SetScreenSize(width, height int) {
//
// This function is concurrent-safe.
func SetScreenScale(scale float64) {
if !loop.IsRunning() {
panic("ebiten: Run is not called yet")
}
if scale <= 0 {
panic("ebiten: scale must be positive")
}
@ -144,13 +137,10 @@ func SetScreenScale(scale float64) {
// ScreenScale returns the current screen scale.
//
// If Run is not called, this returns NaN.
// If Run is not called, this returns 0.
//
// This function is concurrent-safe.
func ScreenScale() float64 {
if !loop.IsRunning() {
return math.NaN()
}
return ui.ScreenScale()
}