Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 
 
 
 
 

797 wiersze
22 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@zoy.org>
  6. * All Rights Reserved
  7. *
  8. * $Id$
  9. *
  10. * This program is free software; you can redistribute it and/or
  11. * modify it under the terms of the Do What The Fuck You Want To
  12. * Public License, Version 2, as published by Sam Hocevar. See
  13. * http://sam.zoy.org/wtfpl/COPYING for more details.
  14. */
  15. #include "config.h"
  16. #include "common.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 "cucul.h"
  27. #include "caca.h"
  28. enum action { PREPARE, INIT, UPDATE, RENDER, FREE };
  29. void transition(cucul_canvas_t *, int, int);
  30. void plasma(enum action, cucul_canvas_t *);
  31. void metaballs(enum action, cucul_canvas_t *);
  32. void moire(enum action, cucul_canvas_t *);
  33. void langton(enum action, cucul_canvas_t *);
  34. void matrix(enum action, cucul_canvas_t *);
  35. void (*fn[])(enum action, cucul_canvas_t *) =
  36. {
  37. plasma,
  38. metaballs,
  39. moire,
  40. //langton,
  41. matrix,
  42. };
  43. #define DEMOS (sizeof(fn)/sizeof(*fn))
  44. #define DEMO_FRAMES 1000
  45. #define TRANSITION_FRAMES 40
  46. #define TRANSITION_COUNT 2
  47. #define TRANSITION_CIRCLE 0
  48. #define TRANSITION_STAR 1
  49. /* Common macros for dither-based demos */
  50. #define XSIZ 256
  51. #define YSIZ 256
  52. /* Global variables */
  53. static int frame = 0;
  54. int main(int argc, char **argv)
  55. {
  56. static caca_display_t *dp;
  57. static cucul_canvas_t *frontcv, *backcv, *mask;
  58. int demo, next = -1, pause = 0, next_transition = DEMO_FRAMES;
  59. unsigned int i;
  60. int tmode = cucul_rand(0, TRANSITION_COUNT);
  61. /* Set up two canvases, a mask, and attach a display to the front one */
  62. frontcv = cucul_create_canvas(0, 0);
  63. backcv = cucul_create_canvas(0, 0);
  64. mask = cucul_create_canvas(0, 0);
  65. dp = caca_create_display(frontcv);
  66. if(!dp)
  67. return 1;
  68. cucul_set_canvas_size(backcv, cucul_get_canvas_width(frontcv),
  69. cucul_get_canvas_height(frontcv));
  70. cucul_set_canvas_size(mask, cucul_get_canvas_width(frontcv),
  71. cucul_get_canvas_height(frontcv));
  72. caca_set_display_time(dp, 20000);
  73. /* Initialise all demos' lookup tables */
  74. for(i = 0; i < DEMOS; i++)
  75. fn[i](PREPARE, frontcv);
  76. /* Choose a demo at random */
  77. demo = cucul_rand(0, DEMOS);
  78. fn[demo](INIT, frontcv);
  79. for(;;)
  80. {
  81. /* Handle events */
  82. caca_event_t ev;
  83. while(caca_get_event(dp, CACA_EVENT_KEY_PRESS
  84. | CACA_EVENT_QUIT, &ev, 0))
  85. {
  86. if(ev.type == CACA_EVENT_QUIT)
  87. goto end;
  88. switch(ev.data.key.ch)
  89. {
  90. case CACA_KEY_ESCAPE:
  91. goto end;
  92. case ' ':
  93. pause = !pause;
  94. break;
  95. case '\r':
  96. if(next == -1)
  97. next_transition = frame;
  98. break;
  99. }
  100. }
  101. /* Resize the spare canvas, just in case the main one changed */
  102. cucul_set_canvas_size(backcv, cucul_get_canvas_width(frontcv),
  103. cucul_get_canvas_height(frontcv));
  104. cucul_set_canvas_size(mask, cucul_get_canvas_width(frontcv),
  105. cucul_get_canvas_height(frontcv));
  106. if(pause)
  107. goto paused;
  108. /* Update demo's data */
  109. fn[demo](UPDATE, frontcv);
  110. /* Handle transitions */
  111. if(frame == next_transition)
  112. {
  113. next = cucul_rand(0, DEMOS);
  114. if(next == demo)
  115. next = (next + 1) % DEMOS;
  116. fn[next](INIT, backcv);
  117. }
  118. else if(frame == next_transition + TRANSITION_FRAMES)
  119. {
  120. fn[demo](FREE, frontcv);
  121. demo = next;
  122. next = -1;
  123. next_transition = frame + DEMO_FRAMES;
  124. tmode = cucul_rand(0, TRANSITION_COUNT);
  125. }
  126. if(next != -1)
  127. fn[next](UPDATE, backcv);
  128. frame++;
  129. paused:
  130. /* Render main demo's canvas */
  131. fn[demo](RENDER, frontcv);
  132. /* If a transition is on its way, render it */
  133. if(next != -1)
  134. {
  135. fn[next](RENDER, backcv);
  136. cucul_set_color(mask, CUCUL_COLOR_LIGHTGRAY, CUCUL_COLOR_BLACK);
  137. cucul_clear_canvas(mask);
  138. cucul_set_color(mask, CUCUL_COLOR_WHITE, CUCUL_COLOR_WHITE);
  139. transition(mask, tmode,
  140. 100 * (frame - next_transition) / TRANSITION_FRAMES);
  141. cucul_blit(frontcv, 0, 0, backcv, mask);
  142. }
  143. cucul_set_color(frontcv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  144. if(frame < 100)
  145. cucul_putstr(frontcv, cucul_get_canvas_width(frontcv) - 30,
  146. cucul_get_canvas_height(frontcv) - 2,
  147. " -=[ Powered by libcaca ]=- ");
  148. caca_refresh_display(dp);
  149. }
  150. end:
  151. if(next != -1)
  152. fn[next](FREE, frontcv);
  153. fn[demo](FREE, frontcv);
  154. caca_free_display(dp);
  155. cucul_free_canvas(mask);
  156. cucul_free_canvas(backcv);
  157. cucul_free_canvas(frontcv);
  158. return 0;
  159. }
  160. /* Transitions */
  161. void transition(cucul_canvas_t *mask, int tmode, int completed)
  162. {
  163. static float const star[] =
  164. {
  165. 0.000000, -1.000000,
  166. 0.308000, -0.349000,
  167. 0.992000, -0.244000,
  168. 0.500000, 0.266000,
  169. 0.632000, 0.998000,
  170. 0.008000, 0.659000,
  171. -0.601000, 0.995000,
  172. -0.496000, 0.275000,
  173. -0.997000, -0.244000,
  174. -0.313000, -0.349000
  175. };
  176. static float star_rot[sizeof(star)/sizeof(*star)];
  177. float mulx = 0.0075f * completed * cucul_get_canvas_width(mask);
  178. float muly = 0.0075f * completed * cucul_get_canvas_height(mask);
  179. int w2 = cucul_get_canvas_width(mask) / 2;
  180. int h2 = cucul_get_canvas_height(mask) / 2;
  181. float angle = (0.0075f * completed * 360) * 3.14 / 180, x, y;
  182. unsigned int i;
  183. switch(tmode)
  184. {
  185. case TRANSITION_STAR:
  186. /* Compute rotated coordinates */
  187. for(i = 0; i < (sizeof(star) / sizeof(*star)) / 2; i++)
  188. {
  189. x = star[i * 2];
  190. y = star[i * 2 + 1];
  191. star_rot[i * 2] = x * cos(angle) - y * sin(angle);
  192. star_rot[i * 2 + 1] = y * cos(angle) + x * sin(angle);
  193. }
  194. mulx *= 1.8;
  195. muly *= 1.8;
  196. #define DO_TRI(a, b, c) \
  197. cucul_fill_triangle(mask, \
  198. star_rot[(a)*2] * mulx + w2, star_rot[(a)*2+1] * muly + h2, \
  199. star_rot[(b)*2] * mulx + w2, star_rot[(b)*2+1] * muly + h2, \
  200. star_rot[(c)*2] * mulx + w2, star_rot[(c)*2+1] * muly + h2, "#")
  201. DO_TRI(0, 1, 9);
  202. DO_TRI(1, 2, 3);
  203. DO_TRI(3, 4, 5);
  204. DO_TRI(5, 6, 7);
  205. DO_TRI(7, 8, 9);
  206. DO_TRI(9, 1, 5);
  207. DO_TRI(9, 5, 7);
  208. DO_TRI(1, 3, 5);
  209. break;
  210. case TRANSITION_CIRCLE:
  211. cucul_fill_ellipse(mask, w2, h2, mulx, muly, "#");
  212. break;
  213. }
  214. }
  215. /* The plasma effect */
  216. #define TABLEX (XSIZ * 2)
  217. #define TABLEY (YSIZ * 2)
  218. static uint8_t table[TABLEX * TABLEY];
  219. static void do_plasma(uint8_t *,
  220. double, double, double, double, double, double);
  221. void plasma(enum action action, cucul_canvas_t *cv)
  222. {
  223. static cucul_dither_t *dither;
  224. static uint8_t *screen;
  225. static unsigned int red[256], green[256], blue[256], alpha[256];
  226. static double r[3], R[6];
  227. int i, x, y;
  228. switch(action)
  229. {
  230. case PREPARE:
  231. /* Fill various tables */
  232. for(i = 0 ; i < 256; i++)
  233. red[i] = green[i] = blue[i] = alpha[i] = 0;
  234. for(i = 0; i < 3; i++)
  235. r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
  236. for(i = 0; i < 6; i++)
  237. R[i] = (double)(cucul_rand(1, 1000)) / 10000;
  238. for(y = 0 ; y < TABLEY ; y++)
  239. for(x = 0 ; x < TABLEX ; x++)
  240. {
  241. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  242. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  243. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  244. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  245. }
  246. break;
  247. case INIT:
  248. screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
  249. dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  250. break;
  251. case UPDATE:
  252. for(i = 0 ; i < 256; i++)
  253. {
  254. double z = ((double)i) / 256 * 6 * M_PI;
  255. red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  256. blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  257. green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  258. }
  259. /* Set the palette */
  260. cucul_set_dither_palette(dither, red, green, blue, alpha);
  261. do_plasma(screen,
  262. (1.0 + sin(((double)frame) * R[0])) / 2,
  263. (1.0 + sin(((double)frame) * R[1])) / 2,
  264. (1.0 + sin(((double)frame) * R[2])) / 2,
  265. (1.0 + sin(((double)frame) * R[3])) / 2,
  266. (1.0 + sin(((double)frame) * R[4])) / 2,
  267. (1.0 + sin(((double)frame) * R[5])) / 2);
  268. break;
  269. case RENDER:
  270. cucul_dither_bitmap(cv, 0, 0,
  271. cucul_get_canvas_width(cv),
  272. cucul_get_canvas_height(cv),
  273. dither, screen);
  274. break;
  275. case FREE:
  276. free(screen);
  277. cucul_free_dither(dither);
  278. break;
  279. }
  280. }
  281. static void do_plasma(uint8_t *pixels, double x_1, double y_1,
  282. double x_2, double y_2, double x_3, double y_3)
  283. {
  284. unsigned int X1 = x_1 * (TABLEX / 2),
  285. Y1 = y_1 * (TABLEY / 2),
  286. X2 = x_2 * (TABLEX / 2),
  287. Y2 = y_2 * (TABLEY / 2),
  288. X3 = x_3 * (TABLEX / 2),
  289. Y3 = y_3 * (TABLEY / 2);
  290. unsigned int y;
  291. uint8_t * t1 = table + X1 + Y1 * TABLEX,
  292. * t2 = table + X2 + Y2 * TABLEX,
  293. * t3 = table + X3 + Y3 * TABLEX;
  294. for(y = 0; y < YSIZ; y++)
  295. {
  296. unsigned int x;
  297. uint8_t * tmp = pixels + y * YSIZ;
  298. unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
  299. for(x = 0; ty < tmax; ty++, tmp++)
  300. tmp[0] = t1[ty] + t2[ty] + t3[ty];
  301. }
  302. }
  303. /* The metaball effect */
  304. #define METASIZE (XSIZ/2)
  305. #define METABALLS 12
  306. #define CROPBALL 200 /* Colour index where to crop balls */
  307. static uint8_t metaball[METASIZE * METASIZE];
  308. static void create_ball(void);
  309. static void draw_ball(uint8_t *, unsigned int, unsigned int);
  310. void metaballs(enum action action, cucul_canvas_t *cv)
  311. {
  312. static cucul_dither_t *cucul_dither;
  313. static uint8_t *screen;
  314. static unsigned int r[256], g[256], b[256], a[256];
  315. static float dd[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
  316. static unsigned int x[METABALLS], y[METABALLS];
  317. static float i = 10.0, j = 17.0, k = 11.0;
  318. static double offset[360 + 80];
  319. int n, angle;
  320. switch(action)
  321. {
  322. case PREPARE:
  323. /* Make the palette eatable by libcaca */
  324. for(n = 0; n < 256; n++)
  325. r[n] = g[n] = b[n] = a[n] = 0x0;
  326. r[255] = g[255] = b[255] = 0xfff;
  327. /* Generate ball sprite */
  328. create_ball();
  329. for(n = 0; n < METABALLS; n++)
  330. {
  331. dd[n] = cucul_rand(0, 100);
  332. di[n] = (float)cucul_rand(500, 4000) / 6000.0;
  333. dj[n] = (float)cucul_rand(500, 4000) / 6000.0;
  334. dk[n] = (float)cucul_rand(500, 4000) / 6000.0;
  335. }
  336. for(n = 0; n < 360 + 80; n++)
  337. offset[n] = 1.0 + sin((double)(n * M_PI / 60));
  338. break;
  339. case INIT:
  340. screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
  341. /* Create a libcucul dither smaller than our pixel buffer, so that we
  342. * display only the interesting part of it */
  343. cucul_dither = cucul_create_dither(8, XSIZ - METASIZE, YSIZ - METASIZE,
  344. XSIZ, 0, 0, 0, 0);
  345. break;
  346. case UPDATE:
  347. angle = frame % 360;
  348. /* Crop the palette */
  349. for(n = CROPBALL; n < 255; n++)
  350. {
  351. int t1, t2, t3;
  352. double c1 = offset[angle];
  353. double c2 = offset[angle + 40];
  354. double c3 = offset[angle + 80];
  355. t1 = n < 0x40 ? 0 : n < 0xc0 ? (n - 0x40) * 0x20 : 0xfff;
  356. t2 = n < 0xe0 ? 0 : (n - 0xe0) * 0x80;
  357. t3 = n < 0x40 ? n * 0x40 : 0xfff;
  358. r[n] = (c1 * t1 + c2 * t2 + c3 * t3) / 4;
  359. g[n] = (c1 * t2 + c2 * t3 + c3 * t1) / 4;
  360. b[n] = (c1 * t3 + c2 * t1 + c3 * t2) / 4;
  361. }
  362. /* Set the palette */
  363. cucul_set_dither_palette(cucul_dither, r, g, b, a);
  364. /* Silly paths for our balls */
  365. for(n = 0; n < METABALLS; n++)
  366. {
  367. float u = di[n] * i + dj[n] * j + dk[n] * sin(di[n] * k);
  368. float v = dd[n] + di[n] * j + dj[n] * k + dk[n] * sin(dk[n] * i);
  369. u = sin(i + u * 2.1) * (1.0 + sin(u));
  370. v = sin(j + v * 1.9) * (1.0 + sin(v));
  371. x[n] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
  372. y[n] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
  373. }
  374. i += 0.011;
  375. j += 0.017;
  376. k += 0.019;
  377. memset(screen, 0, XSIZ * YSIZ);
  378. for(n = 0; n < METABALLS; n++)
  379. draw_ball(screen, x[n], y[n]);
  380. break;
  381. case RENDER:
  382. cucul_dither_bitmap(cv, 0, 0,
  383. cucul_get_canvas_width(cv),
  384. cucul_get_canvas_height(cv),
  385. cucul_dither, screen + (METASIZE / 2) * (1 + XSIZ));
  386. break;
  387. case FREE:
  388. free(screen);
  389. cucul_free_dither(cucul_dither);
  390. break;
  391. }
  392. }
  393. static void create_ball(void)
  394. {
  395. int x, y;
  396. float distance;
  397. for(y = 0; y < METASIZE; y++)
  398. for(x = 0; x < METASIZE; x++)
  399. {
  400. distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
  401. + ((METASIZE/2) - y) * ((METASIZE/2) - y);
  402. distance = sqrt(distance) * 64 / METASIZE;
  403. metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
  404. }
  405. }
  406. static void draw_ball(uint8_t *screen, unsigned int bx, unsigned int by)
  407. {
  408. unsigned int color;
  409. unsigned int i, e = 0;
  410. unsigned int b = (by * XSIZ) + bx;
  411. for(i = 0; i < METASIZE * METASIZE; i++)
  412. {
  413. color = screen[b] + metaball[i];
  414. if(color > 255)
  415. color = 255;
  416. screen[b] = color;
  417. if(e == METASIZE)
  418. {
  419. e = 0;
  420. b += XSIZ - METASIZE;
  421. }
  422. b++;
  423. e++;
  424. }
  425. }
  426. /* The moiré effect */
  427. #define DISCSIZ (XSIZ*2)
  428. #define DISCTHICKNESS (XSIZ*15/40)
  429. static uint8_t disc[DISCSIZ * DISCSIZ];
  430. static void put_disc(uint8_t *, int, int);
  431. static void draw_line(int, int, char);
  432. void moire(enum action action, cucul_canvas_t *cv)
  433. {
  434. static cucul_dither_t *dither;
  435. static uint8_t *screen;
  436. static unsigned int red[256], green[256], blue[256], alpha[256];
  437. int i, x, y;
  438. switch(action)
  439. {
  440. case PREPARE:
  441. /* Fill various tables */
  442. for(i = 0 ; i < 256; i++)
  443. red[i] = green[i] = blue[i] = alpha[i] = 0;
  444. red[0] = green[0] = blue[0] = 0x777;
  445. red[1] = green[1] = blue[1] = 0xfff;
  446. /* Fill the circle */
  447. for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
  448. {
  449. int t, dx, dy;
  450. for(t = 0, dx = 0, dy = i; dx <= dy; dx++)
  451. {
  452. draw_line(dx / 3, dy / 3, (i / DISCTHICKNESS) % 2);
  453. draw_line(dy / 3, dx / 3, (i / DISCTHICKNESS) % 2);
  454. t += t > 0 ? dx - dy-- : dx;
  455. }
  456. }
  457. break;
  458. case INIT:
  459. screen = malloc(XSIZ * YSIZ * sizeof(uint8_t));
  460. dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  461. break;
  462. case UPDATE:
  463. memset(screen, 0, XSIZ * YSIZ);
  464. /* Set the palette */
  465. red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
  466. green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
  467. blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
  468. red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
  469. green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
  470. blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
  471. cucul_set_dither_palette(dither, red, green, blue, alpha);
  472. /* Draw circles */
  473. x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
  474. y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
  475. put_disc(screen, x, y);
  476. x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
  477. y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
  478. put_disc(screen, x, y);
  479. break;
  480. case RENDER:
  481. cucul_dither_bitmap(cv, 0, 0,
  482. cucul_get_canvas_width(cv),
  483. cucul_get_canvas_height(cv),
  484. dither, screen);
  485. break;
  486. case FREE:
  487. free(screen);
  488. cucul_free_dither(dither);
  489. break;
  490. }
  491. }
  492. static void put_disc(uint8_t *screen, int x, int y)
  493. {
  494. char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
  495. int i, j;
  496. for(j = 0; j < YSIZ; j++)
  497. for(i = 0; i < XSIZ; i++)
  498. {
  499. screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
  500. }
  501. }
  502. static void draw_line(int x, int y, char color)
  503. {
  504. if(x == 0 || y == 0 || y > DISCSIZ / 2)
  505. return;
  506. if(x > DISCSIZ / 2)
  507. x = DISCSIZ / 2;
  508. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
  509. color, 2 * x - 1);
  510. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
  511. color, 2 * x - 1);
  512. }
  513. /* Langton ant effect */
  514. #define ANTS 15
  515. #define ITER 2
  516. void langton(enum action action, cucul_canvas_t *cv)
  517. {
  518. static char gradient[] =
  519. {
  520. ' ', ' ', '.', '.', ':', ':', 'x', 'x',
  521. 'X', 'X', '&', '&', 'W', 'W', '@', '@',
  522. };
  523. static int steps[][2] = { { 0, 1 }, { 1, 0 }, { 0, -1 }, { -1, 0 } };
  524. static uint8_t *screen;
  525. static int width, height;
  526. static int ax[ANTS], ay[ANTS], dir[ANTS];
  527. int i, a, x, y;
  528. switch(action)
  529. {
  530. case PREPARE:
  531. width = cucul_get_canvas_width(cv);
  532. height = cucul_get_canvas_height(cv);
  533. for(i = 0; i < ANTS; i++)
  534. {
  535. ax[i] = cucul_rand(0, width);
  536. ay[i] = cucul_rand(0, height);
  537. dir[i] = cucul_rand(0, 4);
  538. }
  539. break;
  540. case INIT:
  541. screen = malloc(width * height);
  542. memset(screen, 0, width * height);
  543. break;
  544. case UPDATE:
  545. for(i = 0; i < ITER; i++)
  546. {
  547. for(x = 0; x < width * height; x++)
  548. {
  549. uint8_t p = screen[x];
  550. if((p & 0x0f) > 1)
  551. screen[x] = p - 1;
  552. }
  553. for(a = 0; a < ANTS; a++)
  554. {
  555. uint8_t p = screen[ax[a] + width * ay[a]];
  556. if(p & 0x0f)
  557. {
  558. dir[a] = (dir[a] + 1) % 4;
  559. screen[ax[a] + width * ay[a]] = a << 4;
  560. }
  561. else
  562. {
  563. dir[a] = (dir[a] + 3) % 4;
  564. screen[ax[a] + width * ay[a]] = (a << 4) | 0x0f;
  565. }
  566. ax[a] = (width + ax[a] + steps[dir[a]][0]) % width;
  567. ay[a] = (height + ay[a] + steps[dir[a]][1]) % height;
  568. }
  569. }
  570. break;
  571. case RENDER:
  572. for(y = 0; y < height; y++)
  573. {
  574. for(x = 0; x < width; x++)
  575. {
  576. uint8_t p = screen[x + width * y];
  577. if(p & 0x0f)
  578. cucul_set_color(cv, CUCUL_COLOR_WHITE, p >> 4);
  579. else
  580. cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_BLACK);
  581. cucul_putchar(cv, x, y, gradient[p & 0x0f]);
  582. }
  583. }
  584. break;
  585. case FREE:
  586. free(screen);
  587. break;
  588. }
  589. }
  590. /* Matrix effect */
  591. #define MAXDROPS 500
  592. #define MINLEN 15
  593. #define MAXLEN 30
  594. void matrix(enum action action, cucul_canvas_t *cv)
  595. {
  596. static struct drop
  597. {
  598. int x, y, speed, len;
  599. char str[MAXLEN];
  600. }
  601. drop[MAXDROPS];
  602. int w, h, i, j;
  603. switch(action)
  604. {
  605. case PREPARE:
  606. for(i = 0; i < MAXDROPS; i++)
  607. {
  608. drop[i].x = cucul_rand(0, 1000);
  609. drop[i].y = cucul_rand(0, 1000);
  610. drop[i].speed = 5 + cucul_rand(0, 30);
  611. drop[i].len = MINLEN + cucul_rand(0, (MAXLEN - MINLEN));
  612. for(j = 0; j < MAXLEN; j++)
  613. drop[i].str[j] = cucul_rand('0', 'z');
  614. }
  615. break;
  616. case INIT:
  617. break;
  618. case UPDATE:
  619. w = cucul_get_canvas_width(cv);
  620. h = cucul_get_canvas_height(cv);
  621. for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
  622. {
  623. drop[i].y += drop[i].speed;
  624. if(drop[i].y > 1000)
  625. {
  626. drop[i].y -= 1000;
  627. drop[i].x = cucul_rand(0, 1000);
  628. }
  629. }
  630. break;
  631. case RENDER:
  632. w = cucul_get_canvas_width(cv);
  633. h = cucul_get_canvas_height(cv);
  634. cucul_set_color(cv, CUCUL_COLOR_BLACK, CUCUL_COLOR_BLACK);
  635. cucul_clear_canvas(cv);
  636. for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
  637. {
  638. int x, y;
  639. x = drop[i].x * w / 1000 / 2 * 2;
  640. y = drop[i].y * (h + MAXLEN) / 1000;
  641. for(j = 0; j < drop[i].len; j++)
  642. {
  643. unsigned int fg;
  644. if(j < 2)
  645. fg = CUCUL_COLOR_WHITE;
  646. else if(j < drop[i].len / 4)
  647. fg = CUCUL_COLOR_LIGHTGREEN;
  648. else if(j < drop[i].len * 4 / 5)
  649. fg = CUCUL_COLOR_GREEN;
  650. else
  651. fg = CUCUL_COLOR_DARKGRAY;
  652. cucul_set_color(cv, fg, CUCUL_COLOR_BLACK);
  653. cucul_putchar(cv, x, y - j,
  654. drop[i].str[(y - j) % drop[i].len]);
  655. }
  656. }
  657. break;
  658. case FREE:
  659. break;
  660. }
  661. }