mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08: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"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -296,7 +296,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) At(x, y int) color.Color {
|
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)")
|
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()
|
imageM.Lock()
|
||||||
|
@ -12,16 +12,22 @@
|
|||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package ui
|
package loop
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
const FPS = 60
|
const FPS = 60
|
||||||
|
|
||||||
|
func Main() {
|
||||||
|
ui.Main()
|
||||||
|
}
|
||||||
|
|
||||||
func CurrentFPS() float64 {
|
func CurrentFPS() float64 {
|
||||||
return currentRunContext.currentFPS()
|
return currentRunContext.currentFPS()
|
||||||
}
|
}
|
||||||
@ -43,7 +49,7 @@ func SetScreenScale(scale int) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ScreenScale() int {
|
func ScreenScale() int {
|
||||||
return currentUI.ScreenScale()
|
return ui.CurrentUI().ScreenScale()
|
||||||
}
|
}
|
||||||
|
|
||||||
type runContext struct {
|
type runContext struct {
|
||||||
@ -116,16 +122,16 @@ func (c *runContext) updateScreenSize(g GraphicsContext) error {
|
|||||||
}
|
}
|
||||||
changed := false
|
changed := false
|
||||||
if 0 < c.newScreenWidth || 0 < c.newScreenHeight {
|
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
|
changed = changed || c
|
||||||
}
|
}
|
||||||
if 0 < c.newScreenScale {
|
if 0 < c.newScreenScale {
|
||||||
c := currentUI.SetScreenScale(c.newScreenScale)
|
c := ui.CurrentUI().SetScreenScale(c.newScreenScale)
|
||||||
changed = changed || c
|
changed = changed || c
|
||||||
}
|
}
|
||||||
if changed {
|
if changed {
|
||||||
w, h := c.newScreenWidth, c.newScreenHeight
|
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
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -171,32 +177,32 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
|
|||||||
currentRunContext.startRunning()
|
currentRunContext.startRunning()
|
||||||
defer currentRunContext.endRunning()
|
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
|
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
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
frames := 0
|
frames := 0
|
||||||
n := Now()
|
n := ui.Now()
|
||||||
beforeForUpdate := n
|
beforeForUpdate := n
|
||||||
beforeForFPS := n
|
beforeForFPS := n
|
||||||
for {
|
for {
|
||||||
if err := currentRunContext.updateScreenSize(g); err != nil {
|
if err := currentRunContext.updateScreenSize(g); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
e, err := currentUI.Update()
|
e, err := ui.CurrentUI().Update()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
switch e.(type) {
|
switch e.(type) {
|
||||||
case CloseEvent:
|
case ui.CloseEvent:
|
||||||
return nil
|
return nil
|
||||||
case RenderEvent:
|
case ui.RenderEvent:
|
||||||
now := Now()
|
now := ui.Now()
|
||||||
// If beforeForUpdate is too old, we assume that screen is not shown.
|
// If beforeForUpdate is too old, we assume that screen is not shown.
|
||||||
if int64(5*time.Second/FPS) < now-beforeForUpdate {
|
if int64(5*time.Second/FPS) < now-beforeForUpdate {
|
||||||
currentRunContext.setRunningSlowly(false)
|
currentRunContext.setRunningSlowly(false)
|
||||||
@ -216,7 +222,7 @@ func Run(g GraphicsContext, width, height, scale int, title string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
currentUI.SwapBuffers()
|
ui.CurrentUI().SwapBuffers()
|
||||||
beforeForUpdate += int64(tt) * int64(time.Second) / FPS
|
beforeForUpdate += int64(tt) * int64(time.Second) / FPS
|
||||||
frames++
|
frames++
|
||||||
}
|
}
|
@ -43,6 +43,10 @@ type UserInterface struct {
|
|||||||
|
|
||||||
var currentUI *UserInterface
|
var currentUI *UserInterface
|
||||||
|
|
||||||
|
func CurrentUI() *UserInterface {
|
||||||
|
return currentUI
|
||||||
|
}
|
||||||
|
|
||||||
func Init() (*opengl.Context, error) {
|
func Init() (*opengl.Context, error) {
|
||||||
runtime.LockOSThread()
|
runtime.LockOSThread()
|
||||||
|
|
||||||
|
18
run.go
18
run.go
@ -15,11 +15,11 @@
|
|||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FPS represents how many times game updating happens in a second.
|
// 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.
|
// 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
|
// Note that logical game updating is assured to happen 60 times in a second
|
||||||
// as long as the screen is active.
|
// as long as the screen is active.
|
||||||
func CurrentFPS() float64 {
|
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.
|
// 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.
|
// This function is concurrent-safe.
|
||||||
func IsRunningSlowly() bool {
|
func IsRunningSlowly() bool {
|
||||||
return ui.IsRunningSlowly()
|
return loop.IsRunningSlowly()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs the game.
|
// 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)
|
ch := make(chan error)
|
||||||
go func() {
|
go func() {
|
||||||
g := newGraphicsContext(f)
|
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
|
return <-ch
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,7 +67,7 @@ func Run(f func(*Image) error, width, height, scale int, title string) error {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func SetScreenSize(width, height int) {
|
func SetScreenSize(width, height int) {
|
||||||
if err := ui.SetScreenSize(width, height); err != nil {
|
if err := loop.SetScreenSize(width, height); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -76,7 +76,7 @@ func SetScreenSize(width, height int) {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func SetScreenScale(scale int) {
|
func SetScreenScale(scale int) {
|
||||||
if err := ui.SetScreenScale(scale); err != nil {
|
if err := loop.SetScreenScale(scale); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -85,5 +85,5 @@ func SetScreenScale(scale int) {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func ScreenScale() int {
|
func ScreenScale() int {
|
||||||
return ui.ScreenScale()
|
return loop.ScreenScale()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user