Debian Codegraph @

examples/demo.c

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