main.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "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 = 240
  27. screenHeight = 240
  28. )
  29. const (
  30. tileSize = 16
  31. )
  32. var (
  33. tilesImage *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.Tiles_png))
  38. if err != nil {
  39. log.Fatal(err)
  40. }
  41. tilesImage = ebiten.NewImageFromImage(img)
  42. }
  43. type Game struct {
  44. layers [][]int
  45. }
  46. func (g *Game) Update() error {
  47. return nil
  48. }
  49. func (g *Game) Draw(screen *ebiten.Image) {
  50. w := tilesImage.Bounds().Dx()
  51. tileXCount := w / tileSize
  52. // Draw each tile with each DrawImage call.
  53. // As the source images of all DrawImage calls are always same,
  54. // this rendering is done very efficiently.
  55. // For more detail, see https://pkg.go.dev/github.com/hajimehoshi/ebiten/v2#Image.DrawImage
  56. const xCount = screenWidth / tileSize
  57. for _, l := range g.layers {
  58. for i, t := range l {
  59. op := &ebiten.DrawImageOptions{}
  60. op.GeoM.Translate(float64((i%xCount)*tileSize), float64((i/xCount)*tileSize))
  61. sx := (t % tileXCount) * tileSize
  62. sy := (t / tileXCount) * tileSize
  63. screen.DrawImage(tilesImage.SubImage(image.Rect(sx, sy, sx+tileSize, sy+tileSize)).(*ebiten.Image), op)
  64. }
  65. }
  66. ebitenutil.DebugPrint(screen, fmt.Sprintf("TPS: %0.2f", ebiten.ActualTPS()))
  67. }
  68. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  69. return screenWidth, screenHeight
  70. }
  71. func main() {
  72. g := &Game{
  73. layers: [][]int{
  74. {
  75. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  76. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  77. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  78. 243, 218, 243, 243, 243, 243, 243, 243, 243, 243, 243, 218, 243, 244, 243,
  79. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  80. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  81. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  82. 243, 243, 244, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  83. 243, 243, 243, 243, 243, 243, 243, 243, 243, 219, 243, 243, 243, 219, 243,
  84. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  85. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  86. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  87. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  88. 243, 218, 243, 243, 243, 243, 243, 243, 243, 243, 243, 244, 243, 243, 243,
  89. 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243, 243,
  90. },
  91. {
  92. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  93. 0, 0, 0, 0, 0, 26, 27, 28, 29, 30, 31, 0, 0, 0, 0,
  94. 0, 0, 0, 0, 0, 51, 52, 53, 54, 55, 56, 0, 0, 0, 0,
  95. 0, 0, 0, 0, 0, 76, 77, 78, 79, 80, 81, 0, 0, 0, 0,
  96. 0, 0, 0, 0, 0, 101, 102, 103, 104, 105, 106, 0, 0, 0, 0,
  97. 0, 0, 0, 0, 0, 126, 127, 128, 129, 130, 131, 0, 0, 0, 0,
  98. 0, 0, 0, 0, 0, 303, 303, 245, 242, 303, 303, 0, 0, 0, 0,
  99. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  106. 0, 0, 0, 0, 0, 0, 0, 245, 242, 0, 0, 0, 0, 0, 0,
  107. },
  108. },
  109. }
  110. ebiten.SetWindowSize(screenWidth*2, screenHeight*2)
  111. ebiten.SetWindowTitle("Tiles (Ebitengine Demo)")
  112. if err := ebiten.RunGame(g); err != nil {
  113. log.Fatal(err)
  114. }
  115. }