2022-02-13 08:30:33 +01:00
|
|
|
// Copyright 2022 The Ebiten Authors
|
|
|
|
//
|
|
|
|
// 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 (
|
2022-02-13 10:31:28 +01:00
|
|
|
"math"
|
2022-02-13 11:50:19 +01:00
|
|
|
"sync"
|
2022-02-13 08:30:33 +01:00
|
|
|
"sync/atomic"
|
2022-02-13 09:23:52 +01:00
|
|
|
|
2022-02-13 10:12:28 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/buffered"
|
2022-02-13 09:23:52 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/clock"
|
2022-02-13 10:12:28 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
|
|
|
graphicspkg "github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-13 12:02:49 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
2022-02-13 08:30:33 +01:00
|
|
|
)
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
const DefaultTPS = 60
|
|
|
|
|
2022-02-13 08:30:33 +01:00
|
|
|
type Context interface {
|
2022-02-13 11:40:03 +01:00
|
|
|
UpdateOffscreen(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int)
|
2022-02-13 12:02:49 +01:00
|
|
|
UpdateGame() error
|
|
|
|
DrawGame(screenScale float64, offsetX, offsetY float64) error
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type contextImpl struct {
|
|
|
|
context Context
|
|
|
|
|
2022-02-13 12:02:49 +01:00
|
|
|
updateCalled bool
|
|
|
|
|
2022-02-13 11:50:19 +01:00
|
|
|
// The following members must be protected by the mutex m.
|
2022-02-13 10:12:28 +01:00
|
|
|
outsideWidth float64
|
|
|
|
outsideHeight float64
|
|
|
|
offscreenWidth int
|
|
|
|
offscreenHeight int
|
2022-02-13 11:50:19 +01:00
|
|
|
|
|
|
|
m sync.Mutex
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newContextImpl(context Context) *contextImpl {
|
|
|
|
return &contextImpl{
|
|
|
|
context: context,
|
|
|
|
}
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 10:31:28 +01:00
|
|
|
func (c *contextImpl) updateFrame(deviceScaleFactor float64) error {
|
2022-02-13 09:23:52 +01:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
2022-02-13 10:31:28 +01:00
|
|
|
return c.updateFrameImpl(clock.Update(theGlobalState.maxTPS()), deviceScaleFactor)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 10:31:28 +01:00
|
|
|
func (c *contextImpl) forceUpdateFrame(deviceScaleFactor float64) error {
|
|
|
|
return c.updateFrameImpl(1, deviceScaleFactor)
|
2022-02-13 10:12:28 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 10:31:28 +01:00
|
|
|
func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64) error {
|
2022-02-13 09:23:52 +01:00
|
|
|
if err := theGlobalState.err(); err != nil {
|
2022-02-13 08:30:33 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-13 09:23:52 +01:00
|
|
|
|
|
|
|
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
2022-02-13 11:50:19 +01:00
|
|
|
if w, h := c.updateOffscreenSize(deviceScaleFactor); w == 0 || h == 0 {
|
2022-02-13 09:23:52 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-13 10:12:28 +01:00
|
|
|
debug.Logf("----\n")
|
|
|
|
|
|
|
|
if err := buffered.BeginFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-13 10:31:28 +01:00
|
|
|
|
2022-02-13 12:02:49 +01:00
|
|
|
// Ensure that Update is called once before Draw so that Update can be used for initialization.
|
|
|
|
if !c.updateCalled && updateCount == 0 {
|
|
|
|
updateCount = 1
|
|
|
|
c.updateCalled = true
|
|
|
|
}
|
|
|
|
debug.Logf("Update count per frame: %d\n", updateCount)
|
|
|
|
|
|
|
|
// Update the game.
|
|
|
|
for i := 0; i < updateCount; i++ {
|
|
|
|
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.context.UpdateGame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
Get().resetForTick()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the game.
|
2022-02-13 11:50:19 +01:00
|
|
|
screenScale, offsetX, offsetY := c.screenScaleAndOffsets(deviceScaleFactor)
|
2022-02-13 12:02:49 +01:00
|
|
|
if err := c.context.DrawGame(screenScale, offsetX, offsetY); err != nil {
|
2022-02-13 10:12:28 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// All the vertices data are consumed at the end of the frame, and the data backend can be
|
|
|
|
// available after that. Until then, lock the vertices backend.
|
|
|
|
return graphicspkg.LockAndResetVertices(func() error {
|
|
|
|
if err := buffered.EndFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 11:50:19 +01:00
|
|
|
func (c *contextImpl) updateOffscreenSize(deviceScaleFactor float64) (int, int) {
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
w, h := c.context.UpdateOffscreen(c.outsideWidth, c.outsideHeight, deviceScaleFactor)
|
|
|
|
c.offscreenWidth = w
|
|
|
|
c.offscreenHeight = h
|
|
|
|
return w, h
|
|
|
|
}
|
|
|
|
|
2022-02-13 08:30:33 +01:00
|
|
|
func (c *contextImpl) layout(outsideWidth, outsideHeight float64) {
|
|
|
|
// The given outside size can be 0 e.g. just after restoring from the fullscreen mode on Windows (#1589)
|
|
|
|
// Just ignore such cases. Otherwise, creating a zero-sized framebuffer causes a panic.
|
|
|
|
if outsideWidth == 0 || outsideHeight == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-13 11:50:19 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2022-02-13 09:23:52 +01:00
|
|
|
c.outsideWidth = outsideWidth
|
|
|
|
c.outsideHeight = outsideHeight
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *contextImpl) adjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
2022-02-13 11:50:19 +01:00
|
|
|
s, ox, oy := c.screenScaleAndOffsets(deviceScaleFactor)
|
2022-02-13 10:31:28 +01:00
|
|
|
// The scale 0 indicates that the offscreen is not initialized yet.
|
|
|
|
// As any cursor values don't make sense, just return NaN.
|
|
|
|
if s == 0 {
|
|
|
|
return math.NaN(), math.NaN()
|
|
|
|
}
|
|
|
|
return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s
|
|
|
|
}
|
|
|
|
|
2022-02-13 11:50:19 +01:00
|
|
|
func (c *contextImpl) screenScaleAndOffsets(deviceScaleFactor float64) (float64, float64, float64) {
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2022-02-13 10:31:28 +01:00
|
|
|
|
|
|
|
if c.offscreenWidth == 0 || c.offscreenHeight == 0 {
|
2022-02-13 11:50:19 +01:00
|
|
|
return 0, 0, 0
|
2022-02-13 10:31:28 +01:00
|
|
|
}
|
2022-02-13 11:50:19 +01:00
|
|
|
|
2022-02-13 10:31:28 +01:00
|
|
|
scaleX := c.outsideWidth / float64(c.offscreenWidth) * deviceScaleFactor
|
|
|
|
scaleY := c.outsideHeight / float64(c.offscreenHeight) * deviceScaleFactor
|
2022-02-13 11:50:19 +01:00
|
|
|
scale := math.Min(scaleX, scaleY)
|
|
|
|
width := float64(c.offscreenWidth) * scale
|
|
|
|
height := float64(c.offscreenHeight) * scale
|
|
|
|
x := (c.outsideWidth*deviceScaleFactor - width) / 2
|
|
|
|
y := (c.outsideHeight*deviceScaleFactor - height) / 2
|
|
|
|
return scale, x, y
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
var theGlobalState = globalState{
|
|
|
|
currentMaxTPS: DefaultTPS,
|
|
|
|
}
|
|
|
|
|
|
|
|
// globalState represents a global state in this package.
|
|
|
|
// This is available even before the game loop starts.
|
|
|
|
type globalState struct {
|
|
|
|
currentErr atomic.Value
|
|
|
|
currentFPSMode int32
|
|
|
|
currentMaxTPS int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) err() error {
|
|
|
|
err, ok := g.currentErr.Load().(error)
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setError(err error) {
|
|
|
|
g.currentErr.Store(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) fpsMode() FPSModeType {
|
|
|
|
return FPSModeType(atomic.LoadInt32(&g.currentFPSMode))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setFPSMode(fpsMode FPSModeType) {
|
|
|
|
atomic.StoreInt32(&g.currentFPSMode, int32(fpsMode))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) maxTPS() int {
|
|
|
|
if g.fpsMode() == FPSModeVsyncOffMinimum {
|
|
|
|
return clock.SyncWithFPS
|
|
|
|
}
|
|
|
|
return int(atomic.LoadInt32(&g.currentMaxTPS))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setMaxTPS(tps int) {
|
|
|
|
if tps < 0 && tps != clock.SyncWithFPS {
|
|
|
|
panic("ebiten: tps must be >= 0 or SyncWithFPS")
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&g.currentMaxTPS, int32(tps))
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func SetError(err error) {
|
2022-02-13 09:23:52 +01:00
|
|
|
theGlobalState.setError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func FPSMode() FPSModeType {
|
|
|
|
return theGlobalState.fpsMode()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetFPSMode(fpsMode FPSModeType) {
|
|
|
|
theGlobalState.setFPSMode(fpsMode)
|
|
|
|
Get().SetFPSMode(fpsMode)
|
|
|
|
}
|
|
|
|
|
|
|
|
func MaxTPS() int {
|
|
|
|
return theGlobalState.maxTPS()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetMaxTPS(tps int) {
|
|
|
|
theGlobalState.setMaxTPS(tps)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|