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 09:23:52 +01:00
|
|
|
|
2022-10-19 17:46:44 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
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"
|
2022-04-01 14:17:26 +02:00
|
|
|
"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-10-02 11:19:26 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/mipmap"
|
2022-02-13 08:30:33 +01:00
|
|
|
)
|
|
|
|
|
2022-10-02 09:05:32 +02:00
|
|
|
var (
|
2022-10-02 13:33:43 +02:00
|
|
|
NearestFilterShader = &Shader{shader: mipmap.NearestFilterShader}
|
|
|
|
LinearFilterShader = &Shader{shader: mipmap.LinearFilterShader}
|
2022-10-02 09:05:32 +02:00
|
|
|
)
|
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
type Game interface {
|
2022-04-01 11:16:03 +02:00
|
|
|
NewOffscreenImage(width, height int) *Image
|
2022-10-13 16:13:25 +02:00
|
|
|
NewScreenImage(width, height int) *Image
|
2022-04-01 11:16:03 +02:00
|
|
|
Layout(outsideWidth, outsideHeight int) (int, int)
|
2022-02-13 12:18:41 +01:00
|
|
|
Update() error
|
2022-10-13 19:55:04 +02:00
|
|
|
DrawOffscreen() error
|
2022-10-23 18:51:37 +02:00
|
|
|
DrawFinalScreen()
|
2022-10-13 18:58:03 +02:00
|
|
|
ScreenScaleAndOffsets() (scale, offsetX, offsetY float64)
|
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-04-01 11:16:03 +02:00
|
|
|
offscreen *Image
|
|
|
|
screen *Image
|
|
|
|
|
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
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-10-28 12:07:33 +02:00
|
|
|
isOffscreenDirty bool
|
|
|
|
|
2022-10-23 18:51:37 +02:00
|
|
|
skipCount int
|
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-09-25 17:46:03 +02:00
|
|
|
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) error {
|
2022-02-13 09:23:52 +01:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
2022-10-23 18:51:37 +02:00
|
|
|
return c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui, false)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-09-25 17:46:03 +02:00
|
|
|
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl) 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++ {
|
2022-10-23 18:51:37 +02:00
|
|
|
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui, true); err != nil {
|
2022-02-11 12:38:45 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-13 10:12:28 +01:00
|
|
|
}
|
|
|
|
|
2022-10-23 18:51:37 +02:00
|
|
|
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *userInterfaceImpl, forceDraw bool) (err 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-09-25 17:46:03 +02:00
|
|
|
ui.beginFrame()
|
|
|
|
defer ui.endFrame()
|
2022-09-25 16:29:58 +02: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 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-04-25 18:12:52 +02:00
|
|
|
defer func() {
|
|
|
|
// 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.
|
|
|
|
err1 := graphics.LockAndResetVertices(func() error {
|
|
|
|
if err := buffered.EndFrame(graphicsDriver); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
|
|
|
if w, h := c.layoutGame(outsideWidth, outsideHeight, deviceScaleFactor); w == 0 || h == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
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-09-25 17:46:03 +02:00
|
|
|
ui.resetForTick()
|
2022-02-13 12:02:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Draw the game.
|
2022-10-23 18:51:37 +02:00
|
|
|
if err := c.drawGame(graphicsDriver, forceDraw); err != nil {
|
2022-10-13 19:55:04 +02:00
|
|
|
return err
|
|
|
|
}
|
2022-02-13 10:12:28 +01:00
|
|
|
|
2022-04-25 18:12:52 +02:00
|
|
|
return nil
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2022-10-23 18:51:37 +02:00
|
|
|
func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, forceDraw bool) error {
|
2022-10-19 17:46:44 +02:00
|
|
|
if (c.offscreen.imageType == atlas.ImageTypeVolatile) != theGlobalState.isScreenClearedEveryFrame() {
|
2022-06-07 16:16:40 +02:00
|
|
|
w, h := c.offscreen.width, c.offscreen.height
|
|
|
|
c.offscreen.MarkDisposed()
|
|
|
|
c.offscreen = c.game.NewOffscreenImage(w, h)
|
|
|
|
}
|
2022-04-01 11:16:03 +02:00
|
|
|
|
|
|
|
// Even though updateCount == 0, the offscreen is cleared and Draw is called.
|
|
|
|
// Draw should not update the game state and then the screen should not be updated without Update, but
|
|
|
|
// users might want to process something at Draw with the time intervals of FPS.
|
|
|
|
if theGlobalState.isScreenClearedEveryFrame() {
|
|
|
|
c.offscreen.clear()
|
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-10-28 12:07:33 +02:00
|
|
|
// isOffscreenDirty is updated when an offscreen's drawCallback.
|
|
|
|
c.isOffscreenDirty = false
|
2022-10-13 19:55:04 +02:00
|
|
|
if err := c.game.DrawOffscreen(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-01 11:16:03 +02:00
|
|
|
|
2022-10-23 18:51:37 +02:00
|
|
|
const maxSkipCount = 3
|
|
|
|
|
2022-10-28 12:07:33 +02:00
|
|
|
if !forceDraw && !theGlobalState.isScreenClearedEveryFrame() && !c.isOffscreenDirty {
|
2022-10-23 18:51:37 +02:00
|
|
|
if c.skipCount < maxSkipCount {
|
|
|
|
c.skipCount++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
c.skipCount = 0
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
2022-10-23 18:51:37 +02:00
|
|
|
// If the offscreen is not updated and the framebuffers don't have to be updated, skip rendering to save GPU power.
|
|
|
|
if c.skipCount < maxSkipCount {
|
|
|
|
if graphicsDriver.NeedsClearingScreen() {
|
|
|
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
|
|
|
c.screen.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
c.game.DrawFinalScreen()
|
2022-10-14 18:59:44 +02:00
|
|
|
|
2022-10-23 18:51:37 +02:00
|
|
|
// The final screen is never used as the rendering source.
|
|
|
|
// Flush its buffer here just in case.
|
|
|
|
c.screen.flushBufferIfNeeded()
|
|
|
|
}
|
2022-10-14 18:59:44 +02:00
|
|
|
|
2022-10-13 19:55:04 +02:00
|
|
|
return nil
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
2022-02-13 19:39:02 +01:00
|
|
|
c.outsideWidth = outsideWidth
|
|
|
|
c.outsideHeight = outsideHeight
|
2022-04-01 11:16:03 +02:00
|
|
|
|
2022-09-20 15:50:34 +02:00
|
|
|
// Adjust the outside size to integer values.
|
|
|
|
// Even if the original value is less than 1, the value must be a positive integer (#2340).
|
|
|
|
iow, ioh := int(outsideWidth), int(outsideHeight)
|
|
|
|
if iow == 0 {
|
|
|
|
iow = 1
|
|
|
|
}
|
|
|
|
if ioh == 0 {
|
|
|
|
ioh = 1
|
|
|
|
}
|
|
|
|
ow, oh := c.game.Layout(iow, ioh)
|
2022-04-01 11:16:03 +02:00
|
|
|
if ow <= 0 || oh <= 0 {
|
|
|
|
panic("ui: Layout must return positive numbers")
|
|
|
|
}
|
|
|
|
|
|
|
|
sw, sh := int(outsideWidth*deviceScaleFactor), int(outsideHeight*deviceScaleFactor)
|
|
|
|
if c.screen != nil {
|
|
|
|
if c.screen.width != sw || c.screen.height != sh {
|
|
|
|
c.screen.MarkDisposed()
|
|
|
|
c.screen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.screen == nil {
|
2022-10-13 16:13:25 +02:00
|
|
|
c.screen = c.game.NewScreenImage(sw, sh)
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if c.offscreen != nil {
|
|
|
|
if c.offscreen.width != ow || c.offscreen.height != oh {
|
|
|
|
c.offscreen.MarkDisposed()
|
|
|
|
c.offscreen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.offscreen == nil {
|
|
|
|
c.offscreen = c.game.NewOffscreenImage(ow, oh)
|
2022-10-28 12:07:33 +02:00
|
|
|
c.offscreen.drawCallback = func() {
|
|
|
|
c.isOffscreenDirty = true
|
|
|
|
}
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ow, oh
|
2022-02-13 11:50:19 +01:00
|
|
|
}
|
|
|
|
|
2022-04-01 10:59:44 +02:00
|
|
|
func (c *context) adjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
2022-10-13 18:58:03 +02:00
|
|
|
s, ox, oy := c.game.ScreenScaleAndOffsets()
|
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
|
|
|
|
}
|