-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
160 lines (126 loc) · 2.88 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"flag"
"fmt"
"time"
"github.com/faiface/pixel"
"github.com/faiface/pixel/imdraw"
"github.com/faiface/pixel/pixelgl"
"github.com/zergon321/cirno"
colors "golang.org/x/image/colornames"
)
const (
width = 1280
height = 720
angleSpeed = 50
)
var (
controlledShape string
vsync bool
)
func cirnoToPixel(vec cirno.Vector) pixel.Vec {
return pixel.V(vec.X, vec.Y)
}
func parseFlags() {
flag.StringVar(&controlledShape, "shape", "rectangle",
"The shape controlled during execution of the demo.")
flag.BoolVar(&vsync, "vsync", true, "Enable vertical synchronization.")
flag.Parse()
}
func run() {
// Create a new window.
cfg := pixelgl.WindowConfig{
Title: "Cirno demo",
Bounds: pixel.R(0, 0, width, height),
VSync: vsync,
}
win, err := pixelgl.NewWindow(cfg)
handleError(err)
base := cirno.NewVector(width/2, height/2)
var object cirno.Shape
switch controlledShape {
case "line":
object, err = cirno.NewLine(
base.Add(cirno.NewVector(40, 0)),
base.Add(cirno.NewVector(90, 50)),
)
handleError(err)
case "rectangle":
object, err = cirno.NewRectangle(
base.Add(cirno.NewVector(60, 0)),
40, 20, 0.0)
handleError(err)
case "circle":
object, err = cirno.NewCircle(
base.Add(cirno.NewVector(60, 0)), 20)
handleError(err)
default:
handleError(fmt.Errorf("unknown argument: %s",
controlledShape))
}
// IMDraw instance to draw shapes.
imd := imdraw.New(nil)
// Setup metrics.
last := time.Now()
fps := 0
perSecond := time.Tick(time.Second)
for !win.Closed() {
deltaTime := time.Since(last).Seconds()
last = time.Now()
win.Clear(colors.White)
// Move the shape around the other shape.
angle := angleSpeed * deltaTime
object.RotateAround(angle, base)
object.Rotate(angle)
// Rendering.
imd.Clear()
imd.Color = colors.Red
imd.Push(cirnoToPixel(base))
imd.Circle(20, 3)
imd.Color = colors.Blue
switch object.(type) {
case *cirno.Line:
lineShape := object.(*cirno.Line)
imd.Push(
pixel.V(lineShape.P().X, lineShape.P().Y),
pixel.V(lineShape.Q().X, lineShape.Q().Y),
)
imd.Line(3)
case *cirno.Circle:
circleShape := object.(*cirno.Circle)
imd.Push(pixel.V(circleShape.Center().X,
circleShape.Center().Y))
imd.Circle(circleShape.Radius(), 3)
case *cirno.Rectangle:
rectShape := object.(*cirno.Rectangle)
vertices := rectShape.Vertices()
imd.Push(
pixel.V(vertices[0].X, vertices[0].Y),
pixel.V(vertices[1].X, vertices[1].Y),
pixel.V(vertices[2].X, vertices[2].Y),
pixel.V(vertices[3].X, vertices[3].Y),
)
imd.Polygon(3)
}
imd.Draw(win)
win.Update()
// Compute the FPS.
fps++
select {
case <-perSecond:
win.SetTitle(fmt.Sprintf("%s | FPS: %d",
cfg.Title, fps))
fps = 0
default:
}
}
}
func main() {
parseFlags()
pixelgl.Run(run)
}
func handleError(err error) {
if err != nil {
panic(err)
}
}