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 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-02-13 12:08:59 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2023-10-06 06:49:42 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hook"
|
2022-02-13 08:30:33 +01:00
|
|
|
)
|
|
|
|
|
2022-10-02 09:05:32 +02:00
|
|
|
var (
|
2023-10-18 17:42:20 +02:00
|
|
|
NearestFilterShader = &Shader{shader: atlas.NearestFilterShader}
|
|
|
|
LinearFilterShader = &Shader{shader: atlas.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-11-08 17:07:03 +01:00
|
|
|
Layout(outsideWidth, outsideHeight float64) (screenWidth, screenHeight float64)
|
2023-01-21 16:04:30 +01:00
|
|
|
UpdateInputState(fn func(*InputState))
|
2022-12-21 03:46:55 +01:00
|
|
|
Update() error
|
2022-10-13 19:55:04 +02:00
|
|
|
DrawOffscreen() error
|
2022-11-08 17:07:03 +01:00
|
|
|
DrawFinalScreen(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-11-08 17:07:03 +01:00
|
|
|
screenWidth float64
|
|
|
|
screenHeight float64
|
|
|
|
offscreenWidth float64
|
|
|
|
offscreenHeight float64
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-12-20 03:34:35 +01:00
|
|
|
isOffscreenModified bool
|
2022-10-28 12:07:33 +02:00
|
|
|
|
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{
|
2023-10-24 16:15:29 +02:00
|
|
|
game: game,
|
2022-02-13 09:23:52 +01:00
|
|
|
}
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (c *context) updateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface, swapBuffersForGL func()) error {
|
2022-02-13 09:23:52 +01:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
2023-07-29 19:25:10 +02:00
|
|
|
return c.updateFrameImpl(graphicsDriver, clock.UpdateFrame(), outsideWidth, outsideHeight, deviceScaleFactor, ui, false, swapBuffersForGL)
|
2022-02-13 08:30:33 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (c *context) forceUpdateFrame(graphicsDriver graphicsdriver.Graphics, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface, swapBuffersForGL func()) error {
|
2022-02-11 12:38:45 +01:00
|
|
|
n := 1
|
2023-10-15 10:21:56 +02:00
|
|
|
if ui.GraphicsLibrary() == GraphicsLibraryDirectX {
|
2022-02-11 12:38:45 +01:00
|
|
|
// 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++ {
|
2023-07-29 19:25:10 +02:00
|
|
|
if err := c.updateFrameImpl(graphicsDriver, 1, outsideWidth, outsideHeight, deviceScaleFactor, ui, true, swapBuffersForGL); err != nil {
|
2022-02-11 12:38:45 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-13 10:12:28 +01:00
|
|
|
}
|
|
|
|
|
2023-10-14 19:51:23 +02:00
|
|
|
func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, updateCount int, outsideWidth, outsideHeight float64, deviceScaleFactor float64, ui *UserInterface, forceDraw bool, swapBuffersForGL func()) (err error) {
|
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")
|
|
|
|
|
2023-10-23 17:18:26 +02:00
|
|
|
if err := atlas.BeginFrame(graphicsDriver); err != nil {
|
2022-02-13 10:12:28 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-04-25 18:12:52 +02:00
|
|
|
|
2023-10-23 19:31:05 +02:00
|
|
|
defer func() {
|
|
|
|
if err1 := atlas.EndFrame(); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err1 := atlas.SwapBuffers(graphicsDriver, swapBuffersForGL); err1 != nil && err == nil {
|
|
|
|
err = err1
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-25 18:12:52 +02:00
|
|
|
// 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
|
|
|
|
2023-09-17 07:22:58 +02:00
|
|
|
// Update the input state after the layout is updated as a cursor position is affected by the layout.
|
|
|
|
if err := ui.updateInputState(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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++ {
|
2022-12-20 01:52:13 +01:00
|
|
|
// Read the input state and use it for one tick to give a consistent result for one tick (#2496, #2501).
|
2023-01-21 16:04:30 +01:00
|
|
|
c.game.UpdateInputState(func(inputState *InputState) {
|
|
|
|
ui.readInputState(inputState)
|
|
|
|
})
|
2022-12-21 03:46:55 +01:00
|
|
|
|
2023-10-06 06:49:42 +02:00
|
|
|
if err := hook.RunBeforeUpdateHooks(); err != nil {
|
2022-12-21 03:46:55 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.game.Update(); err != nil {
|
2022-02-13 12:02:49 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-12-20 01:52:13 +01:00
|
|
|
|
2022-03-04 17:44:59 +01:00
|
|
|
// Catch the error that happened at (*Image).At.
|
2023-10-15 08:50:39 +02:00
|
|
|
if err := ui.error(); err != nil {
|
2022-03-04 17:44:59 +01:00
|
|
|
return err
|
|
|
|
}
|
2022-02-13 12:02:49 +01:00
|
|
|
}
|
|
|
|
|
2022-12-29 15:48:53 +01:00
|
|
|
// Update window icons during a frame, since an icon might be *ebiten.Image and
|
|
|
|
// getting pixels from it needs to be in a frame (#1468).
|
2023-04-19 17:56:08 +02:00
|
|
|
if err := ui.updateIconIfNeeded(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-12-29 15:48:53 +01:00
|
|
|
|
2022-02-13 12:02:49 +01:00
|
|
|
// Draw the game.
|
2023-10-15 08:50:39 +02:00
|
|
|
if err := c.drawGame(graphicsDriver, ui, 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-12-29 06:41:54 +01:00
|
|
|
func (c *context) newOffscreenImage(w, h int) *Image {
|
|
|
|
img := c.game.NewOffscreenImage(w, h)
|
|
|
|
img.modifyCallback = func() {
|
|
|
|
c.isOffscreenModified = true
|
|
|
|
}
|
|
|
|
return img
|
|
|
|
}
|
|
|
|
|
2023-10-15 08:50:39 +02:00
|
|
|
func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics, ui *UserInterface, forceDraw bool) error {
|
|
|
|
if (c.offscreen.imageType == atlas.ImageTypeVolatile) != ui.IsScreenClearedEveryFrame() {
|
2022-06-07 16:16:40 +02:00
|
|
|
w, h := c.offscreen.width, c.offscreen.height
|
2023-11-03 07:06:22 +01:00
|
|
|
c.offscreen.Deallocate()
|
2022-12-29 06:41:54 +01:00
|
|
|
c.offscreen = c.newOffscreenImage(w, h)
|
2022-06-07 16:16:40 +02:00
|
|
|
}
|
2022-04-01 11:16:03 +02:00
|
|
|
|
2022-12-20 03:34:35 +01:00
|
|
|
// isOffscreenModified is updated when an offscreen's modifyCallback.
|
|
|
|
c.isOffscreenModified = false
|
|
|
|
|
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.
|
2023-10-15 08:50:39 +02:00
|
|
|
if ui.IsScreenClearedEveryFrame() {
|
2022-04-01 11:16:03 +02:00
|
|
|
c.offscreen.clear()
|
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
|
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-12-29 06:41:54 +01:00
|
|
|
const maxSkipCount = 3
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2022-12-20 03:34:35 +01:00
|
|
|
if !forceDraw && !c.isOffscreenModified {
|
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
|
|
|
}
|
|
|
|
|
2023-01-03 14:58:14 +01:00
|
|
|
if c.skipCount < maxSkipCount {
|
2023-01-02 17:24:53 +01:00
|
|
|
if graphicsDriver.NeedsClearingScreen() {
|
|
|
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
|
|
|
c.screen.clear()
|
|
|
|
}
|
2022-10-23 18:51:37 +02:00
|
|
|
|
2023-01-02 17:24:53 +01:00
|
|
|
c.game.DrawFinalScreen(c.screenScaleAndOffsets())
|
2022-10-14 18:59:44 +02:00
|
|
|
|
2023-01-02 17:24:53 +01: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-11-08 17:07:03 +01:00
|
|
|
owf, ohf := c.game.Layout(outsideWidth, outsideHeight)
|
|
|
|
if owf <= 0 || ohf <= 0 {
|
2022-04-01 11:16:03 +02:00
|
|
|
panic("ui: Layout must return positive numbers")
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:07:03 +01:00
|
|
|
c.screenWidth = outsideWidth * deviceScaleFactor
|
|
|
|
c.screenHeight = outsideHeight * deviceScaleFactor
|
|
|
|
c.offscreenWidth = owf
|
|
|
|
c.offscreenHeight = ohf
|
|
|
|
|
|
|
|
sw := int(math.Ceil(c.screenWidth))
|
|
|
|
sh := int(math.Ceil(c.screenHeight))
|
|
|
|
ow := int(math.Ceil(c.offscreenWidth))
|
|
|
|
oh := int(math.Ceil(c.offscreenHeight))
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
if c.screen != nil && (c.screen.width != sw || c.screen.height != sh) {
|
|
|
|
c.screen.Deallocate()
|
|
|
|
c.screen = nil
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-03 07:06:22 +01:00
|
|
|
if c.offscreen != nil && (c.offscreen.width != ow || c.offscreen.height != oh) {
|
|
|
|
c.offscreen.Deallocate()
|
|
|
|
c.offscreen = nil
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
if c.offscreen == nil {
|
2022-12-29 06:41:54 +01:00
|
|
|
c.offscreen = c.newOffscreenImage(ow, oh)
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ow, oh
|
2022-02-13 11:50:19 +01:00
|
|
|
}
|
|
|
|
|
2022-12-27 04:52:07 +01:00
|
|
|
func (c *context) clientPositionToLogicalPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
2022-11-08 17:07:03 +01:00
|
|
|
s, ox, oy := c.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
|
|
|
|
}
|
2022-11-08 17:07:03 +01:00
|
|
|
|
2022-12-25 07:29:04 +01:00
|
|
|
func (c *context) logicalPositionToClientPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
|
|
|
s, ox, oy := c.screenScaleAndOffsets()
|
|
|
|
return (x*s + ox) / deviceScaleFactor, (y*s + oy) / deviceScaleFactor
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:07:03 +01:00
|
|
|
func (c *context) screenScaleAndOffsets() (scale, offsetX, offsetY float64) {
|
|
|
|
scaleX := c.screenWidth / c.offscreenWidth
|
|
|
|
scaleY := c.screenHeight / c.offscreenHeight
|
|
|
|
scale = math.Min(scaleX, scaleY)
|
|
|
|
width := c.offscreenWidth * scale
|
|
|
|
height := c.offscreenHeight * scale
|
|
|
|
offsetX = (c.screenWidth - width) / 2
|
|
|
|
offsetY = (c.screenHeight - height) / 2
|
|
|
|
return
|
|
|
|
}
|
2022-12-25 07:29:04 +01:00
|
|
|
|
2023-10-14 17:06:07 +02:00
|
|
|
func (u *UserInterface) LogicalPositionToClientPosition(x, y float64) (float64, float64) {
|
|
|
|
return u.context.logicalPositionToClientPosition(x, y, u.DeviceScaleFactor())
|
2022-12-25 07:29:04 +01:00
|
|
|
}
|