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 (
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2016-06-12 16:54:36 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
2016-05-19 16:37:58 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
|
|
|
)
|
|
|
|
|
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
|
|
|
|
screen *Image
|
|
|
|
screenScale int
|
|
|
|
initialized bool
|
2014-12-07 16:07:36 +01:00
|
|
|
}
|
2014-01-11 03:18:55 +01:00
|
|
|
|
2016-05-18 03:46:23 +02:00
|
|
|
func (c *graphicsContext) SetSize(screenWidth, screenHeight, screenScale int) error {
|
|
|
|
if c.screen != nil {
|
|
|
|
c.screen.Dispose()
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
if c.offscreen != nil {
|
|
|
|
c.offscreen.Dispose()
|
|
|
|
}
|
|
|
|
offscreen, err := NewImage(screenWidth, screenHeight, FilterNearest)
|
2016-05-18 03:46:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
c.screen, err = newImageWithScreenFramebuffer(screenWidth*screenScale, screenHeight*screenScale)
|
2016-05-18 03:46:23 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
c.screen.Clear()
|
|
|
|
c.offscreen = offscreen
|
2016-05-18 03:46:23 +02:00
|
|
|
c.screenScale = screenScale
|
2016-06-17 23:25:40 +02:00
|
|
|
ui.GLContext().ResetViewportSize()
|
2016-05-18 03:46:23 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-12 16:54:36 +02:00
|
|
|
func (c *graphicsContext) needsRestoring(context *opengl.Context) (bool, error) {
|
|
|
|
imageM.Lock()
|
|
|
|
defer imageM.Unlock()
|
2016-06-17 21:46:33 +02:00
|
|
|
// FlushCommands is required because c.offscreen.impl might not have an actual texture.
|
2016-06-12 16:54:36 +02:00
|
|
|
if err := graphics.FlushCommands(ui.GLContext()); err != nil {
|
|
|
|
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-06-13 17:55:25 +02:00
|
|
|
func (c *graphicsContext) initializeIfNeeded() error {
|
2016-06-21 19:08:58 +02:00
|
|
|
// glViewport must be called at every frame on iOS
|
|
|
|
ui.GLContext().ResetViewportSize()
|
2016-06-11 17:32:35 +02:00
|
|
|
if !c.initialized {
|
2016-05-19 16:37:58 +02:00
|
|
|
if err := graphics.Initialize(ui.GLContext()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-11 17:32:35 +02:00
|
|
|
c.initialized = true
|
2016-05-19 16:37:58 +02:00
|
|
|
}
|
2016-06-12 16:54:36 +02:00
|
|
|
r, err := c.needsRestoring(ui.GLContext())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r {
|
|
|
|
if err := c.restore(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-06-13 17:55:25 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-13 19:10:43 +02:00
|
|
|
func (c *graphicsContext) drawToDefaultRenderTarget() 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
|
|
|
|
}
|
|
|
|
scale := float64(c.screenScale)
|
|
|
|
options := &DrawImageOptions{}
|
|
|
|
options.GeoM.Scale(scale, scale)
|
2016-06-17 21:46:33 +02:00
|
|
|
if err := c.screen.DrawImage(c.offscreen, options); err != nil {
|
2016-06-13 19:10:43 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := c.flush(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-13 17:55:25 +02:00
|
|
|
func (c *graphicsContext) UpdateAndDraw() error {
|
|
|
|
if err := c.initializeIfNeeded(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
if err := c.offscreen.Clear(); err != nil {
|
2016-02-27 15:39:06 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-06-17 21:46:33 +02:00
|
|
|
if err := c.f(c.offscreen); err != nil {
|
2016-02-27 15:39:06 +01:00
|
|
|
return err
|
|
|
|
}
|
2016-02-27 18:25:53 +01:00
|
|
|
if IsRunningSlowly() {
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-13 19:10:43 +02:00
|
|
|
if err := c.drawToDefaultRenderTarget(); err != nil {
|
2016-06-11 14:50:13 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-06-12 10:04:20 +02:00
|
|
|
exceptions := map[*imageImpl]struct{}{
|
2016-06-17 21:46:33 +02:00
|
|
|
c.offscreen.impl: {},
|
|
|
|
c.screen.impl: {},
|
2016-06-12 10:04:20 +02:00
|
|
|
}
|
|
|
|
if err := theImages.savePixels(ui.GLContext(), exceptions); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-11 14:50:13 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-13 17:30:16 +02:00
|
|
|
func (c *graphicsContext) Draw() error {
|
2016-06-13 17:55:25 +02:00
|
|
|
if err := c.initializeIfNeeded(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-13 19:10:43 +02:00
|
|
|
if err := c.drawToDefaultRenderTarget(); err != nil {
|
2016-06-13 17:30:16 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-11 14:50:13 +02:00
|
|
|
func (c *graphicsContext) flush() error {
|
|
|
|
// TODO: imageM is necessary to call graphics functions. Move this to graphics package.
|
|
|
|
imageM.Lock()
|
|
|
|
defer imageM.Unlock()
|
|
|
|
if err := graphics.FlushCommands(ui.GLContext()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-06 20:02:15 +02:00
|
|
|
// Call glFlush to prevent black flicking (especially on Android (#226)).
|
|
|
|
ui.GLContext().Flush()
|
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-06-12 16:54:36 +02:00
|
|
|
func (c *graphicsContext) restore() error {
|
2016-06-12 13:57:02 +02:00
|
|
|
imageM.Lock()
|
|
|
|
defer imageM.Unlock()
|
2016-06-09 18:19:10 +02:00
|
|
|
ui.GLContext().Resume()
|
2016-05-23 17:00:54 +02:00
|
|
|
if err := graphics.Initialize(ui.GLContext()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-06-10 18:20:55 +02:00
|
|
|
if err := theImages.restorePixels(ui.GLContext()); err != nil {
|
2016-05-23 17:00:54 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|