2
* demo demo for libcaca version 0.9
3
* Copyright (c) 2003 Sam Hocevar <sam@zoy.org>
6
* $Id: demo0.c 829 2006-09-17 12:44:18Z jylam $
8
* This program is free software; you can redistribute it and/or
9
* modify it under the terms of the GNU Lesser General Public
10
* License as published by the Free Software Foundation; either
11
* version 2 of the License, or (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
* Lesser General Public License for more details.
18
* You should have received a copy of the GNU Lesser General Public
19
* License along with this program; if not, write to the Free Software
20
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
26
#if !defined(__KERNEL__)
34
static void display_menu(void);
36
static void demo_all(void);
38
static void demo_color(void);
39
static void demo_dots(void);
40
static void demo_lines(void);
41
static void demo_boxes(void);
42
static void demo_ellipses(void);
43
static void demo_triangles(void);
45
static void demo_sprites(void);
47
static void demo_render(void);
52
struct caca_sprite *sprite = NULL;
54
int main(int argc, char **argv)
56
void (*demo)(void) = NULL;
62
caca_set_delay(40000);
66
sprite = caca_load_sprite(DATADIR "/caca.txt");
68
sprite = caca_load_sprite("caca.txt");
70
sprite = caca_load_sprite("examples/caca.txt");
79
int menu = 0, mouse = 0, xmouse = 0, ymouse = 0;
82
while((event = caca_get_event(CACA_EVENT_ANY)))
84
if(demo && (event & CACA_EVENT_KEY_PRESS))
89
else if(event & CACA_EVENT_KEY_PRESS)
91
switch(event & 0xffff)
100
outline = (outline + 1) % 3;
105
bounds = (bounds + 1) % 2;
110
dithering = (dithering + 1) % 5;
111
caca_set_dithering(CACA_DITHERING_NONE + dithering);
131
demo = demo_triangles;
134
demo = demo_ellipses;
152
else if(event & CACA_EVENT_MOUSE_MOTION)
155
xmouse = (event & 0xfff000) >> 12;
156
ymouse = event & 0xfff;
160
if(menu || (mouse && !demo))
165
caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
166
caca_putstr(xmouse, ymouse, "|\\");
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);
187
caca_free_sprite(sprite);
194
static void display_menu(void)
196
int xo = caca_get_width() - 2;
197
int yo = caca_get_height() - 2;
200
caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
201
caca_draw_thin_box(1, 1, xo, yo);
203
caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo");
204
caca_putstr((xo - strlen("==============")) / 2, 4, "==============");
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");
216
caca_putstr(4, 15, "'s': sprites");
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));
226
caca_putstr(4, yo - 2, "'q': quit");
230
static void demo_all(void)
234
int j, xo, yo, xa, ya, xb, yb, xc, yc;
241
caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
242
xo = caca_get_width() / 4;
243
yo = caca_get_height() / 4 + 5 * sin(0.03*i);
245
for(j = 0; j < 16; j++)
247
xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8);
248
ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8);
249
caca_draw_thin_line(xo, yo, xa, ya);
252
j = 15 + sin(0.03*i) * 8;
253
caca_set_color(CACA_COLOR_WHITE, CACA_COLOR_BLACK);
254
caca_fill_ellipse(xo, yo, j, j / 2, '#');
255
caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
256
caca_draw_ellipse(xo, yo, j, j / 2, '#');
258
/* Draw the pyramid */
259
xo = caca_get_width() * 5 / 8;
262
xa = caca_get_width() / 8 + sin(0.03*i) * 5;
263
ya = caca_get_height() / 2 + cos(0.03*i) * 5;
265
xb = caca_get_width() - 10 - cos(0.02*i) * 10;
266
yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5;
268
xc = caca_get_width() / 4 - sin(0.02*i) * 5;
269
yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5;
271
caca_set_color(CACA_COLOR_GREEN, CACA_COLOR_BLACK);
272
caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%');
273
caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
274
caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya);
276
caca_set_color(CACA_COLOR_RED, CACA_COLOR_BLACK);
277
caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
278
caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
279
caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
281
caca_set_color(CACA_COLOR_BLUE, CACA_COLOR_BLACK);
282
caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%');
283
caca_set_color(CACA_COLOR_YELLOW, CACA_COLOR_BLACK);
284
caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc);
286
/* Draw a background triangle */
290
xb = caca_get_width() - 3;
291
yb = caca_get_height() / 2;
293
xc = caca_get_width() / 3;
294
yc = caca_get_height() - 3;
296
caca_set_color(CACA_COLOR_CYAN, CACA_COLOR_BLACK);
297
caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
299
xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3;
300
yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2;
302
caca_draw_thin_line(xa, ya, xo, yo);
303
caca_draw_thin_line(xb, yb, xo, yo);
304
caca_draw_thin_line(xc, yc, xo, yo);
306
/* Draw a sprite on the pyramid */
308
caca_draw_sprite(xo, yo, sprite, 0);
311
/* Draw a trail behind the foreground sprite */
312
for(j = i - 60; j < i; j++)
314
int delta = caca_rand(-5, 5);
315
caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
316
caca_putchar(caca_get_width() / 2
317
+ cos(0.02*j) * (delta + caca_get_width() / 4),
318
caca_get_height() / 2
319
+ sin(0.02*j) * (delta + caca_get_height() / 3),
323
/* Draw foreground sprite */
325
caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4,
326
caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3,
331
static void demo_dots(void)
333
int xmax = caca_get_width() - 1;
334
int ymax = caca_get_height() - 1;
336
static char chars[10] =
338
'+', '-', '*', '#', 'X', '@', '%', '$', 'M', 'W'
344
caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
345
caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax),
346
chars[caca_rand(0, 9)]);
350
static void demo_color(void)
356
for(i = 0; i < 16; i++)
358
sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i));
359
caca_set_color(CACA_COLOR_LIGHTGRAY, CACA_COLOR_BLACK);
360
caca_putstr(4, i + (i >= 8 ? 4 : 3), buf);
361
for(j = 0; j < 16; j++)
363
caca_set_color(i, j);
364
caca_putstr((j >= 8 ? 41 : 40) + j * 2, i + (i >= 8 ? 4 : 3), "# ");
369
static void demo_lines(void)
371
int w = caca_get_width();
372
int h = caca_get_height();
377
xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
378
xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
382
xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
383
xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
386
caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
388
caca_draw_thin_line(xa, ya, xb, yb);
390
caca_draw_line(xa, ya, xb, yb, '#');
393
static void demo_boxes(void)
395
int w = caca_get_width();
396
int h = caca_get_height();
401
xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
402
xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
406
xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
407
xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
410
caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
411
caca_fill_box(xa, ya, xb, yb, '#');
413
caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
415
caca_draw_thin_box(xa, ya, xb, yb);
416
else if(outline == 1)
417
caca_draw_box(xa, ya, xb, yb, '#');
420
static void demo_ellipses(void)
422
int w = caca_get_width();
423
int h = caca_get_height();
428
x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h);
429
a = caca_rand(0, w); b = caca_rand(0, h);
435
x = caca_rand(0, w); y = caca_rand(0, h);
436
a = caca_rand(0, w); b = caca_rand(0, h);
438
} while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h);
441
caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
442
caca_fill_ellipse(x, y, a, b, '#');
444
caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
446
caca_draw_thin_ellipse(x, y, a, b);
447
else if(outline == 1)
448
caca_draw_ellipse(x, y, a, b, '#');
451
static void demo_triangles(void)
453
int w = caca_get_width();
454
int h = caca_get_height();
455
int xa, ya, xb, yb, xc, yc;
459
xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h);
460
xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h);
461
xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h);
466
xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1);
467
xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1);
468
xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1);
471
caca_set_color(caca_rand(0, 15), caca_rand(0, 15));
472
caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#');
474
caca_set_color(caca_rand(0, 15), CACA_COLOR_BLACK);
476
caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc);
477
else if(outline == 1)
478
caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#');
482
static void demo_sprites(void)
484
caca_draw_sprite(caca_rand(0, caca_get_width() - 1),
485
caca_rand(0, caca_get_height() - 1), sprite, 0);
490
static void demo_render(void)
492
struct caca_bitmap *bitmap;
493
//short buffer[256*256];
494
//short *dest = buffer;
501
z = i < 256 ? i : 511 - i;
503
for(x = 0; x < 256; x++)
504
for(y = 0; y < 256; y++)
506
//*dest++ = ((x >> 3) << 11) | ((y >> 2) << 5) | ((z >> 3));
507
*dest++ = (x << 16) | (y << 8) | (z);
510
//bitmap = caca_create_bitmap(16, 256, 256, 2 * 256, 0xf800, 0x07e0, 0x001f, 0x0000);
511
bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
512
caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
514
caca_free_bitmap(bitmap);
518
static void draw_circle(int *buffer, int xo, int yo, int r, int mask, int val);
520
static void demo_render(void)
522
struct caca_bitmap *bitmap;
531
for(x = 0; x < 256; x++)
532
for(y = 0; y < 256; y++)
534
*dest++ = 0xff000000;
538
xo = 128 + 48 * sin(0.02 * i);
539
yo = 128 + 48 * cos(0.03 * i);
540
for(z = 0; z < 240; z++)
541
draw_circle(buffer, xo, yo, z, 0x00ff0000, 200 << 16);
544
xo = 128 + 48 * sin(2 + 0.06 * i);
545
yo = 128 + 48 * cos(2 + 0.05 * i);
546
for(z = 0; z < 240; z++)
547
draw_circle(buffer, xo, yo, z, 0x0000ff00, 200 << 8);
550
xo = 128 + 48 * sin(1 + 0.04 * i);
551
yo = 128 + 48 * cos(1 + 0.03 * i);
552
for(z = 0; z < 240; z++)
553
draw_circle(buffer, xo, yo, z, 0x000000ff, 200);
555
bitmap = caca_create_bitmap(32, 256, 256, 4 * 256, 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000);
556
caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
557
bitmap, (char *)buffer);
558
caca_free_bitmap(bitmap);
561
static void draw_circle(int *buffer, int x, int y, int r, int mask, int val)
566
buffer[(X) + 256 * (Y)] = 0xff000000 | (buffer[(X) + 256 * (Y)] & ~mask) | val;
568
for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
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
POINT(x + dx / 3, y + dy / 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
POINT(x + dy / 3, y + dx / 3);
580
t += t > 0 ? dx - dy-- : dx;