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