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 (
|
2019-02-07 09:19:24 +01:00
|
|
|
"fmt"
|
2022-02-22 18:52:15 +01:00
|
|
|
"math"
|
2018-09-29 14:47:39 +02:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2016-05-19 16:37:58 +02:00
|
|
|
)
|
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
type gameForUI struct {
|
2019-12-19 19:56:17 +01:00
|
|
|
game Game
|
|
|
|
offscreen *Image
|
|
|
|
screen *Image
|
2016-07-23 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
2022-02-13 12:47:32 +01:00
|
|
|
func newGameForUI(game Game) *gameForUI {
|
|
|
|
return &gameForUI{
|
|
|
|
game: game,
|
|
|
|
}
|
2019-12-22 10:50:41 +01:00
|
|
|
}
|
2018-06-07 17:06:57 +02:00
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
func (c *gameForUI) Layout(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
2022-02-13 10:12:28 +01:00
|
|
|
ow, oh := c.game.Layout(int(outsideWidth), int(outsideHeight))
|
2021-11-27 10:19:06 +01:00
|
|
|
if ow <= 0 || oh <= 0 {
|
2019-12-22 20:43:04 +01:00
|
|
|
panic("ebiten: Layout must return positive numbers")
|
|
|
|
}
|
2019-12-15 16:58:12 +01:00
|
|
|
|
2022-02-13 11:40:03 +01:00
|
|
|
sw, sh := int(outsideWidth*deviceScaleFactor), int(outsideHeight*deviceScaleFactor)
|
2019-12-09 18:37:10 +01:00
|
|
|
if c.screen != nil {
|
2021-11-27 10:19:06 +01:00
|
|
|
if w, h := c.screen.Size(); w != sw || h != sh {
|
|
|
|
c.screen.Dispose()
|
|
|
|
c.screen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.screen == nil {
|
2022-02-13 11:40:03 +01:00
|
|
|
c.screen = newScreenFramebufferImage(sw, sh)
|
2019-12-09 18:37:10 +01:00
|
|
|
}
|
2016-07-02 18:08:47 +02:00
|
|
|
|
2019-12-15 16:58:12 +01:00
|
|
|
if c.offscreen != nil {
|
2021-11-27 10:19:06 +01:00
|
|
|
if w, h := c.offscreen.Size(); w != ow || h != oh {
|
2020-10-05 17:13:19 +02:00
|
|
|
c.offscreen.Dispose()
|
2019-12-15 16:58:12 +01:00
|
|
|
c.offscreen = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if c.offscreen == nil {
|
2021-11-27 10:19:06 +01:00
|
|
|
c.offscreen = NewImage(ow, oh)
|
2022-01-08 15:21:38 +01:00
|
|
|
|
|
|
|
// Keep the offscreen an independent image from an atlas (#1938).
|
|
|
|
// 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.
|
|
|
|
c.offscreen.mipmap.SetIndependent(true)
|
2019-12-15 16:58:12 +01:00
|
|
|
}
|
2022-02-13 10:12:28 +01:00
|
|
|
|
|
|
|
return ow, oh
|
2019-12-15 18:10:58 +01:00
|
|
|
}
|
2019-12-09 18:37:10 +01:00
|
|
|
|
2022-02-13 12:18:41 +01:00
|
|
|
func (c *gameForUI) Update() error {
|
2022-02-13 12:02:49 +01:00
|
|
|
return c.game.Update()
|
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2022-02-13 14:31:48 +01:00
|
|
|
func (c *gameForUI) Draw(screenScale float64, offsetX, offsetY float64, needsClearingScreen bool, framebufferYDirection graphicsdriver.YDirection, clearScreenEveryFrame bool) error {
|
|
|
|
c.offscreen.mipmap.SetVolatile(clearScreenEveryFrame)
|
2022-02-13 12:30:32 +01:00
|
|
|
|
2021-04-19 04:33:34 +02:00
|
|
|
// Even though updateCount == 0, the offscreen is cleared and Draw is called.
|
2021-02-09 15:25:12 +01:00
|
|
|
// Draw should not update the game state and then the screen should not be updated without Update, but
|
|
|
|
// users might want to process something at Draw with the time intervals of FPS.
|
2022-02-13 14:31:48 +01:00
|
|
|
if clearScreenEveryFrame {
|
2021-04-19 04:33:34 +02:00
|
|
|
c.offscreen.Clear()
|
2020-10-04 10:16:26 +02:00
|
|
|
}
|
2021-04-19 04:33:34 +02:00
|
|
|
c.game.Draw(c.offscreen)
|
2019-12-29 15:14:57 +01:00
|
|
|
|
2022-02-13 12:08:59 +01:00
|
|
|
if needsClearingScreen {
|
2021-07-06 18:43:32 +02:00
|
|
|
// This clear is needed for fullscreen mode or some mobile platforms (#622).
|
|
|
|
c.screen.Clear()
|
|
|
|
}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2018-03-04 18:59:52 +01:00
|
|
|
op := &DrawImageOptions{}
|
2018-03-01 15:05:10 +01:00
|
|
|
|
2022-02-13 10:31:28 +01:00
|
|
|
s := screenScale
|
2022-02-13 12:08:59 +01:00
|
|
|
switch framebufferYDirection {
|
2022-02-06 12:41:32 +01:00
|
|
|
case graphicsdriver.Upward:
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Scale(s, -s)
|
2019-12-15 16:58:12 +01:00
|
|
|
_, h := c.offscreen.Size()
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Translate(0, float64(h)*s)
|
2022-02-06 12:41:32 +01:00
|
|
|
case graphicsdriver.Downward:
|
2019-12-15 18:10:58 +01:00
|
|
|
op.GeoM.Scale(s, s)
|
2018-11-12 16:00:10 +01:00
|
|
|
default:
|
2022-02-13 12:08:59 +01:00
|
|
|
panic(fmt.Sprintf("ebiten: invalid v-direction: %d", framebufferYDirection))
|
2018-11-12 16:00:10 +01:00
|
|
|
}
|
|
|
|
|
2022-02-13 10:31:28 +01:00
|
|
|
op.GeoM.Translate(offsetX, offsetY)
|
2018-03-01 15:05:10 +01:00
|
|
|
op.CompositeMode = CompositeModeCopy
|
2018-08-29 17:17:03 +02:00
|
|
|
|
2022-02-22 18:52:15 +01:00
|
|
|
switch {
|
|
|
|
case math.Floor(s) == s:
|
|
|
|
op.Filter = FilterNearest
|
|
|
|
case s > 1:
|
2018-08-29 17:17:03 +02:00
|
|
|
op.Filter = filterScreen
|
2022-02-22 18:52:15 +01:00
|
|
|
default:
|
2022-02-22 18:54:14 +01:00
|
|
|
// filterScreen works with >=1 scale, but does not well with <1 scale.
|
|
|
|
// Use regular FilterLinear instead so far (#669).
|
2018-08-29 17:17:03 +02:00
|
|
|
op.Filter = FilterLinear
|
|
|
|
}
|
2020-10-05 17:21:11 +02:00
|
|
|
c.screen.DrawImage(c.offscreen, op)
|
2020-10-14 18:23:27 +02:00
|
|
|
return nil
|
2016-05-23 17:00:54 +02:00
|
|
|
}
|