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 (
|
2022-10-13 16:13:25 +02:00
|
|
|
"fmt"
|
2022-06-11 16:06:59 +02:00
|
|
|
"image"
|
2022-10-13 16:13:25 +02:00
|
|
|
"math"
|
2024-10-21 04:17:00 +02:00
|
|
|
"sync"
|
2022-10-13 16:13:25 +02:00
|
|
|
"sync/atomic"
|
2022-06-11 16:06:59 +02:00
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2024-04-20 14:21:57 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
|
2022-04-01 11:16:03 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/ui"
|
2016-05-19 16:37:58 +02:00
|
|
|
)
|
|
|
|
|
2024-04-29 12:02:23 +02:00
|
|
|
var screenFilterEnabled atomic.Bool
|
2022-10-13 16:13:25 +02:00
|
|
|
|
2024-04-29 12:02:23 +02:00
|
|
|
func init() {
|
|
|
|
screenFilterEnabled.Store(true)
|
2022-10-13 16:13:25 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
type gameForUI struct {
|
2024-10-21 04:17:00 +02:00
|
|
|
game Game
|
|
|
|
offscreen *Image
|
|
|
|
screen *Image
|
|
|
|
imageDumper imageDumper
|
|
|
|
transparent bool
|
2016-07-23 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
2022-12-09 13:23:41 +01:00
|
|
|
func newGameForUI(game Game, transparent bool) *gameForUI {
|
2022-10-13 16:13:25 +02:00
|
|
|
g := &gameForUI{
|
2022-12-09 13:23:41 +01:00
|
|
|
game: game,
|
|
|
|
transparent: transparent,
|
2022-02-13 12:47:32 +01:00
|
|
|
}
|
2022-10-13 16:13:25 +02:00
|
|
|
return g
|
2019-12-22 10:50:41 +01:00
|
|
|
}
|
2018-06-07 17:06:57 +02:00
|
|
|
|
2022-10-13 17:38:08 +02:00
|
|
|
func (g *gameForUI) NewOffscreenImage(width, height int) *ui.Image {
|
|
|
|
if g.offscreen != nil {
|
2023-11-03 07:06:22 +01:00
|
|
|
g.offscreen.Deallocate()
|
2022-10-13 17:38:08 +02:00
|
|
|
g.offscreen = nil
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
2022-06-07 16:57:56 +02:00
|
|
|
|
2022-06-09 19:11:08 +02:00
|
|
|
// Keep the offscreen an unmanaged image that is always isolated from an atlas (#1938).
|
2022-06-07 16:57:56 +02:00
|
|
|
// The shader program for the screen is special and doesn't work well with an image on an atlas.
|
|
|
|
// An image on an atlas is surrounded by a transparent edge,
|
|
|
|
// and the shader program unexpectedly picks the pixel on the edges.
|
2022-06-09 19:11:08 +02:00
|
|
|
imageType := atlas.ImageTypeUnmanaged
|
2024-09-06 08:23:40 +02:00
|
|
|
if ui.Get().IsScreenClearedEveryFrame() {
|
|
|
|
// A volatile image is also always isolated.
|
|
|
|
imageType = atlas.ImageTypeVolatile
|
|
|
|
}
|
2022-10-13 17:38:08 +02:00
|
|
|
g.offscreen = newImage(image.Rect(0, 0, width, height), imageType)
|
|
|
|
return g.offscreen.image
|
2022-04-01 11:16:03 +02:00
|
|
|
}
|
2022-02-13 10:12:28 +01:00
|
|
|
|
2022-10-13 17:38:08 +02:00
|
|
|
func (g *gameForUI) NewScreenImage(width, height int) *ui.Image {
|
|
|
|
if g.screen != nil {
|
2023-11-03 07:06:22 +01:00
|
|
|
g.screen.Deallocate()
|
2022-10-13 17:38:08 +02:00
|
|
|
g.screen = nil
|
2022-10-13 16:13:25 +02:00
|
|
|
}
|
|
|
|
|
2022-10-13 17:38:08 +02:00
|
|
|
g.screen = newImage(image.Rect(0, 0, width, height), atlas.ImageTypeScreen)
|
|
|
|
return g.screen.image
|
2022-10-13 16:13:25 +02:00
|
|
|
}
|
|
|
|
|
2022-11-08 17:07:03 +01:00
|
|
|
func (g *gameForUI) Layout(outsideWidth, outsideHeight float64) (float64, float64) {
|
2022-11-08 18:00:07 +01:00
|
|
|
if l, ok := g.game.(LayoutFer); ok {
|
2022-11-08 17:49:51 +01:00
|
|
|
return l.LayoutF(outsideWidth, outsideHeight)
|
|
|
|
}
|
|
|
|
|
2022-11-08 17:07:03 +01:00
|
|
|
// Even if the original value is less than 1, the value must be a positive integer (#2340).
|
|
|
|
// This is for a simple implementation of Layout, which returns the argument values without modifications.
|
|
|
|
// TODO: Remove this hack when Game.Layout takes floats instead of integers.
|
|
|
|
if outsideWidth < 1 {
|
|
|
|
outsideWidth = 1
|
|
|
|
}
|
|
|
|
if outsideHeight < 1 {
|
|
|
|
outsideHeight = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add a new Layout function taking float values (#2285).
|
|
|
|
sw, sh := g.game.Layout(int(outsideWidth), int(outsideHeight))
|
|
|
|
return float64(sw), float64(sh)
|
2019-12-15 18:10:58 +01:00
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2023-01-21 16:04:30 +01:00
|
|
|
func (g *gameForUI) UpdateInputState(fn func(*ui.InputState)) {
|
2023-01-21 17:49:43 +01:00
|
|
|
theInputState.update(fn)
|
2022-12-21 03:46:55 +01:00
|
|
|
}
|
2022-12-16 10:34:14 +01:00
|
|
|
|
2022-12-21 03:46:55 +01:00
|
|
|
func (g *gameForUI) Update() error {
|
2022-10-13 19:55:04 +02:00
|
|
|
if err := g.game.Update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := g.imageDumper.update(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2022-02-13 12:02:49 +01:00
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2022-10-13 19:55:04 +02:00
|
|
|
func (g *gameForUI) DrawOffscreen() error {
|
2022-10-13 17:38:08 +02:00
|
|
|
g.game.Draw(g.offscreen)
|
2022-12-09 13:23:41 +01:00
|
|
|
if err := g.imageDumper.dump(g.offscreen, g.transparent); err != nil {
|
2022-10-13 19:55:04 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2016-05-23 17:00:54 +02:00
|
|
|
}
|
2022-10-13 16:13:25 +02:00
|
|
|
|
2022-11-08 17:07:03 +01:00
|
|
|
func (g *gameForUI) DrawFinalScreen(scale, offsetX, offsetY float64) {
|
2022-10-14 16:35:55 +02:00
|
|
|
var geoM GeoM
|
|
|
|
geoM.Scale(scale, scale)
|
|
|
|
geoM.Translate(offsetX, offsetY)
|
|
|
|
|
2022-10-13 19:22:40 +02:00
|
|
|
if d, ok := g.game.(FinalScreenDrawer); ok {
|
2022-10-14 16:35:55 +02:00
|
|
|
d.DrawFinalScreen(g.screen, g.offscreen, geoM)
|
2022-10-13 19:22:40 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-21 04:17:00 +02:00
|
|
|
DefaultDrawFinalsScreen(g.screen, g.offscreen, geoM)
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
theScreenShader *Shader
|
|
|
|
theScreenShaderOnce sync.Once
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultDrawFinalsScreen is the default implementation of [FinalScreenDrawer.DrawFinalScreen],
|
|
|
|
// used when a [Game] doesn't implement [FinalScreenDrawer].
|
|
|
|
//
|
2024-10-21 04:19:27 +02:00
|
|
|
// You can use DefaultDrawFinalsScreen when you need the default implementation of [FinalScreenDrawer.DrawFinalScreen]
|
2024-10-21 04:17:00 +02:00
|
|
|
// in your implementation of [FinalScreenDrawer], for example.
|
|
|
|
func DefaultDrawFinalsScreen(screen *Image, offscreen *Image, geoM GeoM) {
|
|
|
|
theScreenShaderOnce.Do(func() {
|
|
|
|
s, err := newShader(builtinshader.ScreenShaderSource, "screen")
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
|
|
|
|
}
|
|
|
|
theScreenShader = s
|
|
|
|
})
|
|
|
|
|
|
|
|
scale := geoM.Element(0, 0)
|
2022-10-13 16:13:25 +02:00
|
|
|
switch {
|
2024-04-29 12:02:23 +02:00
|
|
|
case !screenFilterEnabled.Load(), math.Floor(scale) == scale:
|
2022-10-13 16:13:25 +02:00
|
|
|
op := &DrawImageOptions{}
|
2022-10-14 16:35:55 +02:00
|
|
|
op.GeoM = geoM
|
2024-10-21 04:17:00 +02:00
|
|
|
screen.DrawImage(offscreen, op)
|
2022-10-13 16:13:25 +02:00
|
|
|
case scale < 1:
|
|
|
|
op := &DrawImageOptions{}
|
2022-10-14 16:35:55 +02:00
|
|
|
op.GeoM = geoM
|
2022-10-13 16:13:25 +02:00
|
|
|
op.Filter = FilterLinear
|
2024-10-21 04:17:00 +02:00
|
|
|
screen.DrawImage(offscreen, op)
|
2022-10-13 16:13:25 +02:00
|
|
|
default:
|
|
|
|
op := &DrawRectShaderOptions{}
|
2024-10-21 04:17:00 +02:00
|
|
|
op.Images[0] = offscreen
|
2022-10-14 16:35:55 +02:00
|
|
|
op.GeoM = geoM
|
2024-10-21 04:17:00 +02:00
|
|
|
w, h := offscreen.Bounds().Dx(), offscreen.Bounds().Dy()
|
|
|
|
screen.DrawRectShader(w, h, theScreenShader, op)
|
2022-10-13 16:13:25 +02:00
|
|
|
}
|
|
|
|
}
|