-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclock.ps
More file actions
236 lines (195 loc) · 5.5 KB
/
clock.ps
File metadata and controls
236 lines (195 loc) · 5.5 KB
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
%!
% Draws an animated clock.
% Author: Dakota Schneider, dakota@lclark.edu
% Usage: gs midterm2_clockwithsleep.ps
% Options: None.
% Configuration: can set target frame length or calculate from target framerate.
% draws a clock
% taken from: http://doc.novsu.ac.ru/oreilly/web/cgi/ch06_02.htm#CGI-CHP-6-SECT-2.4
% modified to display seconds
/clock
% - clock -
{
gsave
% we get the time from the %Calendar% device, available in ghostscript env
% thanks to https://en.wikibooks.org/wiki/PostScript_FAQ#How_to_get_date_and_time.3F
(%Calendar%) /IODevice resourcestatus {
pop pop (%Calendar%) currentdevparams
dup /Running get {
dup /Year get /year exch def
dup /Month get /month exch def
dup /Day get /day exch def
dup /Hour get /hour exch def
dup /Minute get /min exch def
/Second get /sec exch def
} {
(Clock/calendar is present but not running.\n) print
} ifelse
} {
(No clock/calendar present.\n) print
} ifelse
% configuration
/max_length 150 def
/line_size 1.5 def
/marker 5 def
% calculated values
/center { 0 dup } def
/radius { max_length 2 div } def
/hour_segment { 0.50 radius mul } def
/minute_segment { 0.80 radius mul } def
/second_segment { 0.90 radius mul } def
% some color constants
/red { 1 0 0 setrgbcolor } def
/green { 0 1 0 setrgbcolor } def
/blue { 0 0 1 setrgbcolor } def
/black { 0 0 0 setrgbcolor } def
/white { 1 1 1 setrgbcolor } def
/hour_angle {
hour min 60 div add 3 sub 30 mul
neg
} def
/minute_angle {
min sec 60 div add 15 sub 6 mul
neg
} def
/second_angle {
sec 6 mul
neg
} def
line_size setlinewidth
% draw face (covers up previous drawing)
center radius 1.1 mul 0 360 arc white fill
center radius 0 360 arc black stroke
% draw
black
1 1 12 {
pop
radius marker sub 0 moveto
% marker 0 rlineto red stroke
marker 0 rlineto stroke
30 rotate
} for
% hour hand
center moveto
hour_segment hour_angle cos mul
hour_segment hour_angle sin mul
lineto green stroke
% minute
center moveto
minute_segment minute_angle cos mul
minute_segment minute_angle sin mul
lineto green stroke
% second
center moveto
second_segment second_angle cos mul
second_segment second_angle sin mul
lineto red stroke
% second hand cap
center line_size 2 mul 0 360 arc red fill
grestore
} def
/sleep
% <int:milliseconds> sleep -
{
% note: while realtime rolls over to 0 when too large,
% we ignore this possibility as 32-bit int can contain
% ~50 days worth of milliseconds
% nothing to do if we get a negative value
dup 0 gt {
% delta
realtime
% delta start
add
% end
% busy wait until
{
% end time < current time
dup realtime lt {
exit
} if
} loop
} if
% clean up
pop
} def
%
% Draw FPS
%
% need to know where the upper right corner is
/pagewidth 612.0 def % standard for US Letter size paper
/pageheight 792.0 def
% looks familiar
/prnum {
( ) show
100 string % create a string of 20 characters
cvs % convert object to string : object string cvs => string
show
} def
% framerate calculations
/lastframe realtime def
/framerate 0 def
/drawfps
% - drawfps -
{
% draws current framerate in the upper right corner of the page
gsave
% display setup
pagewidth 48 sub pageheight 32 sub translate
% black background
0 0 0 setrgbcolor
0 0 moveto
0 12 lineto
48 12 lineto
48 0 lineto
closepath
fill
% set up text color and position
0 0 moveto
1 1 1 setrgbcolor
/Helvetica findfont 12 scalefont setfont
% compute framerate, average with previous value
realtime lastframe sub % frame delta in ms
dup 0 eq { 1 } if % handle edge case on first loop
% realtime is interpreter uptime, so running this on
% a camputer can have issues on the first loop where we've
% *just recently* set lastframe and realtime returns the same value
1000 exch div % frames per second
% rolling average for a tiny bit of stability
framerate add 2 div /framerate exch def
% display
framerate prnum
grestore
/lastframe realtime def
} def
% framerate cap
/target_framerate 30 def
% how long should we wait per frame? (milliseconds)
/target_frametime 1000 target_framerate div def
% uncomment below for uncapped
% /target_frametime 0 def
% almost like real programming
/main
% - main -
{
{
% update last frame render time
/starttime realtime def
% provide two minutes of operation before exiting
starttime 1000 60 mul 2 mul gt { exit } if
% do the thing
gsave
300 300 translate
clock
grestore
% draw FPS on top
drawfps
% last but not least, flush this frame
% thanks to http://stackoverflow.com/a/8396072/362042
flushpage
% busy wait until it's time to draw the next frame
starttime target_frametime add realtime sub sleep
} loop
} def
% let's boogie
main
showpage