main.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. "bytes"
  17. "image"
  18. _ "image/jpeg"
  19. "log"
  20. "math"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  24. "github.com/hajimehoshi/ebiten/v2/inpututil"
  25. )
  26. const (
  27. screenWidth = 640
  28. screenHeight = 480
  29. )
  30. var (
  31. gophersImage *ebiten.Image
  32. extraImages []*ebiten.Image
  33. )
  34. type Game struct {
  35. count int
  36. lost bool
  37. }
  38. func (g *Game) Update() error {
  39. if inpututil.IsKeyJustPressed(ebiten.KeySpace) {
  40. g.loseAndRestoreContext()
  41. return nil
  42. }
  43. if inpututil.IsKeyJustPressed(ebiten.KeyS) {
  44. ebiten.SetScreenClearedEveryFrame(!ebiten.IsScreenClearedEveryFrame())
  45. }
  46. g.count++
  47. return nil
  48. }
  49. func (g *Game) Draw(screen *ebiten.Image) {
  50. if g.lost {
  51. // When the context is lost, skip rendering. Usually this logic should not be required, but when the
  52. // context lost happens by the API explicitly, Draw can be called even after the data in GPU
  53. // disappeared.
  54. return
  55. }
  56. s := gophersImage.Bounds().Size()
  57. op := &ebiten.DrawImageOptions{}
  58. // For the details, see examples/rotate.
  59. op.GeoM.Translate(-float64(s.X)/2, -float64(s.Y)/2)
  60. op.GeoM.Rotate(float64(g.count%360) * 2 * math.Pi / 360)
  61. op.GeoM.Translate(screenWidth/2, screenHeight/2)
  62. screen.DrawImage(gophersImage, op)
  63. msg := `Press Space to force to lose/restore the GL context!
  64. (Browser only)
  65. Press S to switch clearing the screen
  66. at the beginning of each frame.`
  67. ebitenutil.DebugPrint(screen, msg)
  68. }
  69. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  70. return screenWidth, screenHeight
  71. }
  72. func main() {
  73. // Decode an image from the image file's byte slice.
  74. img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
  75. if err != nil {
  76. log.Fatal(err)
  77. }
  78. gophersImage = ebiten.NewImageFromImage(img)
  79. // Extend the shared backend GL texture on purpose.
  80. for i := 0; i < 20; i++ {
  81. eimg := ebiten.NewImageFromImage(img)
  82. extraImages = append(extraImages, eimg)
  83. }
  84. ebiten.SetWindowSize(screenWidth, screenHeight)
  85. ebiten.SetWindowTitle("Context Lost (Ebitengine Demo)")
  86. if err := ebiten.RunGame(&Game{}); err != nil {
  87. log.Fatal(err)
  88. }
  89. }