1
0

main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright 2022 The Ebitengine 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. )
  33. type Game struct {
  34. count int
  35. input bool
  36. introShown bool
  37. }
  38. func (g *Game) Update() error {
  39. g.count++
  40. g.input = len(inpututil.AppendPressedKeys(nil)) > 0
  41. if inpututil.IsKeyJustPressed(ebiten.KeyF) {
  42. ebiten.SetFullscreen(!ebiten.IsFullscreen())
  43. }
  44. return nil
  45. }
  46. func (g *Game) Draw(screen *ebiten.Image) {
  47. // If there is no inpnut, skip draw.
  48. if !g.input && g.introShown {
  49. // As SetScreenClearedEveryFrame(false) is called, the screen is not modified.
  50. // In this case, Ebitengine optimizes and reduces GPU usages.
  51. return
  52. }
  53. screen.Clear()
  54. s := gophersImage.Bounds().Size()
  55. op := &ebiten.DrawImageOptions{}
  56. // Move the image's center to the screen's upper-left corner.
  57. // This is a preparation for rotating. When geometry matrices are applied,
  58. // the origin point is the upper-left corner.
  59. op.GeoM.Translate(-float64(s.X)/2, -float64(s.Y)/2)
  60. // Rotate the image. As a result, the anchor point of this rotate is
  61. // the center of the image.
  62. op.GeoM.Rotate(float64(g.count%360) * 2 * math.Pi / 360)
  63. // Move the image to the screen's center.
  64. op.GeoM.Translate(screenWidth/2, screenHeight/2)
  65. screen.DrawImage(gophersImage, op)
  66. ebitenutil.DebugPrint(screen, "Press any keys to keep animations.\nPress F to switch fullscreen.")
  67. g.introShown = true
  68. }
  69. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  70. return screenWidth, screenHeight
  71. }
  72. func main() {
  73. ebiten.SetWindowResizingMode(ebiten.WindowResizingModeEnabled)
  74. ebiten.SetScreenClearedEveryFrame(false)
  75. // Decode an image from the image file's byte slice.
  76. img, _, err := image.Decode(bytes.NewReader(images.Gophers_jpg))
  77. if err != nil {
  78. log.Fatal(err)
  79. }
  80. gophersImage = ebiten.NewImageFromImage(img)
  81. ebiten.SetWindowSize(screenWidth, screenHeight)
  82. ebiten.SetWindowTitle("Skip Draw (Ebitengine Demo)")
  83. if err := ebiten.RunGame(&Game{}); err != nil {
  84. log.Fatal(err)
  85. }
  86. }