Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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