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.
 
 
 
 
 
 

225 lines
7.1 KiB

  1. /*
  2. * cacaball Metaballs effect using libcaca
  3. * Copyright (c) 2003-2004 Jean-Yves Lamoureux <jylam@lnxscene.org>
  4. * All Rights Reserved
  5. *
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2 of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  20. * 02111-1307 USA
  21. */
  22. #include "../config.h"
  23. #include <math.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include "caca.h"
  28. /* Virtual buffer size */
  29. #define XSIZ 128
  30. #define YSIZ 128
  31. #define METASIZE 64
  32. #define METACENTER (METASIZE/2)
  33. static struct caca_bitmap *caca_bitmap;
  34. static unsigned char *bitmap;
  35. unsigned char MetaBoule[METASIZE*METASIZE];
  36. void drawBalls(unsigned int bx, unsigned int by);
  37. void cutBalls(unsigned int bx, unsigned int by);
  38. void generateBalls(void);
  39. /* Screen palette */
  40. const static int pal[] =
  41. {
  42. 0, 0, 0, 0, 0, 6, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 8, 0, 0, 9, 0, 0, 10,
  43. 2, 0, 10, 4, 0, 9, 6, 0, 9, 8, 0, 8, 10, 0, 7, 12, 0, 7, 14, 0, 6, 16, 0, 5,
  44. 18, 0, 5, 20, 0, 4, 22, 0, 4, 24, 0, 3, 26, 0, 2, 28, 0, 2, 30, 0, 1, 32, 0, 0,
  45. 32, 0, 0, 33, 0, 0, 34, 0, 0, 35, 0, 0, 36, 0, 0, 36, 0, 0, 37, 0, 0, 38, 0, 0,
  46. 39, 0, 0, 40, 0, 0, 40, 0, 0, 41, 0, 0, 42, 0, 0, 43, 0, 0, 44, 0, 0, 45, 0, 0,
  47. 46, 1, 0, 47, 1, 0, 48, 2, 0, 49, 2, 0, 50, 3, 0, 51, 3, 0, 52, 4, 0, 53, 4, 0,
  48. 54, 5, 0, 55, 5, 0, 56, 6, 0, 57, 6, 0, 58, 7, 0, 59, 7, 0, 60, 8, 0, 61, 8, 0,
  49. 63, 9, 0, 63, 9, 0, 63, 10, 0, 63, 10, 0, 63, 11, 0, 63, 11, 0, 63, 12, 0, 63, 12, 0,
  50. 63, 13, 0, 63, 13, 0, 63, 14, 0, 63, 14, 0, 63, 15, 0, 63, 15, 0, 63, 16, 0, 63, 16, 0,
  51. 63, 17, 0, 63, 17, 0, 63, 18, 0, 63, 18, 0, 63, 19, 0, 63, 19, 0, 63, 20, 0, 63, 20, 0,
  52. 63, 21, 0, 63, 21, 0, 63, 22, 0, 63, 22, 0, 63, 23, 0, 63, 24, 0, 63, 24, 0, 63, 25, 0,
  53. 63, 25, 0, 63, 26, 0, 63, 26, 0, 63, 27, 0, 63, 27, 0, 63, 28, 0, 63, 28, 0, 63, 29, 0,
  54. 63, 29, 0, 63, 30, 0, 63, 30, 0, 63, 31, 0, 63, 31, 0, 63, 32, 0, 63, 32, 0, 63, 33, 0,
  55. 63, 33, 0, 63, 34, 0, 63, 34, 0, 63, 35, 0, 63, 35, 0, 63, 36, 0, 63, 36, 0, 63, 37, 0,
  56. 63, 38, 0, 63, 38, 0, 63, 39, 0, 63, 39, 0, 63, 40, 0, 63, 40, 0, 63, 41, 0, 63, 41, 0,
  57. 63, 42, 0, 63, 42, 0, 63, 43, 0, 63, 43, 0, 63, 44, 0, 63, 44, 0, 63, 45, 0, 63, 45, 0,
  58. 63, 46, 0, 63, 46, 0, 63, 47, 0, 63, 47, 0, 63, 48, 0, 63, 48, 0, 63, 49, 0, 63, 49, 0,
  59. 63, 50, 0, 63, 50, 0, 63, 51, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0, 63, 52, 0,
  60. 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 53, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0, 63, 54, 0,
  61. 63, 54, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 55, 0, 63, 56, 0, 63, 56, 0, 63, 56, 0,
  62. 63, 56, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 57, 0, 63, 58, 0, 63, 58, 0,
  63. 63, 58, 0, 63, 58, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 59, 0, 63, 60, 0, 63, 60, 0,
  64. 63, 60, 0, 63, 60, 0, 63, 60, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 61, 0, 63, 62, 0,
  65. 63, 62, 0, 63, 62, 0, 63, 62, 0, 63, 63, 0, 63, 63, 1, 63, 63, 2, 63, 63, 3, 63, 63, 4,
  66. 63, 63, 5, 63, 63, 6, 63, 63, 7, 63, 63, 8, 63, 63, 9, 63, 63, 10, 63, 63, 10, 63, 63, 11,
  67. 63, 63, 12, 63, 63, 13, 63, 63, 14, 63, 63, 15, 63, 63, 16, 63, 63, 17, 63, 63, 18, 63, 63, 19,
  68. 63, 63, 20, 63, 63, 21, 63, 63, 21, 63, 63, 22, 63, 63, 23, 63, 63, 24, 63, 63, 25, 63, 63, 26,
  69. 63, 63, 27, 63, 63, 28, 63, 63, 29, 63, 63, 30, 63, 63, 31, 63, 63, 31, 63, 63, 32, 63, 63, 33,
  70. 63, 63, 34, 63, 63, 35, 63, 63, 36, 63, 63, 37, 63, 63, 38, 63, 63, 39, 63, 63, 40, 63, 63, 41,
  71. 63, 63, 42, 63, 63, 42, 63, 63, 43, 63, 63, 44, 63, 63, 45, 63, 63, 46, 63, 63, 47, 63, 63, 48,
  72. 63, 63, 49, 63, 63, 50, 63, 63, 51, 63, 63, 52, 63, 63, 52, 63, 63, 53, 63, 63, 54, 63, 63, 55,
  73. 63, 63, 56, 63, 63, 57, 63, 63, 58, 63, 63, 59, 63, 63, 60, 63, 63, 61, 63, 63, 62, 63, 63, 63};
  74. int main(int argc, char **argv)
  75. {
  76. int quit = 0;
  77. float i=0, j=0, k=0;
  78. int p;
  79. unsigned int x1=0, yy1=0;
  80. unsigned int x2=20, y2=70;
  81. unsigned int x3=30, y3=20;
  82. int r[256], g[256], b[256], a[256];
  83. if(caca_init())
  84. return 1;
  85. caca_set_delay(40000);
  86. /* Make the palette eatable by libcaca */
  87. for (p = 0; p < 256; p++)
  88. {
  89. b[p] = pal[p * 3] * 64;
  90. g[p] = pal[p * 3 + 1] * 64;
  91. r[p] = pal[p * 3 + 2] * 64;
  92. a[p] = 0xfff;
  93. }
  94. /* Create the bitmap */
  95. caca_bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  96. /* Set the palette */
  97. caca_set_bitmap_palette(caca_bitmap, r, g, b, a);
  98. /* Our virtual buffer */
  99. bitmap = malloc(XSIZ * YSIZ * sizeof(char));
  100. /* Generate ball sprite */
  101. generateBalls();
  102. /* Go ! */
  103. while(!quit)
  104. {
  105. /* Get event and test it for a key pressure */
  106. int event = caca_get_event(CACA_EVENT_KEY_PRESS);
  107. if((event & CACA_EVENT_KEY_PRESS))
  108. {
  109. quit = 1;
  110. }
  111. /* Silly paths for our balls */
  112. x1 = abs(sin(i)*(XSIZ-METASIZE));
  113. yy1 = abs(cos(j)*(YSIZ-METASIZE));
  114. x2 = abs(cos(j*2)*(XSIZ-METASIZE));
  115. y2 = abs(cos(i/2)*(YSIZ-METASIZE));
  116. x3 = abs(cos(k*2)*(XSIZ-METASIZE));
  117. y3 = abs(cos(i+j/2)*(YSIZ-METASIZE));
  118. /* Here is all the trick. Maybe if you're that
  119. clever you'll understand.
  120. */
  121. drawBalls(x1,x2);
  122. drawBalls(x2,y2);
  123. drawBalls(x3,y3);
  124. i+=0.01;
  125. j+=0.02;
  126. k+=0.03;
  127. /* Draw our virtual buffer to screen, letting libcaca resize it */
  128. caca_draw_bitmap(0, 0, caca_get_width() , caca_get_height(),
  129. caca_bitmap, bitmap);
  130. caca_refresh();
  131. memset(bitmap, 0, XSIZ*YSIZ);
  132. }
  133. /* End, bye folks */
  134. caca_end();
  135. return 0;
  136. }
  137. /* You missed the trick ? */
  138. void drawBalls(unsigned int bx, unsigned int by)
  139. {
  140. unsigned int color;
  141. unsigned int i, e=0;
  142. unsigned int b = (by*XSIZ)+bx;
  143. for(i=0;i<METASIZE*METASIZE;i++){
  144. color = bitmap[b] + MetaBoule[i];
  145. if(color>255) color = 255;
  146. bitmap[b] = color;
  147. if(e==METASIZE){e=0;b+=(XSIZ-(METASIZE));}
  148. b++;
  149. e++;
  150. }
  151. }
  152. /* 'Limit' the color to 100, to have the cropped effect */
  153. void cutBalls(unsigned int bx, unsigned int by)
  154. {
  155. unsigned int i, e=0;
  156. unsigned int b = (by*XSIZ)+bx;
  157. for(i=0;i<METASIZE*METASIZE;i++){
  158. if(bitmap[b]<100) bitmap[b] = 0;
  159. if(e==METASIZE){e=0;b+=(XSIZ-(METASIZE));}
  160. b++;
  161. e++;
  162. }
  163. }
  164. /* Generate ball sprite
  165. You should read the comments, I already wrote that before ...
  166. */
  167. void generateBalls(void)
  168. {
  169. int x,y,distance;
  170. for(y=0;y<METASIZE;y++)
  171. for(x=0;x<METASIZE;x++)
  172. {
  173. distance = sqrt( ((METACENTER-x)*(METACENTER-x)) + ((METACENTER-y)*(METACENTER-y)));
  174. MetaBoule[x+y*METASIZE]= (255-(distance))*15;
  175. if(distance > 15)
  176. MetaBoule[x+y*METASIZE] = 0;
  177. }
  178. }