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.
 
 
 
 
 
 

955 rader
27 KiB

  1. /*
  2. * cacademo various demo effects for libcaca
  3. * Copyright (c) 1998 Michele Bini <mibin@tin.it>
  4. * 2003-2006 Jean-Yves Lamoureux <jylam@lnxscene.org>
  5. * 2004-2006 Sam Hocevar <sam@hocevar.net>
  6. * All Rights Reserved
  7. *
  8. * $Id$
  9. *
  10. * This program is free software. It comes without any warranty, to
  11. * the extent permitted by applicable law. You can redistribute it
  12. * and/or modify it under the terms of the Do What The Fuck You Want
  13. * To Public License, Version 2, as published by Sam Hocevar. See
  14. * http://sam.zoy.org/wtfpl/COPYING for more details.
  15. */
  16. #include "config.h"
  17. #if !defined(__KERNEL__)
  18. # include <stdio.h>
  19. # include <stdlib.h>
  20. # include <string.h>
  21. # include <math.h>
  22. # ifndef M_PI
  23. # define M_PI 3.14159265358979323846
  24. # endif
  25. #endif
  26. #include "caca.h"
  27. enum action { PREPARE, INIT, UPDATE, RENDER, FREE };
  28. void transition(caca_canvas_t *, int, int);
  29. void plasma(enum action, caca_canvas_t *);
  30. void metaballs(enum action, caca_canvas_t *);
  31. void moire(enum action, caca_canvas_t *);
  32. void langton(enum action, caca_canvas_t *);
  33. void matrix(enum action, caca_canvas_t *);
  34. void rotozoom(enum action, caca_canvas_t *);
  35. void (*fn[])(enum action, caca_canvas_t *) =
  36. {
  37. plasma,
  38. metaballs,
  39. moire,
  40. /*langton,*/
  41. matrix,
  42. rotozoom,
  43. };
  44. #define DEMOS (sizeof(fn)/sizeof(*fn))
  45. #define DEMO_FRAMES caca_rand(500, 1000)
  46. #define TRANSITION_FRAMES 40
  47. #define TRANSITION_COUNT 5
  48. #define TRANSITION_CIRCLE 0
  49. #define TRANSITION_STAR 1
  50. #define TRANSITION_SQUARE 2
  51. #define TRANSITION_VLINES 3
  52. #define TRANSITION_HLINES 4
  53. /* Common macros for dither-based demos */
  54. #define XSIZ 256
  55. #define YSIZ 256
  56. /* Global variables */
  57. static int frame = 0;
  58. int main(int argc, char **argv)
  59. {
  60. static caca_display_t *dp;
  61. static caca_canvas_t *frontcv, *backcv, *mask;
  62. int demo, next = -1, paused = 0, next_transition = DEMO_FRAMES;
  63. unsigned int i;
  64. int tmode = caca_rand(0, TRANSITION_COUNT);
  65. /* Set up two canvases, a mask, and attach a display to the front one */
  66. frontcv = caca_create_canvas(0, 0);
  67. backcv = caca_create_canvas(0, 0);
  68. mask = caca_create_canvas(0, 0);
  69. dp = caca_create_display(frontcv);
  70. if(!dp)
  71. return 1;
  72. caca_set_canvas_size(backcv, caca_get_canvas_width(frontcv),
  73. caca_get_canvas_height(frontcv));
  74. caca_set_canvas_size(mask, caca_get_canvas_width(frontcv),
  75. caca_get_canvas_height(frontcv));
  76. caca_set_display_time(dp, 20000);
  77. /* Initialise all demos' lookup tables */
  78. for(i = 0; i < DEMOS; i++)
  79. fn[i](PREPARE, frontcv);
  80. /* Choose a demo at random */
  81. demo = caca_rand(0, DEMOS);
  82. fn[demo](INIT, frontcv);
  83. for(;;)
  84. {
  85. /* Handle events */
  86. caca_event_t ev;
  87. while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
  88. | CACA_EVENT_QUIT, &ev, 0))
  89. {
  90. if(caca_get_event_type(&ev) == CACA_EVENT_QUIT)
  91. goto end;
  92. switch(caca_get_event_key_ch(&ev))
  93. {
  94. case CACA_KEY_ESCAPE:
  95. case CACA_KEY_CTRL_C:
  96. case CACA_KEY_CTRL_Z:
  97. goto end;
  98. case ' ':
  99. paused = !paused;
  100. break;
  101. case '\r':
  102. if(next == -1)
  103. next_transition = frame;
  104. break;
  105. }
  106. }
  107. /* Resize the spare canvas, just in case the main one changed */
  108. caca_set_canvas_size(backcv, caca_get_canvas_width(frontcv),
  109. caca_get_canvas_height(frontcv));
  110. caca_set_canvas_size(mask, caca_get_canvas_width(frontcv),
  111. caca_get_canvas_height(frontcv));
  112. if(paused)
  113. goto _paused;
  114. /* Update demo's data */
  115. fn[demo](UPDATE, frontcv);
  116. /* Handle transitions */
  117. if(frame == next_transition)
  118. {
  119. next = caca_rand(0, DEMOS);
  120. if(next == demo)
  121. next = (next + 1) % DEMOS;
  122. fn[next](INIT, backcv);
  123. }
  124. else if(frame == next_transition + TRANSITION_FRAMES)
  125. {
  126. fn[demo](FREE, frontcv);
  127. demo = next;
  128. next = -1;
  129. next_transition = frame + DEMO_FRAMES;
  130. tmode = caca_rand(0, TRANSITION_COUNT);
  131. }
  132. if(next != -1)
  133. fn[next](UPDATE, backcv);
  134. frame++;
  135. _paused:
  136. /* Render main demo's canvas */
  137. fn[demo](RENDER, frontcv);
  138. /* If a transition is on its way, render it */
  139. if(next != -1)
  140. {
  141. fn[next](RENDER, backcv);
  142. caca_set_color_ansi(mask, CACA_LIGHTGRAY, CACA_BLACK);
  143. caca_clear_canvas(mask);
  144. caca_set_color_ansi(mask, CACA_WHITE, CACA_WHITE);
  145. transition(mask, tmode,
  146. 100 * (frame - next_transition) / TRANSITION_FRAMES);
  147. caca_blit(frontcv, 0, 0, backcv, mask);
  148. }
  149. caca_set_color_ansi(frontcv, CACA_WHITE, CACA_BLUE);
  150. if(frame < 100)
  151. caca_put_str(frontcv, caca_get_canvas_width(frontcv) - 30,
  152. caca_get_canvas_height(frontcv) - 2,
  153. " -=[ Powered by libcaca ]=- ");
  154. caca_refresh_display(dp);
  155. }
  156. end:
  157. if(next != -1)
  158. fn[next](FREE, frontcv);
  159. fn[demo](FREE, frontcv);
  160. caca_free_display(dp);
  161. caca_free_canvas(mask);
  162. caca_free_canvas(backcv);
  163. caca_free_canvas(frontcv);
  164. return 0;
  165. }
  166. /* Transitions */
  167. void transition(caca_canvas_t *mask, int tmode, int completed)
  168. {
  169. static float const star[] =
  170. {
  171. 0.000000, -1.000000,
  172. 0.308000, -0.349000,
  173. 0.992000, -0.244000,
  174. 0.500000, 0.266000,
  175. 0.632000, 0.998000,
  176. 0.008000, 0.659000,
  177. -0.601000, 0.995000,
  178. -0.496000, 0.275000,
  179. -0.997000, -0.244000,
  180. -0.313000, -0.349000
  181. };
  182. static float star_rot[sizeof(star)/sizeof(*star)];
  183. static float const square[] =
  184. {
  185. -1, -1,
  186. 1, -1,
  187. 1, 1,
  188. -1, 1
  189. };
  190. static float square_rot[sizeof(square)/sizeof(*square)];
  191. float mulx = 0.0075f * completed * caca_get_canvas_width(mask);
  192. float muly = 0.0075f * completed * caca_get_canvas_height(mask);
  193. int w2 = caca_get_canvas_width(mask) / 2;
  194. int h2 = caca_get_canvas_height(mask) / 2;
  195. float angle = (0.0075f * completed * 360) * 3.14 / 180, x, y;
  196. unsigned int i;
  197. int w = caca_get_canvas_width(mask);
  198. int h = caca_get_canvas_height(mask);
  199. switch(tmode)
  200. {
  201. case TRANSITION_SQUARE:
  202. /* Compute rotated coordinates */
  203. for(i = 0; i < (sizeof(square) / sizeof(*square)) / 2; i++)
  204. {
  205. x = square[i * 2];
  206. y = square[i * 2 + 1];
  207. square_rot[i * 2] = x * cos(angle) - y * sin(angle);
  208. square_rot[i * 2 + 1] = y * cos(angle) + x * sin(angle);
  209. }
  210. mulx *= 1.8;
  211. muly *= 1.8;
  212. caca_fill_triangle(mask,
  213. square_rot[0*2] * mulx + w2, square_rot[0*2+1] * muly + h2, \
  214. square_rot[1*2] * mulx + w2, square_rot[1*2+1] * muly + h2, \
  215. square_rot[2*2] * mulx + w2, square_rot[2*2+1] * muly + h2, '#');
  216. caca_fill_triangle(mask,
  217. square_rot[0*2] * mulx + w2, square_rot[0*2+1] * muly + h2, \
  218. square_rot[2*2] * mulx + w2, square_rot[2*2+1] * muly + h2, \
  219. square_rot[3*2] * mulx + w2, square_rot[3*2+1] * muly + h2, '#');
  220. break;
  221. case TRANSITION_STAR:
  222. /* Compute rotated coordinates */
  223. for(i = 0; i < (sizeof(star) / sizeof(*star)) / 2; i++)
  224. {
  225. x = star[i * 2];
  226. y = star[i * 2 + 1];
  227. star_rot[i * 2] = x * cos(angle) - y * sin(angle);
  228. star_rot[i * 2 + 1] = y * cos(angle) + x * sin(angle);
  229. }
  230. mulx *= 1.8;
  231. muly *= 1.8;
  232. #define DO_TRI(a, b, c) \
  233. caca_fill_triangle(mask, \
  234. star_rot[(a)*2] * mulx + w2, star_rot[(a)*2+1] * muly + h2, \
  235. star_rot[(b)*2] * mulx + w2, star_rot[(b)*2+1] * muly + h2, \
  236. star_rot[(c)*2] * mulx + w2, star_rot[(c)*2+1] * muly + h2, '#')
  237. DO_TRI(0, 1, 9);
  238. DO_TRI(1, 2, 3);
  239. DO_TRI(3, 4, 5);
  240. DO_TRI(5, 6, 7);
  241. DO_TRI(7, 8, 9);
  242. DO_TRI(9, 1, 5);
  243. DO_TRI(9, 5, 7);
  244. DO_TRI(1, 3, 5);
  245. break;
  246. case TRANSITION_CIRCLE:
  247. caca_fill_ellipse(mask, w2, h2, mulx, muly, '#');
  248. break;
  249. case TRANSITION_VLINES:
  250. for(i = 0; i < 8; i++)
  251. {
  252. int z = ((i & 1) ? w : (-w)/2) * (100 - completed) / 100;
  253. caca_fill_box(mask, i * w / 8, z , (w / 8) + 1, z + h, '#');
  254. }
  255. break;
  256. case TRANSITION_HLINES:
  257. for(i = 0; i < 6; i++)
  258. {
  259. int z = ((i & 1) ? w : (-w)/2) * (100 - completed) / 100;
  260. caca_fill_box(mask, z, i * h / 6, z + w, (h / 6) + 1, '#');
  261. }
  262. break;
  263. }
  264. }
  265. /* The plasma effect */
  266. #define TABLEX (XSIZ * 2)
  267. #define TABLEY (YSIZ * 2)
  268. static uint8_t table[TABLEX * TABLEY];
  269. static void do_plasma(uint8_t *,
  270. double, double, double, double, double, double);
  271. void plasma(enum action action, caca_canvas_t *cv)
  272. {
  273. static caca_dither_t *dither;
  274. static uint8_t *screen;
  275. static uint32_t red[256], green[256], blue[256], alpha[256];
  276. static double r[3], R[6];
  277. int i, x, y;
  278. switch(action)
  279. {
  280. case PREPARE:
  281. /* Fill various tables */
  282. for(i = 0 ; i < 256; i++)
  283. red[i] = green[i] = blue[i] = alpha[i] = 0;
  284. for(i = 0; i < 3; i++)
  285. r[i] = (double)(caca_rand(1, 1000)) / 60000 * M_PI;
  286. for(i = 0; i < 6; i++)
  287. R[i] = (double)(caca_rand(1, 1000)) / 10000;
  288. for(y = 0 ; y < TABLEY ; y++)
  289. for(x = 0 ; x < TABLEX ; x++)
  290. {
  291. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  292. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  293. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  294. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  295. }
  296. break;
  297. case INIT:
  298. screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
  299. dither = caca_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  300. break;
  301. case UPDATE:
  302. for(i = 0 ; i < 256; i++)
  303. {
  304. double z = ((double)i) / 256 * 6 * M_PI;
  305. red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  306. blue[i] = (1.0 + cos(z + r[0] * (frame + 100))) / 2 * 0xfff;
  307. green[i] = (1.0 + cos(z + r[2] * (frame + 200))) / 2 * 0xfff;
  308. }
  309. /* Set the palette */
  310. caca_set_dither_palette(dither, red, green, blue, alpha);
  311. do_plasma(screen,
  312. (1.0 + sin(((double)frame) * R[0])) / 2,
  313. (1.0 + sin(((double)frame) * R[1])) / 2,
  314. (1.0 + sin(((double)frame) * R[2])) / 2,
  315. (1.0 + sin(((double)frame) * R[3])) / 2,
  316. (1.0 + sin(((double)frame) * R[4])) / 2,
  317. (1.0 + sin(((double)frame) * R[5])) / 2);
  318. break;
  319. case RENDER:
  320. caca_dither_bitmap(cv, 0, 0,
  321. caca_get_canvas_width(cv),
  322. caca_get_canvas_height(cv),
  323. dither, screen);
  324. break;
  325. case FREE:
  326. free(screen);
  327. caca_free_dither(dither);
  328. break;
  329. }
  330. }
  331. static void do_plasma(uint8_t *pixels, double x_1, double y_1,
  332. double x_2, double y_2, double x_3, double y_3)
  333. {
  334. unsigned int X1 = x_1 * (TABLEX / 2),
  335. Y1 = y_1 * (TABLEY / 2),
  336. X2 = x_2 * (TABLEX / 2),
  337. Y2 = y_2 * (TABLEY / 2),
  338. X3 = x_3 * (TABLEX / 2),
  339. Y3 = y_3 * (TABLEY / 2);
  340. unsigned int y;
  341. uint8_t * t1 = table + X1 + Y1 * TABLEX,
  342. * t2 = table + X2 + Y2 * TABLEX,
  343. * t3 = table + X3 + Y3 * TABLEX;
  344. for(y = 0; y < YSIZ; y++)
  345. {
  346. unsigned int x;
  347. uint8_t * tmp = pixels + y * YSIZ;
  348. unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
  349. for(x = 0; ty < tmax; ty++, tmp++)
  350. tmp[0] = t1[ty] + t2[ty] + t3[ty];
  351. }
  352. }
  353. /* The metaball effect */
  354. #define METASIZE (XSIZ/2)
  355. #define METABALLS 12
  356. #define CROPBALL 200 /* Colour index where to crop balls */
  357. static uint8_t metaball[METASIZE * METASIZE];
  358. static void create_ball(void);
  359. static void draw_ball(uint8_t *, unsigned int, unsigned int);
  360. void metaballs(enum action action, caca_canvas_t *cv)
  361. {
  362. static caca_dither_t *caca_dither;
  363. static uint8_t *screen;
  364. static uint32_t r[256], g[256], b[256], a[256];
  365. static float dd[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
  366. static unsigned int x[METABALLS], y[METABALLS];
  367. static float i = 10.0, j = 17.0, k = 11.0;
  368. static double offset[360 + 80];
  369. static unsigned int angleoff;
  370. int n, angle;
  371. switch(action)
  372. {
  373. case PREPARE:
  374. /* Make the palette eatable by libcaca */
  375. for(n = 0; n < 256; n++)
  376. r[n] = g[n] = b[n] = a[n] = 0x0;
  377. r[255] = g[255] = b[255] = 0xfff;
  378. /* Generate ball sprite */
  379. create_ball();
  380. for(n = 0; n < METABALLS; n++)
  381. {
  382. dd[n] = caca_rand(0, 100);
  383. di[n] = (float)caca_rand(500, 4000) / 6000.0;
  384. dj[n] = (float)caca_rand(500, 4000) / 6000.0;
  385. dk[n] = (float)caca_rand(500, 4000) / 6000.0;
  386. }
  387. angleoff = caca_rand(0, 360);
  388. for(n = 0; n < 360 + 80; n++)
  389. offset[n] = 1.0 + sin((double)(n * M_PI / 60));
  390. break;
  391. case INIT:
  392. screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
  393. /* Create a libcaca dither smaller than our pixel buffer, so that we
  394. * display only the interesting part of it */
  395. caca_dither = caca_create_dither(8, XSIZ - METASIZE, YSIZ - METASIZE,
  396. XSIZ, 0, 0, 0, 0);
  397. break;
  398. case UPDATE:
  399. angle = (frame + angleoff) % 360;
  400. /* Crop the palette */
  401. for(n = CROPBALL; n < 255; n++)
  402. {
  403. int t1, t2, t3;
  404. double c1 = offset[angle];
  405. double c2 = offset[angle + 40];
  406. double c3 = offset[angle + 80];
  407. t1 = n < 0x40 ? 0 : n < 0xc0 ? (n - 0x40) * 0x20 : 0xfff;
  408. t2 = n < 0xe0 ? 0 : (n - 0xe0) * 0x80;
  409. t3 = n < 0x40 ? n * 0x40 : 0xfff;
  410. r[n] = (c1 * t1 + c2 * t2 + c3 * t3) / 4;
  411. g[n] = (c1 * t2 + c2 * t3 + c3 * t1) / 4;
  412. b[n] = (c1 * t3 + c2 * t1 + c3 * t2) / 4;
  413. }
  414. /* Set the palette */
  415. caca_set_dither_palette(caca_dither, r, g, b, a);
  416. /* Silly paths for our balls */
  417. for(n = 0; n < METABALLS; n++)
  418. {
  419. float u = di[n] * i + dj[n] * j + dk[n] * sin(di[n] * k);
  420. float v = dd[n] + di[n] * j + dj[n] * k + dk[n] * sin(dk[n] * i);
  421. u = sin(i + u * 2.1) * (1.0 + sin(u));
  422. v = sin(j + v * 1.9) * (1.0 + sin(v));
  423. x[n] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
  424. y[n] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
  425. }
  426. i += 0.011;
  427. j += 0.017;
  428. k += 0.019;
  429. memset(screen, 0, XSIZ * YSIZ);
  430. for(n = 0; n < METABALLS; n++)
  431. draw_ball(screen, x[n], y[n]);
  432. break;
  433. case RENDER:
  434. caca_dither_bitmap(cv, 0, 0,
  435. caca_get_canvas_width(cv),
  436. caca_get_canvas_height(cv),
  437. caca_dither, screen + (METASIZE / 2) * (1 + XSIZ));
  438. break;
  439. case FREE:
  440. free(screen);
  441. caca_free_dither(caca_dither);
  442. break;
  443. }
  444. }
  445. static void create_ball(void)
  446. {
  447. int x, y;
  448. float distance;
  449. for(y = 0; y < METASIZE; y++)
  450. for(x = 0; x < METASIZE; x++)
  451. {
  452. distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
  453. + ((METASIZE/2) - y) * ((METASIZE/2) - y);
  454. distance = sqrt(distance) * 64 / METASIZE;
  455. metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
  456. }
  457. }
  458. static void draw_ball(uint8_t *screen, unsigned int bx, unsigned int by)
  459. {
  460. unsigned int color;
  461. unsigned int i, e = 0;
  462. unsigned int b = (by * XSIZ) + bx;
  463. for(i = 0; i < METASIZE * METASIZE; i++)
  464. {
  465. color = screen[b] + metaball[i];
  466. if(color > 255)
  467. color = 255;
  468. screen[b] = color;
  469. if(e == METASIZE)
  470. {
  471. e = 0;
  472. b += XSIZ - METASIZE;
  473. }
  474. b++;
  475. e++;
  476. }
  477. }
  478. /* The moiré effect */
  479. #define DISCSIZ (XSIZ*2)
  480. #define DISCTHICKNESS (XSIZ*15/40)
  481. static uint8_t disc[DISCSIZ * DISCSIZ];
  482. static void put_disc(uint8_t *, int, int);
  483. static void draw_line(int, int, char);
  484. void moire(enum action action, caca_canvas_t *cv)
  485. {
  486. static caca_dither_t *dither;
  487. static uint8_t *screen;
  488. static float d[6];
  489. static uint32_t red[256], green[256], blue[256], alpha[256];
  490. int i, x, y;
  491. switch(action)
  492. {
  493. case PREPARE:
  494. /* Fill various tables */
  495. for(i = 0 ; i < 256; i++)
  496. red[i] = green[i] = blue[i] = alpha[i] = 0;
  497. for(i = 0; i < 6; i++)
  498. d[i] = ((float)caca_rand(50, 70)) / 1000.0;
  499. red[0] = green[0] = blue[0] = 0x777;
  500. red[1] = green[1] = blue[1] = 0xfff;
  501. /* Fill the circle */
  502. for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
  503. {
  504. int t, dx, dy;
  505. for(t = 0, dx = 0, dy = i; dx <= dy; dx++)
  506. {
  507. draw_line(dx / 3, dy / 3, (i / DISCTHICKNESS) % 2);
  508. draw_line(dy / 3, dx / 3, (i / DISCTHICKNESS) % 2);
  509. t += t > 0 ? dx - dy-- : dx;
  510. }
  511. }
  512. break;
  513. case INIT:
  514. screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
  515. dither = caca_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  516. break;
  517. case UPDATE:
  518. memset(screen, 0, XSIZ * YSIZ);
  519. /* Set the palette */
  520. red[0] = 0.5 * (1 + sin(d[0] * (frame + 1000))) * 0xfff;
  521. green[0] = 0.5 * (1 + cos(d[1] * frame)) * 0xfff;
  522. blue[0] = 0.5 * (1 + cos(d[2] * (frame + 3000))) * 0xfff;
  523. red[1] = 0.5 * (1 + sin(d[3] * (frame + 2000))) * 0xfff;
  524. green[1] = 0.5 * (1 + cos(d[4] * frame + 5.0)) * 0xfff;
  525. blue[1] = 0.5 * (1 + cos(d[5] * (frame + 4000))) * 0xfff;
  526. caca_set_dither_palette(dither, red, green, blue, alpha);
  527. /* Draw circles */
  528. x = cos(d[0] * (frame + 1000)) * 128.0 + (XSIZ / 2);
  529. y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
  530. put_disc(screen, x, y);
  531. x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
  532. y = sin(d[1] * (frame + 2000)) * 64.0 + (YSIZ / 2);
  533. put_disc(screen, x, y);
  534. break;
  535. case RENDER:
  536. caca_dither_bitmap(cv, 0, 0,
  537. caca_get_canvas_width(cv),
  538. caca_get_canvas_height(cv),
  539. dither, screen);
  540. break;
  541. case FREE:
  542. free(screen);
  543. caca_free_dither(dither);
  544. break;
  545. }
  546. }
  547. static void put_disc(uint8_t *screen, int x, int y)
  548. {
  549. char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
  550. int i, j;
  551. for(j = 0; j < YSIZ; j++)
  552. for(i = 0; i < XSIZ; i++)
  553. {
  554. screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
  555. }
  556. }
  557. static void draw_line(int x, int y, char color)
  558. {
  559. if(x == 0 || y == 0 || y > DISCSIZ / 2)
  560. return;
  561. if(x > DISCSIZ / 2)
  562. x = DISCSIZ / 2;
  563. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
  564. color, 2 * x - 1);
  565. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
  566. color, 2 * x - 1);
  567. }
  568. /* Langton ant effect */
  569. #define ANTS 15
  570. #define ITER 2
  571. void langton(enum action action, caca_canvas_t *cv)
  572. {
  573. static char gradient[] =
  574. {
  575. ' ', ' ', '.', '.', ':', ':', 'x', 'x',
  576. 'X', 'X', '&', '&', 'W', 'W', '@', '@',
  577. };
  578. static int steps[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
  579. static uint8_t *screen;
  580. static int width, height;
  581. static int ax[ANTS], ay[ANTS], dir[ANTS];
  582. int i, a, x, y;
  583. switch(action)
  584. {
  585. case PREPARE:
  586. width = caca_get_canvas_width(cv);
  587. height = caca_get_canvas_height(cv);
  588. for(i = 0; i < ANTS; i++)
  589. {
  590. ax[i] = caca_rand(0, width);
  591. ay[i] = caca_rand(0, height);
  592. dir[i] = caca_rand(0, 4);
  593. }
  594. break;
  595. case INIT:
  596. screen = malloc(width * height);
  597. memset(screen, 0, width * height);
  598. break;
  599. case UPDATE:
  600. for(i = 0; i < ITER; i++)
  601. {
  602. for(x = 0; x < width * height; x++)
  603. {
  604. uint8_t p = screen[x];
  605. if((p & 0x0f) > 1)
  606. screen[x] = p - 1;
  607. }
  608. for(a = 0; a < ANTS; a++)
  609. {
  610. uint8_t p = screen[ax[a] + width * ay[a]];
  611. if(p & 0x0f)
  612. {
  613. dir[a] = (dir[a] + 1) % 4;
  614. screen[ax[a] + width * ay[a]] = a << 4;
  615. }
  616. else
  617. {
  618. dir[a] = (dir[a] + 3) % 4;
  619. screen[ax[a] + width * ay[a]] = (a << 4) | 0x0f;
  620. }
  621. ax[a] = (width + ax[a] + steps[dir[a]][0]) % width;
  622. ay[a] = (height + ay[a] + steps[dir[a]][1]) % height;
  623. }
  624. }
  625. break;
  626. case RENDER:
  627. for(y = 0; y < height; y++)
  628. {
  629. for(x = 0; x < width; x++)
  630. {
  631. uint8_t p = screen[x + width * y];
  632. if(p & 0x0f)
  633. caca_set_color_ansi(cv, CACA_WHITE, p >> 4);
  634. else
  635. caca_set_color_ansi(cv, CACA_BLACK, CACA_BLACK);
  636. caca_put_char(cv, x, y, gradient[p & 0x0f]);
  637. }
  638. }
  639. break;
  640. case FREE:
  641. free(screen);
  642. break;
  643. }
  644. }
  645. /* Matrix effect */
  646. #define MAXDROPS 500
  647. #define MINLEN 15
  648. #define MAXLEN 30
  649. void matrix(enum action action, caca_canvas_t *cv)
  650. {
  651. static struct drop
  652. {
  653. int x, y, speed, len;
  654. char str[MAXLEN];
  655. }
  656. drop[MAXDROPS];
  657. int w, h, i, j;
  658. switch(action)
  659. {
  660. case PREPARE:
  661. for(i = 0; i < MAXDROPS; i++)
  662. {
  663. drop[i].x = caca_rand(0, 1000);
  664. drop[i].y = caca_rand(0, 1000);
  665. drop[i].speed = 5 + caca_rand(0, 30);
  666. drop[i].len = MINLEN + caca_rand(0, (MAXLEN - MINLEN));
  667. for(j = 0; j < MAXLEN; j++)
  668. drop[i].str[j] = caca_rand('0', 'z');
  669. }
  670. break;
  671. case INIT:
  672. break;
  673. case UPDATE:
  674. w = caca_get_canvas_width(cv);
  675. h = caca_get_canvas_height(cv);
  676. for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
  677. {
  678. drop[i].y += drop[i].speed;
  679. if(drop[i].y > 1000)
  680. {
  681. drop[i].y -= 1000;
  682. drop[i].x = caca_rand(0, 1000);
  683. }
  684. }
  685. break;
  686. case RENDER:
  687. w = caca_get_canvas_width(cv);
  688. h = caca_get_canvas_height(cv);
  689. caca_set_color_ansi(cv, CACA_BLACK, CACA_BLACK);
  690. caca_clear_canvas(cv);
  691. for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
  692. {
  693. int x, y;
  694. x = drop[i].x * w / 1000 / 2 * 2;
  695. y = drop[i].y * (h + MAXLEN) / 1000;
  696. for(j = 0; j < drop[i].len; j++)
  697. {
  698. unsigned int fg;
  699. if(j < 2)
  700. fg = CACA_WHITE;
  701. else if(j < drop[i].len / 4)
  702. fg = CACA_LIGHTGREEN;
  703. else if(j < drop[i].len * 4 / 5)
  704. fg = CACA_GREEN;
  705. else
  706. fg = CACA_DARKGRAY;
  707. caca_set_color_ansi(cv, fg, CACA_BLACK);
  708. caca_put_char(cv, x, y - j,
  709. drop[i].str[(y - j) % drop[i].len]);
  710. }
  711. }
  712. break;
  713. case FREE:
  714. break;
  715. }
  716. }
  717. /* Rotozoom effect */
  718. #define TEXTURE_SIZE 256
  719. #define TABLE_SIZE 65536
  720. /* 24:8 Fixed point stuff */
  721. #define PRECISION 8
  722. #define FMUL(a, b) (((a)*(b))>>PRECISION)
  723. #define TOFIX(d) ((int)( (d)*(double)(1<<PRECISION) ))
  724. #define TOINT(a) (a>>PRECISION);
  725. #include "texture.h"
  726. void rotozoom(enum action action, caca_canvas_t *canvas)
  727. {
  728. static uint32_t screen[XSIZ * YSIZ];
  729. static int cos_tab[TABLE_SIZE], sin_tab[TABLE_SIZE];
  730. static int y_tab[TEXTURE_SIZE];
  731. static caca_dither_t *dither;
  732. static uint32_t *texture;
  733. uint32_t *p;
  734. static int alphaF, tF;
  735. int scaleF;
  736. /* register is quite a bad idea on CISC, but not on RISC */
  737. register unsigned int x, y;
  738. register unsigned int xxF, yyF, uF, vF, uF_, vF_;
  739. register unsigned int vu, vv;
  740. switch(action)
  741. {
  742. case PREPARE:
  743. for(x = 0; x < TABLE_SIZE; x++)
  744. {
  745. cos_tab[x] = TOFIX(cos(x * (360.0f / (float)TABLE_SIZE)));
  746. sin_tab[x] = TOFIX(sin(x * (360.0f / (float)TABLE_SIZE)));
  747. }
  748. for(x = 0; x < TEXTURE_SIZE; x++)
  749. y_tab[x] = x * TEXTURE_SIZE; /* start of lines offsets */
  750. /* FIXME: this may be an invalid cast */
  751. texture = (uint32_t *)textureByte;
  752. break;
  753. case INIT:
  754. dither = caca_create_dither(32, XSIZ, YSIZ, XSIZ * 4,
  755. 0x00FF0000,
  756. 0x0000FF00,
  757. 0x000000FF,
  758. 0x00000000);
  759. break;
  760. case UPDATE:
  761. alphaF += 4;
  762. tF += 3;
  763. scaleF = FMUL(sin_tab[tF & 0xFFFF], TOFIX(3)) + (TOFIX(4));
  764. xxF = FMUL(cos_tab[(alphaF) & 0xFFFF], scaleF);
  765. yyF = FMUL(sin_tab[(alphaF) & 0xFFFF], scaleF);
  766. uF = vF = 0;
  767. uF_ = vF_ = 0;
  768. p = screen;
  769. for(y = YSIZ; y--;)
  770. {
  771. for(x = XSIZ; x--;)
  772. {
  773. uF += xxF;
  774. vF += yyF;
  775. vu = TOINT(uF);
  776. vv = TOINT(vF);
  777. vu &= 0xFF; /* ARM doesn't like */
  778. vv &= 0xFF; /* chars as local vars */
  779. *p++ = texture[vu + y_tab[vv]];
  780. }
  781. uF = uF_ -= yyF;
  782. vF = vF_ += xxF;
  783. }
  784. break;
  785. case RENDER:
  786. caca_dither_bitmap(canvas, 0, 0,
  787. caca_get_canvas_width(canvas),
  788. caca_get_canvas_height(canvas),
  789. dither, screen);
  790. break;
  791. case FREE:
  792. caca_free_dither(dither);
  793. break;
  794. }
  795. }