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:08:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
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 12:18:41 +01:00
|
|
|
type Game interface {
|
|
|
|
Layout(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int)
|
|
|
|
Update() error
|
2022-04-01 12:10:10 +02:00
|
|
|
Draw(screenScale float64, offsetX, offsetY float64, needsClearingScreen bool, framebufferYDirection graphicsdriver.YDirection, screenClearedEveryFrame, filterEnabled bool)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
type context struct {
|
2022-02-13 12:18:41 +01:00
|
|
|
game Game
|
2022-02-13 08:30:33 +01:00
|
|
|
|
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 12:18:41 +01:00
|
|
|
outsideWidth float64
|
|
|
|
outsideHeight float64
|
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
2022-02-13 11:50:19 +01:00
|
|
|
|
|
|
|
m sync.Mutex
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func newContext(game Game) *context {
|
|
|
|
return &context{
|
2022-02-13 12:18:41 +01:00
|
|
|
game: game,
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, 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-03-21 08:48:33 +01:00
|
|
|
return c.updateFrameImpl(graphicsDriver, clock.Update(theGlobalState.maxTPS()), outsideWidth, outsideHeight, deviceScaleFactor)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64) error {
|
2022-02-11 12:38:45 +01:00
|
|
|
n := 1
|
|
|
|
if graphicsDriver.IsDirectX() {
|
|
|
|
// On DirectX, both framebuffers in the swap chain should be updated.
|
|
|
|
// Or, the rendering result becomes unexpected when the window is resized.
|
|
|
|
n = 2
|
|
|
|
}
|
|
|
|
for i := 0; i < n; i++ {
|
|
|
|
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-13 10:12:28 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64) error {
|
2022-03-04 17:44:59 +01:00
|
|
|
if err := theGlobalState.error(); err != nil {
|
2022-02-13 08:30:33 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-13 09:23:52 +01:00
|
|
|
|
2022-02-13 18:48:28 +01:00
|
|
|
// 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 nil
|
|
|
|
}
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
2022-02-13 19:39:02 +01:00
|
|
|
if w, h := c.layoutGame(outsideWidth, outsideHeight, 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")
|
|
|
|
|
2022-03-21 08:48:33 +01:00
|
|
|
if err := buffered.BeginFrame(graphicsDriver); err != nil {
|
2022-02-13 10:12:28 +01:00
|
|
|
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
|
|
|
|
}
|
2022-02-13 12:18:41 +01:00
|
|
|
if err := c.game.Update(); err != nil {
|
2022-02-13 12:02:49 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-03-04 17:44:59 +01:00
|
|
|
// Catch the error that happened at (*Image).At.
|
|
|
|
if err := theGlobalState.error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-13 12:02:49 +01:00
|
|
|
Get().resetForTick()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the game.
|
2022-02-13 11:50:19 +01:00
|
|
|
screenScale, offsetX, offsetY := c.screenScaleAndOffsets(deviceScaleFactor)
|
2022-04-01 12:10:10 +02:00
|
|
|
c.game.Draw(screenScale, offsetX, offsetY, graphicsDriver.NeedsClearingScreen(), graphicsDriver.FramebufferYDirection(), theGlobalState.isScreenClearedEveryFrame(), theGlobalState.isScreenFilterEnabled())
|
2022-02-13 10:12:28 +01:00
|
|
|
|
|
|
|
// 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 {
|
2022-03-21 08:48:33 +01:00
|
|
|
if err := buffered.EndFrame(graphicsDriver); err != nil {
|
2022-02-13 10:12:28 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
2022-02-13 11:50:19 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
2022-02-13 19:39:02 +01:00
|
|
|
c.outsideWidth = outsideWidth
|
|
|
|
c.outsideHeight = outsideHeight
|
|
|
|
w, h := c.game.Layout(outsideWidth, outsideHeight, deviceScaleFactor)
|
2022-02-13 12:18:41 +01:00
|
|
|
c.screenWidth = w
|
|
|
|
c.screenHeight = h
|
2022-02-13 11:50:19 +01:00
|
|
|
return w, h
|
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) 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 12:18:41 +01:00
|
|
|
// The scale 0 indicates that the screen is not initialized yet.
|
2022-02-13 10:31:28 +01:00
|
|
|
// 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-04-01 10:59:44 +02:00
|
|
|
func (c *context) screenScaleAndOffsets(deviceScaleFactor float64) (float64, float64, float64) {
|
2022-02-13 11:50:19 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
2022-02-13 10:31:28 +01:00
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
if c.screenWidth == 0 || c.screenHeight == 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 12:18:41 +01:00
|
|
|
scaleX := c.outsideWidth / float64(c.screenWidth) * deviceScaleFactor
|
|
|
|
scaleY := c.outsideHeight / float64(c.screenHeight) * deviceScaleFactor
|
2022-02-13 11:50:19 +01:00
|
|
|
scale := math.Min(scaleX, scaleY)
|
2022-02-13 12:18:41 +01:00
|
|
|
width := float64(c.screenWidth) * scale
|
|
|
|
height := float64(c.screenHeight) * scale
|
2022-02-13 11:50:19 +01:00
|
|
|
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{
|
2022-02-13 12:30:32 +01:00
|
|
|
maxTPS_: DefaultTPS,
|
|
|
|
isScreenClearedEveryFrame_: 1,
|
2022-02-23 19:46:27 +01:00
|
|
|
screenFilterEnabled_: 1,
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// globalState represents a global state in this package.
|
|
|
|
// This is available even before the game loop starts.
|
|
|
|
type globalState struct {
|
2022-03-07 16:08:53 +01:00
|
|
|
err_ error
|
|
|
|
errM sync.Mutex
|
|
|
|
|
2022-02-13 12:30:32 +01:00
|
|
|
fpsMode_ int32
|
|
|
|
maxTPS_ int32
|
|
|
|
isScreenClearedEveryFrame_ int32
|
2022-02-23 19:46:27 +01:00
|
|
|
screenFilterEnabled_ int32
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
2022-03-04 17:44:59 +01:00
|
|
|
func (g *globalState) error() error {
|
2022-03-07 16:08:53 +01:00
|
|
|
g.errM.Lock()
|
|
|
|
defer g.errM.Unlock()
|
|
|
|
return g.err_
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setError(err error) {
|
2022-03-07 16:08:53 +01:00
|
|
|
g.errM.Lock()
|
|
|
|
defer g.errM.Unlock()
|
|
|
|
if g.err_ == nil {
|
|
|
|
g.err_ = err
|
|
|
|
}
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) fpsMode() FPSModeType {
|
2022-02-13 12:30:32 +01:00
|
|
|
return FPSModeType(atomic.LoadInt32(&g.fpsMode_))
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setFPSMode(fpsMode FPSModeType) {
|
2022-02-13 12:30:32 +01:00
|
|
|
atomic.StoreInt32(&g.fpsMode_, int32(fpsMode))
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) maxTPS() int {
|
|
|
|
if g.fpsMode() == FPSModeVsyncOffMinimum {
|
|
|
|
return clock.SyncWithFPS
|
|
|
|
}
|
2022-02-13 12:30:32 +01:00
|
|
|
return int(atomic.LoadInt32(&g.maxTPS_))
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setMaxTPS(tps int) {
|
|
|
|
if tps < 0 && tps != clock.SyncWithFPS {
|
2022-04-01 13:11:22 +02:00
|
|
|
panic("ui: tps must be >= 0 or SyncWithFPS")
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
2022-02-13 12:30:32 +01:00
|
|
|
atomic.StoreInt32(&g.maxTPS_, int32(tps))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) isScreenClearedEveryFrame() bool {
|
|
|
|
return atomic.LoadInt32(&g.isScreenClearedEveryFrame_) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setScreenClearedEveryFrame(cleared bool) {
|
|
|
|
v := int32(0)
|
|
|
|
if cleared {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&g.isScreenClearedEveryFrame_, v)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-02-23 19:46:27 +01:00
|
|
|
func (g *globalState) isScreenFilterEnabled() bool {
|
|
|
|
return graphicsdriver.Filter(atomic.LoadInt32(&g.screenFilterEnabled_)) != 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g *globalState) setScreenFilterEnabled(enabled bool) {
|
|
|
|
v := int32(0)
|
|
|
|
if enabled {
|
|
|
|
v = 1
|
|
|
|
}
|
|
|
|
atomic.StoreInt32(&g.screenFilterEnabled_, v)
|
|
|
|
}
|
|
|
|
|
2022-02-13 09:23:52 +01:00
|
|
|
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
|
|
|
}
|
2022-02-13 12:30:32 +01:00
|
|
|
|
|
|
|
func IsScreenClearedEveryFrame() bool {
|
|
|
|
return theGlobalState.isScreenClearedEveryFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetScreenClearedEveryFrame(cleared bool) {
|
|
|
|
theGlobalState.setScreenClearedEveryFrame(cleared)
|
|
|
|
}
|
2022-02-23 19:46:27 +01:00
|
|
|
|
|
|
|
func IsScreenFilterEnabled() bool {
|
|
|
|
return theGlobalState.isScreenFilterEnabled()
|
|
|
|
}
|
|
|
|
|
|
|
|
func SetScreenFilterEnabled(enabled bool) {
|
|
|
|
theGlobalState.setScreenFilterEnabled(enabled)
|
|
|
|
}
|