1 | /* |
---|
2 | * demo demo using libcaca |
---|
3 | * Copyright (c) 2003 Sam Hocevar <sam@zoy.org> |
---|
4 | * All Rights Reserved |
---|
5 | * |
---|
6 | * $Id: demo.c 195 2003-11-16 17:41:43Z sam $ |
---|
7 | * |
---|
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. |
---|
12 | * |
---|
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. |
---|
17 | * |
---|
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 |
---|
21 | * 02111-1307 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include "config.h" |
---|
25 | |
---|
26 | #include <math.h> |
---|
27 | #include <string.h> |
---|
28 | #include <stdio.h> |
---|
29 | |
---|
30 | #include <gdk/gdk.h> |
---|
31 | #include <gdk/gdkpixbuf.h> |
---|
32 | |
---|
33 | #include "caca.h" |
---|
34 | |
---|
35 | static void display_menu(void); |
---|
36 | |
---|
37 | static void demo_all(void); |
---|
38 | |
---|
39 | static void demo_color(void); |
---|
40 | static void demo_dots(void); |
---|
41 | static void demo_lines(void); |
---|
42 | static void demo_boxes(void); |
---|
43 | static void demo_ellipses(void); |
---|
44 | static void demo_triangles(void); |
---|
45 | static void demo_sprites(void); |
---|
46 | static void demo_blit(void); |
---|
47 | |
---|
48 | int bounds = 0; |
---|
49 | int outline = 0; |
---|
50 | int dithering = 0; |
---|
51 | struct caca_sprite *sprite = NULL; |
---|
52 | |
---|
53 | GdkPixbuf *pixbuf; |
---|
54 | char *pixels; |
---|
55 | int bufx, bufy, bufpitch; |
---|
56 | |
---|
57 | int main(int argc, char **argv) |
---|
58 | { |
---|
59 | void (*demo)(void) = NULL; |
---|
60 | int quit = 0; |
---|
61 | |
---|
62 | if(caca_init()) |
---|
63 | { |
---|
64 | return 1; |
---|
65 | } |
---|
66 | |
---|
67 | caca_set_delay(40000); |
---|
68 | |
---|
69 | /* Initialize data */ |
---|
70 | sprite = caca_load_sprite("caca.txt"); |
---|
71 | if(!sprite) |
---|
72 | sprite = caca_load_sprite("examples/caca.txt"); |
---|
73 | |
---|
74 | gdk_init (&argc, &argv); |
---|
75 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gally4.jpeg", NULL); |
---|
76 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/badge1.jpeg", NULL); |
---|
77 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/union.png", NULL); |
---|
78 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/pikachu.jpeg", NULL); |
---|
79 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/gradient.png", NULL); |
---|
80 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/beastie.png", NULL); |
---|
81 | pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/stitch.jpg", NULL); |
---|
82 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/pix/dranac.jpeg", NULL); |
---|
83 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/artwork/aboire.png", NULL); |
---|
84 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/web/sam.zoy.org/artwork/goret.png", NULL); |
---|
85 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/lilkim02.jpg", NULL); |
---|
86 | //pixbuf = gdk_pixbuf_new_from_file("/home/sam/etw.bmp", NULL); |
---|
87 | if(!pixbuf) return -2; |
---|
88 | pixels = gdk_pixbuf_get_pixels(pixbuf); |
---|
89 | bufx = gdk_pixbuf_get_width(pixbuf); |
---|
90 | bufy = gdk_pixbuf_get_height(pixbuf); |
---|
91 | bufpitch = gdk_pixbuf_get_rowstride(pixbuf); |
---|
92 | fprintf(stderr, "bits: %i\n", gdk_pixbuf_get_bits_per_sample(pixbuf)); |
---|
93 | fprintf(stderr, "w %i, h %i, stride %i\n", bufx, bufy, bufpitch); |
---|
94 | |
---|
95 | /* Main menu */ |
---|
96 | display_menu(); |
---|
97 | caca_refresh(); |
---|
98 | |
---|
99 | /* Go ! */ |
---|
100 | while(!quit) |
---|
101 | { |
---|
102 | char key = caca_get_key(); |
---|
103 | |
---|
104 | if(key && demo) |
---|
105 | { |
---|
106 | display_menu(); |
---|
107 | caca_refresh(); |
---|
108 | demo = NULL; |
---|
109 | } |
---|
110 | else if(key) |
---|
111 | { |
---|
112 | handle_key: |
---|
113 | switch(key) |
---|
114 | { |
---|
115 | case 'q': |
---|
116 | case 'Q': |
---|
117 | demo = NULL; |
---|
118 | quit = 1; |
---|
119 | break; |
---|
120 | case 'o': |
---|
121 | case 'O': |
---|
122 | outline = (outline + 1) % 3; |
---|
123 | display_menu(); |
---|
124 | break; |
---|
125 | case 'b': |
---|
126 | case 'B': |
---|
127 | bounds = (bounds + 1) % 2; |
---|
128 | display_menu(); |
---|
129 | break; |
---|
130 | case 'd': |
---|
131 | case 'D': |
---|
132 | dithering = (dithering + 1) % 3; |
---|
133 | caca_set_dithering(dithering == 0 ? CACA_DITHER_NONE : |
---|
134 | dithering == 1 ? CACA_DITHER_ORDERED : |
---|
135 | CACA_DITHER_RANDOM); |
---|
136 | display_menu(); |
---|
137 | break; |
---|
138 | case 'c': |
---|
139 | demo = demo_color; |
---|
140 | break; |
---|
141 | case 'f': |
---|
142 | case 'F': |
---|
143 | demo = demo_all; |
---|
144 | break; |
---|
145 | case '1': |
---|
146 | demo = demo_dots; |
---|
147 | break; |
---|
148 | case '2': |
---|
149 | demo = demo_lines; |
---|
150 | break; |
---|
151 | case '3': |
---|
152 | demo = demo_boxes; |
---|
153 | break; |
---|
154 | case '4': |
---|
155 | demo = demo_triangles; |
---|
156 | break; |
---|
157 | case '5': |
---|
158 | demo = demo_ellipses; |
---|
159 | break; |
---|
160 | case 's': |
---|
161 | case 'S': |
---|
162 | demo = demo_sprites; |
---|
163 | break; |
---|
164 | case 'i': |
---|
165 | case 'I': |
---|
166 | demo = demo_blit; |
---|
167 | break; |
---|
168 | } |
---|
169 | |
---|
170 | if(demo) |
---|
171 | caca_clear(); |
---|
172 | |
---|
173 | key = caca_get_key(); |
---|
174 | if(key) |
---|
175 | goto handle_key; |
---|
176 | |
---|
177 | caca_refresh(); |
---|
178 | } |
---|
179 | |
---|
180 | if(demo) |
---|
181 | { |
---|
182 | demo(); |
---|
183 | |
---|
184 | caca_set_color(CACA_COLOR_WHITE); |
---|
185 | caca_draw_thin_box(1, 1, caca_get_width() - 2, caca_get_height() - 2); |
---|
186 | caca_printf(4, 1, "[%i.%i fps]----", |
---|
187 | 1000000 / caca_get_rendertime(), |
---|
188 | (10000000 / caca_get_rendertime()) % 10); |
---|
189 | caca_refresh(); |
---|
190 | } |
---|
191 | } |
---|
192 | |
---|
193 | /* Clean up */ |
---|
194 | caca_free_sprite(sprite); |
---|
195 | caca_end(); |
---|
196 | |
---|
197 | return 0; |
---|
198 | } |
---|
199 | |
---|
200 | static void display_menu(void) |
---|
201 | { |
---|
202 | int xo = caca_get_width() - 2; |
---|
203 | int yo = caca_get_height() - 2; |
---|
204 | |
---|
205 | caca_clear(); |
---|
206 | caca_set_color(CACA_COLOR_WHITE); |
---|
207 | caca_draw_thin_box(1, 1, xo, yo); |
---|
208 | |
---|
209 | caca_putstr((xo - strlen("libcaca demo")) / 2, 3, "libcaca demo"); |
---|
210 | caca_putstr((xo - strlen("==============")) / 2, 4, "=============="); |
---|
211 | |
---|
212 | caca_putstr(4, 6, "demos:"); |
---|
213 | caca_putstr(4, 7, "'f': full"); |
---|
214 | caca_putstr(4, 8, "'1': dots"); |
---|
215 | caca_putstr(4, 9, "'2': lines"); |
---|
216 | caca_putstr(4, 10, "'3': boxes"); |
---|
217 | caca_putstr(4, 11, "'4': triangles"); |
---|
218 | caca_putstr(4, 12, "'5': ellipses"); |
---|
219 | caca_putstr(4, 13, "'s': sprites"); |
---|
220 | caca_putstr(4, 14, "'c': color"); |
---|
221 | caca_putstr(4, 15, "'i': image blit"); |
---|
222 | |
---|
223 | caca_putstr(4, 17, "settings:"); |
---|
224 | caca_printf(4, 18, "'o': outline: %s", |
---|
225 | outline == 0 ? "none" : outline == 1 ? "solid" : "thin"); |
---|
226 | caca_printf(4, 19, "'b': drawing boundaries: %s", |
---|
227 | bounds == 0 ? "screen" : "infinite"); |
---|
228 | caca_printf(4, 20, "'d': %s dithering", |
---|
229 | dithering == 0 ? "no" : dithering == 1 ? "ordered" : "random"); |
---|
230 | |
---|
231 | caca_putstr(4, yo - 2, "'q': quit"); |
---|
232 | } |
---|
233 | |
---|
234 | static void demo_all(void) |
---|
235 | { |
---|
236 | static int i = 0; |
---|
237 | |
---|
238 | int j, xo, yo, xa, ya, xb, yb, xc, yc; |
---|
239 | |
---|
240 | i++; |
---|
241 | |
---|
242 | caca_clear(); |
---|
243 | |
---|
244 | /* Draw the sun */ |
---|
245 | caca_set_color(CACA_COLOR_YELLOW); |
---|
246 | xo = caca_get_width() / 4; |
---|
247 | yo = caca_get_height() / 4 + 5 * sin(0.03*i); |
---|
248 | |
---|
249 | for(j = 0; j < 16; j++) |
---|
250 | { |
---|
251 | xa = xo - (30 + sin(0.03*i) * 8) * sin(0.03*i + M_PI*j/8); |
---|
252 | ya = yo + (15 + sin(0.03*i) * 4) * cos(0.03*i + M_PI*j/8); |
---|
253 | caca_draw_thin_line(xo, yo, xa, ya); |
---|
254 | } |
---|
255 | |
---|
256 | j = 15 + sin(0.03*i) * 8; |
---|
257 | caca_set_color(CACA_COLOR_WHITE); |
---|
258 | caca_fill_ellipse(xo, yo, j, j / 2, '#'); |
---|
259 | caca_set_color(CACA_COLOR_YELLOW); |
---|
260 | caca_draw_ellipse(xo, yo, j, j / 2, '#'); |
---|
261 | |
---|
262 | /* Draw the pyramid */ |
---|
263 | xo = caca_get_width() * 5 / 8; |
---|
264 | yo = 2; |
---|
265 | |
---|
266 | xa = caca_get_width() / 8 + sin(0.03*i) * 5; |
---|
267 | ya = caca_get_height() / 2 + cos(0.03*i) * 5; |
---|
268 | |
---|
269 | xb = caca_get_width() - 10 - cos(0.02*i) * 10; |
---|
270 | yb = caca_get_height() * 3 / 4 - 5 + sin(0.02*i) * 5; |
---|
271 | |
---|
272 | xc = caca_get_width() / 4 - sin(0.02*i) * 5; |
---|
273 | yc = caca_get_height() * 3 / 4 + cos(0.02*i) * 5; |
---|
274 | |
---|
275 | caca_set_color(CACA_COLOR_GREEN); |
---|
276 | caca_fill_triangle(xo, yo, xb, yb, xa, ya, '%'); |
---|
277 | caca_set_color(CACA_COLOR_YELLOW); |
---|
278 | caca_draw_thin_triangle(xo, yo, xb, yb, xa, ya); |
---|
279 | |
---|
280 | caca_set_color(CACA_COLOR_RED); |
---|
281 | caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); |
---|
282 | caca_set_color(CACA_COLOR_YELLOW); |
---|
283 | caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc); |
---|
284 | |
---|
285 | caca_set_color(CACA_COLOR_BLUE); |
---|
286 | caca_fill_triangle(xo, yo, xb, yb, xc, yc, '%'); |
---|
287 | caca_set_color(CACA_COLOR_YELLOW); |
---|
288 | caca_draw_thin_triangle(xo, yo, xb, yb, xc, yc); |
---|
289 | |
---|
290 | /* Draw a background triangle */ |
---|
291 | xa = 2; |
---|
292 | ya = 2; |
---|
293 | |
---|
294 | xb = caca_get_width() - 3; |
---|
295 | yb = caca_get_height() / 2; |
---|
296 | |
---|
297 | xc = caca_get_width() / 3; |
---|
298 | yc = caca_get_height() - 3; |
---|
299 | |
---|
300 | caca_set_color(CACA_COLOR_CYAN); |
---|
301 | caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc); |
---|
302 | |
---|
303 | xo = caca_get_width() / 2 + cos(0.027*i) * caca_get_width() / 3; |
---|
304 | yo = caca_get_height() / 2 - sin(0.027*i) * caca_get_height() / 2; |
---|
305 | |
---|
306 | caca_draw_thin_line(xa, ya, xo, yo); |
---|
307 | caca_draw_thin_line(xb, yb, xo, yo); |
---|
308 | caca_draw_thin_line(xc, yc, xo, yo); |
---|
309 | |
---|
310 | /* Draw a sprite on the pyramid */ |
---|
311 | caca_draw_sprite(xo, yo, sprite, 0); |
---|
312 | |
---|
313 | /* Draw a trail behind the foreground sprite */ |
---|
314 | for(j = i - 60; j < i; j++) |
---|
315 | { |
---|
316 | int delta = caca_rand(-5, 5); |
---|
317 | caca_set_color(caca_rand(0, 15)); |
---|
318 | caca_putchar(caca_get_width() / 2 |
---|
319 | + cos(0.02*j) * (delta + caca_get_width() / 4), |
---|
320 | caca_get_height() / 2 |
---|
321 | + sin(0.02*j) * (delta + caca_get_height() / 3), |
---|
322 | '#'); |
---|
323 | } |
---|
324 | |
---|
325 | /* Draw foreground sprite */ |
---|
326 | caca_draw_sprite(caca_get_width() / 2 + cos(0.02*i) * caca_get_width() / 4, |
---|
327 | caca_get_height() / 2 + sin(0.02*i) * caca_get_height() / 3, |
---|
328 | sprite, 0); |
---|
329 | } |
---|
330 | |
---|
331 | static void demo_dots(void) |
---|
332 | { |
---|
333 | int xmax = caca_get_width() - 1; |
---|
334 | int ymax = caca_get_height() - 1; |
---|
335 | int i; |
---|
336 | |
---|
337 | for(i = 1000; i--;) |
---|
338 | { |
---|
339 | /* Putpixel */ |
---|
340 | caca_set_color(caca_rand(0, 15)); |
---|
341 | caca_putchar(caca_rand(0, xmax), caca_rand(0, ymax), '#'); |
---|
342 | } |
---|
343 | } |
---|
344 | |
---|
345 | static void demo_color(void) |
---|
346 | { |
---|
347 | int i; |
---|
348 | char buf[BUFSIZ]; |
---|
349 | |
---|
350 | caca_clear(); |
---|
351 | for(i = 0; i < 16; i++) |
---|
352 | { |
---|
353 | sprintf(buf, "'%c': %i (%s)", 'a' + i, i, caca_get_color_name(i)); |
---|
354 | caca_set_color(CACA_COLOR_WHITE); |
---|
355 | caca_putstr(4, i + 3, buf); |
---|
356 | caca_set_color(i); |
---|
357 | caca_putstr(40, i + 3, "XXXXXXXXXX-XX--X----------"); |
---|
358 | } |
---|
359 | } |
---|
360 | |
---|
361 | static void demo_lines(void) |
---|
362 | { |
---|
363 | int w = caca_get_width(); |
---|
364 | int h = caca_get_height(); |
---|
365 | int xa, ya, xb, yb; |
---|
366 | |
---|
367 | if(bounds) |
---|
368 | { |
---|
369 | xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h); |
---|
370 | xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h); |
---|
371 | } |
---|
372 | else |
---|
373 | { |
---|
374 | xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1); |
---|
375 | xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1); |
---|
376 | } |
---|
377 | |
---|
378 | caca_set_color(caca_rand(0, 15)); |
---|
379 | if(outline > 1) |
---|
380 | caca_draw_thin_line(xa, ya, xb, yb); |
---|
381 | else |
---|
382 | caca_draw_line(xa, ya, xb, yb, '#'); |
---|
383 | } |
---|
384 | |
---|
385 | static void demo_boxes(void) |
---|
386 | { |
---|
387 | int w = caca_get_width(); |
---|
388 | int h = caca_get_height(); |
---|
389 | int xa, ya, xb, yb; |
---|
390 | |
---|
391 | if(bounds) |
---|
392 | { |
---|
393 | xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h); |
---|
394 | xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h); |
---|
395 | } |
---|
396 | else |
---|
397 | { |
---|
398 | xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1); |
---|
399 | xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1); |
---|
400 | } |
---|
401 | |
---|
402 | caca_set_color(caca_rand(0, 15)); |
---|
403 | caca_fill_box(xa, ya, xb, yb, '#'); |
---|
404 | |
---|
405 | caca_set_color(caca_rand(0, 15)); |
---|
406 | if(outline == 2) |
---|
407 | caca_draw_thin_box(xa, ya, xb, yb); |
---|
408 | else if(outline == 1) |
---|
409 | caca_draw_box(xa, ya, xb, yb, '#'); |
---|
410 | } |
---|
411 | |
---|
412 | static void demo_ellipses(void) |
---|
413 | { |
---|
414 | int w = caca_get_width(); |
---|
415 | int h = caca_get_height(); |
---|
416 | int x, y, a, b; |
---|
417 | |
---|
418 | if(bounds) |
---|
419 | { |
---|
420 | x = caca_rand(- w, 2 * w); y = caca_rand(- h, 2 * h); |
---|
421 | a = caca_rand(0, w); b = caca_rand(0, h); |
---|
422 | } |
---|
423 | else |
---|
424 | { |
---|
425 | do |
---|
426 | { |
---|
427 | x = caca_rand(0, w); y = caca_rand(0, h); |
---|
428 | a = caca_rand(0, w); b = caca_rand(0, h); |
---|
429 | |
---|
430 | } while(x - a < 0 || x + a >= w || y - b < 0 || y + b >= h); |
---|
431 | } |
---|
432 | |
---|
433 | caca_set_color(caca_rand(0, 15)); |
---|
434 | caca_fill_ellipse(x, y, a, b, '#'); |
---|
435 | |
---|
436 | caca_set_color(caca_rand(0, 15)); |
---|
437 | if(outline == 2) |
---|
438 | caca_draw_thin_ellipse(x, y, a, b); |
---|
439 | else if(outline == 1) |
---|
440 | caca_draw_ellipse(x, y, a, b, '#'); |
---|
441 | } |
---|
442 | |
---|
443 | static void demo_triangles(void) |
---|
444 | { |
---|
445 | int w = caca_get_width(); |
---|
446 | int h = caca_get_height(); |
---|
447 | int xa, ya, xb, yb, xc, yc; |
---|
448 | |
---|
449 | if(bounds) |
---|
450 | { |
---|
451 | xa = caca_rand(- w, 2 * w); ya = caca_rand(- h, 2 * h); |
---|
452 | xb = caca_rand(- w, 2 * w); yb = caca_rand(- h, 2 * h); |
---|
453 | xc = caca_rand(- w, 2 * w); yc = caca_rand(- h, 2 * h); |
---|
454 | } |
---|
455 | else |
---|
456 | { |
---|
457 | |
---|
458 | xa = caca_rand(0, w - 1); ya = caca_rand(0, h - 1); |
---|
459 | xb = caca_rand(0, w - 1); yb = caca_rand(0, h - 1); |
---|
460 | xc = caca_rand(0, w - 1); yc = caca_rand(0, h - 1); |
---|
461 | } |
---|
462 | |
---|
463 | caca_set_color(caca_rand(0, 15)); |
---|
464 | caca_fill_triangle(xa, ya, xb, yb, xc, yc, '#'); |
---|
465 | |
---|
466 | caca_set_color(caca_rand(0, 15)); |
---|
467 | if(outline == 2) |
---|
468 | caca_draw_thin_triangle(xa, ya, xb, yb, xc, yc); |
---|
469 | else if(outline == 1) |
---|
470 | caca_draw_triangle(xa, ya, xb, yb, xc, yc, '#'); |
---|
471 | } |
---|
472 | |
---|
473 | static void demo_sprites(void) |
---|
474 | { |
---|
475 | caca_draw_sprite(caca_rand(0, caca_get_width() - 1), |
---|
476 | caca_rand(0, caca_get_height() - 1), sprite, 0); |
---|
477 | } |
---|
478 | |
---|
479 | static void demo_blit(void) |
---|
480 | { |
---|
481 | caca_blit(6, 4, caca_get_width() - 6, caca_get_height() - 4, |
---|
482 | pixels, bufx, bufy); |
---|
483 | } |
---|
484 | |
---|