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.
 
 
 
 
 
 

158 regels
4.1 KiB

  1. /*
  2. * cacamoir moiré circles effect for libcaca
  3. * Copyright (c) 2004 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2 of the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. */
  23. #include "config.h"
  24. #include <math.h>
  25. #include <string.h>
  26. #include "caca.h"
  27. /* Virtual buffer size */
  28. #define XSIZ 256
  29. #define YSIZ 256
  30. #define DISCSIZ 512
  31. #define DISCTHICKNESS 64
  32. static unsigned char screen[XSIZ * YSIZ];
  33. static unsigned char disc[DISCSIZ * DISCSIZ];
  34. static void put_disc(int, int);
  35. static void draw_disc(int, char);
  36. static void draw_line(int, int, char);
  37. int main (int argc, char **argv)
  38. {
  39. unsigned int red[256], green[256], blue[256], alpha[256];
  40. struct caca_bitmap *bitmap;
  41. int i, x, y, frame = 0, pause = 0;
  42. if(caca_init() < 0)
  43. return 1;
  44. caca_set_delay(20000);
  45. /* Fill various tables */
  46. for(i = 0 ; i < 256; i++)
  47. red[i] = green[i] = blue[i] = alpha[i] = 0;
  48. red[0] = green[0] = blue[0] = 0x777;
  49. red[1] = green[1] = blue[1] = 0xfff;
  50. /* Fill the circle */
  51. for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
  52. draw_disc(i, (i / DISCTHICKNESS) % 2);
  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. memset(screen, 0, XSIZ * YSIZ);
  66. /* Set the palette */
  67. red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
  68. green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
  69. blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
  70. red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
  71. green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
  72. blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
  73. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  74. /* Draw circles */
  75. x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
  76. y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
  77. put_disc(x, y);
  78. x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
  79. y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
  80. put_disc(x, y);
  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 put_disc(int x, int y)
  93. {
  94. char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
  95. int i, j;
  96. for(j = 0; j < YSIZ; j++)
  97. for(i = 0; i < XSIZ; i++)
  98. {
  99. screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
  100. }
  101. }
  102. static void draw_disc(int r, char color)
  103. {
  104. int t, dx, dy;
  105. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  106. {
  107. draw_line(dx / 3, dy / 3, color);
  108. draw_line(dy / 3, dx / 3, color);
  109. t += t > 0 ? dx - dy-- : dx;
  110. }
  111. }
  112. static void draw_line(int x, int y, char color)
  113. {
  114. if(x == 0 || y == 0 || y > DISCSIZ / 2)
  115. return;
  116. if(x > DISCSIZ / 2)
  117. x = DISCSIZ / 2;
  118. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
  119. color, 2 * x - 1);
  120. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
  121. color, 2 * x - 1);
  122. }