gameforui.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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/atomic"
  20. "github.com/hajimehoshi/ebiten/v2/internal/atlas"
  21. "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
  22. "github.com/hajimehoshi/ebiten/v2/internal/ui"
  23. )
  24. var screenFilterEnabled atomic.Bool
  25. func init() {
  26. screenFilterEnabled.Store(true)
  27. }
  28. type gameForUI struct {
  29. game Game
  30. offscreen *Image
  31. screen *Image
  32. screenShader *Shader
  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. s, err := newShader(builtinshader.ScreenShaderSource, "screen")
  42. if err != nil {
  43. panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
  44. }
  45. g.screenShader = s
  46. return g
  47. }
  48. func (g *gameForUI) NewOffscreenImage(width, height int) *ui.Image {
  49. if g.offscreen != nil {
  50. g.offscreen.Deallocate()
  51. g.offscreen = nil
  52. }
  53. // Keep the offscreen an unmanaged image that is always isolated from an atlas (#1938).
  54. // The shader program for the screen is special and doesn't work well with an image on an atlas.
  55. // An image on an atlas is surrounded by a transparent edge,
  56. // and the shader program unexpectedly picks the pixel on the edges.
  57. imageType := atlas.ImageTypeUnmanaged
  58. if ui.Get().IsScreenClearedEveryFrame() {
  59. // A volatile image is also always isolated.
  60. imageType = atlas.ImageTypeVolatile
  61. }
  62. g.offscreen = newImage(image.Rect(0, 0, width, height), imageType)
  63. return g.offscreen.image
  64. }
  65. func (g *gameForUI) NewScreenImage(width, height int) *ui.Image {
  66. if g.screen != nil {
  67. g.screen.Deallocate()
  68. g.screen = nil
  69. }
  70. g.screen = newImage(image.Rect(0, 0, width, height), atlas.ImageTypeScreen)
  71. return g.screen.image
  72. }
  73. func (g *gameForUI) Layout(outsideWidth, outsideHeight float64) (float64, float64) {
  74. if l, ok := g.game.(LayoutFer); ok {
  75. return l.LayoutF(outsideWidth, outsideHeight)
  76. }
  77. // Even if the original value is less than 1, the value must be a positive integer (#2340).
  78. // This is for a simple implementation of Layout, which returns the argument values without modifications.
  79. // TODO: Remove this hack when Game.Layout takes floats instead of integers.
  80. if outsideWidth < 1 {
  81. outsideWidth = 1
  82. }
  83. if outsideHeight < 1 {
  84. outsideHeight = 1
  85. }
  86. // TODO: Add a new Layout function taking float values (#2285).
  87. sw, sh := g.game.Layout(int(outsideWidth), int(outsideHeight))
  88. return float64(sw), float64(sh)
  89. }
  90. func (g *gameForUI) UpdateInputState(fn func(*ui.InputState)) {
  91. theInputState.update(fn)
  92. }
  93. func (g *gameForUI) Update() error {
  94. if err := g.game.Update(); err != nil {
  95. return err
  96. }
  97. if err := g.imageDumper.update(); err != nil {
  98. return err
  99. }
  100. return nil
  101. }
  102. func (g *gameForUI) DrawOffscreen() error {
  103. g.game.Draw(g.offscreen)
  104. if err := g.imageDumper.dump(g.offscreen, g.transparent); err != nil {
  105. return err
  106. }
  107. return nil
  108. }
  109. func (g *gameForUI) DrawFinalScreen(scale, offsetX, offsetY float64) {
  110. var geoM GeoM
  111. geoM.Scale(scale, scale)
  112. geoM.Translate(offsetX, offsetY)
  113. if d, ok := g.game.(FinalScreenDrawer); ok {
  114. d.DrawFinalScreen(g.screen, g.offscreen, geoM)
  115. return
  116. }
  117. switch {
  118. case !screenFilterEnabled.Load(), math.Floor(scale) == scale:
  119. op := &DrawImageOptions{}
  120. op.GeoM = geoM
  121. g.screen.DrawImage(g.offscreen, op)
  122. case scale < 1:
  123. op := &DrawImageOptions{}
  124. op.GeoM = geoM
  125. op.Filter = FilterLinear
  126. g.screen.DrawImage(g.offscreen, op)
  127. default:
  128. op := &DrawRectShaderOptions{}
  129. op.Images[0] = g.offscreen
  130. op.GeoM = geoM
  131. w, h := g.offscreen.Bounds().Dx(), g.offscreen.Bounds().Dy()
  132. g.screen.DrawRectShader(w, h, g.screenShader, op)
  133. }
  134. }