Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

153 řádky
4.4 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 = 0, pause = 0;
  45. if(caca_init() < 0)
  46. return 1;
  47. caca_set_delay(20000);
  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)) / 60000 * M_PI;
  53. for(i = 0; i < 6; i++)
  54. R[i] = (double)(caca_rand(1, 1000)) / 10000;
  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(;;)
  67. {
  68. switch(caca_get_event(CACA_EVENT_KEY_PRESS))
  69. {
  70. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  71. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  72. }
  73. if(pause)
  74. goto paused;
  75. for(i = 0 ; i < 256; i++)
  76. {
  77. double z = ((double)i) / 256 * 6 * M_PI;
  78. red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  79. green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  80. blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  81. }
  82. /* Set the palette */
  83. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  84. do_plasma(screen,
  85. (1.0 + sin(((double)frame) * R[0])) / 2,
  86. (1.0 + sin(((double)frame) * R[1])) / 2,
  87. (1.0 + sin(((double)frame) * R[2])) / 2,
  88. (1.0 + sin(((double)frame) * R[3])) / 2,
  89. (1.0 + sin(((double)frame) * R[4])) / 2,
  90. (1.0 + sin(((double)frame) * R[5])) / 2);
  91. frame++;
  92. paused:
  93. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  94. bitmap, screen);
  95. caca_refresh();
  96. }
  97. end:
  98. caca_free_bitmap(bitmap);
  99. caca_end();
  100. return 0;
  101. }
  102. static void do_plasma(unsigned char *pixels, double x_1, double y_1,
  103. double x_2, double y_2, double x_3, double y_3)
  104. {
  105. unsigned int X1 = x_1 * (TABLEX / 2),
  106. Y1 = y_1 * (TABLEY / 2),
  107. X2 = x_2 * (TABLEX / 2),
  108. Y2 = y_2 * (TABLEY / 2),
  109. X3 = x_3 * (TABLEX / 2),
  110. Y3 = y_3 * (TABLEY / 2);
  111. unsigned int y;
  112. unsigned char * t1 = table + X1 + Y1 * TABLEX,
  113. * t2 = table + X2 + Y2 * TABLEX,
  114. * t3 = table + X3 + Y3 * TABLEX;
  115. for(y = 0; y < YSIZ; y++)
  116. {
  117. unsigned int x;
  118. unsigned char * tmp = pixels + y * YSIZ;
  119. unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
  120. for(x = 0; ty < tmax; ty++, tmp++)
  121. tmp[0] = t1[ty] + t2[ty] + t3[ty];
  122. }
  123. }