Debian Codegraph @

examples/demo0.c

raw
1 /*
2 * demo demo for libcaca version 0.9
3 * Copyright (c) 2003-2010 Sam Hocevar <[email protected]>
4 * All Rights Reserved
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 * 02111-1307 USA
20 */
21
22 #include "config.h"
23
24 #if !defined(__KERNEL__)
25 # include <math.h>
26 # include <string.h>
27 # include <stdio.h>
28 #endif
29
30 #include "caca0.h"
31
32 static void display_menu(void);
33
34 static void demo_all(void);
35
36 static void demo_color(void);
37 static void demo_dots(void);
38 static void demo_lines(void);
39 static void demo_boxes(void);
40 static void demo_ellipses(void);
41 static void demo_triangles(void);
42 #if 0
43 static void demo_sprites(void);
44 #endif
45 static void demo_render(void);
46
47 int bounds = 0;
48 int outline = 0;
49 int dithering = 0;
50 struct caca_sprite *sprite = NULL;
51
52 int main(int argc, char **argv)
53 {
54 void (*demo)(void) = NULL;
55 int quit = 0;
56
57 if(caca_init())
58 return 1;
59
60 caca_set_delay(40000);
61
62 /* Initialize data */
63 #if 0
64 sprite = caca_load_sprite("caca.txt");
65 if(!sprite)
66 sprite = caca_load_sprite("examples/caca.txt");
67 #endif
68
69 /* Main menu */
70 display_menu();
71 caca_refresh();
72
73 /* Go ! */
74 while(!quit)
75 {
76 int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
77 int event;
78
79 while((event = caca_get_event(CACA_EVENT_ANY)))
80 {
81 if(demo && (event & CACA_EVENT_KEY_PRESS))
82 {
83 menu = 1;
84 demo = NULL;
85 }
86 else if(event & CACA_EVENT_KEY_PRESS)
87 {
88 switch(event & 0xffff)
89 {
90 case 'q':
91 case 'Q':
92 case CACA_KEY_ESCAPE:
93 demo = NULL;
94 quit = 1;
95 break;
96 case 'o':
97 case 'O':
98 outline = (outline + 1) % 3;
99 display_menu();
100 caca_refresh();
101 break;
102 case 'b':
103 case 'B':
104 bounds = (bounds + 1) % 2;
105 display_menu();
106 caca_refresh();
107 break;
108 case 'd':
109 case 'D':
110 dithering = (dithering + 1) % 5;
111 caca_set_dithering(CACA_DITHERING_NONE + dithering);
112 display_menu();
113 caca_refresh();
114 break;
115 case 'c':
116 demo = demo_color;
117 break;
118 case 'f':
119 case 'F':
120 demo = demo_all;
121 break;
122 case '1':
123 demo = demo_dots;
124 break;
125 case '2':
126 demo = demo_lines;
127 break;
128 case '3':
129 demo = demo_boxes;
130 break;
131 case '4':
132 demo = demo_triangles;
133 break;
134 case '5':
135 demo = demo_ellipses;
136 break;
137 #if 0
138 case 's':
139 case 'S':
140 if(sprite)
141 demo = demo_sprites;
142 break;
143 #endif
144 case 'r':
145 case 'R':
146 demo = demo_render;
147 break;
148 }
149
150 if(demo)
151 caca_clear();
152 }
153 else if(event & CACA_EVENT_MOUSE_MOTION)
154 {
155 mouse = 1;
156 xmouse = (event & 0xfff000) >> 12;
157 ymouse = event & 0xfff;
158 }
159 }
160
161 if(menu || (mouse && !demo))
162 {
163 display_menu();
164 if(mouse && !demo)
165 {
166 caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
167 caca_putstr(xmouse, ymouse, "|\\");
168 }
169 caca_refresh();
170 mouse = menu = 0;
171 }
172 else if(demo)
173 {
174 demo();
175
176 caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
177 caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2);
178 caca_printf(4, 1, "[%i.%i fps]----",
179 1000000 / caca_get_rendertime(),
180 (10000000 / caca_get_rendertime()) % 10);
181 caca_refresh();
182 }
183 }
184
185 /* Clean up */
186 #if 0
187 caca_free_sprite(sprite);
188 #endif
189 caca_end();
190
191 return 0;
192 }
193
194 static void display_menu(void)
195 {
196 int xo = caca_get_width() - 2;
197 int yo = caca_get_height() - 2;
198
199 caca_clear();
200 caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
201 caca_draw_thin_box(1, 1, xo, yo);
202
203 caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
204 caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
205
206 caca_putstr(4, 6, "demos:");
207 caca_putstr(4, 7, "'f': full");
208 caca_putstr(4, 8, "'1': dots");
209 caca_putstr(4, 9, "'2': lines");
210 caca_putstr(4, 10, "'3': boxes");
211 caca_putstr(4, 11, "'4': triangles");
212 caca_putstr(4, 12, "'5': ellipses");
213 caca_putstr(4, 13, "'c': colour");
214 caca_putstr(4, 14, "'r': render");
215 if(sprite)
216 caca_putstr(4, 15, "'s': sprites");
217
218 caca_putstr(4, 16, "settings:");
219 caca_printf(4, 17, "'o': outline: %s",
220 outline == 0 ? "none" : outline == 1 ? "solid" : "thin");
221 caca_printf(4, 18, "'b': drawing boundaries: %s",
222 bounds == 0 ? "screen" : "infinite");
223 caca_printf(4, 19, "'d': dithering (%s)",
224 caca_get_dithering_name(CACA_DITHERING_NONE + dithering));
225
226 caca_putstr(4, yo - 2, "'q': quit");
227 }
228
229 static void demo_all(void)
230 {
231 static int i = 0;
232
233 int j, xo, yo, xa, ya, xb, yb, xc, yc;
234
235 i++;
236
237 caca_clear();
238
239 /* Draw the sun */
240 caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
241 xo = caca_get_width() / 4;
242 yo = caca_get_height() / 4 + 5 * sin(0.03*i);
243
244 for(j = 0; j < 16; j++)
245 {
246 xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
247 ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
248 caca_draw_thin_line(xo, yo, xa, ya);
249 }
250
251 j = 15 + sin(0.03*i) * 8;
252 caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
253 caca_fill_ellipse(xo, yo, j, j / 2, '#');
254 caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
255 caca_draw_ellipse(xo, yo, j, j / 2, '#');
256
257 /* Draw the pyramid */
258 xo = caca_get_width() * 5 / 8;
259 yo = 2;
260
261 xa = caca_get_width() / 8 + sin(0.03*i) * 5;
262 ya = caca_get_height() / 2 + cos(0.03*i) * 5;
263
264 xb = caca_get_width() - 10 - cos(0.02*i) * 10;
265 yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
266
267 xc = caca_get_width() / 4 - sin(0.02*i) * 5;
268 yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
269
270 caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
271 caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
272 caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
273 caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
274
275 caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
276 caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
277 caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
278 caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
279
280 caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
281 caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
282 caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
283 caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
284
285 /* Draw a background triangle */
286 xa = 2;
287 ya = 2;
288
289 xb = caca_get_width() - 3;
290 yb = caca_get_height() / 2;
291
292 xc = caca_get_width() / 3;
293 yc = caca_get_height() - 3;
294
295 caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
296 caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
297
298 xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
299 yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
300
301 caca_draw_thin_line(xa, ya, xo, yo);
302 caca_draw_thin_line(xb, yb, xo, yo);
303 caca_draw_thin_line(xc, yc, xo, yo);
304
305 /* Draw a sprite on the pyramid */
306 #if 0
307 caca_draw_sprite(xo, yo, sprite, 0);
308 #endif
309
310 /* Draw a trail behind the foreground sprite */
311 for(j = i - 60; j < i; j++)
312 {
313 int delta = caca_rand(-5, 5);
314 caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
315 caca_putchar(caca_get_width() / 2
316 + cos(0.02*j) * (delta + caca_get_width() / 4),
317 caca_get_height() / 2
318 + sin(0.02*j) * (delta + caca_get_height() / 3),
319 '#');
320 }
321
322 /* Draw foreground sprite */
323 #if 0
324 caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
325 caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
326 sprite, 0);
327 #endif
328 }
329
330 static void demo_dots(void)
331 {
332 int xmax = caca_get_width() - 1;
333 int ymax = caca_get_height() - 1;
334 int i;
335 static char chars[10] =
336 {
337 '+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
338 };
339
340 for(i = 1000; i--;)
341 {
342 /* Putpixel */
343 caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
344 caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
345 chars[caca_rand(0, 9)]);
346 }
347 }
348
349 static void demo_color(void)
350 {
351 int i, j;
352 char buf[BUFSIZ];
353
354 caca_clear();
355 for(i = 0; i < 16; i++)
356 {
357 sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
358 caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
359 caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
360 for(j = 0; j < 16; j++)
361 {
362 caca_set_color(i, j);
363 caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
364 }
365 }
366 }
367
368 static void demo_lines(void)
369 {
370 int w = caca_get_width();
371 int h = caca_get_height();
372 int xa, ya, xb, yb;
373
374 if(bounds)
375 {
376 xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
377 xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
378 }
379 else
380 {
381 xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
382 xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
383 }
384
385 caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
386 if(outline > 1)
387 caca_draw_thin_line(xa, ya, xb, yb);
388 else
389 caca_draw_line(xa, ya, xb, yb, '#');
390 }
391
392 static void demo_boxes(void)
393 {
394 int w = caca_get_width();
395 int h = caca_get_height();
396 int xa, ya, xb, yb;
397
398 if(bounds)
399 {
400 xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
401 xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
402 }
403 else
404 {
405 xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
406 xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
407 }
408
409 caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
410 caca_fill_box(xa, ya, xb, yb, '#');
411
412 caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
413 if(outline == 2)
414 caca_draw_thin_box(xa, ya, xb, yb);
415 else if(outline == 1)
416 caca_draw_box(xa, ya, xb, yb, '#');
417 }
418
419 static void demo_ellipses(void)
420 {
421 int w = caca_get_width();
422 int h = caca_get_height();
423 int x, y, a, b;
424
425 if(bounds)
426 {
427 x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
428 a = caca_rand(0, w); b = caca_rand(0, h);
429 }
430 else
431 {
432 do
433 {
434 x = caca_rand(0, w); y = caca_rand(0, h);
435 a = caca_rand(0, w); b = caca_rand(0, h);
436
437 } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
438 }
439
440 caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
441 caca_fill_ellipse(x, y, a, b, '#');
442
443 caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
444 if(outline == 2)
445 caca_draw_thin_ellipse(x, y, a, b);
446 else if(outline == 1)
447 caca_draw_ellipse(x, y, a, b, '#');
448 }
449
450 static void demo_triangles(void)
451 {
452 int w = caca_get_width();
453 int h = caca_get_height();
454 int xa, ya, xb, yb, xc, yc;
455
456 if(bounds)
457 {
458 xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
459 xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
460 xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
461 }
462 else
463 {
464
465 xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
466 xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
467 xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
468 }
469
470 caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
471 caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
472
473 caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
474 if(outline == 2)
475 caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
476 else if(outline == 1)
477 caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
478 }
479
480 #if 0
481 static void demo_sprites(void)
482 {
483 caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
484 caca_rand(0, caca_get_height() - 1), sprite, 0);
485 }
486 #endif
487
488 #if 0
489 static void demo_render(void)
490 {
491 struct caca_bitmap *bitmap;
492 //short buffer[256*256];
493 //short *dest = buffer;
494 int buffer[256*256];
495 int *dest = buffer;
496 int x, y, z;
497 static int i = 0;
498
499 i = (i + 1) % 512;
500 z = i < 256 ? i : 511 - i;
501
502 for(x = 0; x < 256; x++)
503 for(y = 0; y < 256; y++)
504 {
505 //*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
506 *dest++ = (x << 16) | (y << 8) | (z);
507 }
508
509 //bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
510 bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
511 caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
512 bitmap, buffer);
513 caca_free_bitmap(bitmap);
514 }
515 #endif
516
517 static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
518
519 static void demo_render(void)
520 {
521 struct caca_bitmap *bitmap;
522 int buffer[256*256];
523 int *dest;
524 int x, y, z, xo, yo;
525 static int i = 0;
526
527 i++;
528
529 dest = buffer;
530 for(x = 0; x < 256; x++)
531 for(y = 0; y < 256; y++)
532 {
533 *dest++ = 0xff000000;
534 }
535
536 /* red */
537 xo = 128 + 48 * sin(0.02 * i);
538 yo = 128 + 48 * cos(0.03 * i);
539 for(z = 0; z < 240; z++)
540 draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
541
542 /* green */
543 xo = 128 + 48 * sin(2 + 0.06 * i);
544 yo = 128 + 48 * cos(2 + 0.05 * i);
545 for(z = 0; z < 240; z++)
546 draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
547
548 /* blue */
549 xo = 128 + 48 * sin(1 + 0.04 * i);
550 yo = 128 + 48 * cos(1 + 0.03 * i);
551 for(z = 0; z < 240; z++)
552 draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
553
554 bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
555 caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
556 bitmap, (char *)buffer);
557 caca_free_bitmap(bitmap);
558 }
559
560 static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
561 {
562 int t, dx, dy;
563
564 #define POINT(X,Y) \
565 buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
566
567 for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
568 {
569 POINT(x - dx / 3, y - dy / 3);
570 POINT(x + dx / 3, y - dy / 3);
571 POINT(x - dx / 3, y + dy / 3);
572 POINT(x + dx / 3, y + dy / 3);
573
574 POINT(x - dy / 3, y - dx / 3);
575 POINT(x + dy / 3, y - dx / 3);
576 POINT(x - dy / 3, y + dx / 3);
577 POINT(x + dy / 3, y + dx / 3);
578
579 t += t > 0 ? dx - dy-- : dx;
580 }
581 }
582