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