25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

145 satır
3.8 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. int red[256], green[256], blue[256], alpha[256];
  40. struct caca_bitmap *bitmap;
  41. int i, x, y, frame;
  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(frame = 0; !caca_get_event(CACA_EVENT_KEY_PRESS); frame++)
  57. {
  58. memset(screen, 0, XSIZ * YSIZ);
  59. /* Set the palette */
  60. red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
  61. green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
  62. blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
  63. red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
  64. green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
  65. blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
  66. caca_set_bitmap_palette(bitmap, red, green, blue, alpha);
  67. /* Draw circles */
  68. x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
  69. y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
  70. put_disc(x, y);
  71. x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
  72. y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
  73. put_disc(x, y);
  74. caca_draw_bitmap(0, 0, caca_get_width() - 1, caca_get_height() - 1,
  75. bitmap, screen);
  76. caca_refresh();
  77. }
  78. caca_free_bitmap(bitmap);
  79. caca_end();
  80. return 0;
  81. }
  82. static void put_disc(int x, int y)
  83. {
  84. char *src = disc + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
  85. int i, j;
  86. for(j = 0; j < YSIZ; j++)
  87. for(i = 0; i < XSIZ; i++)
  88. {
  89. screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
  90. }
  91. }
  92. static void draw_disc(int r, char color)
  93. {
  94. int t, dx, dy;
  95. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  96. {
  97. draw_line(dx / 3, dy / 3, color);
  98. draw_line(dy / 3, dx / 3, color);
  99. t += t > 0 ? dx - dy-- : dx;
  100. }
  101. }
  102. static void draw_line(int x, int y, char color)
  103. {
  104. if(x == 0 || y == 0 || y > DISCSIZ / 2)
  105. return;
  106. if(x > DISCSIZ / 2)
  107. x = DISCSIZ / 2;
  108. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
  109. color, 2 * x - 1);
  110. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
  111. color, 2 * x - 1);
  112. }