You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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