1 /* -*- Mode: C; c-basic-offset: 4; tab-width: 4 -*-
2 * speedmine, Copyright (C) 2001 Conrad Parker <conrad@deephackmode.org>
4 * Permission to use, copy, modify, distribute, and sell this software and its
5 * documentation for any purpose is hereby granted without fee, provided that
6 * the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation. No representations are made about the suitability of this
9 * software for any purpose. It is provided "as is" without express or
14 * Written mostly over the Easter holiday, 2001. Psychedelic option due to
15 * a night at Home nightclub, Sydney. Three all-nighters of solid partying
16 * were involved in the week this hack was written.
18 * Happy Birthday to WierdArms (17 April) and Pat (18 April)
24 * This program generates a rectangular terrain grid and maps this onto
25 * a semi-circular tunnel. The terrain has length TERRAIN_LENGTH, which
26 * corresponds to length along the tunnel, and breadth TERRAIN_BREADTH,
27 * which corresponds to circumference around the tunnel. For each frame,
28 * the tunnel is perspective mapped onto a set of X and Y screen values.
30 * Throughout this code the following temporary variable names are used:
32 * i iterates along the tunnel in the direction of travel
33 * j iterates around the tunnel clockwise
34 * t iterates along the length of the perspective mapped values
35 * from the furthest to the nearest
37 * Thus, the buffers are used with these iterators:
39 * terrain[i][j] terrain map
40 * worldx[i][j], worldy[i][j] world coordinates (after wrapping)
41 * {x,y,z}curvature[i] tunnel curvature
42 * wideness[i] tunnel wideness
43 * bonuses[i] bonus values
45 * xvals[t][j], yvals[t][j] screen coordinates
46 * {min,max}{x,y}[t] bounding boxes of screen coords
49 /* Define or undefine NDEBUG to turn assert and abort debugging off or on */
55 #include "screenhack.h"
58 #define MIN(a,b) ((a)<(b)?(a):(b))
59 #define MAX(a,b) ((a)>(b)?(a):(b))
61 #define RAND(r) (int)(((r)>0)?(random() % (long)(r)): -(random() % (long)(-r)))
63 #define SIGN3(a) ((a)>0?1:((a)<0?-1:0))
65 #define MODULO(a,b) while ((a)<0) (a)+=(b); (a) %= (b);
67 static Display * display;
68 static Pixmap dbuf, stars_mask;
70 static unsigned int default_fg_pixel;
71 static GC draw_gc, erase_gc, tunnelend_gc, stars_gc, stars_erase_gc;
73 /* No. of shades of each color (ground, walls, bonuses) */
75 static int ncolors, nr_ground_colors, nr_wall_colors, nr_bonus_colors;
76 static XColor ground_colors[MAX_COLORS], wall_colors[MAX_COLORS];
77 static XColor bonus_colors[MAX_COLORS];
78 static GC ground_gcs[MAX_COLORS], wall_gcs[MAX_COLORS], bonus_gcs[MAX_COLORS];
82 static int width, height;
85 static int smoothness;
86 static int verbose_flag;
88 static int terrain_flag;
89 static int widening_flag;
90 static int bumps_flag;
91 static int bonuses_flag;
92 static int crosshair_flag;
93 static int psychedelic_flag;
101 static double maxspeed;
103 static double thrust, gravity;
105 static double vertigo;
106 static double curviness;
107 static double twistiness;
109 static double pos=0.0;
110 static double speed=-1.1;
111 static double accel=0.00000001;
112 static double step=0.0;
117 static int direction = FORWARDS;
119 static int pindex=0, nearest=0;
120 static int flipped_at=0;
121 static int xoffset=0, yoffset=0;
123 static int bonus_bright = 0;
124 static int wire_bonus=0;
125 #define wireframe (wire_flag||wire_bonus>8||wire_bonus%2==1)
127 static double speed_bonus=0.0;
128 #define effective_speed (direction*(speed+speed_bonus))
130 static int spin_bonus = 0;
131 static int backwards_bonus = 0;
133 /* No. of levels of interpolation, for perspective */
136 /* These must be powers of 2 */
137 #define TERRAIN_LENGTH 256
138 #define TERRAIN_BREADTH 32
140 /* total "perspective distance" of terrain */
141 #define TERRAIN_PDIST (INTERP*TERRAIN_LENGTH)
144 #define TB_MUL (ROTS/TERRAIN_BREADTH)
145 static double sintab[ROTS], costab[ROTS];
147 static int orientation = (17*ROTS)/22;
149 static int terrain[TERRAIN_LENGTH][TERRAIN_BREADTH];
150 static double xcurvature[TERRAIN_LENGTH];
151 static double ycurvature[TERRAIN_LENGTH];
152 static double zcurvature[TERRAIN_LENGTH];
153 static int wideness[TERRAIN_LENGTH];
154 static int bonuses[TERRAIN_LENGTH];
155 static int xvals[TERRAIN_LENGTH][TERRAIN_BREADTH];
156 static int yvals[TERRAIN_LENGTH][TERRAIN_BREADTH];
157 static double worldx[TERRAIN_LENGTH][TERRAIN_BREADTH];
158 static double worldy[TERRAIN_LENGTH][TERRAIN_BREADTH];
159 static int minx[TERRAIN_LENGTH], maxx[TERRAIN_LENGTH];
160 static int miny[TERRAIN_LENGTH], maxy[TERRAIN_LENGTH];
162 #define random_elevation() (terrain_flag?(random() % 200):0)
163 #define random_curvature() (curviness>0.0?((double)(random() % 40)-20)*curviness:0.0)
164 #define random_twist() (twistiness>0.0?((double)(random() % 40)-20)*twistiness:0.0)
165 #define random_wideness() (widening_flag?(int)(random() % 1200):0)
167 #define STEEL_ELEVATION 300
169 /* a forward declaration ... */
170 static void change_colors(void);
172 #if HAVE_GETTIMEOFDAY
173 static int total_nframes = 0;
174 static int nframes = 0;
175 static double fps = 0.0;
176 static double fps_start, fps_end;
177 static struct timeval start_time;
182 * returns the total time elapsed since the beginning of the demo
184 static double get_time(void) {
187 #if GETTIMEOFDAY_TWO_ARGS
188 gettimeofday(&t, NULL);
192 t.tv_sec -= start_time.tv_sec;
193 f = ((double)t.tv_sec) + t.tv_usec*1e-6;
200 * initialises the timing structures
202 static void init_time(void) {
203 #if GETTIMEOFDAY_TWO_ARGS
204 gettimeofday(&start_time, NULL);
206 gettimeofday(&start_time);
208 fps_start = get_time();
215 * perspective map the world coordinates worldx[i][j], worldy[i][j] onto
216 * screen coordinates xvals[t][j], yvals[t][j]
221 static int rotation_offset=0;
223 int i, j, jj, t=0, depth, view_pos;
224 int rotation_bias, r;
225 double xc=0.0, yc=0.0, zc=0.0;
226 double xcc=0.0, ycc=0.0, zcc=0.0;
230 zf = 8.0*28.0 / (double)(width*TERRAIN_LENGTH);
231 if (be_wormy) zf *= 3.0;
233 depth = TERRAIN_PDIST - INTERP + pindex;
235 view_pos = (nearest+3*TERRAIN_LENGTH/4)%TERRAIN_LENGTH;
237 xoffset += - xcurvature[view_pos]*curviness/8;
240 yoffset += - ycurvature[view_pos]*curviness/4;
243 rotation_offset += (int)((zcurvature[view_pos]-zcurvature[nearest])*ROTS/8);
244 rotation_offset /= 2;
245 rotation_bias = orientation + spin_bonus - rotation_offset;
249 yoffset -= ((terrain[view_pos][TERRAIN_BREADTH/4] * width /(8*1600)));
250 rotation_bias += (terrain[view_pos][TERRAIN_BREADTH/4+2] -
251 terrain[view_pos][TERRAIN_BREADTH/4-2])/8;
253 yoffset -= ((terrain[view_pos][TERRAIN_BREADTH/4] * width /(2*1600)));
254 rotation_bias += (terrain[view_pos][TERRAIN_BREADTH/4+2] -
255 terrain[view_pos][TERRAIN_BREADTH/4-2])/16;
259 MODULO(rotation_bias, ROTS);
261 for (t=0; t < TERRAIN_LENGTH; t++) {
262 i = nearest + t; MODULO(i, TERRAIN_LENGTH);
263 xc += xcurvature[i]; yc += ycurvature[i]; zc += zcurvature[i];
264 xcc += xc; ycc += yc; zcc += zc;
265 maxx[i] = maxy[i] = 0;
266 minx[i] = width; miny[i] = height;
269 for (t=0; t < TERRAIN_LENGTH; t++) {
270 i = nearest - 1 - t; MODULO(i, TERRAIN_LENGTH);
272 zfactor = (double)depth* (12.0 - TERRAIN_LENGTH/8.0) * zf;
273 for (j=0; j < TERRAIN_BREADTH; j++) {
274 jj = direction * j; MODULO(jj, TERRAIN_BREADTH);
275 /* jwz: not totally sure if this is right, but it avoids div0 */
277 xx = (worldx[i][jj]-(vertigo*xcc))/zfactor;
278 yy = (worldy[i][j]-(vertigo*ycc))/zfactor;
283 r = rotation_bias + (int)(vertigo*zcc); MODULO(r, ROTS);
285 xvals[t][j] = xoffset + width/2 +
286 (int)(xx * costab[r] - yy * sintab[r]);
287 maxx[t] = MAX(maxx[t], xvals[t][j]);
288 minx[t] = MIN(minx[t], xvals[t][j]);
290 yvals[t][j] = yoffset + height/2 +
291 (int)(xx * sintab[r] + yy * costab[r]);
292 maxy[t] = MAX(maxy[t], yvals[t][j]);
293 miny[t] = MIN(miny[t], yvals[t][j]);
295 xcc -= xc; ycc -= yc; zcc -= zc;
296 xc -= xcurvature[i]; yc -= ycurvature[i]; zc -= zcurvature[i];
302 * wrap_tunnel (start, end)
304 * wrap the terrain terrain[i][j] around the semi-circular tunnel function
306 * x' = x/2 * cos(theta) - (y-k) * x * sin(theta)
307 * y' = x/4 * sin(theta) + y * cos(theta)
309 * between i=start and i=end inclusive, producing world coordinates
310 * worldx[i][j], worldy[i][j]
313 wrap_tunnel (int start, int end)
318 assert (start < end);
320 for (i=start; i <= end; i++) {
321 for (j=0; j < TERRAIN_BREADTH; j++) {
322 x = j * (1.0/TERRAIN_BREADTH);
324 y = (double)(v==STEEL_ELEVATION?200:v) - wideness[i] - 1200;
327 if (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8) y -= 300;
329 worldx[i][j] = x/2 * costab[j*TB_MUL] -
330 (y-height/4.0)*x*sintab[j*TB_MUL];
331 worldy[i][j] = x/4 * sintab[j*TB_MUL] +
332 y * costab[j*TB_MUL];
340 * perform the state transitions and terrain transformation for the
341 * "look backwards/look forwards" bonus
344 flip_direction (void)
348 direction = -direction;
352 for (i=0; i < TERRAIN_LENGTH; i++) {
353 in = nearest + i; MODULO(in, TERRAIN_BREADTH);
354 ip = nearest - i; MODULO(ip, TERRAIN_BREADTH);
355 for (j=0; j < TERRAIN_BREADTH; j++) {
357 terrain[ip][j] = terrain[in][j];
364 * generate_smooth (start, end)
366 * generate smooth terrain between i=start and i=end inclusive
369 generate_smooth (int start, int end)
373 assert (start < end);
375 for (i=start; i <= end; i++) {
376 ii = i; MODULO(ii, TERRAIN_LENGTH);
377 for (j=0; j < TERRAIN_BREADTH; j++) {
378 terrain[i][j] = STEEL_ELEVATION;
384 * generate_straight (start, end)
386 * zero the curvature and wideness between i=start and i=end inclusive
389 generate_straight (int start, int end)
393 assert (start < end);
395 for (i=start; i <= end; i++) {
396 ii = i; MODULO(ii, TERRAIN_LENGTH);
397 for (j=0; j < TERRAIN_BREADTH; j++) {
407 * int generate_terrain_value (v1, v2, v3, v4, w)
409 * generate terrain value near the average of v1, v2, v3, v4, with
410 * perturbation proportional to w
413 generate_terrain_value (int v1, int v2, int v3, int v4, int w)
418 if (!terrain_flag) return 0;
420 sum = v1 + v2 + v3 + v4;
422 rval = w*sum/smoothness;
423 if (rval == 0) rval = 2;
425 ret = (sum/4 -(rval/2) + RAND(rval));
427 if (ret < -400 || ret > 400) {
435 * generate_terrain (start, end, final)
437 * generate terrain[i][j] between i=start and i=end inclusive
439 * This is performed by successive subdivision of the terrain into
440 * rectangles of decreasing size. Subdivision continues until the
441 * the minimum width or height of these rectangles is 'final'; ie.
442 * final=1 indicates to subdivide as far as possible, final=2 indicates
443 * to stop one subdivision before that (leaving a checkerboard pattern
447 generate_terrain (int start, int end, int final)
450 int ip, jp, in, jn; /* prev, next values */
453 assert (start < end);
454 assert (start >= 0 && start < TERRAIN_LENGTH);
455 assert (end >= 0 && end < TERRAIN_LENGTH);
457 diff = end - start + 1;
459 terrain[end][0] = random_elevation();
460 terrain[end][TERRAIN_BREADTH/2] = random_elevation();
462 for (w= diff/2, l=TERRAIN_BREADTH/4;
463 w >= final || l >= final; w /= 2, l /= 2) {
465 if (w<1) w=1; if (l<1) l=1;
467 for (i=start+w-1; i < end; i += (w*2)) {
468 ip = i-w; MODULO(ip, TERRAIN_LENGTH);
469 in = i+w; MODULO(in, TERRAIN_LENGTH);
470 for (j=l-1; j < TERRAIN_BREADTH; j += (l*2)) {
471 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
472 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
474 generate_terrain_value (terrain[ip][jp], terrain[in][jp],
475 terrain[ip][jn], terrain[in][jn], w);
479 for (i=start+(w*2)-1; i < end; i += (w*2)) {
480 ip = i-w; MODULO(ip, TERRAIN_LENGTH);
481 in = i+w; MODULO(in, TERRAIN_LENGTH);
482 for (j=l-1; j < TERRAIN_BREADTH; j += (l*2)) {
483 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
484 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
486 generate_terrain_value (terrain[ip][j], terrain[in][j],
487 terrain[i][jp], terrain[i][jn], w);
491 for (i=start+w-1; i < end; i += (w*2)) {
492 ip = i-w; MODULO(ip, TERRAIN_LENGTH);
493 in = i+w; MODULO(in, TERRAIN_LENGTH);
494 for (j=2*l-1; j < TERRAIN_BREADTH; j += (l*2)) {
495 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
496 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
498 generate_terrain_value (terrain[ip][j], terrain[in][j],
499 terrain[i][jp], terrain[i][jn], w);
506 * double generate_curvature_value (v1, v2, w)
508 * generate curvature value near the average of v1 and v2, with perturbation
512 generate_curvature_value (double v1, double v2, int w)
514 double sum, avg, diff, ret;
517 assert (!isnan(v1) && !isnan(v2));
522 diff = MIN(v1 - avg, v2 - avg);
524 rval = (int)diff * w;
525 if (rval == 0.0) return avg;
527 ret = (avg -((double)rval)/500.0 + ((double)RAND(rval))/1000.0);
529 assert (!isnan(ret));
535 * generate_curves (start, end)
537 * generate xcurvature[i], ycurvature[i], zcurvature[i] and wideness[i]
538 * between start and end inclusive
541 generate_curves (int start, int end)
543 int i, diff, ii, in, ip, w;
545 assert (start < end);
547 diff = end - start + 1; MODULO (diff, TERRAIN_LENGTH);
549 if (random() % 100 == 0)
550 xcurvature[end] = 30 * random_curvature();
551 else if (random() % 10 == 0)
552 xcurvature[end] = 20 * random_curvature();
554 xcurvature[end] = 10 * random_curvature();
556 if (random() % 50 == 0)
557 ycurvature[end] = 20 * random_curvature();
558 else if (random() % 25 == 0)
559 ycurvature[end] = 30 * random_curvature();
561 ycurvature[end] = 10 * random_curvature();
563 if (random() % 3 == 0)
564 zcurvature[end] = random_twist();
567 generate_curvature_value (zcurvature[end], random_twist(), 1);
570 wideness[end] = random_wideness();
573 generate_curvature_value (wideness[end], random_wideness(), 1);
575 for (w=diff/2; w >= 1; w /= 2) {
576 for (i=start+w-1; i < end; i+=(w*2)) {
577 ii = i; MODULO (ii, TERRAIN_LENGTH);
578 ip = i-w; MODULO (ip, TERRAIN_LENGTH);
579 in = i+w; MODULO (in, TERRAIN_LENGTH);
581 generate_curvature_value (xcurvature[ip], xcurvature[in], w);
583 generate_curvature_value (ycurvature[ip], ycurvature[in], w);
585 generate_curvature_value (zcurvature[ip], zcurvature[in], w);
587 generate_curvature_value (wideness[ip], wideness[in], w);
595 * choose a random bonus and perform its state transition
600 static int jamming=0;
606 nearest -= 2; MODULO(nearest, TERRAIN_LENGTH);
610 if (psychedelic_flag) change_colors();
612 switch (random() % 7) {
613 case 0: /* switch to or from wireframe */
614 wire_bonus = (wire_bonus?0:300);
616 case 1: /* speedup */
625 case 4: /* look backwards / look forwards */
626 flipped_at = nearest;
628 backwards_bonus = (backwards_bonus?0:10);
633 case 6: /* jam against the bonus a few times; deja vu! */
634 nearest -= 2; MODULO(nearest, TERRAIN_LENGTH);
646 * check if a bonus has been passed in the last frame, and handle it
651 int i, ii, start, end;
653 if (!bonuses_flag) return;
656 start = nearest; end = nearest + (int)floor(step);
658 end = nearest; start = nearest + (int)floor(step);
662 start += TERRAIN_LENGTH/4;
663 end += TERRAIN_LENGTH/4;
666 for (i=start; i < end; i++) {
667 ii = i; MODULO(ii, TERRAIN_LENGTH);
668 if (bonuses[ii] == 1) do_bonus ();
673 * decrement_bonuses (double time_per_frame)
675 * decrement timers associated with bonuses
678 decrement_bonuses (double time_per_frame)
680 if (!bonuses_flag) return;
682 if (bonus_bright > 0) bonus_bright-=4;
683 if (wire_bonus > 0) wire_bonus--;
684 if (speed_bonus > 0) speed_bonus -= 2.0;
686 if (spin_bonus > 10) spin_bonus -= (int)(step*13.7);
687 else if (spin_bonus < -10) spin_bonus += (int)(step*11.3);
689 if (backwards_bonus > 1) backwards_bonus--;
690 else if (backwards_bonus == 1) {
691 nearest += 2*(MAX(flipped_at, nearest) - MIN(flipped_at,nearest));
692 MODULO(nearest, TERRAIN_LENGTH);
699 * set_bonuses (start, end)
701 * choose if to and where to set a bonus between i=start and i=end inclusive
704 set_bonuses (int start, int end)
708 if (!bonuses_flag) return;
710 assert (start < end);
714 for (i=start; i <= end; i++) {
715 ii = i; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
718 if (random() % 4 == 0) {
719 i = start + RAND(diff-3);
720 ii = i; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
721 bonuses[ii] = 2; /* marker */
722 ii = i+3; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
723 bonuses[ii] = 1; /* real thing */
728 * regenerate_terrain ()
730 * regenerate a portion of the terrain map of length TERRAIN_LENGTH/4 iff
731 * we just passed between two quarters of the terrain.
733 * choose the kind of terrain to produce, produce it and wrap the tunnel
736 regenerate_terrain (void)
741 passed = nearest % (TERRAIN_LENGTH/4);
744 (speed > 0.0 && passed > (int)step) ||
745 (speed < 0.0 && (TERRAIN_LENGTH/4)-passed > (int)fabs(step))) {
750 end = nearest - passed - 1; MODULO(end, TERRAIN_LENGTH);
751 start = end - TERRAIN_LENGTH/4 + 1; MODULO(start, TERRAIN_LENGTH);
753 if (DEBUG_FLAG) printf ("Regenerating [%d - %d]\n", start, end);
755 set_bonuses (start, end);
757 switch (random() % 64) {
760 generate_terrain (start, end, 1);
761 generate_smooth (start,
762 start + TERRAIN_LENGTH/8 + (random() % TERRAIN_LENGTH/8));
765 generate_smooth (start, end);
766 generate_terrain (start, end, 4); break;
768 generate_smooth (start, end);
769 generate_terrain (start, end, 2); break;
771 generate_terrain (start, end, 1);
774 if (random() % 16 == 0) {
775 generate_straight (start, end);
777 generate_curves (start, end);
780 wrap_tunnel (start, end);
786 * initialise the terrain map for the beginning of the demo
793 for (i=0; i < TERRAIN_LENGTH; i++) {
794 for (j=0; j < TERRAIN_BREADTH; j++) {
799 terrain[TERRAIN_LENGTH-1][0] = - (random() % 300);
800 terrain[TERRAIN_LENGTH-1][TERRAIN_BREADTH/2] = - (random() % 300);
802 generate_smooth (0, TERRAIN_LENGTH-1);
803 generate_terrain (0, TERRAIN_LENGTH/4 -1, 4);
804 generate_terrain (TERRAIN_LENGTH/4, TERRAIN_LENGTH/2 -1, 2);
805 generate_terrain (TERRAIN_LENGTH/2, 3*TERRAIN_LENGTH/4 -1, 1);
806 generate_smooth (3*TERRAIN_LENGTH/4, TERRAIN_LENGTH-1);
812 * initialise the curvatures and wideness for the beginning of the demo.
819 for (i=0; i < TERRAIN_LENGTH-1; i++) {
825 xcurvature[TERRAIN_LENGTH-1] = random_curvature();
826 ycurvature[TERRAIN_LENGTH-1] = random_curvature();
827 zcurvature[TERRAIN_LENGTH-1] = random_twist();
829 generate_straight (0, TERRAIN_LENGTH/4-1);
830 generate_curves (TERRAIN_LENGTH/4, TERRAIN_LENGTH/2-1);
831 generate_curves (TERRAIN_LENGTH/2, 3*TERRAIN_LENGTH/4-1);
832 generate_straight (3*TERRAIN_LENGTH/4, TERRAIN_LENGTH-1);
837 * render_quads (dpy, d, t, dt, i)
839 * renders the quadrilaterals from perspective depth t to t+dt.
840 * i is passed as a hint, where i corresponds to t as asserted.
843 render_quads (Display * dpy, Drawable d, int t, int dt, int i)
850 assert (i == (nearest - (t + dt) + TERRAIN_LENGTH) % TERRAIN_LENGTH);
852 in = i + 1; MODULO(in, TERRAIN_LENGTH);
854 for (j=0; j < TERRAIN_BREADTH; j+=dt) {
855 t2 = t+dt; if (t2 >= TERRAIN_LENGTH) t2 -= TERRAIN_LENGTH;
856 j2 = j+dt; if (j2 >= TERRAIN_BREADTH) j2 -= TERRAIN_BREADTH;
857 points[0].x = xvals[t][j]; points[0].y = yvals[t][j];
858 points[1].x = xvals[t2][j]; points[1].y = yvals[t2][j];
859 points[2].x = xvals[t2][j2]; points[2].y = yvals[t2][j2];
860 points[3].x = xvals[t][j2]; points[3].y = yvals[t][j2];
862 index = bonus_bright + ncolors/3 +
863 t*(t*INTERP + pindex) * ncolors /
864 (3*TERRAIN_LENGTH*TERRAIN_PDIST);
866 index += (int)((points[0].y - points[3].y) / 8);
867 index += (int)((worldx[i][j] - worldx[in][j]) / 40);
868 index += (int)((terrain[in][j] - terrain[i][j]) / 100);
870 if (be_wormy && psychedelic_flag) index += ncolors/4;
872 index = MIN (index, ncolors-1);
873 index = MAX (index, 0);
876 XSetClipMask (dpy, bonus_gcs[index], None);
880 if (bonuses[i]) gc = bonus_gcs[index];
881 else gc = ground_gcs[index];
882 XDrawLines (dpy, d, gc, points, 4, CoordModeOrigin);
885 gc = bonus_gcs[index];
886 else if ((direction>0 && j < TERRAIN_BREADTH/8) ||
887 (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8-1) ||
888 (direction < 0 && j > 3*TERRAIN_BREADTH/8-1 &&
889 j < TERRAIN_BREADTH/2) ||
890 terrain[i][j] == STEEL_ELEVATION ||
891 wideness[in] - wideness[i] > 200)
892 gc = ground_gcs[index];
894 gc = wall_gcs[index];
896 XFillPolygon (dpy, d, gc, points, 4, Nonconvex, CoordModeOrigin);
902 * render_pentagons (dpy, d, t, dt, i)
904 * renders the pentagons from perspective depth t to t+dt.
905 * i is passed as a hint, where i corresponds to t as asserted.
908 render_pentagons (Display *dpy, Drawable d, int t, int dt, int i)
910 int j, t2, j2, j3, in;
915 assert (i == (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH);
917 in = i + 1; MODULO(in, TERRAIN_LENGTH);
919 for (j=0; j < TERRAIN_BREADTH; j+=dt*2) {
920 t2 = t+(dt*2); if (t2 >= TERRAIN_LENGTH) t2 -= TERRAIN_LENGTH;
921 j2 = j+dt; if (j2 >= TERRAIN_BREADTH) j2 -= TERRAIN_BREADTH;
922 j3 = j+dt+dt; if (j3 >= TERRAIN_BREADTH) j3 -= TERRAIN_BREADTH;
923 points[0].x = xvals[t][j]; points[0].y = yvals[t][j];
924 points[1].x = xvals[t2][j]; points[1].y = yvals[t2][j];
925 points[2].x = xvals[t2][j2]; points[2].y = yvals[t2][j2];
926 points[3].x = xvals[t2][j3]; points[3].y = yvals[t2][j3];
927 points[4].x = xvals[t][j3]; points[4].y = yvals[t][j3];
929 index = bonus_bright + ncolors/3 +
930 t*(t*INTERP + pindex) * ncolors /
931 (3*TERRAIN_LENGTH*TERRAIN_PDIST);
933 index += (int)((points[0].y - points[3].y) / 8);
934 index += (int)((worldx[i][j] - worldx[in][j]) / 40);
935 index += (int)((terrain[in][j] - terrain[i][j]) / 100);
937 if (be_wormy && psychedelic_flag) index += ncolors/4;
939 index = MIN (index, ncolors-1);
940 index = MAX (index, 0);
943 XSetClipMask (dpy, bonus_gcs[index], None);
947 if (bonuses[i]) gc = bonus_gcs[index];
948 else gc = ground_gcs[index];
949 XDrawLines (dpy, d, gc, points, 5, CoordModeOrigin);
952 gc = bonus_gcs[index];
953 else if (j < TERRAIN_BREADTH/8 ||
954 (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8-1) ||
955 terrain[i][j] == STEEL_ELEVATION ||
956 wideness[in] - wideness[i] > 200)
957 gc = ground_gcs[index];
959 gc = wall_gcs[index];
961 XFillPolygon (dpy, d, gc, points, 5, Complex, CoordModeOrigin);
967 * render_block (dpy, d, gc, t)
969 * render a filled polygon at perspective depth t using the given GC
972 render_block (Display * dpy, Drawable d, GC gc, int t)
976 XPoint erase_points[TERRAIN_BREADTH/2];
978 for (i=0; i < TERRAIN_BREADTH/2; i++) {
979 erase_points[i].x = xvals[t][i*2];
980 erase_points[i].y = yvals[t][i*2];
983 XFillPolygon (dpy, d, gc, erase_points,
984 TERRAIN_BREADTH/2, Complex, CoordModeOrigin);
988 * regenerate_stars_mask (dpy, t)
990 * regenerate the clip mask 'stars_mask' for drawing the bonus stars at
991 * random positions within the bounding box at depth t
994 regenerate_stars_mask (Display * dpy, int t)
996 int i, w, h, a, b, l1, l2;
997 const int lim = width*TERRAIN_LENGTH/(300*(TERRAIN_LENGTH-t));
999 w = maxx[t] - minx[t];
1000 h = maxy[t] - miny[t];
1002 if (w<6||h<6) return;
1004 XFillRectangle (dpy, stars_mask, stars_erase_gc,
1005 0, 0, width, height);
1007 l1 = (t>3*TERRAIN_LENGTH/4?2:1);
1008 l2 = (t>7*TERRAIN_LENGTH/8?2:1);
1010 for (i=0; i < lim; i++) {
1011 a = RAND(w); b = RAND(h);
1012 XDrawLine (dpy, stars_mask, stars_gc,
1013 minx[t]+a-l1, miny[t]+b, minx[t]+a+l1, miny[t]+b);
1014 XDrawLine (dpy, stars_mask, stars_gc,
1015 minx[t]+a, miny[t]+b-l1, minx[t]+a, miny[t]+b+l1);
1017 for (i=0; i < lim; i++) {
1018 a = RAND(w); b = RAND(h);
1019 XDrawLine (dpy, stars_mask, stars_gc,
1020 minx[t]+a-l2, miny[t]+b, minx[t]+a+l2, miny[t]+b);
1021 XDrawLine (dpy, stars_mask, stars_gc,
1022 minx[t]+a, miny[t]+b-l2, minx[t]+a, miny[t]+b+l2);
1027 * render_bonus_block (dpy, d, t, i)
1029 * draw the bonus stars at depth t.
1030 * i is passed as a hint, where i corresponds to t as asserted.
1033 render_bonus_block (Display * dpy, Drawable d, int t, int i)
1037 assert (i == (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH);
1039 if (!bonuses[i] || wireframe) return;
1041 regenerate_stars_mask (dpy, t);
1043 bt = t * nr_bonus_colors / (2*TERRAIN_LENGTH);
1045 XSetClipMask (dpy, bonus_gcs[bt], stars_mask);
1047 render_block (dpy, d, bonus_gcs[bt], t);
1054 int max_minx=0, min_maxx=width, max_miny=0, min_maxy=height;
1056 for (t=TERRAIN_LENGTH-1; t > 0; t--) {
1057 max_minx = MAX (max_minx, minx[t]);
1058 min_maxx = MIN (min_maxx, maxx[t]);
1059 max_miny = MAX (max_miny, miny[t]);
1060 min_maxy = MIN (min_maxy, maxy[t]);
1062 if (max_miny >= min_maxy || max_minx >= min_maxx) break;
1069 * render_speedmine (dpy, d)
1071 * render the current frame.
1074 render_speedmine (Display * dpy, Drawable d)
1076 int t, i=nearest, dt=1;
1079 assert (nearest >= 0 && nearest < TERRAIN_LENGTH);
1081 if (be_wormy || wireframe) {
1082 XFillRectangle (dpy, d, erase_gc, 0, 0, width, height);
1085 for (t=0; t < TERRAIN_LENGTH/4; t+=dt) {
1086 render_bonus_block (dpy, d, t, i);
1087 i -= dt; MODULO(i, TERRAIN_LENGTH);
1088 render_quads (dpy, d, t, dt, i);
1091 assert (t == TERRAIN_LENGTH/4);
1093 t = MAX(begin_at(), TERRAIN_LENGTH/4);
1094 /*t = TERRAIN_LENGTH/4; dt = 2; */
1095 dt = (t >= 3*TERRAIN_LENGTH/4 ? 1 : 2);
1096 i = (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH;
1097 render_block (dpy, d, tunnelend_gc, t);
1102 if (t == TERRAIN_LENGTH/4)
1103 render_pentagons (dpy, d, t, dt, i);
1105 for (; t < 3*TERRAIN_LENGTH/4; t+=dt) {
1106 render_bonus_block (dpy, d, t, i);
1107 i -= dt; MODULO(i, TERRAIN_LENGTH);
1108 render_quads (dpy, d, t, dt, i);
1113 for (; t < TERRAIN_LENGTH-(1+(pindex<INTERP/2)); t+=dt) {
1114 render_bonus_block (dpy, d, t, i);
1115 i -= dt; MODULO(i, TERRAIN_LENGTH);
1118 if (wireframe) {assert (t == 3*TERRAIN_LENGTH/4);}
1120 if (t == 3*TERRAIN_LENGTH/4)
1121 render_pentagons (dpy, d, t, dt, i);
1123 for (; t < TERRAIN_LENGTH-(1+(pindex<INTERP/2)); t+=dt) {
1124 render_bonus_block (dpy, d, t, i);
1125 i -= dt; MODULO(i, TERRAIN_LENGTH);
1126 render_quads (dpy, d, t, dt, i);
1130 /* Draw crosshair */
1131 if (crosshair_flag) {
1132 gc = (wireframe ? bonus_gcs[nr_bonus_colors/2] : erase_gc);
1133 XFillRectangle (dpy, d, gc,
1134 width/2+(xoffset)-8, height/2+(yoffset*2)-1, 16, 3);
1135 XFillRectangle (dpy, d, gc,
1136 width/2+(xoffset)-1, height/2+(yoffset*2)-8, 3, 16);
1144 * move to the position for the next frame, and modify the state variables
1145 * nearest, pindex, pos, speed
1153 dpos = SIGN3(pos) * floor(fabs(pos));
1155 pindex += SIGN3(effective_speed) + INTERP;
1156 while (pindex >= INTERP) {
1160 while (pindex < 0) {
1165 nearest += dpos; MODULO(nearest, TERRAIN_LENGTH);
1169 accel = thrust + ycurvature[nearest] * gravity;
1171 if (speed > maxspeed) speed = maxspeed;
1172 if (speed < -maxspeed) speed = -maxspeed;
1176 * speedmine (dpy, window)
1178 * do everything required for one frame of the demo
1181 speedmine (Display *dpy, Window window)
1183 double elapsed, time_per_frame = 0.04;
1185 regenerate_terrain ();
1189 render_speedmine (dpy, dbuf);
1190 XCopyArea (dpy, dbuf, window, draw_gc, 0, 0, width, height, 0, 0);
1192 #if HAVE_GETTIMEOFDAY
1193 fps_end = get_time();
1197 if (fps_end > fps_start + 0.5) {
1198 elapsed = fps_end - fps_start;
1199 fps_start = get_time();
1201 time_per_frame = elapsed / nframes - delay*1e-6;
1202 fps = nframes / elapsed;
1204 printf ("%f s elapsed\t%3f s/frame\t%.1f FPS\n", elapsed,
1205 time_per_frame, fps);
1207 step = effective_speed * elapsed;
1212 time_per_frame = 0.04;
1213 step = effective_speed;
1218 decrement_bonuses (time_per_frame);
1224 * speedmine_color_ramp (dpy, cmap, gcs, colors, ncolors, s1, s2, v1, v2)
1226 * generate a color ramp of up to *ncolors between randomly chosen hues,
1227 * varying from saturation s1 to s2 and value v1 to v2, placing the colors
1228 * in 'colors' and creating corresponding GCs in 'gcs'.
1230 * The number of colors actually allocated is returned in ncolors.
1233 speedmine_color_ramp (Display * dpy, Colormap cmap, GC *gcs, XColor * colors,
1234 int *ncolors, double s1, double s2, double v1, double v2)
1238 unsigned long flags;
1241 assert (*ncolors >= 0);
1242 assert (s1 >= 0.0 && s1 <= 1.0 && v1 >= 0.0 && v2 <= 1.0);
1244 if (psychedelic_flag) {
1245 h1 = RAND(360); h2 = (h1 + 180) % 360;
1247 h1 = h2 = RAND(360);
1250 make_color_ramp (dpy, cmap, h1, s1, v1, h2, s2, v2,
1251 colors, ncolors, False, True, False);
1253 flags = GCForeground;
1254 for (i=0; i < *ncolors; i++) {
1255 gcv.foreground = colors[i].pixel;
1256 gcs[i] = XCreateGC (dpy, dbuf, flags, &gcv);
1264 * perform the color changing bonus. New colors are allocated for the
1265 * walls and bonuses, and if the 'psychedelic' option is set then new
1266 * colors are also chosen for the ground.
1269 change_colors (void)
1273 if (psychedelic_flag) {
1274 free_colors (display, cmap, bonus_colors, nr_bonus_colors);
1275 free_colors (display, cmap, wall_colors, nr_wall_colors);
1276 free_colors (display, cmap, ground_colors, nr_ground_colors);
1277 ncolors = MAX_COLORS;
1281 speedmine_color_ramp (display, cmap, ground_gcs, ground_colors,
1282 &ncolors, 0.0, 0.8, 0.0, 0.9);
1283 nr_ground_colors = ncolors;
1285 free_colors (display, cmap, bonus_colors, nr_bonus_colors);
1286 free_colors (display, cmap, wall_colors, nr_wall_colors);
1287 ncolors = nr_ground_colors;
1292 speedmine_color_ramp (display, cmap, wall_gcs, wall_colors, &ncolors,
1294 nr_wall_colors = ncolors;
1296 speedmine_color_ramp (display, cmap, bonus_gcs, bonus_colors, &ncolors,
1297 0.6, 0.9, 0.4, 1.0);
1298 nr_bonus_colors = ncolors;
1302 * init_psychedelic_colors (dpy, window, cmap)
1304 * initialise a psychedelic colormap
1307 init_psychedelic_colors (Display * dpy, Window window, Colormap cmap)
1311 gcv.foreground = get_pixel_resource ("tunnelend", "TunnelEnd", dpy, cmap);
1312 tunnelend_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1314 ncolors = MAX_COLORS;
1316 speedmine_color_ramp (dpy, cmap, ground_gcs, ground_colors, &ncolors,
1317 0.0, 0.8, 0.0, 0.9);
1318 nr_ground_colors = ncolors;
1320 speedmine_color_ramp (dpy, cmap, wall_gcs, wall_colors, &ncolors,
1321 0.0, 0.6, 0.0, 0.9);
1322 nr_wall_colors = ncolors;
1324 speedmine_color_ramp (dpy, cmap, bonus_gcs, bonus_colors, &ncolors,
1325 0.6, 0.9, 0.4, 1.0);
1326 nr_bonus_colors = ncolors;
1330 * init_colors (dpy, window, cmap)
1332 * initialise a normal colormap
1335 init_colors (Display * dpy, Window window, Colormap cmap)
1340 double s1, s2, v1, v2;
1341 unsigned long flags;
1344 gcv.foreground = get_pixel_resource ("tunnelend", "TunnelEnd", dpy, cmap);
1345 tunnelend_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1347 ncolors = MAX_COLORS;
1349 dark.pixel = get_pixel_resource ("darkground", "DarkGround", dpy, cmap);
1350 XQueryColor (dpy, cmap, &dark);
1352 light.pixel = get_pixel_resource ("lightground", "LightGround", dpy, cmap);
1353 XQueryColor (dpy, cmap, &light);
1355 rgb_to_hsv (dark.red, dark.green, dark.blue, &h1, &s1, &v1);
1356 rgb_to_hsv (light.red, light.green, light.blue, &h2, &s2, &v2);
1357 make_color_ramp (dpy, cmap, h1, s1, v1, h2, s2, v2,
1358 ground_colors, &ncolors, False, True, False);
1359 nr_ground_colors = ncolors;
1361 flags = GCForeground;
1362 for (i=0; i < ncolors; i++) {
1363 gcv.foreground = ground_colors[i].pixel;
1364 ground_gcs[i] = XCreateGC (dpy, dbuf, flags, &gcv);
1367 speedmine_color_ramp (dpy, cmap, wall_gcs, wall_colors, &ncolors,
1368 0.0, 0.6, 0.0, 0.9);
1369 nr_wall_colors = ncolors;
1371 speedmine_color_ramp (dpy, cmap, bonus_gcs, bonus_colors, &ncolors,
1372 0.6, 0.9, 0.4, 1.0);
1373 nr_bonus_colors = ncolors;
1379 * print out average FPS stats for the demo
1384 if (total_nframes >= 1)
1385 printf ("Rendered %d frames averaging %f FPS\n", total_nframes,
1386 total_nframes / get_time());
1390 * init_speedmine (dpy, window)
1392 * initialise the demo
1395 init_speedmine (Display *dpy, Window window)
1398 XWindowAttributes xgwa;
1405 XGetWindowAttributes (dpy, window, &xgwa);
1406 cmap = xgwa.colormap;
1408 height = xgwa.height;
1410 verbose_flag = get_boolean_resource ("verbose", "Boolean");
1412 dbuf = XCreatePixmap (dpy, window, width, height, xgwa.depth);
1413 stars_mask = XCreatePixmap (dpy, window, width, height, 1);
1415 gcv.foreground = default_fg_pixel =
1416 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
1417 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1418 stars_gc = XCreateGC (dpy, stars_mask, GCForeground, &gcv);
1420 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
1421 erase_gc = XCreateGC (dpy, dbuf, GCForeground, &gcv);
1422 stars_erase_gc = XCreateGC (dpy, stars_mask, GCForeground, &gcv);
1424 wire_flag = get_boolean_resource ("wire", "Boolean");
1426 psychedelic_flag = get_boolean_resource ("psychedelic", "Boolean");
1428 delay = get_integer_resource("delay", "Integer");
1430 smoothness = get_integer_resource("smoothness", "Integer");
1431 if (smoothness < 1) smoothness = 1;
1433 maxspeed = get_float_resource("maxspeed", "Float");
1435 maxspeed = fabs(maxspeed);
1437 thrust = get_float_resource("thrust", "Float");
1440 gravity = get_float_resource("gravity", "Float");
1441 gravity *= 0.002/9.8;
1443 vertigo = get_float_resource("vertigo", "Float");
1446 curviness = get_float_resource("curviness", "Float");
1449 twistiness = get_float_resource("twistiness", "Float");
1450 twistiness *= 0.125;
1452 terrain_flag = get_boolean_resource ("terrain", "Boolean");
1453 widening_flag = get_boolean_resource ("widening", "Boolean");
1454 bumps_flag = get_boolean_resource ("bumps", "Boolean");
1455 bonuses_flag = get_boolean_resource ("bonuses", "Boolean");
1456 crosshair_flag = get_boolean_resource ("crosshair", "Boolean");
1458 be_wormy = get_boolean_resource ("worm", "Boolean");
1467 psychedelic_flag = True;
1468 crosshair_flag = False;
1471 if (psychedelic_flag) init_psychedelic_colors (dpy, window, cmap);
1472 else init_colors (dpy, window, cmap);
1474 for (i=0; i<ROTS; i++) {
1475 th = M_PI * 2.0 * i / ROTS;
1476 costab[i] = cos(th);
1477 sintab[i] = sin(th);
1480 wide = random_wideness();
1482 for (i=0; i < TERRAIN_LENGTH; i++) {
1489 wrap_tunnel (0, TERRAIN_LENGTH-1);
1491 if (DEBUG_FLAG || verbose_flag) atexit(print_stats);
1493 step = effective_speed;
1495 #ifdef HAVE_GETTIMEOFDAY
1503 * Down the speedmine, you'll find speed
1504 * to satisfy your moving needs;
1505 * So if you're looking for a blast
1506 * then hit the speedmine, really fast.
1510 * Speedworm likes to choke and spit
1511 * and chase his tail, and dance a bit
1512 * he really is a funky friend;
1513 * he's made of speed from end to end.
1516 char *progclass = "Speedmine";
1518 char *defaults [] = {
1522 ".background: black",
1523 ".foreground: white",
1524 "*darkground: #101010",
1525 "*lightground: #a0a0a0",
1526 "*tunnelend: #000000",
1540 "*psychedelic: False",
1544 XrmOptionDescRec options [] = {
1545 { "-verbose", ".verbose", XrmoptionNoArg, "True"},
1546 { "-worm", ".worm", XrmoptionNoArg, "True"},
1547 { "-wire", ".wire", XrmoptionNoArg, "True"},
1548 { "-nowire", ".wire", XrmoptionNoArg, "False"},
1549 { "-darkground", ".darkground", XrmoptionSepArg, 0 },
1550 { "-lightground", ".lightground", XrmoptionSepArg, 0 },
1551 { "-tunnelend", ".tunnelend", XrmoptionSepArg, 0 },
1552 { "-delay", ".delay", XrmoptionSepArg, 0 },
1553 { "-maxspeed", ".maxspeed", XrmoptionSepArg, 0 },
1554 { "-thrust", ".thrust", XrmoptionSepArg, 0 },
1555 { "-gravity", ".gravity", XrmoptionSepArg, 0 },
1556 { "-vertigo", ".vertigo", XrmoptionSepArg, 0 },
1557 { "-terrain", ".terrain", XrmoptionNoArg, "True"},
1558 { "-noterrain", ".terrain", XrmoptionNoArg, "False"},
1559 { "-smoothness", ".smoothness", XrmoptionSepArg, 0 },
1560 { "-curviness", ".curviness", XrmoptionSepArg, 0 },
1561 { "-twistiness", ".twistiness", XrmoptionSepArg, 0 },
1562 { "-widening", ".widening", XrmoptionNoArg, "True"},
1563 { "-nowidening", ".widening", XrmoptionNoArg, "False"},
1564 { "-bumps", ".bumps", XrmoptionNoArg, "True"},
1565 { "-nobumps", ".bumps", XrmoptionNoArg, "False"},
1566 { "-bonuses", ".bonuses", XrmoptionNoArg, "True"},
1567 { "-nobonuses", ".bonuses", XrmoptionNoArg, "False"},
1568 { "-crosshair", ".crosshair", XrmoptionNoArg, "True"},
1569 { "-nocrosshair", ".crosshair", XrmoptionNoArg, "False"},
1570 { "-psychedelic", ".psychedelic", XrmoptionNoArg, "True"},
1571 { "-nopsychedelic", ".psychedelic", XrmoptionNoArg, "False"},
1577 screenhack (Display *dpy, Window window)
1583 init_speedmine (dpy, window);
1586 speedmine (dpy, window);
1588 screenhack_handle_events (dpy);
1589 if (delay) usleep(delay);