選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

193 行
5.3 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 Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <math.h>
  18. #ifndef M_PI
  19. # define M_PI 3.14159265358979323846
  20. #endif
  21. #include "caca.h"
  22. /* Virtual buffer size */
  23. #define XSIZ 256
  24. #define YSIZ 256
  25. #define METASIZE 100
  26. #define METABALLS 16
  27. /* Colour index where to crop balls */
  28. #define CROPBALL 160
  29. static void create_ball(void);
  30. static void draw_ball(unsigned int, unsigned int);
  31. static unsigned char pixels[XSIZ * YSIZ];
  32. static unsigned char metaball[METASIZE * METASIZE];
  33. int main(int argc, char **argv)
  34. {
  35. unsigned int r[256], g[256], b[256], a[256];
  36. float d[METABALLS], di[METABALLS], dj[METABALLS], dk[METABALLS];
  37. unsigned int x[METABALLS], y[METABALLS];
  38. struct caca_bitmap *caca_bitmap;
  39. float i = 10.0, j = 17.0, k = 11.0;
  40. int p, frame = 0, pause = 0;
  41. if(caca_init())
  42. return 1;
  43. caca_set_delay(20000);
  44. /* Make the palette eatable by libcaca */
  45. for(p = 0; p < 256; p++)
  46. r[p] = g[p] = b[p] = a[p] = 0x0;
  47. r[255] = g[255] = b[255] = 0xfff;
  48. /* Create a libcaca bitmap smaller than our pixel buffer, so that we
  49. * display only the interesting part of it */
  50. caca_bitmap = caca_create_bitmap(8, XSIZ - METASIZE, YSIZ - METASIZE,
  51. XSIZ, 0, 0, 0, 0);
  52. /* Generate ball sprite */
  53. create_ball();
  54. for(p = 0; p < METABALLS; p++)
  55. {
  56. d[p] = caca_rand(0, 100);
  57. di[p] = (float)caca_rand(500, 4000) / 6000.0;
  58. dj[p] = (float)caca_rand(500, 4000) / 6000.0;
  59. dk[p] = (float)caca_rand(500, 4000) / 6000.0;
  60. }
  61. /* Go ! */
  62. for(;;)
  63. {
  64. switch(caca_get_event(CACA_EVENT_KEY_PRESS))
  65. {
  66. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  67. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  68. }
  69. if(pause)
  70. goto paused;
  71. frame++;
  72. /* Crop the palette */
  73. for(p = CROPBALL; p < 255; p++)
  74. {
  75. int t1, t2, t3;
  76. t1 = p < 0x40 ? 0 : p < 0xc0 ? (p - 0x40) * 0x20 : 0xfff;
  77. t2 = p < 0xe0 ? 0 : (p - 0xe0) * 0x80;
  78. t3 = p < 0x40 ? p * 0x40 : 0xfff;
  79. r[p] = (1.0 + sin((double)frame * M_PI / 60)) * t1 / 4
  80. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t2 / 4
  81. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t3 / 4;
  82. g[p] = (1.0 + sin((double)frame * M_PI / 60)) * t2 / 4
  83. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t3 / 4
  84. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t1 / 4;
  85. b[p] = (1.0 + sin((double)frame * M_PI / 60)) * t3 / 4
  86. + (1.0 + sin((double)(frame + 40) * M_PI / 60)) * t1 / 4
  87. + (1.0 + sin((double)(frame + 80) * M_PI / 60)) * t2 / 4;
  88. }
  89. /* Set the palette */
  90. caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
  91. /* Silly paths for our balls */
  92. for(p = 0; p < METABALLS; p++)
  93. {
  94. float u = di[p] * i + dj[p] * j + dk[p] * sin(di[p] * k);
  95. float v = d[p] + di[p] * j + dj[p] * k + dk[p] * sin(dk[p] * i);
  96. u = sin(i + u * 2.1) * (1.0 + sin(u));
  97. v = sin(j + v * 1.9) * (1.0 + sin(v));
  98. x[p] = (XSIZ - METASIZE) / 2 + u * (XSIZ - METASIZE) / 4;
  99. y[p] = (YSIZ - METASIZE) / 2 + v * (YSIZ - METASIZE) / 4;
  100. }
  101. i += 0.011;
  102. j += 0.017;
  103. k += 0.019;
  104. memset(pixels, 0, XSIZ * YSIZ);
  105. /* Here is all the trick. Maybe if you're that
  106. * clever you'll understand. */
  107. for(p = 0; p < METABALLS; p++)
  108. draw_ball(x[p], y[p]);
  109. paused:
  110. /* Draw our virtual buffer to screen, letting libcaca resize it */
  111. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  112. caca_bitmap, pixels + (METASIZE / 2) * (1 + XSIZ));
  113. caca_refresh();
  114. }
  115. /* End, bye folks */
  116. end:
  117. caca_free_bitmap(caca_bitmap);
  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. }