-
Notifications
You must be signed in to change notification settings - Fork 2
/
logo.c
307 lines (250 loc) · 5.67 KB
/
logo.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#include "jio.c"
#include "graphics.c"
#include <math.h>
// TODO: use gx and gy?
float pi= 3.141592654; // TODO: fix
int c= white, pen= 1, turtle= 1, size= 1;
float rad= 0;
void moveto(int x, int y) {
gx= x; gy= y;
}
int forward(int n) {
int dx= n*cos(rad);
int dy= n*sin(rad);
if (pen) gline(gx, gy, dx, dy, c);
gx+= dx;
gy+= dy;
return n;
}
float angle(float d) {
return rad= 2*pi*d/360;
}
float left(float d) {
return rad-= 2*pi*d/360;
}
float right(float d) {
return rad+= 2*pi*d/360;
}
int xc, yc;
void home() {
moveto(0, 0);
}
void center() {
gx= xc= gsizex/2;
gy= yc= gsizey/2;
rad= 0;
c= white;
}
// plot a Function in x=[xa,xb] with STeP, on y=[ya,yb] using Color
void graph(float xa, float xb, float stp, float ya, float yb, double f(double), int c) {
for(float x= xa; x<=xb; x+= stp) {
float y= f(x);
gset(x/(xb-xa)*2*xc+xc, y/(yb-ya)*2*yc+yc, c);
gupdate();
}
}
// graphs w axis and label
void mygraph() {
wclear();
center();
// draw axis
//center(); angle( 0); forward(xc); {
center(); forward(xc); {
gx-= 8; gputc('x');
}
center(); left(90); forward(xc); {
gx-= 8; gy=0; gputc('y');
}
// TODO: these two doesn't work???
center(); angle(180); forward(xc);
center(); angle(270); forward(xc);
center(); right(180); forward(xc);
center(); left(90); forward(xc);
graph(-10,10, 0.01, -1.2,1.2, sin, red);
graph(-10,10, 0.1, -1.2,1.2, cos, green);
graph(-10,10, 0.01, -1.2,1.2, tan, cyan);
gupdate();
}
void myclock() {
int h= 1, m= 45, s= 0;
int w= gsizex/2;
if (gsizey/2 < w) w= gsizey/2;
w-= 3;
wclear();
while(peekey()<0) {
//usleep(1000*1000);
usleep(1000*1000/10);
s++;
if (s>=60) m++,s=0;
if (m>=60) h++,m=0;
gclear();
// hours
center(); left(90);
right(h*360/12);
c= red;
forward(w*0.7);
// minutes
center(); left(90);
right(m*360/60);
c= cyan;
forward(w*0.8);
// seconds
center(); left(90);
right(s*360/60);
c= white;
forward(w);
gupdate();
}
}
// get a value from *P and move pointer
// if no value return DEF
int argdef(char** p, int def) {
if (!p || !*p) return 0;
char* s = *p;
int r= 0;
while(isdigit(*s)) {
r= 10*r + *s++-'0';
}
if (s== *p && *s) {
if (*s=='$') { // arg
assert(!"TODO");
} else if (*s=='*') { // pop stack?
assert(!"TODO");
}
}
if (s==*p) r= def;
*p= s;
return r;
}
int arg(char** p) {
return argdef(p, 0);
}
// LOGO single char commands
// used: J K M N Q (T) V
// Forward [N]
// Up (Pen)
// Down (Pen)
// Show (turtle)
// Hide (turtle)
// Left [D] (default 90)
// Right [D] (default 90)
// Angle [D] (default 90/up)
// cOlor [N] (default white)
// siZe [N] (default 1)
// #repeat [N] [...]
// Wait [N] (default 1000ms)
// Erase
// Box W [H] (default W)
// I - cewnter lol
// Text [foobar] or Text "foobar"
// X [N] (default 0)
// Y [N] (default 0)
// V [N] (background default none)
// Graphics [W],[H],[%s] (bitblt/"sprite")
// TODO: how to say filled?
// Circle
// Pattern
void logobin(char* p, char* bin) {
while(*p) {
//printf(">%s\n", p);
switch(tolower(*p++)) {
case ' ': case '\t': case '\n': break;
case '[': break;
case ']': case 0: return;
case 'f': forward(arg(&p)); break;
case 'e': gclear(); break;
case 'u': pen= 0; assert(!"TODO"); break;
case 'd': pen= 1; assert(!"TODO"); break;
case 'h': turtle= 0; assert(!"TODO"); break;
case 's': turtle= 1; assert(!"TODO"); break;
case 'l': left(argdef(&p, 90)); break;
case 'r': right(argdef(&p, 90)); break;
case 'a': angle(argdef(&p, 90)); break;
case 'o': gfg= c= argdef(&p, white); break; // TODO: cOlor names
case 'v': gbg= argdef(&p, none); break; // TODO: cOlor names
case 'z': size= argdef(&p, 1); break;
case '#': { int n= arg(&p); for(int i=1; i<=n; i++) logobin(p, bin); }
case 'w': usleep(argdef(&p, 1000)*1000); break;
case 'i': center(); break;
case 'b': { int w= arg(&p), h=argdef(&p, w); gbox(gx, gy, w, h, c); break; }
case 't': { char d= *p++=='['?']':'"'; while(*p && *p!=d) gputc(*p++); }
case 'x': gx= argdef(&p, 0); break;
case 'y': gy= argdef(&p, 0); break;
case 'g': if (bin) gbitblit(gx, gy, bin); break;
//case 'c': { int x=arg(&p), y=arg(&p), r=arg(&p); circle(x, y, r); break; }
default: printf("\n%%ERROR.logo: %s\n", p);
}
}
}
void logo(char* p) {
logobin(p, 0);
}
// bitmap w size "sprite"?
char bits[]= { 0x10, 0x10,
0xff, 0xff,
0x80, 0x01,
0xff, 0xff,
0x80, 0x01,
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
0x80, 0x01, 0x80, 0x01, 0x80, 0x01, 0x80, 0x01,
0x20, 0x20,
0x40, 0x40,
0x80, 0x80,
0xff, 0xff,
};
// TODO: rgb multicolor "sprite"
char* rgbs[]= {
};
int main(void) {
jio();
cursoroff();
pixels_per_col= 2;
//pixels_per_col= 1; // TODO: 1
pixels_per_row= 2;
gcanvas();
switch(4) {
case 1: {
forward(10);
c=green;
forward(10);
left(45);
c=red;
forward(10);
right(45+90);
c=cyan;
forward(9);
//gline(x,y,0,10,c);
//gline(x,y,+10,0,green);
//gline(x,y,0,10,c);
//gline(x,y,+10,0,green);
//x+=10; y+=0;
gupdate();
break; }
case 2: {
mygraph();
key();
break;
}
case 3: {
myclock();
break;
}
case 4: {
logo("e");
logo("i#4[f30l]");
logo("i#90[f2l4]"); // "circle"
logo("ib50r45o0f50");
logo("o3t\"foo\"xyo1t[FiSH]");
logo("ib50t[GURKA]");
logobin("x30y30o2g", bits);
gupdate();
char* b= gcopy(xc, yc, 100, 100);
gpaste(20, 20, b);
free(b);
gupdate();
break;
}
}
cursoron();
exit(0);
}