gameforui.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2014 Hajime Hoshi
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package ebiten
  15. import (
  16. "fmt"
  17. "image"
  18. "math"
  19. "sync"
  20. "sync/atomic"
  21. "github.com/hajimehoshi/ebiten/v2/internal/atlas"
  22. "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
  23. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  24. )
  25. var screenFilterEnabled atomic.Bool
  26. func init() {
  27. screenFilterEnabled.Store(true)
  28. }
  29. type gameForUI struct {
  30. game Game
  31. offscreen *Image
  32. screen *Image
  33. imageDumper imageDumper
  34. transparent bool
  35. }
  36. func newGameForUI(game Game, transparent bool) *gameForUI {
  37. g := &gameForUI{
  38. game: game,
  39. transparent: transparent,
  40. }
  41. return g
  42. }
  43. func (g *gameForUI) NewOffscreenImage(width, height int) *ui.Image {
  44. if g.offscreen != nil {
  45. g.offscreen.Deallocate()
  46. g.offscreen = nil
  47. }
  48. // Keep the offscreen an unmanaged image that is always isolated from an atlas (#1938).
  49. // The shader program for the screen is special and doesn't work well with an image on an atlas.
  50. // An image on an atlas is surrounded by a transparent edge,
  51. // and the shader program unexpectedly picks the pixel on the edges.
  52. imageType := atlas.ImageTypeUnmanaged
  53. if ui.Get().IsScreenClearedEveryFrame() {
  54. // A volatile image is also always isolated.
  55. imageType = atlas.ImageTypeVolatile
  56. }
  57. g.offscreen = newImage(image.Rect(0, 0, width, height), imageType)
  58. return g.offscreen.image
  59. }
  60. func (g *gameForUI) NewScreenImage(width, height int) *ui.Image {
  61. if g.screen != nil {
  62. g.screen.Deallocate()
  63. g.screen = nil
  64. }
  65. g.screen = newImage(image.Rect(0, 0, width, height), atlas.ImageTypeScreen)
  66. return g.screen.image
  67. }
  68. func (g *gameForUI) Layout(outsideWidth, outsideHeight float64) (float64, float64) {
  69. if l, ok := g.game.(LayoutFer); ok {
  70. return l.LayoutF(outsideWidth, outsideHeight)
  71. }
  72. // Even if the original value is less than 1, the value must be a positive integer (#2340).
  73. // This is for a simple implementation of Layout, which returns the argument values without modifications.
  74. // TODO: Remove this hack when Game.Layout takes floats instead of integers.
  75. if outsideWidth < 1 {
  76. outsideWidth = 1
  77. }
  78. if outsideHeight < 1 {
  79. outsideHeight = 1
  80. }
  81. // TODO: Add a new Layout function taking float values (#2285).
  82. sw, sh := g.game.Layout(int(outsideWidth), int(outsideHeight))
  83. return float64(sw), float64(sh)
  84. }
  85. func (g *gameForUI) UpdateInputState(fn func(*ui.InputState)) {
  86. theInputState.update(fn)
  87. }
  88. func (g *gameForUI) Update() error {
  89. if err := g.game.Update(); err != nil {
  90. return err
  91. }
  92. if err := g.imageDumper.update(); err != nil {
  93. return err
  94. }
  95. return nil
  96. }
  97. func (g *gameForUI) DrawOffscreen() error {
  98. g.game.Draw(g.offscreen)
  99. if err := g.imageDumper.dump(g.offscreen, g.transparent); err != nil {
  100. return err
  101. }
  102. return nil
  103. }
  104. func (g *gameForUI) DrawFinalScreen(scale, offsetX, offsetY float64) {
  105. var geoM GeoM
  106. geoM.Scale(scale, scale)
  107. geoM.Translate(offsetX, offsetY)
  108. if d, ok := g.game.(FinalScreenDrawer); ok {
  109. d.DrawFinalScreen(g.screen, g.offscreen, geoM)
  110. return
  111. }
  112. DefaultDrawFinalScreen(g.screen, g.offscreen, geoM)
  113. }
  114. var (
  115. theScreenShader *Shader
  116. theScreenShaderOnce sync.Once
  117. )
  118. // DefaultDrawFinalScreen is the default implementation of [FinalScreenDrawer.DrawFinalScreen],
  119. // used when a [Game] doesn't implement [FinalScreenDrawer].
  120. //
  121. // You can use DefaultDrawFinalScreen when you need the default implementation of [FinalScreenDrawer.DrawFinalScreen]
  122. // in your implementation of [FinalScreenDrawer], for example.
  123. func DefaultDrawFinalScreen(screen FinalScreen, offscreen *Image, geoM GeoM) {
  124. theScreenShaderOnce.Do(func() {
  125. s, err := newShader(builtinshader.ScreenShaderSource, "screen")
  126. if err != nil {
  127. panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
  128. }
  129. theScreenShader = s
  130. })
  131. scale := geoM.Element(0, 0)
  132. switch {
  133. case !screenFilterEnabled.Load(), math.Floor(scale) == scale:
  134. op := &DrawImageOptions{}
  135. op.GeoM = geoM
  136. screen.DrawImage(offscreen, op)
  137. case scale < 1:
  138. op := &DrawImageOptions{}
  139. op.GeoM = geoM
  140. op.Filter = FilterLinear
  141. screen.DrawImage(offscreen, op)
  142. default:
  143. op := &DrawRectShaderOptions{}
  144. op.Images[0] = offscreen
  145. op.GeoM = geoM
  146. w, h := offscreen.Bounds().Dx(), offscreen.Bounds().Dy()
  147. screen.DrawRectShader(w, h, theScreenShader, op)
  148. }
  149. }