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.
 
 
 
 
 
 

143 lines
4.0 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 Do What The Fuck You Want To
  11. * Public License, Version 2, as published by Sam Hocevar. See
  12. * http://sam.zoy.org/wtfpl/COPYING for more details.
  13. */
  14. #include "config.h"
  15. #include <math.h>
  16. #ifndef M_PI
  17. # define M_PI 3.14159265358979323846
  18. #endif
  19. #include "caca.h"
  20. /* Virtual buffer size */
  21. #define XSIZ 256
  22. #define YSIZ 256
  23. #define TABLEX (XSIZ * 2)
  24. #define TABLEY (YSIZ * 2)
  25. static unsigned char screen[XSIZ * YSIZ];
  26. static unsigned char table[TABLEX * TABLEY];
  27. static void do_plasma(unsigned char *,
  28. double, double, double, double, double, double);
  29. int main (int argc, char **argv)
  30. {
  31. unsigned int red[256], green[256], blue[256], alpha[256];
  32. double r[3], R[6];
  33. struct caca_bitmap *bitmap;
  34. int i, x, y, frame = 0, pause = 0;
  35. if(caca_init() < 0)
  36. return 1;
  37. caca_set_delay(20000);
  38. /* Fill various tables */
  39. for(i = 0 ; i < 256; i++)
  40. red[i] = green[i] = blue[i] = alpha[i] = 0;
  41. for(i = 0; i < 3; i++)
  42. r[i] = (double)(caca_rand(1, 1000)) / 60000 * M_PI;
  43. for(i = 0; i < 6; i++)
  44. R[i] = (double)(caca_rand(1, 1000)) / 10000;
  45. for(y = 0 ; y < TABLEY ; y++)
  46. for(x = 0 ; x < TABLEX ; x++)
  47. {
  48. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  49. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  50. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  51. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  52. }
  53. /* Create a libcaca bitmap */
  54. bitmap = caca_create_bitmap(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  55. /* Main loop */
  56. for(;;)
  57. {
  58. switch(caca_get_event(CACA_EVENT_KEY_PRESS))
  59. {
  60. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  61. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  62. }
  63. if(pause)
  64. goto paused;
  65. for(i = 0 ; i < 256; i++)
  66. {
  67. double z = ((double)i) / 256 * 6 * M_PI;
  68. red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  69. green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  70. blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  71. }
  72. /* Set the palette */
  73. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  74. do_plasma(screen,
  75. (1.0 + sin(((double)frame) * R[0])) / 2,
  76. (1.0 + sin(((double)frame) * R[1])) / 2,
  77. (1.0 + sin(((double)frame) * R[2])) / 2,
  78. (1.0 + sin(((double)frame) * R[3])) / 2,
  79. (1.0 + sin(((double)frame) * R[4])) / 2,
  80. (1.0 + sin(((double)frame) * R[5])) / 2);
  81. frame++;
  82. paused:
  83. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  84. bitmap, screen);
  85. caca_refresh();
  86. }
  87. end:
  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. }