Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

191 rinda
5.5 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. 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;
  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. while(!caca_get_event(CACA_EVENT_KEY_PRESS))
  73. {
  74. frame++;
  75. /* Crop the palette */
  76. for(p = CROPBALL; p < 255; p++)
  77. {
  78. int t1, t2, t3;
  79. t1 = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
  80. t2 = p < 0xe0 ? 0 : (p - 0xe0) * 0x80;
  81. t3 = p < 0x40 ? p * 0x40 : 0xfff;
  82. r[p] = (1.0 + sin((double)frame * M_PI / 60)) * t1 / 4
  83. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t2 / 4
  84. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t3 / 4;
  85. g[p] = (1.0 + sin((double)frame * M_PI / 60)) * t2 / 4
  86. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t3 / 4
  87. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t1 / 4;
  88. b[p] = (1.0 + sin((double)frame * M_PI / 60)) * t3 / 4
  89. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t1 / 4
  90. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t2 / 4;
  91. }
  92. /* Set the palette */
  93. caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
  94. /* Silly paths for our balls */
  95. for(p = 0; p < METABALLS; p++)
  96. {
  97. float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
  98. float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
  99. u = sin(i + u * 2.1) * (1.0 + sin(u));
  100. v = sin(j + v * 1.9) * (1.0 + sin(v));
  101. x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
  102. y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
  103. }
  104. i += 0.011;
  105. j += 0.017;
  106. k += 0.019;
  107. memset(pixels, 0, XSIZ * YSIZ);
  108. /* Here is all the trick. Maybe if you're that
  109. * clever you'll understand. */
  110. for(p = 0; p < METABALLS; p++)
  111. draw_ball(x[p], y[p]);
  112. /* Draw our virtual buffer to screen, letting libcaca resize it */
  113. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  114. caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
  115. caca_refresh();
  116. }
  117. /* End, bye folks */
  118. caca_end();
  119. return 0;
  120. }
  121. /* Generate ball sprite
  122. * You should read the comments, I already wrote that before ... */
  123. static void create_ball(void)
  124. {
  125. int x, y;
  126. float distance;
  127. for(y = 0; y < METASIZE; y++)
  128. for(x = 0; x < METASIZE; x++)
  129. {
  130. distance = ((METASIZE/2) - x) * ((METASIZE/2) - x)
  131. + ((METASIZE/2) - y) * ((METASIZE/2) - y);
  132. distance = sqrt(distance) * 64 / METASIZE;
  133. metaball[x + y * METASIZE] = distance > 15 ? 0 : (255 - distance) * 15;
  134. }
  135. }
  136. /* You missed the trick ? */
  137. static void draw_ball(unsigned int bx, unsigned int by)
  138. {
  139. unsigned int color;
  140. unsigned int i, e = 0;
  141. unsigned int b = (by * XSIZ) + bx;
  142. for(i = 0; i < METASIZE * METASIZE; i++)
  143. {
  144. color = pixels[b] + metaball[i];
  145. if(color > 255)
  146. color = 255;
  147. pixels[b] = color;
  148. if(e == METASIZE)
  149. {
  150. e = 0;
  151. b += XSIZ - METASIZE;
  152. }
  153. b++;
  154. e++;
  155. }
  156. }