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.

cacamoir.c 3.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #if !defined(__KERNEL__)
  15. # include <math.h>
  16. # include <string.h>
  17. #endif
  18. #include "cucul.h"
  19. #include "caca.h"
  20. /* Virtual buffer size */
  21. #define XSIZ 256
  22. #define YSIZ 256
  23. #define DISCSIZ 512
  24. #define DISCTHICKNESS 64
  25. static unsigned char screen[XSIZ * YSIZ];
  26. static unsigned char disc[DISCSIZ * DISCSIZ];
  27. static void put_disc(int, int);
  28. static void draw_disc(int, char);
  29. static void draw_line(int, int, char);
  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. struct cucul_bitmap *bitmap;
  35. int i, x, y, frame = 0, pause = 0;
  36. qq = cucul_init();
  37. if(!qq)
  38. return 1;
  39. kk = caca_attach(qq);
  40. if(!kk)
  41. return 1;
  42. caca_set_delay(kk, 20000);
  43. /* Fill various tables */
  44. for(i = 0 ; i < 256; i++)
  45. red[i] = green[i] = blue[i] = alpha[i] = 0;
  46. red[0] = green[0] = blue[0] = 0x777;
  47. red[1] = green[1] = blue[1] = 0xfff;
  48. /* Fill the circle */
  49. for(i = DISCSIZ * 2; i > 0; i -= DISCTHICKNESS)
  50. draw_disc(i, (i / DISCTHICKNESS) % 2);
  51. /* Create a libcucul bitmap */
  52. bitmap = cucul_create_bitmap(qq, 8, XSIZ, YSIZ, XSIZ, 0, 0, 0, 0);
  53. /* Main loop */
  54. for(;;)
  55. {
  56. switch(caca_get_event(kk, CACA_EVENT_KEY_PRESS))
  57. {
  58. case CACA_EVENT_KEY_PRESS | CACA_KEY_ESCAPE: goto end;
  59. case CACA_EVENT_KEY_PRESS | ' ': pause = !pause;
  60. }
  61. if(pause)
  62. goto paused;
  63. memset(screen, 0, XSIZ * YSIZ);
  64. /* Set the palette */
  65. red[0] = 0.5 * (1 + sin(0.05 * frame)) * 0xfff;
  66. green[0] = 0.5 * (1 + cos(0.07 * frame)) * 0xfff;
  67. blue[0] = 0.5 * (1 + cos(0.06 * frame)) * 0xfff;
  68. red[1] = 0.5 * (1 + sin(0.07 * frame + 5.0)) * 0xfff;
  69. green[1] = 0.5 * (1 + cos(0.06 * frame + 5.0)) * 0xfff;
  70. blue[1] = 0.5 * (1 + cos(0.05 * frame + 5.0)) * 0xfff;
  71. cucul_set_bitmap_palette(qq, bitmap, red, green, blue, alpha);
  72. /* Draw circles */
  73. x = cos(0.07 * frame + 5.0) * 128.0 + (XSIZ / 2);
  74. y = sin(0.11 * frame) * 128.0 + (YSIZ / 2);
  75. put_disc(x, y);
  76. x = cos(0.13 * frame + 2.0) * 64.0 + (XSIZ / 2);
  77. y = sin(0.09 * frame + 1.0) * 64.0 + (YSIZ / 2);
  78. put_disc(x, y);
  79. frame++;
  80. paused:
  81. cucul_draw_bitmap(qq, 0, 0,
  82. cucul_get_width(qq) - 1, cucul_get_height(qq) - 1,
  83. bitmap, screen);
  84. caca_display(kk);
  85. }
  86. end:
  87. cucul_free_bitmap(qq, bitmap);
  88. caca_detach(kk);
  89. cucul_end(qq);
  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. }