mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 18:58:54 +01:00
Move the run loop to internal/loop
This commit is contained in:
parent
7f19d4a1ac
commit
063ed564fd
4
image.go
4
image.go
@ -25,7 +25,7 @@ import (
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -296,7 +296,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
||||
}
|
||||
|
||||
func (i *imageImpl) At(x, y int) color.Color {
|
||||
if !ui.IsRunning() {
|
||||
if !loop.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()
|
||||
|
@ -12,16 +12,22 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package ui
|
||||
package loop
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
)
|
||||
|
||||
const FPS = 60
|
||||
|
||||
func Main() {
|
||||
ui.Main()
|
||||
}
|
||||
|
||||
func CurrentFPS() float64 {
|
||||
return currentRunContext.currentFPS()
|
||||
}
|
||||
@ -43,7 +49,7 @@ func SetScreenScale(scale int) error {
|
||||
}
|
||||
|
||||
func ScreenScale() int {
|
||||
return currentUI.ScreenScale()
|
||||
return ui.CurrentUI().ScreenScale()
|
||||
}
|
||||
|
||||
type runContext struct {
|
||||
@ -116,16 +122,16 @@ func (c *runContext) updateScreenSize(g GraphicsContext) error {
|
||||
}
|
||||
changed := false
|
||||
if 0 < c.newScreenWidth || 0 < c.newScreenHeight {
|
||||
c := currentUI.SetScreenSize(c.newScreenWidth, c.newScreenHeight)
|
||||
c := ui.CurrentUI().SetScreenSize(c.newScreenWidth, c.newScreenHeight)
|
||||
changed = changed || c
|
||||
}
|
||||
if 0 < c.newScreenScale {
|
||||
c := currentUI.SetScreenScale(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, currentUI.ActualScreenScale()); err != nil {
|
||||
if err := g.SetSize(w, h, ui.CurrentUI().ActualScreenScale()); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@ -171,32 +177,32 @@ 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 {
|
||||
if err := ui.CurrentUI().Start(width, height, scale, title); err != nil {
|
||||
return err
|
||||
}
|
||||
defer currentUI.Terminate()
|
||||
defer ui.CurrentUI().Terminate()
|
||||
|
||||
if err := g.SetSize(width, height, currentUI.ActualScreenScale()); err != nil {
|
||||
if err := g.SetSize(width, height, ui.CurrentUI().ActualScreenScale()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
frames := 0
|
||||
n := Now()
|
||||
n := ui.Now()
|
||||
beforeForUpdate := n
|
||||
beforeForFPS := n
|
||||
for {
|
||||
if err := currentRunContext.updateScreenSize(g); err != nil {
|
||||
return err
|
||||
}
|
||||
e, err := currentUI.Update()
|
||||
e, err := ui.CurrentUI().Update()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
switch e.(type) {
|
||||
case CloseEvent:
|
||||
case ui.CloseEvent:
|
||||
return nil
|
||||
case RenderEvent:
|
||||
now := Now()
|
||||
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)
|
||||
@ -216,7 +222,7 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
|
||||
return err
|
||||
}
|
||||
}
|
||||
currentUI.SwapBuffers()
|
||||
ui.CurrentUI().SwapBuffers()
|
||||
beforeForUpdate += int64(tt) * int64(time.Second) / FPS
|
||||
frames++
|
||||
}
|
@ -43,6 +43,10 @@ type UserInterface struct {
|
||||
|
||||
var currentUI *UserInterface
|
||||
|
||||
func CurrentUI() *UserInterface {
|
||||
return currentUI
|
||||
}
|
||||
|
||||
func Init() (*opengl.Context, error) {
|
||||
runtime.LockOSThread()
|
||||
|
||||
|
18
run.go
18
run.go
@ -15,11 +15,11 @@
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||
)
|
||||
|
||||
// FPS represents how many times game updating happens in a second.
|
||||
const FPS = ui.FPS
|
||||
const FPS = loop.FPS
|
||||
|
||||
// CurrentFPS returns the current number of frames per second of rendering.
|
||||
//
|
||||
@ -30,7 +30,7 @@ const FPS = ui.FPS
|
||||
// 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 ui.CurrentFPS()
|
||||
return loop.CurrentFPS()
|
||||
}
|
||||
|
||||
// IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS of rendering.
|
||||
@ -39,7 +39,7 @@ func CurrentFPS() float64 {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func IsRunningSlowly() bool {
|
||||
return ui.IsRunningSlowly()
|
||||
return loop.IsRunningSlowly()
|
||||
}
|
||||
|
||||
// Run runs the game.
|
||||
@ -56,9 +56,9 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
ch := make(chan error)
|
||||
go func() {
|
||||
g := newGraphicsContext(f)
|
||||
ch <- ui.Run(g, width, height, scale, title)
|
||||
ch <- loop.Run(g, width, height, scale, title)
|
||||
}()
|
||||
ui.Main()
|
||||
loop.Main()
|
||||
return <-ch
|
||||
}
|
||||
|
||||
@ -67,7 +67,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func SetScreenSize(width, height int) {
|
||||
if err := ui.SetScreenSize(width, height); err != nil {
|
||||
if err := loop.SetScreenSize(width, height); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -76,7 +76,7 @@ func SetScreenSize(width, height int) {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func SetScreenScale(scale int) {
|
||||
if err := ui.SetScreenScale(scale); err != nil {
|
||||
if err := loop.SetScreenScale(scale); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
}
|
||||
@ -85,5 +85,5 @@ func SetScreenScale(scale int) {
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func ScreenScale() int {
|
||||
return ui.ScreenScale()
|
||||
return loop.ScreenScale()
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user