Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

584 Zeilen
16 KiB

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