main.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2016 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. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  24. )
  25. const (
  26. screenWidth = 320
  27. screenHeight = 240
  28. )
  29. var (
  30. bgImage *ebiten.Image
  31. )
  32. func init() {
  33. // Decode an image from the image file's byte slice.
  34. img, _, err := image.Decode(bytes.NewReader(images.Tile_png))
  35. if err != nil {
  36. log.Fatal(err)
  37. }
  38. bgImage = ebiten.NewImageFromImage(img)
  39. }
  40. type viewport struct {
  41. x16 int
  42. y16 int
  43. }
  44. func (p *viewport) Move() {
  45. s := bgImage.Bounds().Size()
  46. maxX16 := s.X * 16
  47. maxY16 := s.Y * 16
  48. p.x16 += s.X / 32
  49. p.y16 += s.Y / 32
  50. p.x16 %= maxX16
  51. p.y16 %= maxY16
  52. }
  53. func (p *viewport) Position() (int, int) {
  54. return p.x16, p.y16
  55. }
  56. type Game struct {
  57. viewport viewport
  58. }
  59. func (g *Game) Update() error {
  60. g.viewport.Move()
  61. return nil
  62. }
  63. func (g *Game) Draw(screen *ebiten.Image) {
  64. x16, y16 := g.viewport.Position()
  65. offsetX, offsetY := float64(-x16)/16, float64(-y16)/16
  66. // Draw bgImage on the screen repeatedly.
  67. const repeat = 3
  68. w, h := bgImage.Bounds().Dx(), bgImage.Bounds().Dy()
  69. for j := 0; j < repeat; j++ {
  70. for i := 0; i < repeat; i++ {
  71. op := &ebiten.DrawImageOptions{}
  72. op.GeoM.Translate(float64(w*i), float64(h*j))
  73. op.GeoM.Translate(offsetX, offsetY)
  74. screen.DrawImage(bgImage, op)
  75. }
  76. }
  77. ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS()))
  78. }
  79. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  80. return screenWidth, screenHeight
  81. }
  82. func main() {
  83. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  84. ebiten.SetWindowTitle("Infinite Scroll (Ebitengine Demo)")
  85. if err := ebiten.RunGame(&Game{}); err != nil {
  86. log.Fatal(err)
  87. }
  88. }