ebiten/uicontext.go

237 lines
5.9 KiB
Go
Raw Normal View History

// 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
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"
"math"
"sync"
"sync/atomic"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/buffered"
"github.com/hajimehoshi/ebiten/v2/internal/clock"
"github.com/hajimehoshi/ebiten/v2/internal/debug"
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/hooks"
2016-05-19 16:37:58 +02:00
)
type uiContext struct {
2019-12-19 19:56:17 +01:00
game Game
offscreen *Image
screen *Image
updateCalled bool
outsideSizeUpdated bool
outsideWidth float64
outsideHeight float64
err atomic.Value
m sync.Mutex
}
var theUIContext = &uiContext{}
func (c *uiContext) set(game Game) {
c.m.Lock()
defer c.m.Unlock()
c.game = game
}
func (c *uiContext) setError(err error) {
c.err.Store(err)
}
func (c *uiContext) Layout(outsideWidth, outsideHeight float64) {
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 sw <= 0 || sh <= 0 {
panic("ebiten: Layout must return positive numbers")
}
if c.offscreen != nil && !c.outsideSizeUpdated {
if w, h := c.offscreen.Size(); w == sw && h == sh {
return
}
}
c.outsideSizeUpdated = false
if c.screen != nil {
c.screen.Dispose()
c.screen = nil
}
2016-07-02 18:08:47 +02: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)
c.offscreen.mipmap.SetVolatile(IsScreenClearedEveryFrame())
}
// TODO: This is duplicated with mobile/ebitenmobileview/funcs.go. Refactor this.
d := uiDriver().DeviceScaleFactor()
c.screen = newScreenFramebufferImage(int(c.outsideWidth*d), int(c.outsideHeight*d))
2019-12-15 18:10:58 +01:00
}
func (c *uiContext) setScreenClearedEveryFrame(cleared bool) {
c.m.Lock()
defer c.m.Unlock()
if c.offscreen != nil {
c.offscreen.mipmap.SetVolatile(cleared)
}
}
func (c *uiContext) setWindowResizable(resizable bool) {
c.m.Lock()
defer c.m.Unlock()
2019-12-24 16:05:56 +01:00
if w := uiDriver().Window(); w != nil {
w.SetResizable(resizable)
}
}
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()
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)
}
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()
s := c.screenScale(deviceScaleFactor)
2019-12-15 18:10:58 +01:00
width := float64(sw) * s
height := float64(sh) * s
x := (c.outsideWidth*deviceScaleFactor - width) / 2
y := (c.outsideHeight*deviceScaleFactor - height) / 2
return x, y
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.
if err, ok := c.err.Load().(error); ok && err != nil {
return err
}
if err := buffered.BeginFrame(); err != nil {
return err
}
if err := c.update(clock.Update(MaxTPS())); err != nil {
return err
}
if err := buffered.EndFrame(); err != nil {
return err
}
return nil
}
func (c *uiContext) ForceUpdate() error {
if err, ok := c.err.Load().(error); ok && err != nil {
return err
}
if err := buffered.BeginFrame(); err != nil {
return err
}
if err := c.update(1); err != nil {
return err
}
if err := buffered.EndFrame(); err != nil {
return err
}
return nil
}
func (c *uiContext) update(updateCount int) error {
c.updateOffscreen()
// 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("--\nUpdate count per frame: %d\n", updateCount)
2020-03-31 20:02:32 +02:00
for i := 0; i < updateCount; i++ {
if err := hooks.RunBeforeUpdateHooks(); err != nil {
return err
}
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
}
// 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 IsScreenClearedEveryFrame() {
c.offscreen.Clear()
}
c.game.Draw(c.offscreen)
// This clear is needed for fullscreen mode or some mobile platforms (#622).
c.screen.Clear()
op := &DrawImageOptions{}
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)
_, 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
}
op.GeoM.Translate(c.offsets(uiDriver().DeviceScaleFactor()))
op.CompositeMode = CompositeModeCopy
// 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 {
op.Filter = filterScreen
} else {
op.Filter = FilterLinear
}
c.screen.DrawImage(c.offscreen, op)
return nil
2016-05-23 17:00:54 +02:00
}
func (c *uiContext) AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
ox, oy := c.offsets(deviceScaleFactor)
s := c.screenScale(deviceScaleFactor)
return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s
}