main.go 4.8 KB

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