25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

203 lines
5.8 KiB

  1. /*
  2. * cacaball metaballs effect for libcaca
  3. * Copyright (c) 2003-2004 Jean-Yves Lamoureux <jylam@lnxscene.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. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <math.h>
  28. #ifndef M_PI
  29. # define M_PI 3.14159265358979323846
  30. #endif
  31. #include "caca.h"
  32. /* Virtual buffer size */
  33. #define XSIZ 256
  34. #define YSIZ 256
  35. #define METASIZE 100
  36. #define METABALLS 16
  37. /* Colour index where to crop balls */
  38. #define CROPBALL 160
  39. static void create_ball(void);
  40. static void draw_ball(unsigned int, unsigned int);
  41. static unsigned char pixels[XSIZ * YSIZ];
  42. static unsigned char metaball[METASIZE * METASIZE];
  43. int main(int argc, char **argv)
  44. {
  45. unsigned int r[256], g[256], b[256], a[256];
  46. float d[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
  47. unsigned int x[METABALLS], y[METABALLS];
  48. struct caca_bitmap *caca_bitmap;
  49. float i = 10.0, j = 17.0, k = 11.0;
  50. int p, frame = 0, pause = 0;
  51. if(caca_init())
  52. return 1;
  53. caca_set_delay(20000);
  54. /* Make the palette eatable by libcaca */
  55. for(p = 0; p < 256; p++)
  56. r[p] = g[p] = b[p] = a[p] = 0x0;
  57. r[255] = g[255] = b[255] = 0xfff;
  58. /* Create a libcaca bitmap smaller than our pixel buffer, so that we
  59. * display only the interesting part of it */
  60. caca_bitmap = caca_create_bitmap(8, XSIZ - METASIZE, YSIZ - METASIZE,
  61. XSIZ, 0, 0, 0, 0);
  62. /* Generate ball sprite */
  63. create_ball();
  64. for(p = 0; p < METABALLS; p++)
  65. {
  66. d[p] = caca_rand(0, 100);
  67. di[p] = (float)caca_rand(500, 4000) / 6000.0;
  68. dj[p] = (float)caca_rand(500, 4000) / 6000.0;
  69. dk[p] = (float)caca_rand(500, 4000) / 6000.0;
  70. }
  71. /* Go ! */
  72. for(;;)
  73. {
  74. switch(caca_get_event(CACA_EVENT_KEY_PRESS))
  75. {
  76. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  77. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  78. }
  79. if(pause)
  80. goto paused;
  81. frame++;
  82. /* Crop the palette */
  83. for(p = CROPBALL; p < 255; p++)
  84. {
  85. int t1, t2, t3;
  86. t1 = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
  87. t2 = p < 0xe0 ? 0 : (p - 0xe0) * 0x80;
  88. t3 = p < 0x40 ? p * 0x40 : 0xfff;
  89. r[p] = (1.0 + sin((double)frame * M_PI / 60)) * t1 / 4
  90. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t2 / 4
  91. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t3 / 4;
  92. g[p] = (1.0 + sin((double)frame * M_PI / 60)) * t2 / 4
  93. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t3 / 4
  94. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t1 / 4;
  95. b[p] = (1.0 + sin((double)frame * M_PI / 60)) * t3 / 4
  96. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t1 / 4
  97. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t2 / 4;
  98. }
  99. /* Set the palette */
  100. caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
  101. /* Silly paths for our balls */
  102. for(p = 0; p < METABALLS; p++)
  103. {
  104. float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
  105. float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
  106. u = sin(i + u * 2.1) * (1.0 + sin(u));
  107. v = sin(j + v * 1.9) * (1.0 + sin(v));
  108. x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
  109. y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
  110. }
  111. i += 0.011;
  112. j += 0.017;
  113. k += 0.019;
  114. memset(pixels, 0, XSIZ * YSIZ);
  115. /* Here is all the trick. Maybe if you're that
  116. * clever you'll understand. */
  117. for(p = 0; p < METABALLS; p++)
  118. draw_ball(x[p], y[p]);
  119. paused:
  120. /* Draw our virtual buffer to screen, letting libcaca resize it */
  121. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  122. caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
  123. caca_refresh();
  124. }
  125. /* End, bye folks */
  126. end:
  127. caca_free_bitmap(caca_bitmap);
  128. caca_end();
  129. return 0;
  130. }
  131. /* Generate ball sprite
  132. * You should read the comments, I already wrote that before ... */
  133. static void create_ball(void)
  134. {
  135. int x, y;
  136. float distance;
  137. for(y = 0; y < METASIZE; y++)
  138. for(x = 0; x < METASIZE; x++)
  139. {
  140. distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
  141. + ((METASIZE/2) - y) * ((METASIZE/2) - y);
  142. distance = sqrt(distance) * 64 / METASIZE;
  143. metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
  144. }
  145. }
  146. /* You missed the trick ? */
  147. static void draw_ball(unsigned int bx, unsigned int by)
  148. {
  149. unsigned int color;
  150. unsigned int i, e = 0;
  151. unsigned int b = (by * XSIZ) + bx;
  152. for(i = 0; i < METASIZE * METASIZE; i++)
  153. {
  154. color = pixels[b] + metaball[i];
  155. if(color > 255)
  156. color = 255;
  157. pixels[b] = color;
  158. if(e == METASIZE)
  159. {
  160. e = 0;
  161. b += XSIZ - METASIZE;
  162. }
  163. b++;
  164. e++;
  165. }
  166. }