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
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/buffered"
|
2018-01-06 15:30:11 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/clock"
|
2019-03-30 14:26:27 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
2018-02-04 09:33:17 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/hooks"
|
2016-05-19 16:37:58 +02:00
|
|
|
)
|
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
type defaultGame struct {
|
2019-12-15 17:44:55 +01:00
|
|
|
update func(screen *Image) error
|
|
|
|
width int
|
|
|
|
height int
|
|
|
|
context *uiContext
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (d *defaultGame) Update(screen *Image) error {
|
|
|
|
return d.update(screen)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (d *defaultGame) Layout(outsideWidth, outsideHeight int) (screenWidth, screenHeight int) {
|
|
|
|
// Ignore the outside size.
|
2019-12-15 17:44:55 +01:00
|
|
|
d.context.m.Lock()
|
|
|
|
w, h := d.width, d.height
|
|
|
|
d.context.m.Unlock()
|
|
|
|
return w, h
|
2019-12-15 16:58:12 +01: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
|
|
|
|
|
|
|
|
// scaleForWindow is the scale of a window. This doesn't represent the scale on fullscreen. This value works
|
|
|
|
// only on desktops.
|
|
|
|
//
|
|
|
|
// scaleForWindow is for backward compatibility and is used to calculate the window size when SetScreenSize
|
|
|
|
// is called.
|
2019-12-09 18:37:10 +01:00
|
|
|
scaleForWindow float64
|
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
outsideSizeUpdated bool
|
|
|
|
outsideWidth float64
|
|
|
|
outsideHeight float64
|
|
|
|
|
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{}
|
|
|
|
|
|
|
|
func (c *uiContext) set(game Game, scaleForWindow float64) {
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
c.game = game
|
|
|
|
|
|
|
|
if g, ok := game.(*defaultGame); ok {
|
|
|
|
c.scaleForWindow = scaleForWindow
|
|
|
|
g.context = c
|
|
|
|
}
|
|
|
|
}
|
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) setScaleForWindow(scale float64) {
|
2019-12-22 10:50:41 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
if c.game == nil {
|
|
|
|
panic("ebiten: setScaleForWindow can be called only after the main loop starts")
|
|
|
|
}
|
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
g, ok := c.game.(*defaultGame)
|
|
|
|
if !ok {
|
2019-12-16 02:52:53 +01:00
|
|
|
panic("ebiten: setScaleForWindow can be called only when Run is used")
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
2019-12-15 17:44:55 +01:00
|
|
|
|
2019-12-24 16:05:56 +01:00
|
|
|
if w := uiDriver().Window(); w != nil {
|
|
|
|
ww, wh := g.width, g.height
|
|
|
|
c.scaleForWindow = scale
|
|
|
|
w.SetSize(int(float64(ww)*scale), int(float64(wh)*scale))
|
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uiContext) getScaleForWindow() float64 {
|
2019-12-22 10:50:41 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
if c.game == nil {
|
|
|
|
panic("ebiten: getScaleForWindow can be called only after the main loop starts")
|
|
|
|
}
|
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
if _, ok := c.game.(*defaultGame); !ok {
|
2019-12-16 02:52:53 +01:00
|
|
|
panic("ebiten: getScaleForWindow can be called only when Run is used")
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
2019-12-15 11:53:33 +01:00
|
|
|
s := c.scaleForWindow
|
|
|
|
return s
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
2019-09-21 07:53:52 +02:00
|
|
|
|
2020-02-11 09:53:50 +01:00
|
|
|
// setScreenSize sets the (logical) screen size and adjusts the window size.
|
2019-12-19 19:56:17 +01:00
|
|
|
//
|
2020-02-11 09:53:50 +01:00
|
|
|
// setScreenSize is for backward compatibility. This is called from ebiten.SetScreenSize.
|
|
|
|
func (c *uiContext) setScreenSize(width, height int) {
|
2019-12-22 10:50:41 +01:00
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
if c.game == nil {
|
|
|
|
panic("ebiten: SetScreenSize can be called only after the main loop starts")
|
|
|
|
}
|
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
g, ok := c.game.(*defaultGame)
|
|
|
|
if !ok {
|
2019-12-16 02:52:53 +01:00
|
|
|
panic("ebiten: SetScreenSize can be called only when Run is used")
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
2019-12-15 17:44:55 +01:00
|
|
|
|
|
|
|
g.width = width
|
|
|
|
g.height = height
|
2019-12-24 16:05:56 +01:00
|
|
|
if w := uiDriver().Window(); w != nil {
|
|
|
|
s := c.scaleForWindow
|
|
|
|
w.SetSize(int(float64(width)*s), int(float64(height)*s))
|
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uiContext) Layout(outsideWidth, outsideHeight float64) {
|
2019-12-15 16:58:12 +01:00
|
|
|
c.outsideSizeUpdated = true
|
|
|
|
c.outsideWidth = outsideWidth
|
|
|
|
c.outsideHeight = outsideHeight
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uiContext) updateOffscreen() {
|
|
|
|
sw, sh := c.game.Layout(int(c.outsideWidth), int(c.outsideHeight))
|
2019-12-22 20:43:04 +01:00
|
|
|
if sw <= 0 || sh <= 0 {
|
|
|
|
panic("ebiten: Layout must return positive numbers")
|
|
|
|
}
|
2019-12-15 16:58:12 +01:00
|
|
|
|
|
|
|
if c.offscreen != nil && !c.outsideSizeUpdated {
|
|
|
|
if w, h := c.offscreen.Size(); w == sw && h == sh {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
c.outsideSizeUpdated = false
|
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
if c.screen != nil {
|
|
|
|
_ = c.screen.Dispose()
|
|
|
|
c.screen = nil
|
|
|
|
}
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
if c.offscreen != nil {
|
|
|
|
if w, h := c.offscreen.Size(); w != sw || h != sh {
|
|
|
|
_ = c.offscreen.Dispose()
|
|
|
|
c.offscreen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.offscreen == nil {
|
|
|
|
c.offscreen = newImage(sw, sh, FilterDefault, true)
|
|
|
|
}
|
2019-12-16 02:52:53 +01:00
|
|
|
|
|
|
|
// The window size is automatically adjusted when Run is used.
|
|
|
|
if _, ok := c.game.(*defaultGame); ok {
|
2020-02-11 09:53:50 +01:00
|
|
|
c.setScreenSize(sw, sh)
|
2019-12-16 02:52:53 +01:00
|
|
|
}
|
2019-12-15 16:58:12 +01:00
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
// TODO: This is duplicated with mobile/ebitenmobileview/funcs.go. Refactor this.
|
|
|
|
d := uiDriver().DeviceScaleFactor()
|
2019-12-15 16:58:12 +01:00
|
|
|
c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d))
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2019-12-19 19:56:17 +01:00
|
|
|
// Do not have to update scaleForWindow since this is used only for backward compatibility.
|
|
|
|
// Then, if a window is resizable, scaleForWindow (= ebiten.ScreenScale) might not match with the actual
|
|
|
|
// scale. This is fine since ebiten.ScreenScale will be deprecated.
|
2019-12-15 18:10:58 +01:00
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2019-12-22 10:08:02 +01:00
|
|
|
func (c *uiContext) setWindowResizable(resizable bool) {
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
if resizable && c.game != nil {
|
|
|
|
if _, ok := c.game.(*defaultGame); ok {
|
|
|
|
panic("ebiten: a resizable window works with RunGame, not Run")
|
|
|
|
}
|
|
|
|
}
|
2019-12-24 16:05:56 +01:00
|
|
|
if w := uiDriver().Window(); w != nil {
|
|
|
|
w.SetResizable(resizable)
|
|
|
|
}
|
2019-12-22 10:08:02 +01:00
|
|
|
}
|
|
|
|
|
2019-12-15 18:10:58 +01:00
|
|
|
func (c *uiContext) screenScale() float64 {
|
|
|
|
if c.offscreen == nil {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
sw, sh := c.offscreen.Size()
|
|
|
|
d := uiDriver().DeviceScaleFactor()
|
|
|
|
scaleX := c.outsideWidth / float64(sw) * d
|
|
|
|
scaleY := c.outsideHeight / float64(sh) * d
|
|
|
|
return math.Min(scaleX, scaleY)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uiContext) offsets() (float64, float64) {
|
|
|
|
if c.offscreen == nil {
|
|
|
|
return 0, 0
|
|
|
|
}
|
|
|
|
sw, sh := c.offscreen.Size()
|
|
|
|
d := uiDriver().DeviceScaleFactor()
|
|
|
|
s := c.screenScale()
|
|
|
|
width := float64(sw) * s
|
|
|
|
height := float64(sh) * s
|
|
|
|
return (c.outsideWidth*d - width) / 2, (c.outsideHeight*d - height) / 2
|
2016-05-18 03:46:23 +02:00
|
|
|
}
|
|
|
|
|
2020-04-02 17:06:42 +02:00
|
|
|
func (c *uiContext) Update() error {
|
2018-07-17 20:05:09 +02:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
|
|
|
|
2020-01-18 17:18:56 +01:00
|
|
|
if err, ok := c.err.Load().(error); ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-09-19 05:02:35 +02:00
|
|
|
if err := buffered.BeginFrame(); err != nil {
|
2019-08-12 17:18:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2020-04-02 17:06:42 +02:00
|
|
|
if err := c.update(); err != nil {
|
2019-12-29 15:14:57 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := buffered.EndFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-03-31 19:55:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-24 17:14:37 +02:00
|
|
|
|
2020-03-31 19:55:48 +02:00
|
|
|
func (c *uiContext) Draw() error {
|
|
|
|
if err, ok := c.err.Load().(error); ok && err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := buffered.BeginFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.draw()
|
|
|
|
if err := buffered.EndFrame(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-12-29 15:14:57 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-04-02 17:06:42 +02:00
|
|
|
func (c *uiContext) update() error {
|
2020-03-31 20:02:32 +02:00
|
|
|
_, hasDraw := c.game.(interface{ Draw(*Image) })
|
|
|
|
|
2019-12-29 15:14:57 +01:00
|
|
|
updateCount := clock.Update(MaxTPS())
|
2020-03-31 20:02:32 +02:00
|
|
|
for i := 0; i < updateCount; i++ {
|
|
|
|
c.updateOffscreen()
|
|
|
|
|
2020-03-31 20:38:32 +02:00
|
|
|
// When the game's Draw exists, rendering should be always skipped.
|
|
|
|
//
|
|
|
|
// When the game's Draw does not exist, the last Update call should process drawing.
|
|
|
|
// This assumes that (*uiContext).Update and (*uiContext).Draw are called successively.
|
|
|
|
//
|
|
|
|
// TODO: Make (Game).Draw mandatory and remove this assumption when we can update the major version.
|
|
|
|
// Move the clock usage to the UI driver side.
|
2020-03-31 20:02:32 +02:00
|
|
|
setDrawingSkipped(hasDraw || i < updateCount-1)
|
2020-03-24 04:01:37 +01:00
|
|
|
|
2020-03-31 20:02:32 +02:00
|
|
|
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
|
|
|
return err
|
2020-03-24 04:01:37 +01:00
|
|
|
}
|
2020-03-31 20:02:32 +02:00
|
|
|
|
|
|
|
// Multiple successive Clear call should be integrated into one graphics command, then
|
|
|
|
// calling Clear on every Update should not affect the performance.
|
|
|
|
c.offscreen.Clear()
|
|
|
|
if err := c.game.Update(c.offscreen); err != nil {
|
|
|
|
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
|
|
|
}
|
2020-03-31 19:55:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2020-03-31 19:55:48 +02:00
|
|
|
func (c *uiContext) draw() {
|
2019-12-29 15:14:57 +01:00
|
|
|
// c.screen might be nil when updateCount is 0 in the initial state (#1039).
|
|
|
|
if c.screen == nil {
|
2020-03-31 19:55:48 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if game, ok := c.game.(interface{ Draw(*Image) }); ok {
|
|
|
|
c.offscreen.Clear()
|
|
|
|
game.Draw(c.offscreen)
|
2019-12-29 15:14:57 +01:00
|
|
|
}
|
|
|
|
|
2019-01-17 03:37:15 +01:00
|
|
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
2019-12-01 18:26:03 +01:00
|
|
|
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
|
|
|
|
2019-12-15 18:10:58 +01:00
|
|
|
s := c.screenScale()
|
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
|
|
|
}
|
|
|
|
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Translate(c.offsets())
|
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
|
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
_ = c.screen.DrawImage(c.offscreen, op)
|
2016-05-23 17:00:54 +02:00
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
|
|
|
func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) {
|
|
|
|
d := uiDriver().DeviceScaleFactor()
|
2019-12-15 18:10:58 +01:00
|
|
|
ox, oy := c.offsets()
|
|
|
|
s := c.screenScale()
|
|
|
|
return (x*d - ox) / s, (y*d - oy) / s
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|