hopalong.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * CDE - Common Desktop Environment
  3. *
  4. * Copyright (c) 1993-2012, The Open Group. All rights reserved.
  5. *
  6. * These libraries and programs are free software; you can
  7. * redistribute them and/or modify them under the terms of the GNU
  8. * Lesser General Public License as published by the Free Software
  9. * Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * These libraries and programs are distributed in the hope that
  13. * they will be useful, but WITHOUT ANY WARRANTY; without even the
  14. * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  15. * PURPOSE. See the GNU Lesser General Public License for more
  16. * details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with these libraries and programs; if not, write
  20. * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
  21. * Floor, Boston, MA 02110-1301 USA
  22. */
  23. /* $XConsortium: hopalong.c /main/3 1995/11/02 16:07:11 rswiston $ */
  24. /*
  25. */
  26. /* *
  27. * (c) Copyright 1993, 1994 Hewlett-Packard Company *
  28. * (c) Copyright 1993, 1994 International Business Machines Corp. *
  29. * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
  30. * (c) Copyright 1993, 1994 Novell, Inc. *
  31. */
  32. /*-
  33. * hopalong.c - Real Plane Fractals for dtscreen, the X Window System lockscreen.
  34. *
  35. * Copyright (c) 1991 by Patrick J. Naughton.
  36. *
  37. * See dtscreen.c for copying information.
  38. *
  39. * Revision History:
  40. * 29-Oct-90: fix bad (int) cast.
  41. * 29-Jul-90: support for multiple screens.
  42. * 08-Jul-90: new timing and colors and new algorithm for fractals.
  43. * 15-Dec-89: Fix for proper skipping of {White,Black}Pixel() in colors.
  44. * 08-Oct-89: Fixed long standing typo bug in RandomInitHop();
  45. * Fixed bug in memory allocation in inithop();
  46. * Moved seconds() to an extern.
  47. * Got rid of the % mod since .mod is slow on a sparc.
  48. * 20-Sep-89: Lint.
  49. * 31-Aug-88: Forked from dtscreen.c for modularity.
  50. * 23-Mar-88: Coded HOPALONG routines from Scientific American Sept. 86 p. 14.
  51. */
  52. #include "dtscreen.h"
  53. #include <math.h>
  54. #include <stdlib.h>
  55. typedef struct {
  56. int centerx;
  57. int centery; /* center of the screen */
  58. double a;
  59. double b;
  60. double c;
  61. double i;
  62. double j; /* hopalong parameters */
  63. int inc;
  64. int pix;
  65. long startTime;
  66. } hopstruct;
  67. static XPoint *pointBuffer = 0; /* pointer for XDrawPoints */
  68. #define TIMEOUT 30
  69. void
  70. inithop(perwindow *pwin)
  71. {
  72. double range;
  73. XWindowAttributes xgwa;
  74. hopstruct *hp;
  75. if (pwin->data) free(pwin->data);
  76. pwin->data = (void *)malloc(sizeof(hopstruct));
  77. memset(pwin->data, '\0', sizeof(hopstruct));
  78. hp = (hopstruct *)pwin->data;
  79. XGetWindowAttributes(dsp, pwin->w, &xgwa);
  80. hp->centerx = xgwa.width / 2;
  81. hp->centery = xgwa.height / 2;
  82. range = sqrt((double) hp->centerx * hp->centerx +
  83. (double) hp->centery * hp->centery) /
  84. (10.0 + random() % 10);
  85. hp->pix = 0;
  86. hp->inc = (int) ((random() / MAXRAND) * 200) - 100;
  87. hp->a = (random() / MAXRAND) * range - range / 2.0;
  88. hp->b = (random() / MAXRAND) * range - range / 2.0;
  89. hp->c = (random() / MAXRAND) * range - range / 2.0;
  90. if (!(random() % 2))
  91. hp->c = 0.0;
  92. hp->i = hp->j = 0.0;
  93. if (!pointBuffer)
  94. pointBuffer = (XPoint *) malloc(batchcount * sizeof(XPoint));
  95. XSetForeground(dsp, pwin->gc, BlackPixelOfScreen(pwin->perscreen->screen));
  96. XFillRectangle(dsp, pwin->w, pwin->gc, 0, 0,
  97. hp->centerx * 2, hp->centery * 2);
  98. XSetForeground(dsp, pwin->gc, WhitePixelOfScreen(pwin->perscreen->screen));
  99. hp->startTime = seconds();
  100. }
  101. void
  102. drawhop(perwindow *pwin)
  103. {
  104. double oldj;
  105. int k = batchcount;
  106. XPoint *xp = pointBuffer;
  107. hopstruct *hp = (hopstruct *)pwin->data;
  108. hp->inc++;
  109. if (!mono && pwin->perscreen->npixels > 2) {
  110. XSetForeground(dsp, pwin->gc, pwin->perscreen->pixels[hp->pix]);
  111. if (++hp->pix >= pwin->perscreen->npixels)
  112. hp->pix = 0;
  113. }
  114. while (k--) {
  115. oldj = hp->j;
  116. hp->j = hp->a - hp->i;
  117. hp->i = oldj + (hp->i < 0
  118. ? sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c))
  119. : -sqrt(fabs(hp->b * (hp->i + hp->inc) - hp->c)));
  120. xp->x = hp->centerx + (int) (hp->i + hp->j);
  121. xp->y = hp->centery - (int) (hp->i - hp->j);
  122. xp++;
  123. }
  124. XDrawPoints(dsp, pwin->w, pwin->gc,
  125. pointBuffer, batchcount, CoordModeOrigin);
  126. if (seconds() - hp->startTime > TIMEOUT)
  127. inithop(pwin);
  128. }