-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathbenchmark.nim
216 lines (186 loc) · 4.94 KB
/
benchmark.nim
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import nico
import times
import strformat
import nico/utils
type BenchmarkMode = enum
bmPoints
bmLines
bmHVLines
bmRect
bmRectfill
bmCirc
bmCircfill
bmSprite
bmSpriteIntScaled
bmSpriteScaled
bmSpriteRot
bmSpriteShearRot
bmTrifill
bmTTrifill
var mode = bmPoints
var autoAdjust = false
var lastMs: float32
var useClip = false
var vsync = true
var toDraw: array[BenchmarkMode, int]
toDraw[bmPoints] = 50000
toDraw[bmLines] = 1000
toDraw[bmHVLines] = 2000
toDraw[bmRect] = 2000
toDraw[bmRectfill] = 2000
toDraw[bmCirc] = 2000
toDraw[bmCircfill] = 2000
toDraw[bmSprite] = 1000
toDraw[bmSpriteIntScaled] = 1000
toDraw[bmSpriteScaled] = 1000
toDraw[bmSpriteRot] = 1000
toDraw[bmSpriteShearRot] = 1000
toDraw[bmTrifill] = 1000
toDraw[bmTTrifill] = 1000
var avgMs = 0f
var time = 0f
var cx = 0
var cy = 0
var secondTimer = 0f
var framesRendered = 0
var framesRenderedLastSecond = 0
proc gameInit() =
loadSpriteSheet(0, "spritesheet.png")
vsync = getVSync()
proc gameUpdate(dt: float32) =
time += dt
secondTimer += getRealDt()
if secondTimer >= 1f:
secondTimer = 0f
framesRenderedLastSecond = framesRendered
framesRendered = 0
cx = (sin(time / 5f) * 32f).int
cy = (sin(time / 3f) * 32f).int
if keyp(K_c):
useClip = not useClip
if keyp(K_v):
vsync = not vsync
setVSync(vsync)
if btn(pcLeft) and toDraw[mode] > 0:
toDraw[mode] -= 10
if btn(pcRight):
toDraw[mode] += 10
if btnpr(pcDown) and toDraw[mode] > 1000:
toDraw[mode] -= 1000
if btnpr(pcUp):
toDraw[mode] += 1000
if btnp(pcA):
mode.incWrap()
if btnp(pcB):
autoAdjust = not autoAdjust
if autoAdjust:
if lastMs < 15.0'f:
toDraw[mode] += 100
elif lastMs > 16.0'f:
if toDraw[mode] > 100:
toDraw[mode] -= 100
proc gameDraw() =
cls()
let tstart = getPerformanceCounter()
var count = 0
let toDraw = toDraw[mode]
setCamera(cx,cy)
if useClip:
clip(10,10,screenWidth-1-20,screenHeight-1-20)
case mode:
of bmPoints:
for i in 0..<toDraw:
setColor(8+rnd(8))
pset(rnd(screenWidth),rnd(screenHeight))
count += 1
of bmLines:
for i in 0..<toDraw:
setColor(8+rnd(8))
line(rnd(screenWidth),rnd(screenHeight),rnd(screenWidth),rnd(screenHeight))
count += 1
of bmHVLines:
for i in 0..<toDraw:
setColor(8+rnd(8))
if rnd(2) == 0:
hline(rnd(screenWidth),rnd(screenHeight),rnd(screenWidth))
else:
vline(rnd(screenWidth),rnd(screenHeight),rnd(screenHeight))
count += 1
of bmRect:
for i in 0..<toDraw:
setColor(8+rnd(8))
rect(rnd(screenWidth),rnd(screenHeight),rnd(screenWidth),rnd(screenHeight))
count += 1
of bmRectfill:
for i in 0..<toDraw:
setColor(8+rnd(8))
rectfill(rnd(screenWidth),rnd(screenHeight),rnd(screenWidth),rnd(screenHeight))
count += 1
of bmCirc:
for i in 0..<toDraw:
setColor(8+rnd(8))
circ(rnd(screenWidth),rnd(screenHeight),rnd(32))
count += 1
of bmCircfill:
for i in 0..<toDraw:
setColor(8+rnd(8))
circfill(rnd(screenWidth),rnd(screenHeight),rnd(32))
count += 1
of bmSprite:
for i in 0..<toDraw:
spr(16+rnd(8), rnd(screenWidth),rnd(screenHeight))
count += 1
of bmSpriteIntScaled:
for i in 0..<toDraw:
sprs(16+rnd(8), rnd(screenWidth),rnd(screenHeight), 1, 1, 2, 2)
count += 1
of bmSpriteScaled:
for i in 0..<toDraw:
sprss(16+rnd(8), rnd(screenWidth),rnd(screenHeight), 1, 1, 4+rnd(32), 4+rnd(32))
count += 1
of bmSpriteRot:
for i in 0..<toDraw:
sprRot(16+rnd(8), rnd(screenWidth),rnd(screenHeight), rnd(TAU))
count += 1
of bmSpriteShearRot:
for i in 0..<toDraw:
sprShearRot(16+rnd(8), rnd(screenWidth),rnd(screenHeight), rnd(TAU))
count += 1
of bmTrifill:
for i in 0..<toDraw:
setColor(8+rnd(8))
trifill(rnd(screenWidth),rnd(screenHeight),rnd(screenWidth),rnd(screenHeight),rnd(screenWidth),rnd(screenHeight))
count += 1
of bmTTrifill:
for i in 0..<toDraw:
let au = 0
let av = 8
let bu = 8
let bv = 16
let cu = 0
let cv = 16
ttrifill(rnd(screenWidth),rnd(screenHeight),au,av, rnd(screenWidth),rnd(screenHeight),bu,bv, rnd(screenWidth),rnd(screenHeight),cu,cv)
count += 1
let tend = getPerformanceCounter()
let ms = ((tend - tstart)*1000).float / getPerformanceFrequency().float
clip()
setCamera()
setColor(1)
box(-cx,-cy,screenWidth, screenHeight)
if useClip:
setColor(8)
box(10,10,screenWidth-20, screenHeight-20)
let str = &"{mode} x {toDraw} : {avgMs:0.2f} ms"
setColor(7)
printOutline(str, 4, 4)
if vsync:
printOutline("vsync", 4, 14)
else:
printOutline("delay", 4, 14)
lastMs = ms.float32
avgMs = lerp(avgMs, lastMs, 0.25f)
printOutline("fps: " & $framesRenderedLastSecond, 4, 24)
framesRendered += 1
nico.init("nico","benchmark")
nico.createWindow("nico benchmark", 128, 128, 4, false)
nico.run(gameInit, gameUpdate, gameDraw)