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 (
|
2016-06-24 18:24:57 +02:00
|
|
|
"math"
|
2016-07-23 23:02:04 +02:00
|
|
|
"sync/atomic"
|
2016-06-24 18:24:57 +02:00
|
|
|
|
2016-05-19 16:37:58 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2016-06-12 16:54:36 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
2016-07-23 23:02:04 +02:00
|
|
|
"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-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 {
|
2016-06-17 21:46:33 +02:00
|
|
|
f func(*Image) error
|
|
|
|
offscreen *Image
|
2016-06-24 18:24:57 +02:00
|
|
|
offscreen2 *Image // TODO: better name
|
2016-06-17 21:46:33 +02:00
|
|
|
screen *Image
|
2016-06-18 19:59:17 +02:00
|
|
|
screenScale float64
|
2016-07-23 23:02:04 +02:00
|
|
|
initialized int32
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *graphicsContext) GLContext() *opengl.Context {
|
|
|
|
if atomic.LoadInt32(&c.initialized) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return ui.GLContext()
|
2014-12-07 16:07:36 +01:00
|
|
|
}
|
2014-01-11 03:18:55 +01:00
|
|
|
|
2016-06-18 19:59:17 +02:00
|
|
|
func (c *graphicsContext) SetSize(screenWidth, screenHeight int, screenScale float64) error {
|
2016-05-18 03:46:23 +02:00
|
|
|
if c.screen != nil {
|
|
|
|
c.screen.Dispose()
|
|
|
|
}
|
2016-06-17 21:46:33 +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-07-11 18:58:04 +02:00
|
|
|
offscreen, err := newVolatileImage(screenWidth, screenHeight, FilterNearest)
|
2016-05-18 03:46:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2016-06-24 21:15:22 +02:00
|
|
|
intScreenScale := int(math.Ceil(screenScale))
|
2016-06-24 18:24:57 +02:00
|
|
|
w := screenWidth * intScreenScale
|
|
|
|
h := screenHeight * intScreenScale
|
2016-07-11 18:58:04 +02:00
|
|
|
offscreen2, err := newVolatileImage(w, h, FilterLinear)
|
2016-06-24 18:24:57 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2016-06-24 18:24:57 +02:00
|
|
|
w = int(float64(screenWidth) * screenScale)
|
|
|
|
h = int(float64(screenHeight) * screenScale)
|
2016-06-18 19:59:17 +02:00
|
|
|
c.screen, err = newImageWithScreenFramebuffer(w, h)
|
2016-05-18 03:46:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
c.screen.Clear()
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2016-06-17 21:46:33 +02:00
|
|
|
c.offscreen = offscreen
|
2016-06-24 18:24:57 +02:00
|
|
|
c.offscreen2 = offscreen2
|
2016-05-18 03:46:23 +02:00
|
|
|
c.screenScale = screenScale
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:54:36 +02:00
|
|
|
func (c *graphicsContext) needsRestoring(context *opengl.Context) (bool, error) {
|
2016-06-17 21:46:33 +02:00
|
|
|
// FlushCommands is required because c.offscreen.impl might not have an actual texture.
|
2016-07-03 08:41:04 +02:00
|
|
|
if err := graphics.FlushCommands(context); err != nil {
|
2016-06-12 16:54:36 +02:00
|
|
|
return false, err
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
return c.offscreen.impl.isInvalidated(context), nil
|
2016-06-12 16:54:36 +02:00
|
|
|
}
|
|
|
|
|
2016-07-03 08:41:04 +02:00
|
|
|
func (c *graphicsContext) initializeIfNeeded(context *opengl.Context) error {
|
2016-07-23 23:02:04 +02:00
|
|
|
if atomic.LoadInt32(&c.initialized) == 0 {
|
2016-07-03 17:30:14 +02:00
|
|
|
if err := graphics.Reset(context); err != nil {
|
2016-05-19 16:37:58 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-07-23 23:02:04 +02:00
|
|
|
atomic.StoreInt32(&c.initialized, 1)
|
2016-05-19 16:37:58 +02:00
|
|
|
}
|
2016-07-03 08:41:04 +02:00
|
|
|
r, err := c.needsRestoring(context)
|
2016-06-12 16:54:36 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-12 19:07:35 +02:00
|
|
|
if !r {
|
|
|
|
return nil
|
2016-06-12 16:54:36 +02:00
|
|
|
}
|
2016-07-12 19:07:35 +02:00
|
|
|
if err := c.restore(context); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-13 17:55:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-24 18:24:57 +02:00
|
|
|
func drawWithFittingScale(dst *Image, src *Image) error {
|
|
|
|
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)
|
|
|
|
if err := dst.DrawImage(src, op); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-03 08:41:04 +02:00
|
|
|
func (c *graphicsContext) drawToDefaultRenderTarget(context *opengl.Context) error {
|
2016-06-17 21:46:33 +02:00
|
|
|
if err := c.screen.Clear(); err != nil {
|
2016-06-13 19:10:43 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-06-24 18:24:57 +02:00
|
|
|
if err := drawWithFittingScale(c.screen, c.offscreen2); err != nil {
|
2016-06-13 19:10:43 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-07-03 11:56:32 +02:00
|
|
|
if err := graphics.FlushCommands(context); err != nil {
|
2016-06-13 19:10:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-07-03 10:27:33 +02:00
|
|
|
func (c *graphicsContext) UpdateAndDraw(context *opengl.Context, updateCount int) error {
|
|
|
|
if err := c.initializeIfNeeded(context); err != nil {
|
2016-06-13 17:55:25 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-07-27 15:49:23 +02:00
|
|
|
if err := theImagesForRestoring.resolveStalePixels(context); err != nil {
|
2016-07-26 03:35:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-07-03 09:18:29 +02:00
|
|
|
for i := 0; i < updateCount; i++ {
|
2016-07-05 18:30:49 +02:00
|
|
|
if err := theImagesForRestoring.clearVolatileImages(); err != nil {
|
2016-07-03 09:18:29 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
setRunningSlowly(i < updateCount-1)
|
|
|
|
if err := c.f(c.offscreen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-27 18:25:53 +01:00
|
|
|
}
|
2016-07-03 11:56:32 +02:00
|
|
|
if 0 < updateCount {
|
|
|
|
if err := drawWithFittingScale(c.offscreen2, c.offscreen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-13 17:55:25 +02:00
|
|
|
}
|
2016-07-03 10:27:33 +02:00
|
|
|
if err := c.drawToDefaultRenderTarget(context); err != nil {
|
2016-06-13 17:30:16 +02:00
|
|
|
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
|
|
|
|
2016-07-03 08:41:04 +02:00
|
|
|
func (c *graphicsContext) restore(context *opengl.Context) error {
|
2016-07-03 17:30:14 +02:00
|
|
|
if err := graphics.Reset(context); err != nil {
|
2016-05-23 17:00:54 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-07-04 20:01:32 +02:00
|
|
|
if err := theImagesForRestoring.restore(context); err != nil {
|
2016-05-23 17:00:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|