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"
|
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"
|
2019-03-30 14:13:48 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
|
2018-02-04 09:33:17 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/hooks"
|
2018-03-25 16:37:32 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shareable"
|
2016-05-19 16:37:58 +02:00
|
|
|
)
|
|
|
|
|
2019-03-30 14:13:48 +01:00
|
|
|
func init() {
|
2019-09-02 18:42:53 +02:00
|
|
|
shareable.SetGraphicsDriver(graphicsDriver())
|
|
|
|
graphicscommand.SetGraphicsDriver(graphicsDriver())
|
2019-03-30 14:13:48 +01: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-12-16 02:57:57 +01:00
|
|
|
func newUIContext(game Game, scaleForWindow float64) *uiContext {
|
2019-12-15 17:44:55 +01:00
|
|
|
u := &uiContext{
|
2019-12-15 16:58:12 +01:00
|
|
|
game: game,
|
2019-12-09 18:37:10 +01:00
|
|
|
scaleForWindow: scaleForWindow,
|
2014-12-19 21:22:10 +01:00
|
|
|
}
|
2019-12-15 17:44:55 +01:00
|
|
|
if g, ok := game.(*defaultGame); ok {
|
|
|
|
g.context = u
|
|
|
|
}
|
|
|
|
return u
|
2014-12-07 20:22:50 +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
|
|
|
|
|
|
|
|
m sync.Mutex
|
2016-07-23 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
var theUIContext *uiContext
|
2018-06-07 17:06:57 +02:00
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
func (c *uiContext) setScaleForWindow(scale float64) {
|
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-15 11:53:33 +01:00
|
|
|
c.m.Lock()
|
2019-12-15 17:44:55 +01:00
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
w, h := g.width, g.height
|
2019-12-09 18:37:10 +01:00
|
|
|
c.scaleForWindow = scale
|
2019-12-15 11:53:33 +01:00
|
|
|
uiDriver().SetWindowSize(int(float64(w)*scale), int(float64(h)*scale))
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *uiContext) getScaleForWindow() float64 {
|
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 17:44:55 +01:00
|
|
|
|
2019-12-15 11:53:33 +01:00
|
|
|
c.m.Lock()
|
|
|
|
s := c.scaleForWindow
|
|
|
|
c.m.Unlock()
|
|
|
|
return s
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
2019-09-21 07:53:52 +02:00
|
|
|
|
2019-12-19 19:56:17 +01:00
|
|
|
// SetScreenSize sets the (logical) screen size and adjusts the window size.
|
|
|
|
//
|
|
|
|
// SetScreenSize is for backward compatibility. This is called from ebiten.SetScreenSize and
|
|
|
|
// uidriver/mobile.UserInterface.
|
2019-12-09 18:37:10 +01:00
|
|
|
func (c *uiContext) SetScreenSize(width, height int) {
|
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
|
|
|
|
|
|
|
c.m.Lock()
|
|
|
|
defer c.m.Unlock()
|
|
|
|
|
|
|
|
g.width = width
|
|
|
|
g.height = height
|
2019-12-15 11:53:33 +01:00
|
|
|
s := c.scaleForWindow
|
|
|
|
uiDriver().SetWindowSize(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))
|
|
|
|
|
|
|
|
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 {
|
|
|
|
c.SetScreenSize(sw, sh)
|
|
|
|
}
|
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-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
|
|
|
}
|
|
|
|
|
2019-04-09 05:21:37 +02:00
|
|
|
func (c *uiContext) Update(afterFrameUpdate func()) error {
|
2019-11-24 18:26:12 +01:00
|
|
|
updateCount := clock.Update(MaxTPS())
|
2018-01-06 15:30:11 +01:00
|
|
|
|
2018-07-17 20:05:09 +02:00
|
|
|
// TODO: If updateCount is 0 and vsync is disabled, swapping buffers can be skipped.
|
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
if err := buffered.BeginFrame(); err != nil {
|
2019-08-12 17:18:49 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-08-24 17:14:37 +02:00
|
|
|
|
2016-07-03 09:18:29 +02:00
|
|
|
for i := 0; i < updateCount; i++ {
|
2019-12-15 16:58:12 +01:00
|
|
|
c.updateOffscreen()
|
|
|
|
|
2019-12-09 18:37:10 +01:00
|
|
|
// Mipmap images should be disposed by Clear.
|
2019-01-21 18:07:20 +01:00
|
|
|
c.offscreen.Clear()
|
2018-03-01 17:55:32 +01:00
|
|
|
|
2018-07-09 17:36:43 +02:00
|
|
|
setDrawingSkipped(i < updateCount-1)
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2018-03-14 18:50:10 +01:00
|
|
|
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
2018-02-04 09:33:17 +01:00
|
|
|
return err
|
|
|
|
}
|
2019-12-15 16:58:12 +01:00
|
|
|
if err := c.game.Update(c.offscreen); err != nil {
|
2016-07-03 09:18:29 +02:00
|
|
|
return err
|
|
|
|
}
|
2019-09-02 18:42:53 +02:00
|
|
|
uiDriver().Input().ResetForFrame()
|
2017-11-09 17:18:41 +01:00
|
|
|
afterFrameUpdate()
|
2016-02-27 18:25:53 +01:00
|
|
|
}
|
2018-03-01 15:05:10 +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()
|
2019-09-02 18:42:53 +02:00
|
|
|
switch vd := graphicsDriver().VDirection(); vd {
|
2019-03-30 14:26:27 +01:00
|
|
|
case driver.VDownward:
|
2018-11-12 16:00:10 +01:00
|
|
|
// c.screen is special: its Y axis is down to up,
|
|
|
|
// and the origin point is lower left.
|
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)
|
2019-03-30 14:26:27 +01:00
|
|
|
case driver.VUpward:
|
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)
|
2017-08-06 16:00:24 +02:00
|
|
|
|
2019-09-19 05:02:35 +02:00
|
|
|
if err := buffered.EndFrame(); err != nil {
|
2016-05-23 17:00:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
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
|
|
|
}
|