ebiten/uicontext.go

162 lines
4.1 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/atomic"
2018-01-06 15:30:11 +01:00
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/driver"
"github.com/hajimehoshi/ebiten/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/shareable"
"github.com/hajimehoshi/ebiten/internal/uidriver"
2016-05-19 16:37:58 +02:00
)
func init() {
shareable.SetGraphicsDriver(graphicsdriver.Get())
graphicscommand.SetGraphicsDriver(graphicsdriver.Get())
}
func newUIContext(f func(*Image) error) *uiContext {
return &uiContext{
2016-05-18 03:46:23 +02:00
f: f,
}
2014-12-07 20:22:50 +01:00
}
type uiContext struct {
f func(*Image) error
offscreen *Image
screen *Image
screenWidth int
screenHeight int
screenScale float64
initialized bool
offsetX float64
offsetY float64
}
func (c *uiContext) SetSize(screenWidth, screenHeight int, screenScale float64) {
c.screenScale = screenScale
2016-05-18 03:46:23 +02:00
if c.screen != nil {
_ = c.screen.Dispose()
2016-05-18 03:46:23 +02:00
}
if c.offscreen != nil {
_ = c.offscreen.Dispose()
}
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)
w := int(math.Ceil(float64(screenWidth) * screenScale))
h := int(math.Ceil(float64(screenHeight) * screenScale))
px0, py0, px1, py1 := uidriver.Get().ScreenPadding()
c.screen = newScreenFramebufferImage(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
c.offsetX = px0
c.offsetY = py0
2016-05-18 03:46:23 +02:00
}
func (c *uiContext) initializeIfNeeded() error {
if !c.initialized {
if err := shareable.InitializeGraphicsDriverState(); err != nil {
2016-05-19 16:37:58 +02:00
return err
}
c.initialized = true
2016-05-19 16:37:58 +02:00
}
return nil
}
func (c *uiContext) Update(afterFrameUpdate func()) error {
2018-07-17 15:41:27 +02:00
tps := int(MaxTPS())
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.
if err := c.initializeIfNeeded(); err != nil {
return err
}
if err := shareable.BeginFrame(); err != nil {
return err
}
// Images are available after shareable is initialized.
atomic.StoreInt32(&isImageAvailable, 1)
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()
// Mipmap images should be disposed by fill.
setDrawingSkipped(i < updateCount-1)
if err := hooks.RunBeforeUpdateHooks(); err != nil {
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
uidriver.Get().Input().ResetForFrame()
afterFrameUpdate()
2016-02-27 18:25:53 +01:00
}
// This clear is needed for fullscreen mode or some mobile platforms (#622).
c.screen.mipmap.clearFramebuffer()
op := &DrawImageOptions{}
switch vd := graphicsdriver.Get().VDirection(); vd {
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)
op.GeoM.Translate(0, float64(c.screenHeight))
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)
op.CompositeMode = CompositeModeCopy
// 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
}
_ = c.screen.DrawImage(c.offscreen, op)
2017-08-06 16:00:24 +02:00
if err := shareable.EndFrame(); err != nil {
2016-05-23 17:00:54 +02:00
return err
}
return nil
}
func (c *uiContext) SuspendAudio() {
hooks.SuspendAudio()
}
func (c *uiContext) ResumeAudio() {
hooks.ResumeAudio()
}