main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. "image"
  18. _ "image/png"
  19. "log"
  20. "github.com/hajimehoshi/ebiten/v2"
  21. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  22. "github.com/hajimehoshi/ebiten/v2/examples/resources/images"
  23. )
  24. const (
  25. screenWidth = 640
  26. screenHeight = 480
  27. )
  28. var ebitenImage *ebiten.Image
  29. func init() {
  30. // Decode an image from the image file's byte slice.
  31. img, _, err := image.Decode(bytes.NewReader(images.Ebiten_png))
  32. if err != nil {
  33. log.Fatal(err)
  34. }
  35. ebitenImage = ebiten.NewImageFromImage(img)
  36. }
  37. func drawRect(screen *ebiten.Image, img *ebiten.Image, x, y, width, height float32, address ebiten.Address, msg string) {
  38. sx, sy := -width/2, -height/2
  39. vs := []ebiten.Vertex{
  40. {
  41. DstX: x,
  42. DstY: y,
  43. SrcX: sx,
  44. SrcY: sy,
  45. ColorR: 1,
  46. ColorG: 1,
  47. ColorB: 1,
  48. ColorA: 1,
  49. },
  50. {
  51. DstX: x + width,
  52. DstY: y,
  53. SrcX: sx + width,
  54. SrcY: sy,
  55. ColorR: 1,
  56. ColorG: 1,
  57. ColorB: 1,
  58. ColorA: 1,
  59. },
  60. {
  61. DstX: x,
  62. DstY: y + height,
  63. SrcX: sx,
  64. SrcY: sy + height,
  65. ColorR: 1,
  66. ColorG: 1,
  67. ColorB: 1,
  68. ColorA: 1,
  69. },
  70. {
  71. DstX: x + width,
  72. DstY: y + height,
  73. SrcX: sx + width,
  74. SrcY: sy + height,
  75. ColorR: 1,
  76. ColorG: 1,
  77. ColorB: 1,
  78. ColorA: 1,
  79. },
  80. }
  81. op := &ebiten.DrawTrianglesOptions{}
  82. op.Address = address
  83. screen.DrawTriangles(vs, []uint16{0, 1, 2, 1, 2, 3}, img, op)
  84. ebitenutil.DebugPrintAt(screen, msg, int(x), int(y)-16)
  85. }
  86. type Game struct{}
  87. func (g *Game) Update() error {
  88. return nil
  89. }
  90. func (g *Game) Draw(screen *ebiten.Image) {
  91. const ox, oy = 40, 60
  92. drawRect(screen, ebitenImage, ox, oy, 200, 100, ebiten.AddressClampToZero, "Regular")
  93. drawRect(screen, ebitenImage, 220+ox, oy, 200, 100, ebiten.AddressRepeat, "Regular, Repeat")
  94. subImage := ebitenImage.SubImage(image.Rect(10, 5, 20, 30)).(*ebiten.Image)
  95. drawRect(screen, subImage, ox, 200+oy, 200, 100, ebiten.AddressClampToZero, "Subimage")
  96. drawRect(screen, subImage, 220+ox, 200+oy, 200, 100, ebiten.AddressRepeat, "Subimage, Repeat")
  97. }
  98. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  99. return screenWidth, screenHeight
  100. }
  101. func main() {
  102. ebiten.SetWindowSize(screenWidth, screenHeight)
  103. ebiten.SetWindowTitle("Sampler Address (Ebitengine Demo)")
  104. if err := ebiten.RunGame(&Game{}); err != nil {
  105. log.Fatal(err)
  106. }
  107. }