ebiten/graphicscontext.go

152 lines
3.7 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 (
"math"
"github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/ui"
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
offscreen2 *Image // TODO: better name
screen *Image
2016-06-18 19:59:17 +02:00
screenScale float64
initialized bool
invalidated bool // browser only
}
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()
}
2016-07-19 19:09:09 +02:00
if c.offscreen2 != nil {
_ = c.offscreen2.Dispose()
2016-05-18 03:46:23 +02:00
}
offscreen := newVolatileImage(screenWidth, screenHeight, FilterNearest)
2016-07-02 18:08:47 +02:00
intScreenScale := int(math.Ceil(screenScale))
w := screenWidth * intScreenScale
h := screenHeight * intScreenScale
offscreen2 := newVolatileImage(w, h, FilterLinear)
2016-07-02 18:08:47 +02:00
w = int(float64(screenWidth) * screenScale)
h = int(float64(screenHeight) * screenScale)
ox, oy := ui.ScreenOffset()
c.screen = newImageWithScreenFramebuffer(w, h, ox, oy)
_ = c.screen.Clear()
2016-07-02 18:08:47 +02:00
c.offscreen = offscreen
c.offscreen2 = offscreen2
2016-05-18 03:46:23 +02:00
c.screenScale = screenScale
}
func (c *graphicsContext) initializeIfNeeded() error {
if !c.initialized {
if err := restorable.Reset(); 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
}
func drawWithFittingScale(dst *Image, src *Image) {
wd, hd := dst.Size()
ws, hs := src.Size()
sw := float64(wd) / float64(ws)
sh := float64(hd) / float64(hs)
op := &DrawImageOptions{}
op.GeoM.Scale(sw, sh)
_ = dst.DrawImage(src, op)
}
func (c *graphicsContext) drawToDefaultRenderTarget() error {
_ = c.screen.Clear()
drawWithFittingScale(c.screen, c.offscreen2)
if err := restorable.FlushCommands(); err != nil {
2016-06-13 19:10:43 +02:00
return err
}
return nil
}
2017-08-05 13:14:49 +02:00
func (c *graphicsContext) Update(updateCount int) error {
if err := c.initializeIfNeeded(); err != nil {
return err
}
2016-07-03 09:18:29 +02:00
for i := 0; i < updateCount; i++ {
2017-05-03 17:08:07 +02:00
restorable.ClearVolatileImages()
2016-07-03 09:18:29 +02:00
setRunningSlowly(i < updateCount-1)
if err := c.f(c.offscreen); err != nil {
return err
}
2016-02-27 18:25:53 +01:00
}
if 0 < updateCount {
drawWithFittingScale(c.offscreen2, c.offscreen)
}
if err := c.drawToDefaultRenderTarget(); err != nil {
return err
}
// TODO: Add tests to check if this behavior is correct (#357)
if err := restorable.ResolveStalePixels(); err != nil {
return err
}
return nil
2014-12-07 16:07:36 +01:00
}
2016-05-23 17:00:54 +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.Reset(); err != nil {
2016-05-23 17:00:54 +02:00
return err
}
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
}