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.

matrix.c 4.1 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * matrix matrix effect with HDAPS support
  3. * Copyright (c) 2006 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 "common.h"
  15. #if defined(HAVE_INTTYPES_H)
  16. # include <inttypes.h>
  17. #endif
  18. #if !defined(__KERNEL__)
  19. # include <stdio.h>
  20. # include <math.h>
  21. #endif
  22. #include "cucul.h"
  23. #include "caca.h"
  24. #define MAXDROPS 500
  25. #define MINLEN 15
  26. #define MAXLEN 30
  27. struct drop
  28. {
  29. int x, y, speed, len;
  30. char str[MAXLEN];
  31. }
  32. drop[MAXDROPS];
  33. int main(void)
  34. {
  35. cucul_canvas_t *cv;
  36. caca_display_t *dp;
  37. int i, j;
  38. int w, h, xo, yo, dx = 0, dy = 0, newdx, newdy;
  39. int have_hdaps = 1, have_ams = 1;
  40. cv = cucul_create_canvas(0, 0);
  41. dp = caca_create_display(cv);
  42. caca_set_display_time(dp, 20000);
  43. for(i = 0; i < MAXDROPS; i++)
  44. {
  45. drop[i].x = cucul_rand(0, 1000);
  46. drop[i].y = cucul_rand(0, 1000);
  47. drop[i].speed = 5 + cucul_rand(0, 30);
  48. drop[i].len = MINLEN + cucul_rand(0, (MAXLEN - MINLEN));
  49. for(j = 0; j < MAXLEN; j++)
  50. drop[i].str[j] = cucul_rand('0', 'z');
  51. }
  52. for(;;)
  53. {
  54. int sensorx, sensory;
  55. if(have_hdaps)
  56. {
  57. /* IBM HDAPS support */
  58. FILE *f = fopen("/sys/devices/platform/hdaps/position", "r");
  59. if(f)
  60. {
  61. sensorx = sensory = -387; /* FIXME */
  62. fscanf(f, "(%d,%d)", &sensorx, &sensory);
  63. fclose(f);
  64. sensorx = (sensorx + 387) * 180 / 387 * 4;
  65. sensory = (sensory + 387) * 180 / 387 * 4;
  66. }
  67. else
  68. have_hdaps = 0;
  69. }
  70. else if(have_ams)
  71. {
  72. /* Apple Motion Sensor support */
  73. FILE *fx = fopen("/sys/devices/ams/x", "r");
  74. FILE *fy = fopen("/sys/devices/ams/y", "r");
  75. if(fx && fy)
  76. {
  77. sensorx = sensory = 0;
  78. fscanf(fx, "%d", &sensorx);
  79. fscanf(fy, "%d", &sensory);
  80. fclose(fx);
  81. fclose(fy);
  82. sensorx = - sensorx * 180 / 60;
  83. }
  84. else
  85. have_ams = 0;
  86. }
  87. w = cucul_get_canvas_width(cv);
  88. h = cucul_get_canvas_height(cv);
  89. xo = w / 2;
  90. yo = h / 2;
  91. //newdx = 1000.0 * cos((M_PI / 360.0) * (sensorx));
  92. //newdy = -1000.0 * sin((M_PI / 360.0) * (sensorx));
  93. newdx = sensorx;
  94. newdy = 100;
  95. if(newdx > -10 && newdx < 10)
  96. newdx = 0;
  97. dx = (newdx + 3 * dx) / 4;
  98. dy = (newdy + 3 * dy) / 4;
  99. cucul_clear_canvas(cv);
  100. for(i = 0; i < MAXDROPS && i < (w * h / 32); i++)
  101. {
  102. int x, y;
  103. drop[i].y += drop[i].speed;
  104. if(drop[i].y > 1000)
  105. {
  106. drop[i].y -= 1000;
  107. drop[i].x = cucul_rand(0, 1000);
  108. }
  109. x = drop[i].x * w / 1000 / 2 * 2;
  110. y = drop[i].y * (h + MAXLEN) / 1000;
  111. x += (y - h / 2) * dx / dy;
  112. for(j = 0; j < drop[i].len; j++)
  113. {
  114. unsigned int fg;
  115. if(j < 2)
  116. fg = CUCUL_COLOR_WHITE;
  117. else if(j < drop[i].len / 4)
  118. fg = CUCUL_COLOR_LIGHTGREEN;
  119. else if(j < drop[i].len * 4 / 5)
  120. fg = CUCUL_COLOR_GREEN;
  121. else
  122. fg = CUCUL_COLOR_DARKGRAY;
  123. cucul_set_color(cv, fg, CUCUL_COLOR_BLACK);
  124. cucul_putchar(cv, (x * dy + dy / 2 - j * dx) / dy, y - j,
  125. drop[i].str[(y - j) % drop[i].len]);
  126. }
  127. }
  128. caca_refresh_display(dp);
  129. if(caca_get_event(dp, CACA_EVENT_KEY_PRESS, NULL, 0))
  130. break;
  131. }
  132. caca_free_display(dp);
  133. cucul_free_canvas(cv);
  134. return 0;
  135. }