ebiten/graphicscontext.go

157 lines
4.0 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 (
2018-01-06 15:30:11 +01:00
"github.com/hajimehoshi/ebiten/internal/clock"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/hooks"
"github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/ui"
2017-10-04 19:41:18 +02:00
"github.com/hajimehoshi/ebiten/internal/web"
2016-05-19 16:37:58 +02:00
)
2016-05-18 03:46:23 +02:00
func newGraphicsContext(f func(*Image) error) *graphicsContext {
return &graphicsContext{
f: f,
}
2014-12-07 20:22:50 +01:00
}
type graphicsContext struct {
f func(*Image) error
offscreen *Image
screen *Image
initialized bool
invalidated bool // browser only
offsetX float64
offsetY float64
}
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
}
func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) {
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()
}
2018-03-05 02:53:42 +01:00
c.offscreen = newVolatileImage(screenWidth, screenHeight, FilterDefault)
2016-07-02 18:08:47 +02:00
w := int(float64(screenWidth) * screenScale)
h := int(float64(screenHeight) * screenScale)
px0, py0, _, _ := ui.ScreenPadding()
c.screen = newImageWithScreenFramebuffer(w, 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 *graphicsContext) initializeIfNeeded() error {
if !c.initialized {
2017-09-14 17:24:18 +02:00
if err := restorable.InitializeGLState(); err != nil {
2016-05-19 16:37:58 +02:00
return err
}
c.initialized = true
2016-05-19 16:37:58 +02:00
}
if err := c.restoreIfNeeded(); err != nil {
return err
}
return nil
}
2018-01-06 15:30:11 +01:00
func (c *graphicsContext) Update(afterFrameUpdate func()) error {
updateCount := clock.Update()
if err := c.initializeIfNeeded(); err != nil {
return err
}
2016-07-03 09:18:29 +02:00
for i := 0; i < updateCount; i++ {
c.offscreen.fill(0, 0, 0, 0)
2016-07-03 09:18:29 +02:00
setRunningSlowly(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
}
afterFrameUpdate()
2016-02-27 18:25:53 +01:00
}
// Clear the screen framebuffer by DrawImage instad of Fill
// to clear the whole region including fullscreen's padding.
// TODO: This clear is needed only when the screen size is changed.
if c.offsetX > 0 || c.offsetY > 0 {
op := &DrawImageOptions{}
w, h := emptyImage.Size()
s := float64(graphics.MaxImageSize())
op.GeoM.Scale(s/float64(w), s/float64(h))
op.CompositeMode = CompositeModeCopy
c.screen.DrawImage(emptyImage, op)
}
dw, dh := c.screen.Size()
sw, _ := c.offscreen.Size()
scale := float64(dw) / float64(sw)
op := &DrawImageOptions{}
// c.screen is special: its Y axis is down to up,
// and the origin point is lower left.
op.GeoM.Scale(scale, -scale)
op.GeoM.Translate(0, float64(dh))
op.GeoM.Translate(c.offsetX, c.offsetY)
op.CompositeMode = CompositeModeCopy
op.Filter = filterScreen
_ = c.screen.DrawImage(c.offscreen, op)
2017-08-06 16:00:24 +02:00
2017-09-14 17:24:18 +02:00
if err := restorable.ResolveStaleImages(); err != nil {
return err
}
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) {
if web.IsBrowser() {
return c.invalidated, nil
}
2018-03-10 16:05:06 +01:00
return c.offscreen.shareableImage.IsInvalidated()
2017-10-04 19:41:18 +02:00
}
func (c *graphicsContext) restoreIfNeeded() error {
if !restorable.IsRestoringEnabled() {
return nil
}
r, err := c.needsRestoring()
if err != nil {
return err
}
if !r {
return nil
}
if err := restorable.Restore(); err != nil {
2016-05-23 17:00:54 +02:00
return err
}
c.invalidated = false
2016-05-23 17:00:54 +02:00
return nil
}