Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

156 righe
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 Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. #include "config.h"
  14. #include <math.h>
  15. #include <string.h>
  16. #include "cucul.h"
  17. #include "caca.h"
  18. /* Virtual buffer size */
  19. #define XSIZ 256
  20. #define YSIZ 256
  21. #define DISCSIZ 512
  22. #define DISCTHICKNESS 64
  23. static unsigned char screen[XSIZ * YSIZ];
  24. static unsigned char disc[DISCSIZ * DISCSIZ];
  25. static void put_disc(int, int);
  26. static void draw_disc(int, char);
  27. static void draw_line(int, int, char);
  28. int main (int argc, char **argv)
  29. {
  30. cucul_t *qq; caca_t *kk;
  31. unsigned int red[256], green[256], blue[256], alpha[256];
  32. struct cucul_bitmap *bitmap;
  33. int i, x, y, frame = 0, pause = 0;
  34. qq = cucul_init();
  35. if(!qq)
  36. return 1;
  37. kk = caca_attach(qq);
  38. if(!kk)
  39. return 1;
  40. caca_set_delay(kk, 20000);
  41. /* Fill various tables */
  42. for(i = 0 ; i < 256; i++)
  43. red[i] = green[i] = blue[i] = alpha[i] = 0;
  44. red[0] = green[0] = blue[0] = 0x777;
  45. red[1] = green[1] = blue[1] = 0xfff;
  46. /* Fill the circle */
  47. for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
  48. draw_disc(i, (i / DISCTHICKNESS) % 2);
  49. /* Create a libcucul bitmap */
  50. bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  51. /* Main loop */
  52. for(;;)
  53. {
  54. switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
  55. {
  56. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  57. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  58. }
  59. if(pause)
  60. goto paused;
  61. memset(screen, 0, XSIZ * YSIZ);
  62. /* Set the palette */
  63. red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
  64. green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
  65. blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
  66. red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
  67. green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
  68. blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
  69. cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha);
  70. /* Draw circles */
  71. x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
  72. y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
  73. put_disc(x, y);
  74. x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
  75. y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
  76. put_disc(x, y);
  77. frame++;
  78. paused:
  79. cucul_draw_bitmap(qq, 0, 0,
  80. cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
  81. bitmap, screen);
  82. caca_display(kk);
  83. }
  84. end:
  85. cucul_free_bitmap(qq, bitmap);
  86. caca_detach(kk);
  87. cucul_end(qq);
  88. return 0;
  89. }
  90. static void put_disc(int x, int y)
  91. {
  92. char *src = ((char*)disc) + (DISCSIZ / 2 - x) + (DISCSIZ / 2 - y) * DISCSIZ;
  93. int i, j;
  94. for(j = 0; j < YSIZ; j++)
  95. for(i = 0; i < XSIZ; i++)
  96. {
  97. screen[i + XSIZ * j] ^= src[i + DISCSIZ * j];
  98. }
  99. }
  100. static void draw_disc(int r, char color)
  101. {
  102. int t, dx, dy;
  103. for(t = 0, dx = 0, dy = r; dx <= dy; dx++)
  104. {
  105. draw_line(dx / 3, dy / 3, color);
  106. draw_line(dy / 3, dx / 3, color);
  107. t += t > 0 ? dx - dy-- : dx;
  108. }
  109. }
  110. static void draw_line(int x, int y, char color)
  111. {
  112. if(x == 0 || y == 0 || y > DISCSIZ / 2)
  113. return;
  114. if(x > DISCSIZ / 2)
  115. x = DISCSIZ / 2;
  116. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) - y),
  117. color, 2 * x - 1);
  118. memset(disc + (DISCSIZ / 2) - x + DISCSIZ * ((DISCSIZ / 2) + y - 1),
  119. color, 2 * x - 1);
  120. }