main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. "fmt"
  17. "image"
  18. "image/color"
  19. "log"
  20. "math"
  21. "github.com/hajimehoshi/ebiten/v2"
  22. "github.com/hajimehoshi/ebiten/v2/ebitenutil"
  23. "github.com/hajimehoshi/ebiten/v2/inpututil"
  24. )
  25. const (
  26. screenWidth = 640
  27. screenHeight = 480
  28. )
  29. var (
  30. whiteImage = ebiten.NewImage(3, 3)
  31. )
  32. func init() {
  33. whiteImage.Fill(color.White)
  34. }
  35. func genVertices(num int) []ebiten.Vertex {
  36. const (
  37. centerX = screenWidth / 2
  38. centerY = screenHeight / 2
  39. r = 160
  40. )
  41. vs := []ebiten.Vertex{}
  42. for i := 0; i < num; i++ {
  43. rate := float64(i) / float64(num)
  44. cr := 0.0
  45. cg := 0.0
  46. cb := 0.0
  47. if rate < 1.0/3.0 {
  48. cb = 2 - 2*(rate*3)
  49. cr = 2 * (rate * 3)
  50. }
  51. if 1.0/3.0 <= rate && rate < 2.0/3.0 {
  52. cr = 2 - 2*(rate-1.0/3.0)*3
  53. cg = 2 * (rate - 1.0/3.0) * 3
  54. }
  55. if 2.0/3.0 <= rate {
  56. cg = 2 - 2*(rate-2.0/3.0)*3
  57. cb = 2 * (rate - 2.0/3.0) * 3
  58. }
  59. vs = append(vs, ebiten.Vertex{
  60. DstX: float32(r*math.Cos(2*math.Pi*rate)) + centerX,
  61. DstY: float32(r*math.Sin(2*math.Pi*rate)) + centerY,
  62. SrcX: 0,
  63. SrcY: 0,
  64. ColorR: float32(cr),
  65. ColorG: float32(cg),
  66. ColorB: float32(cb),
  67. ColorA: 1,
  68. })
  69. }
  70. vs = append(vs, ebiten.Vertex{
  71. DstX: centerX,
  72. DstY: centerY,
  73. SrcX: 0,
  74. SrcY: 0,
  75. ColorR: 1,
  76. ColorG: 1,
  77. ColorB: 1,
  78. ColorA: 1,
  79. })
  80. return vs
  81. }
  82. type Game struct {
  83. vertices []ebiten.Vertex
  84. ngon int
  85. prevNgon int
  86. }
  87. func (g *Game) Update() error {
  88. if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
  89. g.ngon--
  90. if g.ngon < 1 {
  91. g.ngon = 1
  92. }
  93. }
  94. if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
  95. g.ngon++
  96. if g.ngon > 120 {
  97. g.ngon = 120
  98. }
  99. }
  100. if g.prevNgon != g.ngon || len(g.vertices) == 0 {
  101. g.vertices = genVertices(g.ngon)
  102. g.prevNgon = g.ngon
  103. }
  104. return nil
  105. }
  106. func (g *Game) Draw(screen *ebiten.Image) {
  107. op := &ebiten.DrawTrianglesOptions{}
  108. op.Address = ebiten.AddressUnsafe
  109. indices := []uint16{}
  110. for i := 0; i < g.ngon; i++ {
  111. indices = append(indices, uint16(i), uint16(i+1)%uint16(g.ngon), uint16(g.ngon))
  112. }
  113. screen.DrawTriangles(g.vertices, indices, whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
  114. msg := fmt.Sprintf("TPS: %0.2f\n%d-gon\nPress <- or -> to change the number of the vertices", ebiten.ActualTPS(), g.ngon)
  115. ebitenutil.DebugPrint(screen, msg)
  116. }
  117. func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
  118. return screenWidth, screenHeight
  119. }
  120. func main() {
  121. ebiten.SetWindowSize(screenWidth, screenHeight)
  122. ebiten.SetWindowTitle("Polygons (Ebitengine Demo)")
  123. if err := ebiten.RunGame(&Game{ngon: 10}); err != nil {
  124. log.Fatal(err)
  125. }
  126. }