loop: Prevent re-entering to Run

This commit is contained in:
Hajime Hoshi 2016-05-29 02:51:34 +09:00
parent a329f7939d
commit 857bc1ed51

View File

@ -15,6 +15,7 @@
package loop package loop
import ( import (
"errors"
"sync" "sync"
"time" "time"
@ -40,7 +41,7 @@ type runContext struct {
m sync.RWMutex m sync.RWMutex
} }
var currentRunContext runContext var currentRunContext *runContext
func (c *runContext) startRunning() { func (c *runContext) startRunning() {
c.m.Lock() c.m.Lock()
@ -100,6 +101,10 @@ type GraphicsContext interface {
} }
func Run(g GraphicsContext, width, height, scale int, title string, fps int) error { func Run(g GraphicsContext, width, height, scale int, title string, fps int) error {
if currentRunContext != nil {
return errors.New("loop: The game is already running")
}
currentRunContext = &runContext{}
currentRunContext.startRunning() currentRunContext.startRunning()
defer currentRunContext.endRunning() defer currentRunContext.endRunning()