1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
|
/* -*- Mode: C; tab-width: 4 -*- */
/* triangle --- create a triangle-mountain */
#if !defined( lint ) && !defined( SABER )
static const char sccsid[] = "@(#)triangle.c 5.00 2000/11/01 xlockmore";
#endif
/*-
* Copyright (c) 1995 by Tobias Gloth
*
* Permission to use, copy, modify, and distribute this software and its
* documentation for any purpose and without fee is hereby granted,
* provided that the above copyright notice appear in all copies and that
* both that copyright notice and this permission notice appear in
* supporting documentation.
*
* This file is provided AS IS with no warranties of any kind. The author
* shall have no liability with respect to the infringement of copyrights,
* trade secrets or any patents by this file or any part thereof. In no
* event will the author be liable for any lost revenue or profits or
* other special, indirect and consequential damages.
*
* Revision History:
* 01-Nov-2000: Allocation checks
* 22-Dec-1997: Removed MI_PAUSE since it does not work on multiscreens.
* 10-May-1997: Compatible with xscreensaver
* 10-Mar-1996: re-arranged and re-formatted the code for appearance and
* to make common subroutines. Simplified.
* Ron Hitchens <ron@idiom.com>
* 07-Mar-1996: Removed internal delay code, set MI_PAUSE(mi) for inter-scene
* delays. No other delays are needed here.
* Made pause time sensitive to value of cycles (in 10ths of a
* second). Removed (hopefully) all references to globals.
* Ron Hitchens <ron@idiom.com>
* 27-Feb-1996: Undid the changes listed below. Added ModeInfo argument.
* Implemented delay between scenes using the MI_PAUSE(mi)
* scheme. Ron Hitchens <ron@idiom.com>
* 27-Dec-1995: Ron Hitchens <ron@idiom.com>
* Modified logic of draw_triangle() to provide a delay
* (sensitive to the value of cycles) between each iteration.
* Because this mode is so compute intensive, when the new
* event loop adjusted the delay to compensate, this mode had
* almost no delay time left. This change pauses between each
* new landscape, but could still be done better (it is not
* sensitive to input events while drawing, for example).
* 03-Nov-1995: Many changes (hopefully some good ones) by David Bagley
* 01-Oct-1995: Written by Tobias Gloth
*/
#ifdef STANDALONE
#define MODE_triangle
#define PROGCLASS "Triangle"
#define HACK_INIT init_triangle
#define HACK_DRAW draw_triangle
#define triangle_opts xlockmore_opts
#define DEFAULTS "*delay: 10000 \n" \
"*ncolors: 128 \n " \
"*wireframe: False \n" \
"*fullrandom: False \n"
#define SMOOTH_COLORS
#if 0
#define UNIFORM_COLORS /* To get blue water uncomment, but ... */
#endif
#include "xlockmore.h" /* in xscreensaver distribution */
#else /* STANDALONE */
#include "xlock.h" /* in xlockmore distribution */
#endif /* STANDALONE */
#ifdef MODE_triangle
ModeSpecOpt triangle_opts =
{0, (XrmOptionDescRec *) NULL, 0, (argtype *) NULL, (OptionStruct *) NULL};
#ifdef USE_MODULES
ModStruct triangle_description =
{"triangle", "init_triangle", "draw_triangle", "release_triangle",
"refresh_triangle", "init_triangle", (char *) NULL, &triangle_opts,
10000, 1, 1, 1, 64, 1.0, "",
"Shows a triangle mountain range", 0, NULL};
#endif
#define MAX_STEPS 8
#define MAX_SIZE (1<<MAX_STEPS)
#define MAX_LEVELS 1000
#define DELTA 0.4
#define LEFT -0.25
#define RIGHT 1.25
#define TOP 0.3
#define BOTTOM 1.0
#define BLUE (45 * MI_NPIXELS(mi) / 64) /* Just the right shade of blue */
#define BACKFACE_REMOVAL
#define DISPLACE(h,d) ((h)/2+LRAND()/(MAXRAND/(2*(d)+1))-d)
typedef struct {
int width;
int height;
int size;
int steps;
int stage;
int busyLoop;
int fast;
int i;
int j;
int d;
short level[MAX_LEVELS];
int xpos[2 * MAX_SIZE + 1];
int ypos[MAX_SIZE + 1];
short H[(MAX_SIZE + 1) * (MAX_SIZE + 2) / 2];
short *h[MAX_SIZE + 1];
short delta[MAX_STEPS];
Bool wireframe;
Bool joke;
} trianglestruct;
static trianglestruct *triangles = (trianglestruct *) NULL;
static
void
draw_atriangle(ModeInfo * mi, XPoint * p, int y_0, int y_1, int y_2, double dinv)
{
Display *display = MI_DISPLAY(mi);
Window window = MI_WINDOW(mi);
GC gc = MI_GC(mi);
trianglestruct *tp = &triangles[MI_SCREEN(mi)];
if (MI_NPIXELS(mi) > 2) { /* color */
int dmax, dmin;
long color;
dmin = MIN(y_0, y_1);
dmin = MIN(dmin, y_2);
dmax = MAX(y_0, y_1);
dmax = MAX(dmax, y_2);
if (dmax == 0) {
color = BLUE;
} else {
color = MI_NPIXELS(mi) -
(int) ((double) MI_NPIXELS(mi) / M_PI_2 * atan(dinv * (dmax - dmin)));
}
XSetForeground(display, gc, MI_PIXEL(mi, color % MI_NPIXELS(mi)));
if (tp->joke) {
if ((Bool) (LRAND() & 1)) {
XDrawLines(display, window, gc, p, 4, CoordModeOrigin);
} else {
XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
}
} else if (tp->wireframe) {
XDrawLines(display, window, gc, p, 4, CoordModeOrigin);
} else {
/* dieing on my Sun here with gcc -g -O2, flakey */
XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
}
} else {
/* mono */
#ifdef BACKFACE_REMOVAL
XSetForeground(display, gc, MI_BLACK_PIXEL(mi));
XFillPolygon(display, window, gc, p, 3, Convex, CoordModeOrigin);
#endif
XSetForeground(display, gc, MI_WHITE_PIXEL(mi));
XDrawLines(display, window, gc, p, 4, CoordModeOrigin);
}
}
static
void
calc_points1(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
{
*y0_p = tp->level[MAX(tp->h[tp->i][tp->j], 0)];
*y1_p = tp->level[MAX(tp->h[tp->i + d][tp->j], 0)];
*y2_p = tp->level[MAX(tp->h[tp->i][tp->j + d], 0)];
p[0].x = tp->xpos[2 * tp->i + tp->j];
p[1].x = tp->xpos[2 * (tp->i + d) + tp->j];
p[2].x = tp->xpos[2 * tp->i + (tp->j + d)];
p[0].y = tp->ypos[tp->j] - *y0_p;
p[1].y = tp->ypos[tp->j] - *y1_p;
p[2].y = tp->ypos[tp->j + d] - *y2_p;
p[3] = p[0];
}
static
void
calc_points2(trianglestruct * tp, int d, int *y0_p, int *y1_p, int *y2_p, XPoint * p)
{
*y0_p = tp->level[MAX(tp->h[tp->i + d][tp->j], 0)];
*y1_p = tp->level[MAX(tp->h[tp->i + d][tp->j + d], 0)];
*y2_p = tp->level[MAX(tp->h[tp->i][tp->j + d], 0)];
p[0].x = tp->xpos[2 * (tp->i + d) + tp->j];
p[1].x = tp->xpos[2 * (tp->i + d) + (tp->j + d)];
p[2].x = tp->xpos[2 * tp->i + (tp->j + d)];
p[0].y = tp->ypos[tp->j] - *y0_p;
p[1].y = tp->ypos[tp->j + d] - *y1_p;
p[2].y = tp->ypos[tp->j + d] - *y2_p;
p[3]= p[0];
}
static
void
draw_mesh(ModeInfo * mi, trianglestruct * tp, int d, int count)
{
XPoint p[4];
int first = 1;
int y_0, y_1, y_2;
double dinv = 0.2 / d;
if ((tp->j == 0) && (tp->i == 0)) {
MI_CLEARWINDOWCOLORMAPFAST(mi, MI_GC(mi), MI_BLACK_PIXEL(mi));
}
for (; (tp->j < tp->size) && (count > 0); tp->j += ((count) ? d : 0)) {
for (tp->i = (first) ? tp->i : 0, first = 0;
(tp->i < MAX_SIZE - tp->j) && (count > 0);
tp->i += d, count--) {
if (tp->i + tp->j < tp->size) {
calc_points1(tp, d, &y_0, &y_1, &y_2, p);
draw_atriangle(mi, p, y_0, y_1, y_2, dinv);
}
if (tp->i + tp->j + d < tp->size) {
calc_points2(tp, d, &y_0, &y_1, &y_2, p);
draw_atriangle(mi, p, y_0, y_1, y_2, dinv);
}
}
}
if (tp->j == tp->size) {
tp->busyLoop = 1;
}
}
void
init_triangle(ModeInfo * mi)
{
short *tmp;
int i, dim, one;
trianglestruct *tp;
if (triangles == NULL) {
if ((triangles = (trianglestruct *) calloc(MI_NUM_SCREENS(mi),
sizeof (trianglestruct))) == NULL)
return;
}
tp = &triangles[MI_SCREEN(mi)];
tp->width = MI_WIDTH(mi);
tp->height = MI_HEIGHT(mi);
tp->busyLoop = -1;
tp->fast = 2;
if (MI_IS_FULLRANDOM(mi)) {
tp->joke = (Bool) (NRAND(10) == 0);
tp->wireframe = (Bool) (LRAND() & 1);
} else
tp->wireframe = MI_IS_WIREFRAME(mi);
MI_CLEARWINDOW(mi);
tp->steps = MAX_STEPS;
do {
tp->size = 1 << --tp->steps;
} while (tp->size * 5 > tp->width);
tmp = tp->H;
for (i = 0; i < tp->size + 1; i++) {
tp->h[i] = tmp;
tmp += (tp->size) + 1 - i;
}
tp->stage = -1;
dim = MIN(tp->width, tp->height);
for (i = 0; i < 2 * tp->size + 1; i++) {
tp->xpos[i] = (short) ((((double) i)
/ ((double) (2 * tp->size)) * (RIGHT - LEFT) + LEFT)
* dim) + (tp->width - dim) / 2;
}
for (i = 0; i < (tp->size + 1); i++) {
tp->ypos[i] = (short) ((((double) i)
/ ((double) tp->size) * (BOTTOM - TOP) + TOP) * dim)
+ (tp->height - dim) / 2;
}
for (i = 0; i < tp->steps; i++) {
tp->delta[i] = ((short) (DELTA * dim)) >> i;
}
one = tp->delta[0];
if (one > 0)
for (i = 0; i < MAX_LEVELS; i++) {
tp->level[i] = (i * i) / one;
}
}
void
draw_triangle(ModeInfo * mi)
{
int d, d2, i, j, delta;
trianglestruct *tp;
if (triangles == NULL)
return;
tp = &triangles[MI_SCREEN(mi)];
MI_IS_DRAWN(mi) = True;
if (tp->busyLoop > 0) {
if (tp->busyLoop >= 100)
tp->busyLoop = -1;
else
tp->busyLoop++;
return;
}
if (!tp->busyLoop) {
draw_mesh(mi, tp, tp->d / 2, MAX_SIZE / tp->d);
return;
}
if (tp->delta[0] > 0) {
if (!(++tp->stage)) {
tp->h[0][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
tp->h[tp->size][0] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
tp->h[0][tp->size] = (short int) MAX(0, DISPLACE(0, tp->delta[0]));
} else {
d = 2 << (tp->steps - tp->stage);
d2 = d / 2;
delta = tp->delta[tp->stage - 1];
for (i = 0; i < tp->size; i += d) {
for (j = 0; j < (tp->size - i); j += d) {
tp->h[i + d2][j] = (short int) DISPLACE(tp->h[i][j] +
tp->h[i + d][j], delta);
tp->h[i][j + d2] = (short int) DISPLACE(tp->h[i][j] +
tp->h[i][j + d], delta);
tp->h[i + d2][j + d2] = (short int) DISPLACE(tp->h[i + d][j] +
tp->h[i][j + d], delta);
}
tp->busyLoop = 0;
tp->i = 0;
tp->j = 0;
tp->d = d;
}
}
}
if (tp->stage == tp->steps) {
#ifdef STANDALONE
erase_full_window(MI_DISPLAY(mi), MI_WINDOW(mi));
#endif
init_triangle(mi);
}
}
void
release_triangle(ModeInfo * mi)
{
if (triangles != NULL) {
free(triangles);
triangles = (trianglestruct *) NULL;
}
}
void
refresh_triangle(ModeInfo * mi)
{
MI_CLEARWINDOW(mi);
}
#endif /* MODE_triangle */
|