main.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2018 The Ebiten Authors
  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 main
  15. import (
  16. "flag"
  17. "fmt"
  18. _ "image/jpeg"
  19. "log"
  20. "runtime"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. )
  24. type Game struct {
  25. highDPIImageCh chan *ebiten.Image
  26. highDPIImage *ebiten.Image
  27. }
  28. func NewGame() *Game {
  29. g := &Game{
  30. highDPIImageCh: make(chan *ebiten.Image),
  31. }
  32. // Licensed under Public Domain
  33. // https://commons.wikimedia.org/wiki/File:As08-16-2593.jpg
  34. const url = "https://upload.wikimedia.org/wikipedia/commons/1/1f/As08-16-2593.jpg"
  35. // Load the image asynchronously.
  36. go func() {
  37. img, err := ebitenutil.NewImageFromURL(url)
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. g.highDPIImageCh <- img
  42. close(g.highDPIImageCh)
  43. }()
  44. return g
  45. }
  46. func (g *Game) Update() error {
  47. if g.highDPIImage != nil {
  48. return nil
  49. }
  50. // Use select and 'default' clause for non-blocking receiving.
  51. select {
  52. case img := <-g.highDPIImageCh:
  53. g.highDPIImage = img
  54. default:
  55. }
  56. return nil
  57. }
  58. func (g *Game) Draw(screen *ebiten.Image) {
  59. if g.highDPIImage == nil {
  60. ebitenutil.DebugPrint(screen, "Loading the image...")
  61. return
  62. }
  63. sw, sh := screen.Bounds().Dx(), screen.Bounds().Dy()
  64. w, h := g.highDPIImage.Bounds().Dx(), g.highDPIImage.Bounds().Dy()
  65. op := &ebiten.DrawImageOptions{}
  66. // Move the images's center to the upper left corner.
  67. op.GeoM.Translate(float64(-w)/2, float64(-h)/2)
  68. // The image is just too big. Adjust the scale.
  69. op.GeoM.Scale(0.25, 0.25)
  70. // Scale the image by the device ratio so that the rendering result can be same
  71. // on various (different-DPI) environments.
  72. scale := ebiten.Monitor().DeviceScaleFactor()
  73. op.GeoM.Scale(scale, scale)
  74. // Move the image's center to the screen's center.
  75. op.GeoM.Translate(float64(sw)/2, float64(sh)/2)
  76. op.Filter = ebiten.FilterLinear
  77. screen.DrawImage(g.highDPIImage, op)
  78. msg := fmt.Sprintf("Device Scale Ratio: %0.2f", scale)
  79. if runtime.GOOS == "js" {
  80. if !*flagDisable {
  81. msg += "\nHiDPI rendering is enabled. You can disable HiDPI by -disable flag."
  82. } else {
  83. msg += "\nHiDPI rendering is disabled."
  84. }
  85. }
  86. // TODO: The texts might be too small. Improve legibility.
  87. ebitenutil.DebugPrint(screen, msg)
  88. }
  89. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  90. // The unit of outsideWidth/Height is device-independent pixels.
  91. // By multiplying them by the device scale factor, we can get a hi-DPI screen size.
  92. s := ebiten.Monitor().DeviceScaleFactor()
  93. return int(float64(outsideWidth) * s), int(float64(outsideHeight) * s)
  94. }
  95. var flagDisable = flag.Bool("disable", false, "disables HiDPI rendering (only on browsers)")
  96. func main() {
  97. flag.Parse()
  98. ebiten.SetWindowSize(640, 480)
  99. ebiten.SetWindowTitle("High DPI (Ebitengine Demo)")
  100. op := &ebiten.RunGameOptions{}
  101. op.DisableHiDPI = *flagDisable
  102. if err := ebiten.RunGameWithOptions(NewGame(), op); err != nil {
  103. log.Fatal(err)
  104. }
  105. }