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.
 
 
 
 
 
 

141 lines
4.2 KiB

  1. /*
  2. * cacaplas plasma effect for libcaca
  3. * Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
  4. * 1998 Michele Bini <mibin@tin.it>
  5. * All Rights Reserved
  6. *
  7. * $Id$
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  22. * 02111-1307 USA
  23. */
  24. #include "config.h"
  25. #include <math.h>
  26. #ifndef M_PI
  27. # define M_PI 3.14159265358979323846
  28. #endif
  29. #include "caca.h"
  30. /* Virtual buffer size */
  31. #define XSIZ 256
  32. #define YSIZ 256
  33. #define TABLEX (XSIZ * 2)
  34. #define TABLEY (YSIZ * 2)
  35. static unsigned char screen[XSIZ * YSIZ];
  36. static unsigned char table[TABLEX * TABLEY];
  37. static void do_plasma(unsigned char *,
  38. double, double, double, double, double, double);
  39. int main (int argc, char **argv)
  40. {
  41. int red[256], green[256], blue[256], alpha[256];
  42. double r[3], R[6];
  43. struct caca_bitmap *bitmap;
  44. int i, x, y, frame;
  45. if(caca_init() < 0)
  46. return 1;
  47. caca_set_delay(10000);
  48. /* Fill various tables */
  49. for(i = 0 ; i < 256; i++)
  50. red[i] = green[i] = blue[i] = alpha[i] = 0;
  51. for(i = 0; i < 3; i++)
  52. r[i] = (double)(caca_rand(1, 1000)) / 30000 * M_PI;
  53. for(i = 0; i < 6; i++)
  54. R[i] = (double)(caca_rand(1, 1000)) / 5000;
  55. for(y = 0 ; y < TABLEY ; y++)
  56. for(x = 0 ; x < TABLEX ; x++)
  57. {
  58. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  59. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  60. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  61. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  62. }
  63. /* Create a libcaca bitmap */
  64. bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  65. /* Main loop */
  66. for(frame = 0; !caca_get_event(CACA_EVENT_KEY_PRESS); frame++)
  67. {
  68. for(i = 0 ; i < 256; i++)
  69. {
  70. double z = ((double)i) / 256 * 6 * M_PI;
  71. red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  72. green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  73. blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  74. }
  75. /* Set the palette */
  76. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  77. do_plasma(screen,
  78. (1.0 + sin(((double)frame) * R[0])) / 2,
  79. (1.0 + sin(((double)frame) * R[1])) / 2,
  80. (1.0 + sin(((double)frame) * R[2])) / 2,
  81. (1.0 + sin(((double)frame) * R[3])) / 2,
  82. (1.0 + sin(((double)frame) * R[4])) / 2,
  83. (1.0 + sin(((double)frame) * R[5])) / 2);
  84. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  85. bitmap, screen);
  86. caca_refresh();
  87. }
  88. caca_free_bitmap(bitmap);
  89. caca_end();
  90. return 0;
  91. }
  92. static void do_plasma(unsigned char *pixels, double x_1, double y_1,
  93. double x_2, double y_2, double x_3, double y_3)
  94. {
  95. unsigned int X1 = x_1 * (TABLEX / 2),
  96. Y1 = y_1 * (TABLEY / 2),
  97. X2 = x_2 * (TABLEX / 2),
  98. Y2 = y_2 * (TABLEY / 2),
  99. X3 = x_3 * (TABLEX / 2),
  100. Y3 = y_3 * (TABLEY / 2);
  101. unsigned int y;
  102. unsigned char * t1 = table + X1 + Y1 * TABLEX,
  103. * t2 = table + X2 + Y2 * TABLEX,
  104. * t3 = table + X3 + Y3 * TABLEX;
  105. for(y = 0; y < YSIZ; y++)
  106. {
  107. unsigned int x;
  108. unsigned char * tmp = pixels + y * YSIZ;
  109. unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
  110. for(x = 0; ty < tmax; ty++, tmp++)
  111. tmp[0] = t1[ty] + t2[ty] + t3[ty];
  112. }
  113. }