main.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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. "fmt"
  18. "image"
  19. _ "image/png"
  20. "log"
  21. "math"
  22. "math/rand/v2"
  23. "github.com/hajimehoshi/ebiten/v2"
  24. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  25. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  26. "github.com/hajimehoshi/ebiten/v2/inpututil"
  27. )
  28. const (
  29. screenWidth = 1920
  30. screenHeight = 1080
  31. maxAngle = 256
  32. )
  33. var (
  34. ebitenImage *ebiten.Image
  35. )
  36. func init() {
  37. // Decode an image from the image file's byte slice.
  38. img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
  39. if err != nil {
  40. log.Fatal(err)
  41. }
  42. origEbitenImage := ebiten.NewImageFromImage(img)
  43. s := origEbitenImage.Bounds().Size()
  44. ebitenImage = ebiten.NewImage(s.X, s.Y)
  45. op := &ebiten.DrawImageOptions{}
  46. op.ColorScale.ScaleAlpha(0.5)
  47. ebitenImage.DrawImage(origEbitenImage, op)
  48. }
  49. type Sprite struct {
  50. imageWidth int
  51. imageHeight int
  52. x int
  53. y int
  54. vx int
  55. vy int
  56. angle int
  57. }
  58. func (s *Sprite) Update() {
  59. s.x += s.vx
  60. s.y += s.vy
  61. if s.x < 0 {
  62. s.x = -s.x
  63. s.vx = -s.vx
  64. } else if screenWidth <= s.x+s.imageWidth {
  65. s.x = 2*(screenWidth-s.imageWidth) - s.x
  66. s.vx = -s.vx
  67. }
  68. if s.y < 0 {
  69. s.y = -s.y
  70. s.vy = -s.vy
  71. } else if screenHeight <= s.y+s.imageHeight {
  72. s.y = 2*(screenHeight-s.imageHeight) - s.y
  73. s.vy = -s.vy
  74. }
  75. s.angle++
  76. s.angle %= maxAngle
  77. }
  78. type Sprites struct {
  79. sprites []*Sprite
  80. num int
  81. }
  82. func (s *Sprites) Update() {
  83. for _, sprite := range s.sprites {
  84. sprite.Update()
  85. }
  86. }
  87. const (
  88. MinSprites = 0
  89. MaxSprites = 50000
  90. )
  91. type Game struct {
  92. sprites Sprites
  93. op ebiten.DrawImageOptions
  94. inited bool
  95. }
  96. func (g *Game) init() {
  97. defer func() {
  98. g.inited = true
  99. }()
  100. g.sprites.sprites = make([]*Sprite, MaxSprites)
  101. g.sprites.num = 500
  102. for i := range g.sprites.sprites {
  103. w, h := ebitenImage.Bounds().Dx(), ebitenImage.Bounds().Dy()
  104. x, y := rand.IntN(screenWidth-w), rand.IntN(screenHeight-h)
  105. vx, vy := 2*rand.IntN(2)-1, 2*rand.IntN(2)-1
  106. a := rand.IntN(maxAngle)
  107. g.sprites.sprites[i] = &Sprite{
  108. imageWidth: w,
  109. imageHeight: h,
  110. x: x,
  111. y: y,
  112. vx: vx,
  113. vy: vy,
  114. angle: a,
  115. }
  116. }
  117. }
  118. func (g *Game) Update() error {
  119. if !g.inited {
  120. g.init()
  121. }
  122. if inpututil.IsKeyJustPressed(ebiten.KeyQ) {
  123. return ebiten.Termination
  124. }
  125. // Decrease the number of the sprites.
  126. if ebiten.IsKeyPressed(ebiten.KeyArrowLeft) {
  127. g.sprites.num -= 20
  128. if g.sprites.num < MinSprites {
  129. g.sprites.num = MinSprites
  130. }
  131. }
  132. // Increase the number of the sprites.
  133. if ebiten.IsKeyPressed(ebiten.KeyArrowRight) {
  134. g.sprites.num += 20
  135. if MaxSprites < g.sprites.num {
  136. g.sprites.num = MaxSprites
  137. }
  138. }
  139. g.sprites.Update()
  140. return nil
  141. }
  142. func (g *Game) Draw(screen *ebiten.Image) {
  143. // Draw each sprite.
  144. // DrawImage can be called many many times, but in the implementation,
  145. // the actual draw call to GPU is very few since these calls satisfy
  146. // some conditions e.g. all the rendering sources and targets are same.
  147. // For more detail, see:
  148. // https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#Image.DrawImage
  149. w, h := ebitenImage.Bounds().Dx(), ebitenImage.Bounds().Dy()
  150. for i := 0; i < g.sprites.num; i++ {
  151. s := g.sprites.sprites[i]
  152. g.op.GeoM.Reset()
  153. g.op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
  154. g.op.GeoM.Rotate(2 * math.Pi * float64(s.angle) / maxAngle)
  155. g.op.GeoM.Translate(float64(w)/2, float64(h)/2)
  156. g.op.GeoM.Translate(float64(s.x), float64(s.y))
  157. screen.DrawImage(ebitenImage, &g.op)
  158. }
  159. msg := fmt.Sprintf(`TPS: %0.2f
  160. FPS: %0.2f
  161. Num of sprites: %d
  162. Press <- or -> to change the number of sprites
  163. Press Q to quit`, ebiten.ActualTPS(), ebiten.ActualFPS(), g.sprites.num)
  164. ebitenutil.DebugPrint(screen, msg)
  165. }
  166. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  167. return screenWidth, screenHeight
  168. }
  169. func main() {
  170. ebiten.SetFullscreen(true)
  171. ebiten.SetWindowTitle("Sprites HD (Ebitengine Demo)")
  172. if err := ebiten.RunGame(&Game{}); err != nil {
  173. log.Fatal(err)
  174. }
  175. }