2014-12-24 03:04:10 +01:00
|
|
|
// Copyright 2014 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// 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.
|
2014-12-09 15:16:04 +01:00
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
package ebiten
|
2013-12-09 14:40:54 +01:00
|
|
|
|
2016-05-19 16:37:58 +02:00
|
|
|
import (
|
2019-02-07 09:19:24 +01:00
|
|
|
"fmt"
|
2018-09-29 14:47:39 +02:00
|
|
|
"math"
|
2019-12-09 18:37:10 +01:00
|
|
|
"sync"
|
2020-01-18 17:18:56 +01:00
|
|
|
"sync/atomic"
|
2018-09-29 14:47:39 +02:00
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/buffered"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/clock"
|
2020-11-24 14:22:45 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/debug"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/driver"
|
2021-06-26 19:21:51 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
|
2016-05-19 16:37:58 +02:00
|
|
|
)
|
|
|
|
|
2019-04-09 05:21:37 +02:00
|
|
|
type uiContext struct {
|
2019-12-19 19:56:17 +01:00
|
|
|
game Game
|
|
|
|
offscreen *Image
|
|
|
|
screen *Image
|
|
|
|
|
2020-05-14 21:04:29 +02:00
|
|
|
updateCalled bool
|
|
|
|
|
2021-11-27 10:21:04 +01:00
|
|
|
outsideWidth float64
|
|
|
|
outsideHeight float64
|
2019-12-15 16:58:12 +01:00
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
err atomic.Value
|
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
m sync.Mutex
|
2016-07-23 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
2019-12-22 10:50:41 +01:00
|
|
|
var theUIContext = &uiContext{}
|
|
|
|
|
2020-10-14 19:27:42 +02:00
|
|
|
func (c *uiContext) set(game Game) {
|
2019-12-22 10:50:41 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
c.game = game
|
|
|
|
}
|
2018-06-07 17:06:57 +02:00
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
func (c *uiContext) setError(err error) {
|
|
|
|
c.err.Store(err)
|
|
|
|
}
|
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
func (c *uiContext) Layout(outsideWidth, outsideHeight float64) {
|
2021-04-18 16:02:29 +02: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
|
|
|
|
}
|
2019-12-15 16:58:12 +01:00
|
|
|
c.outsideWidth = outsideWidth
|
|
|
|
c.outsideHeight = outsideHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uiContext) updateOffscreen() {
|
2021-11-27 10:19:06 +01:00
|
|
|
ow, oh := c.game.Layout(int(c.outsideWidth), int(c.outsideHeight))
|
|
|
|
if ow <= 0 || oh <= 0 {
|
2019-12-22 20:43:04 +01:00
|
|
|
panic("ebiten: Layout must return positive numbers")
|
|
|
|
}
|
2019-12-15 16:58:12 +01:00
|
|
|
|
2021-11-27 10:19:06 +01:00
|
|
|
// TODO: This is duplicated with mobile/ebitenmobileview/funcs.go. Refactor this.
|
|
|
|
d := uiDriver().DeviceScaleFactor()
|
|
|
|
sw, sh := int(c.outsideWidth*d), int(c.outsideHeight*d)
|
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
if c.screen != nil {
|
2021-11-27 10:19:06 +01:00
|
|
|
if w, h := c.screen.Size(); w != sw || h != sh {
|
|
|
|
c.screen.Dispose()
|
|
|
|
c.screen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.screen == nil {
|
|
|
|
c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d))
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
if c.offscreen != nil {
|
2021-11-27 10:19:06 +01:00
|
|
|
if w, h := c.offscreen.Size(); w != ow || h != oh {
|
2020-10-05 17:13:19 +02:00
|
|
|
c.offscreen.Dispose()
|
2019-12-15 16:58:12 +01:00
|
|
|
c.offscreen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.offscreen == nil {
|
2021-11-27 10:19:06 +01:00
|
|
|
c.offscreen = NewImage(ow, oh)
|
2020-08-20 10:12:22 +02:00
|
|
|
c.offscreen.mipmap.SetVolatile(IsScreenClearedEveryFrame())
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
2019-12-15 18:10:58 +01:00
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2020-08-20 10:12:22 +02:00
|
|
|
func (c *uiContext) setScreenClearedEveryFrame(cleared bool) {
|
2020-08-18 18:23:27 +02:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
if c.offscreen != nil {
|
2020-08-20 10:12:22 +02:00
|
|
|
c.offscreen.mipmap.SetVolatile(cleared)
|
2020-08-18 18:23:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
func (c *uiContext) screenScale(deviceScaleFactor float64) float64 {
|
2019-12-15 18:10:58 +01:00
|
|
|
if c.offscreen == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
sw, sh := c.offscreen.Size()
|
2020-10-16 22:16:02 +02:00
|
|
|
scaleX := c.outsideWidth / float64(sw) * deviceScaleFactor
|
|
|
|
scaleY := c.outsideHeight / float64(sh) * deviceScaleFactor
|
2019-12-15 18:10:58 +01:00
|
|
|
return math.Min(scaleX, scaleY)
|
|
|
|
}
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
func (c *uiContext) offsets(deviceScaleFactor float64) (float64, float64) {
|
2019-12-15 18:10:58 +01:00
|
|
|
if c.offscreen == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
sw, sh := c.offscreen.Size()
|
2020-10-16 22:16:02 +02:00
|
|
|
s := c.screenScale(deviceScaleFactor)
|
2019-12-15 18:10:58 +01:00
|
|
|
width := float64(sw) * s
|
|
|
|
height := float64(sh) * s
|
2020-10-16 22:16:02 +02:00
|
|
|
x := (c.outsideWidth*deviceScaleFactor - width) / 2
|
|
|
|
y := (c.outsideHeight*deviceScaleFactor - height) / 2
|
|
|
|
return x, y
|
2016-05-18 03:46:23 +02:00
|
|
|
}
|
|
|
|
|
2021-08-04 18:29:45 +02:00
|
|
|
func (c *uiContext) UpdateFrame() error {
|
2018-07-17 20:05:09 +02:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
2021-08-04 18:29:45 +02:00
|
|
|
return c.updateFrame(clock.Update(MaxTPS()))
|
2020-03-31 19:55:48 +02:00
|
|
|
}
|
2019-08-24 17:14:37 +02:00
|
|
|
|
2021-08-04 18:29:45 +02:00
|
|
|
func (c *uiContext) ForceUpdateFrame() error {
|
2021-04-18 17:35:14 +02:00
|
|
|
// ForceUpdate can be invoked even if uiContext it not initialized yet (#1591).
|
|
|
|
if c.outsideWidth == 0 || c.outsideHeight == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2021-08-04 18:29:45 +02:00
|
|
|
return c.updateFrame(1)
|
2021-06-26 18:46:35 +02:00
|
|
|
}
|
2021-04-18 17:35:14 +02:00
|
|
|
|
2021-08-04 18:29:45 +02:00
|
|
|
func (c *uiContext) updateFrame(updateCount int) error {
|
2021-01-17 17:17:55 +01:00
|
|
|
if err, ok := c.err.Load().(error); ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-06-26 19:21:51 +02:00
|
|
|
|
2021-08-04 18:15:41 +02:00
|
|
|
debug.Logf("----\n")
|
|
|
|
|
2021-01-17 17:17:55 +01:00
|
|
|
if err := buffered.BeginFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-08-04 18:29:45 +02:00
|
|
|
if err := c.updateFrameImpl(updateCount); err != nil {
|
2021-01-17 17:17:55 +01:00
|
|
|
return err
|
|
|
|
}
|
2021-06-26 19:21:51 +02: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 graphics.LockAndResetVertices(func() error {
|
|
|
|
if err := buffered.EndFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2021-01-17 17:17:55 +01:00
|
|
|
}
|
2020-12-20 13:10:47 +01:00
|
|
|
|
2021-08-04 18:29:45 +02:00
|
|
|
func (c *uiContext) updateFrameImpl(updateCount int) error {
|
2021-01-17 17:17:55 +01:00
|
|
|
c.updateOffscreen()
|
2020-05-14 21:04:29 +02: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
|
|
|
|
}
|
2021-08-04 18:15:41 +02:00
|
|
|
debug.Logf("Update count per frame: %d\n", updateCount)
|
2020-05-14 21:04:29 +02:00
|
|
|
|
2020-03-31 20:02:32 +02:00
|
|
|
for i := 0; i < updateCount; i++ {
|
|
|
|
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
2020-03-24 04:01:37 +01:00
|
|
|
}
|
2020-10-04 10:42:54 +02:00
|
|
|
if err := c.game.Update(); err != nil {
|
2020-03-31 20:02:32 +02:00
|
|
|
return err
|
2016-07-03 09:18:29 +02:00
|
|
|
}
|
2020-04-02 17:06:42 +02:00
|
|
|
uiDriver().ResetForFrame()
|
2016-02-27 18:25:53 +01:00
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2021-04-19 04:33:34 +02:00
|
|
|
// Even though updateCount == 0, the offscreen is cleared and Draw is called.
|
2021-02-09 15:25:12 +01:00
|
|
|
// 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.
|
2021-04-19 04:33:34 +02:00
|
|
|
if IsScreenClearedEveryFrame() {
|
|
|
|
c.offscreen.Clear()
|
2020-10-04 10:16:26 +02:00
|
|
|
}
|
2021-04-19 04:33:34 +02:00
|
|
|
c.game.Draw(c.offscreen)
|
2019-12-29 15:14:57 +01:00
|
|
|
|
2021-07-06 18:43:32 +02:00
|
|
|
if uiDriver().Graphics().NeedsClearingScreen() {
|
|
|
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
|
|
|
c.screen.Clear()
|
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2018-03-04 18:59:52 +01:00
|
|
|
op := &DrawImageOptions{}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
s := c.screenScale(uiDriver().DeviceScaleFactor())
|
2020-05-08 09:49:19 +02:00
|
|
|
switch vd := uiDriver().Graphics().FramebufferYDirection(); vd {
|
|
|
|
case driver.Upward:
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Scale(s, -s)
|
2019-12-15 16:58:12 +01:00
|
|
|
_, h := c.offscreen.Size()
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Translate(0, float64(h)*s)
|
2020-05-08 09:49:19 +02:00
|
|
|
case driver.Downward:
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Scale(s, s)
|
2018-11-12 16:00:10 +01:00
|
|
|
default:
|
2019-02-07 09:19:24 +01:00
|
|
|
panic(fmt.Sprintf("ebiten: invalid v-direction: %d", vd))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
op.GeoM.Translate(c.offsets(uiDriver().DeviceScaleFactor()))
|
2018-03-01 15:05:10 +01:00
|
|
|
op.CompositeMode = CompositeModeCopy
|
2018-08-29 17:17:03 +02:00
|
|
|
|
|
|
|
// filterScreen works with >=1 scale, but does not well with <1 scale.
|
|
|
|
// Use regular FilterLinear instead so far (#669).
|
2019-12-15 18:10:58 +01:00
|
|
|
if s >= 1 {
|
2018-08-29 17:17:03 +02:00
|
|
|
op.Filter = filterScreen
|
|
|
|
} else {
|
|
|
|
op.Filter = FilterLinear
|
|
|
|
}
|
2020-10-05 17:21:11 +02:00
|
|
|
c.screen.DrawImage(c.offscreen, op)
|
2020-10-14 18:23:27 +02:00
|
|
|
return nil
|
2016-05-23 17:00:54 +02:00
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2020-10-16 22:16:02 +02:00
|
|
|
func (c *uiContext) AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
|
|
|
ox, oy := c.offsets(deviceScaleFactor)
|
|
|
|
s := c.screenScale(deviceScaleFactor)
|
2021-03-27 10:40:39 +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()
|
|
|
|
}
|
2020-10-16 22:16:02 +02:00
|
|
|
return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|