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"
|
|
|
|
|
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"
|
2019-03-31 11:47:06 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/input"
|
2018-03-25 16:37:32 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shareable"
|
2017-06-30 21:12:09 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
2016-05-19 16:37:58 +02:00
|
|
|
)
|
|
|
|
|
2019-03-30 14:13:48 +01:00
|
|
|
func init() {
|
|
|
|
graphicscommand.SetGraphicsDriver(graphicsDriver())
|
|
|
|
}
|
|
|
|
|
2016-05-18 03:46:23 +02:00
|
|
|
func newGraphicsContext(f func(*Image) error) *graphicsContext {
|
|
|
|
return &graphicsContext{
|
|
|
|
f: f,
|
2014-12-19 21:22:10 +01:00
|
|
|
}
|
2014-12-07 20:22:50 +01:00
|
|
|
}
|
|
|
|
|
2014-12-14 07:26:10 +01:00
|
|
|
type graphicsContext struct {
|
2018-09-29 14:47:39 +02:00
|
|
|
f func(*Image) error
|
|
|
|
offscreen *Image
|
|
|
|
screen *Image
|
|
|
|
screenWidth int
|
|
|
|
screenHeight int
|
|
|
|
screenScale float64
|
|
|
|
initialized bool
|
|
|
|
invalidated bool // browser only
|
|
|
|
offsetX float64
|
|
|
|
offsetY float64
|
2016-07-23 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:37:08 +01:00
|
|
|
func (c *graphicsContext) Invalidate() {
|
|
|
|
// Note that this is called only on browsers so far.
|
|
|
|
// TODO: On mobiles, this function is not called and instead IsTexture is called
|
|
|
|
// to detect if the context is lost. This is simple but might not work on some platforms.
|
|
|
|
// Should Invalidate be called explicitly?
|
|
|
|
c.invalidated = true
|
|
|
|
}
|
|
|
|
|
2017-05-31 03:47:52 +02:00
|
|
|
func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) {
|
2018-06-07 17:06:57 +02:00
|
|
|
c.screenScale = screenScale
|
|
|
|
|
2016-05-18 03:46:23 +02:00
|
|
|
if c.screen != nil {
|
2017-05-31 03:47:52 +02:00
|
|
|
_ = c.screen.Dispose()
|
2016-05-18 03:46:23 +02:00
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
if c.offscreen != nil {
|
2017-05-31 03:47:52 +02:00
|
|
|
_ = c.offscreen.Dispose()
|
2016-06-17 21:46:33 +02:00
|
|
|
}
|
2019-02-12 07:17:07 +01:00
|
|
|
c.offscreen, _ = NewImage(screenWidth, screenHeight, FilterDefault)
|
|
|
|
c.offscreen.makeVolatile()
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2019-01-24 19:04:25 +01:00
|
|
|
// Round up the screensize not to cause glitches e.g. on Xperia (#622)
|
2019-01-16 15:41:49 +01:00
|
|
|
w := int(math.Ceil(float64(screenWidth) * screenScale))
|
|
|
|
h := int(math.Ceil(float64(screenHeight) * screenScale))
|
2018-09-29 14:47:39 +02:00
|
|
|
px0, py0, px1, py1 := ui.ScreenPadding()
|
|
|
|
c.screen = newImageWithScreenFramebuffer(w+int(math.Ceil(px0+px1)), h+int(math.Ceil(py0+py1)))
|
|
|
|
c.screenWidth = w
|
|
|
|
c.screenHeight = h
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2018-02-24 15:32:36 +01:00
|
|
|
c.offsetX = px0
|
|
|
|
c.offsetY = py0
|
2016-05-18 03:46:23 +02:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
func (c *graphicsContext) initializeIfNeeded() error {
|
|
|
|
if !c.initialized {
|
2018-10-31 19:02:08 +01:00
|
|
|
if err := shareable.InitializeGraphicsDriverState(); err != nil {
|
2016-05-19 16:37:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-05-30 19:09:27 +02:00
|
|
|
c.initialized = true
|
2016-05-19 16:37:58 +02:00
|
|
|
}
|
2017-07-02 08:58:00 +02:00
|
|
|
if err := c.restoreIfNeeded(); err != nil {
|
2016-07-12 19:07:35 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-06-13 17:55:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-01-06 15:30:11 +01:00
|
|
|
func (c *graphicsContext) Update(afterFrameUpdate func()) error {
|
2018-07-17 15:41:27 +02:00
|
|
|
tps := int(MaxTPS())
|
2018-07-17 15:33:53 +02:00
|
|
|
updateCount := clock.Update(tps)
|
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.
|
|
|
|
|
2017-05-30 19:09:27 +02:00
|
|
|
if err := c.initializeIfNeeded(); err != nil {
|
2016-06-13 17:55:25 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-07-03 09:18:29 +02:00
|
|
|
for i := 0; i < updateCount; i++ {
|
2019-01-21 18:07:20 +01:00
|
|
|
c.offscreen.Clear()
|
2018-07-29 17:25:51 +02:00
|
|
|
// Mipmap images should be disposed by fill.
|
2018-03-01 17:55:32 +01:00
|
|
|
|
2018-07-09 17:36:43 +02:00
|
|
|
setDrawingSkipped(i < updateCount-1)
|
2018-03-14 18:50:10 +01:00
|
|
|
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
2018-02-04 09:33:17 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-07-03 09:18:29 +02:00
|
|
|
if err := c.f(c.offscreen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-03-31 11:47:06 +02:00
|
|
|
|
|
|
|
input.Get().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-21 18:07:20 +01:00
|
|
|
// Before clearing the screen, the offscreen's pixels must be resolved.
|
2019-01-21 17:44:47 +01:00
|
|
|
// After clearing the screen, resolving doesn't work. This is very hacky
|
|
|
|
// but we could not find other way so far (#792).
|
|
|
|
c.offscreen.resolvePixelsToSet(true)
|
|
|
|
|
2019-01-17 03:37:15 +01:00
|
|
|
// 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
|
|
|
|
2019-03-30 14:13:48 +01: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.
|
|
|
|
op.GeoM.Scale(c.screenScale, -c.screenScale)
|
2019-01-16 15:50:09 +01:00
|
|
|
op.GeoM.Translate(0, float64(c.screenHeight))
|
2019-03-30 14:26:27 +01:00
|
|
|
case driver.VUpward:
|
2018-11-12 16:00:10 +01:00
|
|
|
op.GeoM.Scale(c.screenScale, c.screenScale)
|
|
|
|
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.offsetX, c.offsetY)
|
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).
|
|
|
|
if c.screenScale >= 1 {
|
|
|
|
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
|
|
|
|
2018-07-11 19:11:18 +02:00
|
|
|
shareable.ResolveStaleImages()
|
2018-07-11 20:16:29 +02:00
|
|
|
|
|
|
|
if err := shareable.Error(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-12-20 12:54:14 +01:00
|
|
|
return nil
|
2014-12-07 16:07:36 +01:00
|
|
|
}
|
2016-05-23 17:00:54 +02:00
|
|
|
|
2017-10-04 19:41:18 +02:00
|
|
|
func (c *graphicsContext) needsRestoring() (bool, error) {
|
2018-10-24 19:11:54 +02:00
|
|
|
return c.offscreen.mipmap.original().IsInvalidated()
|
2017-10-04 19:41:18 +02:00
|
|
|
}
|
|
|
|
|
2017-07-02 08:58:00 +02:00
|
|
|
func (c *graphicsContext) restoreIfNeeded() error {
|
2018-03-25 16:37:32 +02:00
|
|
|
if !shareable.IsRestoringEnabled() {
|
2017-07-02 08:58:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
r, err := c.needsRestoring()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !r {
|
|
|
|
return nil
|
|
|
|
}
|
2018-03-25 16:37:32 +02:00
|
|
|
if err := shareable.Restore(); err != nil {
|
2016-05-23 17:00:54 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-01-21 17:37:08 +01:00
|
|
|
c.invalidated = false
|
2016-05-23 17:00:54 +02:00
|
|
|
return nil
|
|
|
|
}
|