mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-13 04:22:05 +01:00
loop: Remove dependency on ui
This commit is contained in:
parent
56a17a7f79
commit
eef8289854
@ -20,7 +20,6 @@ import (
|
|||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/clock"
|
"github.com/hajimehoshi/ebiten/internal/clock"
|
||||||
"github.com/hajimehoshi/ebiten/internal/sync"
|
"github.com/hajimehoshi/ebiten/internal/sync"
|
||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func CurrentFPS() float64 {
|
func CurrentFPS() float64 {
|
||||||
@ -81,30 +80,11 @@ func (c *runContext) updateFPS(fps float64) {
|
|||||||
c.m.Unlock()
|
c.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
type GraphicsContext interface {
|
type Runner interface {
|
||||||
SetSize(width, height int, scale float64)
|
Run(width, height int, scale float64, title string) error
|
||||||
Update(updateCount int) error
|
|
||||||
Invalidate()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type loopGraphicsContext struct {
|
func Run(runner Runner, width, height int, scale float64, title string) (err error) {
|
||||||
runContext *runContext
|
|
||||||
graphicsContext GraphicsContext
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *loopGraphicsContext) SetSize(width, height int, scale float64) {
|
|
||||||
g.graphicsContext.SetSize(width, height, scale)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *loopGraphicsContext) Update() error {
|
|
||||||
return g.runContext.render(g.graphicsContext)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (g *loopGraphicsContext) Invalidate() {
|
|
||||||
g.graphicsContext.Invalidate()
|
|
||||||
}
|
|
||||||
|
|
||||||
func Run(g GraphicsContext, width, height int, scale float64, title string) (err error) {
|
|
||||||
if currentRunContext != nil {
|
if currentRunContext != nil {
|
||||||
return errors.New("loop: The game is already running")
|
return errors.New("loop: The game is already running")
|
||||||
}
|
}
|
||||||
@ -118,14 +98,7 @@ func Run(g GraphicsContext, width, height int, scale float64, title string) (err
|
|||||||
|
|
||||||
close(contextInitCh)
|
close(contextInitCh)
|
||||||
|
|
||||||
lg := &loopGraphicsContext{currentRunContext, g}
|
return runner.Run(width, height, scale, title)
|
||||||
if err := ui.Run(width, height, scale, title, lg); err != nil {
|
|
||||||
if _, ok := err.(*ui.RegularTermination); ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *runContext) updateCount(now int64) int {
|
func (c *runContext) updateCount(now int64) int {
|
||||||
@ -187,7 +160,16 @@ func (c *runContext) registerPing(ping func()) {
|
|||||||
c.m.Unlock()
|
c.m.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *runContext) render(g GraphicsContext) error {
|
type Updater interface {
|
||||||
|
Update(updateCount int) error
|
||||||
|
}
|
||||||
|
|
||||||
|
func Update(u Updater) error {
|
||||||
|
<-contextInitCh
|
||||||
|
return currentRunContext.update(u)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *runContext) update(u Updater) error {
|
||||||
n := now()
|
n := now()
|
||||||
|
|
||||||
c.m.Lock()
|
c.m.Lock()
|
||||||
@ -197,7 +179,7 @@ func (c *runContext) render(g GraphicsContext) error {
|
|||||||
c.m.Unlock()
|
c.m.Unlock()
|
||||||
|
|
||||||
count := c.updateCount(n)
|
count := c.updateCount(n)
|
||||||
if err := g.Update(count); err != nil {
|
if err := u.Update(count); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
c.framesForFPS++
|
c.framesForFPS++
|
||||||
|
32
run.go
32
run.go
@ -60,6 +60,32 @@ func IsRunningSlowly() bool {
|
|||||||
|
|
||||||
var theGraphicsContext atomic.Value
|
var theGraphicsContext atomic.Value
|
||||||
|
|
||||||
|
type runner struct {
|
||||||
|
g *graphicsContext
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) Run(width, height int, scale float64, title string) error {
|
||||||
|
if err := ui.Run(width, height, scale, title, r); err != nil {
|
||||||
|
if _, ok := err.(*ui.RegularTermination); ok {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) SetSize(width, height int, scale float64) {
|
||||||
|
r.g.SetSize(width, height, scale)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) Update() error {
|
||||||
|
return loop.Update(r.g)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *runner) Invalidate() {
|
||||||
|
r.g.Invalidate()
|
||||||
|
}
|
||||||
|
|
||||||
// Run runs the game.
|
// Run runs the game.
|
||||||
// f is a function which is called at every frame.
|
// f is a function which is called at every frame.
|
||||||
// The argument (*Image) is the render target that represents the screen.
|
// The argument (*Image) is the render target that represents the screen.
|
||||||
@ -82,7 +108,8 @@ func Run(f func(*Image) error, width, height int, scale float64, title string) e
|
|||||||
go func() {
|
go func() {
|
||||||
g := newGraphicsContext(f)
|
g := newGraphicsContext(f)
|
||||||
theGraphicsContext.Store(g)
|
theGraphicsContext.Store(g)
|
||||||
if err := loop.Run(g, width, height, scale, title); err != nil {
|
r := &runner{g}
|
||||||
|
if err := loop.Run(r, width, height, scale, title); err != nil {
|
||||||
ch <- err
|
ch <- err
|
||||||
}
|
}
|
||||||
close(ch)
|
close(ch)
|
||||||
@ -106,7 +133,8 @@ func RunWithoutMainLoop(f func(*Image) error, width, height int, scale float64,
|
|||||||
go func() {
|
go func() {
|
||||||
g := newGraphicsContext(f)
|
g := newGraphicsContext(f)
|
||||||
theGraphicsContext.Store(g)
|
theGraphicsContext.Store(g)
|
||||||
if err := loop.Run(g, width, height, scale, title); err != nil {
|
r := &runner{g}
|
||||||
|
if err := loop.Run(r, width, height, scale, title); err != nil {
|
||||||
ch <- err
|
ch <- err
|
||||||
}
|
}
|
||||||
close(ch)
|
close(ch)
|
||||||
|
Loading…
Reference in New Issue
Block a user