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
|
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <draw.h>
#include <mouse.h>
#include <keyboard.h>
#include <geometry.h>
enum {
MAXMAGNITUDE = 40
};
enum {
Cbg,
Cfg,
NCOLOR
};
typedef struct Strut Strut;
struct Strut
{
Point2 v;
Strut *next;
};
RFrame worldrf;
Image *pal[NCOLOR];
Strut *struts;
Point2 origin;
Strut*
newstrut(Point2 v)
{
Strut *s;
s = malloc(sizeof(Strut));
if(s == nil)
sysfatal("malloc: %r");
setmalloctag(s, getcallerpc(&v));
s->v = v;
s->next = nil;
return s;
}
Point
toscreen(Point2 p)
{
p = invrframexform(p, worldrf);
return Pt(p.x,p.y);
}
Point2
fromscreen(Point p)
{
return rframexform(Pt2(p.x,p.y,1), worldrf);
}
void
initpalette(void)
{
pal[Cbg] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0x000000ff);
pal[Cfg] = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xffffffff);
}
void
redraw(void)
{
Strut *s;
Point2 p0, p1;
p0 = origin;
lockdisplay(display);
draw(screen, screen->r, pal[Cbg], nil, ZP);
for(s = struts; s != nil; s = s->next){
p1 = addpt2(p0, s->v);
line(screen, toscreen(p0), toscreen(p1), Endsquare, Endarrow, 0, pal[Cfg], ZP);
p0 = p1;
}
flushimage(display, 1);
unlockdisplay(display);
}
void
resized(void)
{
lockdisplay(display);
if(getwindow(display, Refnone) < 0)
sysfatal("resize failed");
unlockdisplay(display);
worldrf.p = Pt2(screen->r.min.x,screen->r.max.y,1);
origin = Pt2(Dx(screen->r)/2,Dy(screen->r)/2,1);
redraw();
}
void
mouse(Mouse m)
{
Strut *s;
Point2 mpos, v;
double θ;
if((m.buttons & 1) != 0){
mpos = subpt2(fromscreen(m.xy), origin);
θ = atan2(mpos.y, mpos.x);
//v = Vec2(cos(frand() * 2*PI),sin(frand() * 2*PI));
v = Vec2(cos(θ),sin(θ));
v = mulpt2(v, frand()*MAXMAGNITUDE);
s = newstrut(v);
if(struts == nil)
struts = s;
else{
s->next = struts;
struts = s;
}
}
}
void
key(Rune r)
{
switch(r){
case Kdel:
case 'q':
threadexitsall(nil);
}
}
void
usage(void)
{
fprint(2, "usage: %s\n", argv0);
exits("usage");
}
void
threadmain(int argc, char *argv[])
{
Mousectl *mc;
Keyboardctl *kc;
Rune r;
ARGBEGIN{
default: usage();
}ARGEND;
if(argc > 0)
usage();
if(initdraw(nil, nil, nil) < 0)
sysfatal("initdraw: %r");
if((mc = initmouse(nil, screen)) == nil)
sysfatal("initmouse: %r");
if((kc = initkeyboard(nil)) == nil)
sysfatal("initkeyboard: %r");
initpalette();
worldrf.p = Pt2(screen->r.min.x,screen->r.max.y,1);
worldrf.bx = Vec2(1, 0);
worldrf.by = Vec2(0,-1);
origin = Pt2(Dx(screen->r)/2,Dy(screen->r)/2,1);
display->locking = 1;
unlockdisplay(display);
redraw();
for(;;){
enum { MOUSE, RESIZE, KEYBOARD };
Alt a[] = {
{mc->c, &mc->Mouse, CHANRCV},
{mc->resizec, nil, CHANRCV},
{kc->c, &r, CHANRCV},
{nil, nil, CHANEND}
};
switch(alt(a)){
case MOUSE:
mouse(mc->Mouse);
break;
case RESIZE:
resized();
break;
case KEYBOARD:
key(r);
break;
}
redraw();
}
}
|