mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Move the run loop to internal/ui
This commit is contained in:
parent
5c68ee4034
commit
4d04413bf9
@ -14,44 +14,21 @@
|
||||
|
||||
package ebiten
|
||||
|
||||
func newGraphicsContext(screenWidth, screenHeight, screenScale int) (*graphicsContext, error) {
|
||||
c := &graphicsContext{}
|
||||
if err := c.setSize(screenWidth, screenHeight, screenScale); err != nil {
|
||||
return nil, err
|
||||
func newGraphicsContext(f func(*Image) error) *graphicsContext {
|
||||
return &graphicsContext{
|
||||
f: f,
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
type graphicsContext struct {
|
||||
f func(*Image) error
|
||||
screen *Image
|
||||
defaultRenderTarget *Image
|
||||
screenScale int
|
||||
initialized bool
|
||||
}
|
||||
|
||||
func (c *graphicsContext) update(f func(*Image) error) error {
|
||||
if err := c.screen.Clear(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f(c.screen); err != nil {
|
||||
return err
|
||||
}
|
||||
if IsRunningSlowly() {
|
||||
return nil
|
||||
}
|
||||
// TODO: In WebGL, we don't need to clear the image here.
|
||||
if err := c.defaultRenderTarget.Clear(); err != nil {
|
||||
return err
|
||||
}
|
||||
scale := float64(c.screenScale)
|
||||
options := &DrawImageOptions{}
|
||||
options.GeoM.Scale(scale, scale)
|
||||
if err := c.defaultRenderTarget.DrawImage(c.screen, options); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *graphicsContext) setSize(screenWidth, screenHeight, screenScale int) error {
|
||||
func (c *graphicsContext) SetSize(screenWidth, screenHeight, screenScale int) error {
|
||||
if c.defaultRenderTarget != nil {
|
||||
c.defaultRenderTarget.Dispose()
|
||||
}
|
||||
@ -69,5 +46,35 @@ func (c *graphicsContext) setSize(screenWidth, screenHeight, screenScale int) er
|
||||
c.defaultRenderTarget.Clear()
|
||||
c.screen = screen
|
||||
c.screenScale = screenScale
|
||||
// TODO: This code is a little hacky. Refactor this.
|
||||
if !c.initialized {
|
||||
if err := theDelayedImageTasks.exec(); err != nil {
|
||||
return err
|
||||
}
|
||||
c.initialized = true
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *graphicsContext) Update() error {
|
||||
if err := c.screen.Clear(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.f(c.screen); err != nil {
|
||||
return err
|
||||
}
|
||||
if IsRunningSlowly() {
|
||||
return nil
|
||||
}
|
||||
// TODO: In WebGL, we don't need to clear the image here.
|
||||
if err := c.defaultRenderTarget.Clear(); err != nil {
|
||||
return err
|
||||
}
|
||||
scale := float64(c.screenScale)
|
||||
options := &DrawImageOptions{}
|
||||
options.GeoM.Scale(scale, scale)
|
||||
if err := c.defaultRenderTarget.DrawImage(c.screen, options); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
3
image.go
3
image.go
@ -25,6 +25,7 @@ import (
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -295,7 +296,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
||||
}
|
||||
|
||||
func (i *imageImpl) At(x, y int) color.Color {
|
||||
if !currentRunContext.isRunning() {
|
||||
if !ui.IsRunning() {
|
||||
panic("ebiten: At can't be called when the GL context is not initialized (this panic happens as of version 1.4.0-alpha)")
|
||||
}
|
||||
imageM.Lock()
|
||||
|
230
internal/ui/run.go
Normal file
230
internal/ui/run.go
Normal file
@ -0,0 +1,230 @@
|
||||
// Copyright 2016 Hajime Hoshi
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ui
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
const FPS = 60
|
||||
|
||||
func CurrentFPS() float64 {
|
||||
return currentRunContext.currentFPS()
|
||||
}
|
||||
|
||||
func IsRunning() bool {
|
||||
return currentRunContext.isRunning()
|
||||
}
|
||||
|
||||
func IsRunningSlowly() bool {
|
||||
return currentRunContext.isRunningSlowly()
|
||||
}
|
||||
|
||||
func SetScreenSize(width, height int) error {
|
||||
return currentRunContext.setScreenSize(width, height)
|
||||
}
|
||||
|
||||
func SetScreenScale(scale int) error {
|
||||
return currentRunContext.setScreenScale(scale)
|
||||
}
|
||||
|
||||
type runContext struct {
|
||||
running bool
|
||||
fps float64
|
||||
newScreenWidth int
|
||||
newScreenHeight int
|
||||
newScreenScale int
|
||||
runningSlowly bool
|
||||
m sync.RWMutex
|
||||
}
|
||||
|
||||
var currentRunContext runContext
|
||||
|
||||
func (c *runContext) startRunning() {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.running = true
|
||||
}
|
||||
|
||||
func (c *runContext) isRunning() bool {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
return c.running
|
||||
}
|
||||
|
||||
func (c *runContext) endRunning() {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.running = false
|
||||
}
|
||||
|
||||
func (c *runContext) currentFPS() float64 {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if !c.running {
|
||||
// TODO: Should panic here?
|
||||
return 0
|
||||
}
|
||||
return c.fps
|
||||
}
|
||||
|
||||
func (c *runContext) updateFPS(fps float64) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.fps = fps
|
||||
}
|
||||
|
||||
func (c *runContext) isRunningSlowly() bool {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if !c.running {
|
||||
// TODO: Should panic here?
|
||||
return false
|
||||
}
|
||||
return c.runningSlowly
|
||||
}
|
||||
|
||||
func (c *runContext) setRunningSlowly(isRunningSlowly bool) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.runningSlowly = isRunningSlowly
|
||||
}
|
||||
|
||||
func (c *runContext) updateScreenSize(g GraphicsContext) error {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
if c.newScreenWidth == 0 && c.newScreenHeight == 0 && c.newScreenScale == 0 {
|
||||
return nil
|
||||
}
|
||||
changed := false
|
||||
if 0 < c.newScreenWidth || 0 < c.newScreenHeight {
|
||||
c := CurrentUI().SetScreenSize(c.newScreenWidth, c.newScreenHeight)
|
||||
changed = changed || c
|
||||
}
|
||||
if 0 < c.newScreenScale {
|
||||
c := CurrentUI().SetScreenScale(c.newScreenScale)
|
||||
changed = changed || c
|
||||
}
|
||||
if changed {
|
||||
w, h := c.newScreenWidth, c.newScreenHeight
|
||||
if err := g.SetSize(w, h, CurrentUI().ActualScreenScale()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c.newScreenWidth = 0
|
||||
c.newScreenHeight = 0
|
||||
c.newScreenScale = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *runContext) setScreenSize(width, height int) error {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
if !c.running {
|
||||
return errors.New("ebiten: SetScreenSize must be called during Run")
|
||||
}
|
||||
if width <= 0 || height <= 0 {
|
||||
return errors.New("ebiten: width and height must be positive")
|
||||
}
|
||||
c.newScreenWidth = width
|
||||
c.newScreenHeight = height
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *runContext) setScreenScale(scale int) error {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
if !c.running {
|
||||
return errors.New("ebiten: SetScreenScale must be called during Run")
|
||||
}
|
||||
if scale <= 0 {
|
||||
return errors.New("ebiten: scale must be positive")
|
||||
}
|
||||
c.newScreenScale = scale
|
||||
return nil
|
||||
}
|
||||
|
||||
type GraphicsContext interface {
|
||||
SetSize(width, height, scale int) error
|
||||
Update() error
|
||||
}
|
||||
|
||||
func Run(g GraphicsContext, width, height, scale int, title string) error {
|
||||
currentRunContext.startRunning()
|
||||
defer currentRunContext.endRunning()
|
||||
|
||||
if err := CurrentUI().Start(width, height, scale, title); err != nil {
|
||||
return err
|
||||
}
|
||||
defer CurrentUI().Terminate()
|
||||
|
||||
if err := g.SetSize(width, height, CurrentUI().ActualScreenScale()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
frames := 0
|
||||
n := Now()
|
||||
beforeForUpdate := n
|
||||
beforeForFPS := n
|
||||
for {
|
||||
if err := currentRunContext.updateScreenSize(g); err != nil {
|
||||
return err
|
||||
}
|
||||
e, err := CurrentUI().Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch e.(type) {
|
||||
case CloseEvent:
|
||||
return nil
|
||||
case RenderEvent:
|
||||
now := Now()
|
||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
||||
if int64(5*time.Second/FPS) < now-beforeForUpdate {
|
||||
currentRunContext.setRunningSlowly(false)
|
||||
beforeForUpdate = now
|
||||
} else {
|
||||
// Note that generally t is a little different from 1/60[sec].
|
||||
t := now - beforeForUpdate
|
||||
currentRunContext.setRunningSlowly(t*FPS >= int64(time.Second*5/2))
|
||||
tt := int(t * FPS / int64(time.Second))
|
||||
// As t is not accurate 1/60[sec], errors are accumulated.
|
||||
// To make the FPS stable, set tt 1 if t is a little less than 1/60[sec].
|
||||
if tt == 0 && (int64(time.Second)/FPS-int64(5*time.Millisecond)) < t {
|
||||
tt = 1
|
||||
}
|
||||
for i := 0; i < tt; i++ {
|
||||
if err := g.Update(); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
CurrentUI().SwapBuffers()
|
||||
beforeForUpdate += int64(tt) * int64(time.Second) / FPS
|
||||
frames++
|
||||
}
|
||||
|
||||
// Calc the current FPS.
|
||||
if time.Second <= time.Duration(now-beforeForFPS) {
|
||||
currentRunContext.updateFPS(float64(frames) * float64(time.Second) / float64(now-beforeForFPS))
|
||||
beforeForFPS = now
|
||||
frames = 0
|
||||
}
|
||||
default:
|
||||
panic("not reach")
|
||||
}
|
||||
}
|
||||
}
|
204
run.go
204
run.go
@ -15,131 +15,11 @@
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
)
|
||||
|
||||
type runContext struct {
|
||||
running bool
|
||||
fps float64
|
||||
newScreenWidth int
|
||||
newScreenHeight int
|
||||
newScreenScale int
|
||||
runningSlowly bool
|
||||
m sync.RWMutex
|
||||
}
|
||||
|
||||
var currentRunContext runContext
|
||||
|
||||
func (c *runContext) startRunning() {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.running = true
|
||||
}
|
||||
|
||||
func (c *runContext) isRunning() bool {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
return c.running
|
||||
}
|
||||
|
||||
func (c *runContext) endRunning() {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.running = false
|
||||
}
|
||||
|
||||
func (c *runContext) FPS() float64 {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if !c.running {
|
||||
// TODO: Should panic here?
|
||||
return 0
|
||||
}
|
||||
return c.fps
|
||||
}
|
||||
|
||||
func (c *runContext) updateFPS(fps float64) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.fps = fps
|
||||
}
|
||||
|
||||
func (c *runContext) IsRunningSlowly() bool {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if !c.running {
|
||||
// TODO: Should panic here?
|
||||
return false
|
||||
}
|
||||
return c.runningSlowly
|
||||
}
|
||||
|
||||
func (c *runContext) setRunningSlowly(isRunningSlowly bool) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.runningSlowly = isRunningSlowly
|
||||
}
|
||||
|
||||
func (c *runContext) updateScreenSize(g *graphicsContext) error {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
if c.newScreenWidth == 0 && c.newScreenHeight == 0 && c.newScreenScale == 0 {
|
||||
return nil
|
||||
}
|
||||
changed := false
|
||||
if 0 < c.newScreenWidth || 0 < c.newScreenHeight {
|
||||
c := ui.CurrentUI().SetScreenSize(c.newScreenWidth, c.newScreenHeight)
|
||||
changed = changed || c
|
||||
}
|
||||
if 0 < c.newScreenScale {
|
||||
c := ui.CurrentUI().SetScreenScale(c.newScreenScale)
|
||||
changed = changed || c
|
||||
}
|
||||
if changed {
|
||||
w, h := c.newScreenWidth, c.newScreenHeight
|
||||
if err := g.setSize(w, h, ui.CurrentUI().ActualScreenScale()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
c.newScreenWidth = 0
|
||||
c.newScreenHeight = 0
|
||||
c.newScreenScale = 0
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *runContext) SetScreenSize(width, height int) error {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
if !c.running {
|
||||
return errors.New("ebiten: SetScreenSize must be called during Run")
|
||||
}
|
||||
if width <= 0 || height <= 0 {
|
||||
return errors.New("ebiten: width and height must be positive")
|
||||
}
|
||||
c.newScreenWidth = width
|
||||
c.newScreenHeight = height
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *runContext) SetScreenScale(scale int) error {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
if !c.running {
|
||||
return errors.New("ebiten: SetScreenScale must be called during Run")
|
||||
}
|
||||
if scale <= 0 {
|
||||
return errors.New("ebiten: scale must be positive")
|
||||
}
|
||||
c.newScreenScale = scale
|
||||
return nil
|
||||
}
|
||||
|
||||
// FPS represents how many times game updating happens in a second.
|
||||
const FPS = 60
|
||||
const FPS = ui.FPS
|
||||
|
||||
// CurrentFPS returns the current number of frames per second of rendering.
|
||||
//
|
||||
@ -150,7 +30,7 @@ const FPS = 60
|
||||
// Note that logical game updating is assured to happen 60 times in a second
|
||||
// as long as the screen is active.
|
||||
func CurrentFPS() float64 {
|
||||
return currentRunContext.FPS()
|
||||
return ui.CurrentFPS()
|
||||
}
|
||||
|
||||
// IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS of rendering.
|
||||
@ -159,7 +39,7 @@ func CurrentFPS() float64 {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func IsRunningSlowly() bool {
|
||||
return currentRunContext.IsRunningSlowly()
|
||||
return ui.IsRunningSlowly()
|
||||
}
|
||||
|
||||
// Run runs the game.
|
||||
@ -175,89 +55,19 @@ func IsRunningSlowly() bool {
|
||||
func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
ch <- run(f, width, height, scale, title)
|
||||
g := newGraphicsContext(f)
|
||||
ch <- ui.Run(g, width, height, scale, title)
|
||||
}()
|
||||
ui.Main()
|
||||
return <-ch
|
||||
}
|
||||
|
||||
func run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
currentRunContext.startRunning()
|
||||
defer currentRunContext.endRunning()
|
||||
|
||||
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
|
||||
return err
|
||||
}
|
||||
defer ui.CurrentUI().Terminate()
|
||||
|
||||
graphicsContext, err := newGraphicsContext(width, height, ui.CurrentUI().ActualScreenScale())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := theDelayedImageTasks.exec(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
frames := 0
|
||||
n := ui.Now()
|
||||
beforeForUpdate := n
|
||||
beforeForFPS := n
|
||||
for {
|
||||
if err := currentRunContext.updateScreenSize(graphicsContext); err != nil {
|
||||
return err
|
||||
}
|
||||
e, err := ui.CurrentUI().Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch e.(type) {
|
||||
case ui.CloseEvent:
|
||||
return nil
|
||||
case ui.RenderEvent:
|
||||
now := ui.Now()
|
||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
||||
if int64(5*time.Second/FPS) < now-beforeForUpdate {
|
||||
currentRunContext.setRunningSlowly(false)
|
||||
beforeForUpdate = now
|
||||
} else {
|
||||
// Note that generally t is a little different from 1/60[sec].
|
||||
t := now - beforeForUpdate
|
||||
currentRunContext.setRunningSlowly(t*FPS >= int64(time.Second*5/2))
|
||||
tt := int(t * FPS / int64(time.Second))
|
||||
// As t is not accurate 1/60[sec], errors are accumulated.
|
||||
// To make the FPS stable, set tt 1 if t is a little less than 1/60[sec].
|
||||
if tt == 0 && (int64(time.Second)/FPS-int64(5*time.Millisecond)) < t {
|
||||
tt = 1
|
||||
}
|
||||
for i := 0; i < tt; i++ {
|
||||
if err := graphicsContext.update(f); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
ui.CurrentUI().SwapBuffers()
|
||||
beforeForUpdate += int64(tt) * int64(time.Second) / FPS
|
||||
frames++
|
||||
}
|
||||
|
||||
// Calc the current FPS.
|
||||
if time.Second <= time.Duration(now-beforeForFPS) {
|
||||
currentRunContext.updateFPS(float64(frames) * float64(time.Second) / float64(now-beforeForFPS))
|
||||
beforeForFPS = now
|
||||
frames = 0
|
||||
}
|
||||
default:
|
||||
panic("not reach")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// SetScreenSize changes the (logical) size of the screen.
|
||||
// This doesn't affect the current scale of the screen.
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func SetScreenSize(width, height int) {
|
||||
if err := currentRunContext.SetScreenSize(width, height); err != nil {
|
||||
if err := ui.SetScreenSize(width, height); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -266,7 +76,7 @@ func SetScreenSize(width, height int) {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func SetScreenScale(scale int) {
|
||||
if err := currentRunContext.SetScreenScale(scale); err != nil {
|
||||
if err := ui.SetScreenScale(scale); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user