No puede seleccionar más de 25 temas Los temas deben comenzar con una letra o número, pueden incluir guiones ('-') y pueden tener hasta 35 caracteres de largo.
 
 
 
 
 
 

151 líneas
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 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 "cucul.h"
  20. #include "caca.h"
  21. /* Virtual buffer size */
  22. #define XSIZ 256
  23. #define YSIZ 256
  24. #define TABLEX (XSIZ * 2)
  25. #define TABLEY (YSIZ * 2)
  26. static unsigned char screen[XSIZ * YSIZ];
  27. static unsigned char table[TABLEX * TABLEY];
  28. static void do_plasma(unsigned char *,
  29. double, double, double, double, double, double);
  30. int main (int argc, char **argv)
  31. {
  32. cucul_t *qq; caca_t *kk;
  33. unsigned int red[256], green[256], blue[256], alpha[256];
  34. double r[3], R[6];
  35. struct cucul_bitmap *bitmap;
  36. int i, x, y, frame = 0, pause = 0;
  37. qq = cucul_init();
  38. if(!qq)
  39. return 1;
  40. kk = caca_attach(qq);
  41. if(!kk)
  42. return 1;
  43. caca_set_delay(kk, 20000);
  44. /* Fill various tables */
  45. for(i = 0 ; i < 256; i++)
  46. red[i] = green[i] = blue[i] = alpha[i] = 0;
  47. for(i = 0; i < 3; i++)
  48. r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
  49. for(i = 0; i < 6; i++)
  50. R[i] = (double)(cucul_rand(1, 1000)) / 10000;
  51. for(y = 0 ; y < TABLEY ; y++)
  52. for(x = 0 ; x < TABLEX ; x++)
  53. {
  54. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  55. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  56. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  57. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  58. }
  59. /* Create a libcucul bitmap */
  60. bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  61. /* Main loop */
  62. for(;;)
  63. {
  64. switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
  65. {
  66. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  67. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  68. }
  69. if(pause)
  70. goto paused;
  71. for(i = 0 ; i < 256; i++)
  72. {
  73. double z = ((double)i) / 256 * 6 * M_PI;
  74. red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  75. green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  76. blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  77. }
  78. /* Set the palette */
  79. cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha);
  80. do_plasma(screen,
  81. (1.0 + sin(((double)frame) * R[0])) / 2,
  82. (1.0 + sin(((double)frame) * R[1])) / 2,
  83. (1.0 + sin(((double)frame) * R[2])) / 2,
  84. (1.0 + sin(((double)frame) * R[3])) / 2,
  85. (1.0 + sin(((double)frame) * R[4])) / 2,
  86. (1.0 + sin(((double)frame) * R[5])) / 2);
  87. frame++;
  88. paused:
  89. cucul_draw_bitmap(qq, 0, 0,
  90. cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
  91. bitmap, screen);
  92. caca_display(kk);
  93. }
  94. end:
  95. cucul_free_bitmap(qq, bitmap);
  96. caca_detach(kk);
  97. cucul_end(qq);
  98. return 0;
  99. }
  100. static void do_plasma(unsigned char *pixels, double x_1, double y_1,
  101. double x_2, double y_2, double x_3, double y_3)
  102. {
  103. unsigned int X1 = x_1 * (TABLEX / 2),
  104. Y1 = y_1 * (TABLEY / 2),
  105. X2 = x_2 * (TABLEX / 2),
  106. Y2 = y_2 * (TABLEY / 2),
  107. X3 = x_3 * (TABLEX / 2),
  108. Y3 = y_3 * (TABLEY / 2);
  109. unsigned int y;
  110. unsigned char * t1 = table + X1 + Y1 * TABLEX,
  111. * t2 = table + X2 + Y2 * TABLEX,
  112. * t3 = table + X3 + Y3 * TABLEX;
  113. for(y = 0; y < YSIZ; y++)
  114. {
  115. unsigned int x;
  116. unsigned char * tmp = pixels + y * YSIZ;
  117. unsigned int ty = y * TABLEX, tmax = ty + XSIZ;
  118. for(x = 0; ty < tmax; ty++, tmp++)
  119. tmp[0] = t1[ty] + t2[ty] + t3[ty];
  120. }
  121. }