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.
 
 
 
 
 
 

175 lines
5.0 KiB

  1. /*
  2. * cacaplas plasma effect for libcaca
  3. * Copyright (c) 1998 Michele Bini <mibin@tin.it>
  4. * 2004 Sam Hocevar <sam@zoy.org>
  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 "common.h"
  16. #if !defined(__KERNEL__)
  17. # include <stdio.h>
  18. # include <math.h>
  19. # ifndef M_PI
  20. # define M_PI 3.14159265358979323846
  21. # endif
  22. #endif
  23. #include "cucul.h"
  24. #include "caca.h"
  25. /* Virtual buffer size */
  26. #define XSIZ 256
  27. #define YSIZ 256
  28. #define TABLEX (XSIZ * 2)
  29. #define TABLEY (YSIZ * 2)
  30. static unsigned char screen[XSIZ * YSIZ];
  31. static unsigned char table[TABLEX * TABLEY];
  32. static void do_plasma(unsigned char *,
  33. double, double, double, double, double, double);
  34. int main (int argc, char **argv)
  35. {
  36. cucul_canvas_t *cv, *c2, *mask; caca_display_t *dp;
  37. unsigned int red[256], green[256], blue[256], alpha[256];
  38. double r[3], R[6];
  39. cucul_dither_t *dither;
  40. int i, x, y, frame = 0, pause = 0;
  41. cv = cucul_create_canvas(0, 0);
  42. if(!cv)
  43. return 1;
  44. dp = caca_create_display(cv);
  45. if(!dp)
  46. return 1;
  47. caca_set_delay(dp, 20000);
  48. c2 = cucul_create_canvas(cucul_get_canvas_width(cv),
  49. cucul_get_canvas_height(cv));
  50. mask = cucul_create_canvas(cucul_get_canvas_width(cv),
  51. cucul_get_canvas_height(cv));
  52. /* Fill various tables */
  53. for(i = 0 ; i < 256; i++)
  54. red[i] = green[i] = blue[i] = alpha[i] = 0;
  55. for(i = 0; i < 3; i++)
  56. r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
  57. for(i = 0; i < 6; i++)
  58. R[i] = (double)(cucul_rand(1, 1000)) / 10000;
  59. for(y = 0 ; y < TABLEY ; y++)
  60. for(x = 0 ; x < TABLEX ; x++)
  61. {
  62. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  63. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  64. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  65. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  66. }
  67. /* Create a libcucul dither */
  68. dither = cucul_create_dither(8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  69. /* Main loop */
  70. for(;;)
  71. {
  72. caca_event_t ev;
  73. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, &ev, 0))
  74. {
  75. switch(ev.data.key.ch)
  76. {
  77. case CACA_KEY_ESCAPE: goto end;
  78. case ' ': pause = !pause;
  79. }
  80. }
  81. if(pause)
  82. goto paused;
  83. for(i = 0 ; i < 256; i++)
  84. {
  85. double z = ((double)i) / 256 * 6 * M_PI;
  86. red[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  87. blue[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  88. green[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  89. }
  90. /* Set the palette */
  91. cucul_set_dither_palette(dither, red, green, blue, alpha);
  92. do_plasma(screen,
  93. (1.0 + sin(((double)frame) * R[0])) / 2,
  94. (1.0 + sin(((double)frame) * R[1])) / 2,
  95. (1.0 + sin(((double)frame) * R[2])) / 2,
  96. (1.0 + sin(((double)frame) * R[3])) / 2,
  97. (1.0 + sin(((double)frame) * R[4])) / 2,
  98. (1.0 + sin(((double)frame) * R[5])) / 2);
  99. frame++;
  100. paused:
  101. cucul_dither_bitmap(cv, 0, 0,
  102. cucul_get_canvas_width(cv),
  103. cucul_get_canvas_height(cv),
  104. dither, screen);
  105. cucul_blit(c2, 0, 0, cv, NULL);
  106. cucul_invert(c2);
  107. cucul_blit(cv, 0, 0, c2, mask);
  108. cucul_set_color(cv, CUCUL_COLOR_WHITE, CUCUL_COLOR_BLUE);
  109. cucul_putstr(cv, cucul_get_canvas_width(cv) - 30,
  110. cucul_get_canvas_height(cv) - 2,
  111. " -=[ Powered by libcaca ]=- ");
  112. caca_refresh_display(dp);
  113. }
  114. end:
  115. cucul_free_dither(dither);
  116. caca_free_display(dp);
  117. cucul_free_canvas(cv);
  118. return 0;
  119. }
  120. static void do_plasma(unsigned char *pixels, double x_1, double y_1,
  121. double x_2, double y_2, double x_3, double y_3)
  122. {
  123. unsigned int X1 = x_1 * (TABLEX / 2),
  124. Y1 = y_1 * (TABLEY / 2),
  125. X2 = x_2 * (TABLEX / 2),
  126. Y2 = y_2 * (TABLEY / 2),
  127. X3 = x_3 * (TABLEX / 2),
  128. Y3 = y_3 * (TABLEY / 2);
  129. unsigned int y;
  130. unsigned char * t1 = table + X1 + Y1 * TABLEX,
  131. * t2 = table + X2 + Y2 * TABLEX,
  132. * t3 = table + X3 + Y3 * TABLEX;
  133. for(y = 0; y < YSIZ; y++)
  134. {
  135. unsigned int x;
  136. unsigned char * tmp = pixels + y * YSIZ;
  137. unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
  138. for(x = 0; ty < tmax; ty++, tmp++)
  139. tmp[0] = t1[ty] + t2[ty] + t3[ty];
  140. }
  141. }