-
-
Notifications
You must be signed in to change notification settings - Fork 648
/
main.go
144 lines (126 loc) · 3.11 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"fmt"
"image"
"image/color"
"log"
"math"
"github.com/hajimehoshi/ebiten/v2"
"github.com/hajimehoshi/ebiten/v2/ebitenutil"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
screenWidth = 640
screenHeight = 480
)
var (
whiteImage = ebiten.NewImage(3, 3)
)
func init() {
whiteImage.Fill(color.White)
}
func genVertices(num int) []ebiten.Vertex {
const (
centerX = screenWidth / 2
centerY = screenHeight / 2
r = 160
)
vs := []ebiten.Vertex{}
for i := 0; i < num; i++ {
rate := float64(i) / float64(num)
cr := 0.0
cg := 0.0
cb := 0.0
if rate < 1.0/3.0 {
cb = 2 - 2*(rate*3)
cr = 2 * (rate * 3)
}
if 1.0/3.0 <= rate && rate < 2.0/3.0 {
cr = 2 - 2*(rate-1.0/3.0)*3
cg = 2 * (rate - 1.0/3.0) * 3
}
if 2.0/3.0 <= rate {
cg = 2 - 2*(rate-2.0/3.0)*3
cb = 2 * (rate - 2.0/3.0) * 3
}
vs = append(vs, ebiten.Vertex{
DstX: float32(r*math.Cos(2*math.Pi*rate)) + centerX,
DstY: float32(r*math.Sin(2*math.Pi*rate)) + centerY,
SrcX: 0,
SrcY: 0,
ColorR: float32(cr),
ColorG: float32(cg),
ColorB: float32(cb),
ColorA: 1,
})
}
vs = append(vs, ebiten.Vertex{
DstX: centerX,
DstY: centerY,
SrcX: 0,
SrcY: 0,
ColorR: 1,
ColorG: 1,
ColorB: 1,
ColorA: 1,
})
return vs
}
type Game struct {
vertices []ebiten.Vertex
ngon int
prevNgon int
}
func (g *Game) Update() error {
if inpututil.IsKeyJustPressed(ebiten.KeyArrowLeft) {
g.ngon--
if g.ngon < 1 {
g.ngon = 1
}
}
if inpututil.IsKeyJustPressed(ebiten.KeyArrowRight) {
g.ngon++
if g.ngon > 120 {
g.ngon = 120
}
}
if g.prevNgon != g.ngon || len(g.vertices) == 0 {
g.vertices = genVertices(g.ngon)
g.prevNgon = g.ngon
}
return nil
}
func (g *Game) Draw(screen *ebiten.Image) {
op := &ebiten.DrawTrianglesOptions{}
op.Address = ebiten.AddressUnsafe
indices := []uint16{}
for i := 0; i < g.ngon; i++ {
indices = append(indices, uint16(i), uint16(i+1)%uint16(g.ngon), uint16(g.ngon))
}
screen.DrawTriangles(g.vertices, indices, whiteImage.SubImage(image.Rect(1, 1, 2, 2)).(*ebiten.Image), op)
msg := fmt.Sprintf("TPS: %0.2f\n%d-gon\nPress <- or -> to change the number of the vertices", ebiten.ActualTPS(), g.ngon)
ebitenutil.DebugPrint(screen, msg)
}
func (g *Game) Layout(outsideWidth, outsideHeight int) (int, int) {
return screenWidth, screenHeight
}
func main() {
ebiten.SetWindowSize(screenWidth, screenHeight)
ebiten.SetWindowTitle("Polygons (Ebitengine Demo)")
if err := ebiten.RunGame(&Game{ngon: 10}); err != nil {
log.Fatal(err)
}
}