Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

152 rader
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. #if !defined(__KERNEL__)
  16. # include <math.h>
  17. # ifndef M_PI
  18. # define M_PI 3.14159265358979323846
  19. # endif
  20. #endif
  21. #include "cucul.h"
  22. #include "caca.h"
  23. /* Virtual buffer size */
  24. #define XSIZ 256
  25. #define YSIZ 256
  26. #define TABLEX (XSIZ * 2)
  27. #define TABLEY (YSIZ * 2)
  28. static unsigned char screen[XSIZ * YSIZ];
  29. static unsigned char table[TABLEX * TABLEY];
  30. static void do_plasma(unsigned char *,
  31. double, double, double, double, double, double);
  32. int main (int argc, char **argv)
  33. {
  34. cucul_t *qq; caca_t *kk;
  35. unsigned int red[256], green[256], blue[256], alpha[256];
  36. double r[3], R[6];
  37. struct cucul_bitmap *bitmap;
  38. int i, x, y, frame = 0, pause = 0;
  39. qq = cucul_init();
  40. if(!qq)
  41. return 1;
  42. kk = caca_attach(qq);
  43. if(!kk)
  44. return 1;
  45. caca_set_delay(kk, 20000);
  46. /* Fill various tables */
  47. for(i = 0 ; i < 256; i++)
  48. red[i] = green[i] = blue[i] = alpha[i] = 0;
  49. for(i = 0; i < 3; i++)
  50. r[i] = (double)(cucul_rand(1, 1000)) / 60000 * M_PI;
  51. for(i = 0; i < 6; i++)
  52. R[i] = (double)(cucul_rand(1, 1000)) / 10000;
  53. for(y = 0 ; y < TABLEY ; y++)
  54. for(x = 0 ; x < TABLEX ; x++)
  55. {
  56. double tmp = (((double)((x - (TABLEX / 2)) * (x - (TABLEX / 2))
  57. + (y - (TABLEX / 2)) * (y - (TABLEX / 2))))
  58. * (M_PI / (TABLEX * TABLEX + TABLEY * TABLEY)));
  59. table[x + y * TABLEX] = (1.0 + sin(12.0 * sqrt(tmp))) * 256 / 6;
  60. }
  61. /* Create a libcucul bitmap */
  62. bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  63. /* Main loop */
  64. for(;;)
  65. {
  66. switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
  67. {
  68. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  69. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  70. }
  71. if(pause)
  72. goto paused;
  73. for(i = 0 ; i < 256; i++)
  74. {
  75. double z = ((double)i) / 256 * 6 * M_PI;
  76. red[i] = (1.0 + cos(z + r[0] * frame)) / 2 * 0xfff;
  77. green[i] = (1.0 + sin(z + r[1] * frame)) / 2 * 0xfff;
  78. blue[i] = (1.0 + cos(z + r[2] * frame)) / 2 * 0xfff;
  79. }
  80. /* Set the palette */
  81. cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha);
  82. do_plasma(screen,
  83. (1.0 + sin(((double)frame) * R[0])) / 2,
  84. (1.0 + sin(((double)frame) * R[1])) / 2,
  85. (1.0 + sin(((double)frame) * R[2])) / 2,
  86. (1.0 + sin(((double)frame) * R[3])) / 2,
  87. (1.0 + sin(((double)frame) * R[4])) / 2,
  88. (1.0 + sin(((double)frame) * R[5])) / 2);
  89. frame++;
  90. paused:
  91. cucul_draw_bitmap(qq, 0, 0,
  92. cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
  93. bitmap, screen);
  94. caca_display(kk);
  95. }
  96. end:
  97. cucul_free_bitmap(qq, bitmap);
  98. caca_detach(kk);
  99. cucul_end(qq);
  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. }