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 xx = (worldx[i][jj]-(vertigo*xcc))/zfactor;
276 yy = (worldy[i][j]-(vertigo*ycc))/zfactor;
278 r = rotation_bias + (int)(vertigo*zcc); MODULO(r, ROTS);
280 xvals[t][j] = xoffset + width/2 +
281 (int)(xx * costab[r] - yy * sintab[r]);
282 maxx[t] = MAX(maxx[t], xvals[t][j]);
283 minx[t] = MIN(minx[t], xvals[t][j]);
285 yvals[t][j] = yoffset + height/2 +
286 (int)(xx * sintab[r] + yy * costab[r]);
287 maxy[t] = MAX(maxy[t], yvals[t][j]);
288 miny[t] = MIN(miny[t], yvals[t][j]);
290 xcc -= xc; ycc -= yc; zcc -= zc;
291 xc -= xcurvature[i]; yc -= ycurvature[i]; zc -= zcurvature[i];
297 * wrap_tunnel (start, end)
299 * wrap the terrain terrain[i][j] around the semi-circular tunnel function
301 * x' = x/2 * cos(theta) - (y-k) * x * sin(theta)
302 * y' = x/4 * sin(theta) + y * cos(theta)
304 * between i=start and i=end inclusive, producing world coordinates
305 * worldx[i][j], worldy[i][j]
308 wrap_tunnel (int start, int end)
313 assert (start < end);
315 for (i=start; i <= end; i++) {
316 for (j=0; j < TERRAIN_BREADTH; j++) {
317 x = j * (1.0/TERRAIN_BREADTH);
319 y = (double)(v==STEEL_ELEVATION?200:v) - wideness[i] - 1200;
322 if (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8) y -= 300;
324 worldx[i][j] = x/2 * costab[j*TB_MUL] -
325 (y-height/4.0)*x*sintab[j*TB_MUL];
326 worldy[i][j] = x/4 * sintab[j*TB_MUL] +
327 y * costab[j*TB_MUL];
335 * perform the state transitions and terrain transformation for the
336 * "look backwards/look forwards" bonus
339 flip_direction (void)
343 direction = -direction;
347 for (i=0; i < TERRAIN_LENGTH; i++) {
348 in = nearest + i; MODULO(in, TERRAIN_BREADTH);
349 ip = nearest - i; MODULO(ip, TERRAIN_BREADTH);
350 for (j=0; j < TERRAIN_BREADTH; j++) {
352 terrain[ip][j] = terrain[in][j];
359 * generate_smooth (start, end)
361 * generate smooth terrain between i=start and i=end inclusive
364 generate_smooth (int start, int end)
368 assert (start < end);
370 for (i=start; i <= end; i++) {
371 ii = i; MODULO(ii, TERRAIN_LENGTH);
372 for (j=0; j < TERRAIN_BREADTH; j++) {
373 terrain[i][j] = STEEL_ELEVATION;
379 * generate_straight (start, end)
381 * zero the curvature and wideness between i=start and i=end inclusive
384 generate_straight (int start, int end)
388 assert (start < end);
390 for (i=start; i <= end; i++) {
391 ii = i; MODULO(ii, TERRAIN_LENGTH);
392 for (j=0; j < TERRAIN_BREADTH; j++) {
402 * int generate_terrain_value (v1, v2, v3, v4, w)
404 * generate terrain value near the average of v1, v2, v3, v4, with
405 * perturbation proportional to w
408 generate_terrain_value (int v1, int v2, int v3, int v4, int w)
413 if (!terrain_flag) return 0;
415 sum = v1 + v2 + v3 + v4;
417 rval = w*sum/smoothness;
418 if (rval == 0) rval = 2;
420 ret = (sum/4 -(rval/2) + RAND(rval));
422 if (ret < -400 || ret > 400) {
430 * generate_terrain (start, end, final)
432 * generate terrain[i][j] between i=start and i=end inclusive
434 * This is performed by successive subdivision of the terrain into
435 * rectangles of decreasing size. Subdivision continues until the
436 * the minimum width or height of these rectangles is 'final'; ie.
437 * final=1 indicates to subdivide as far as possible, final=2 indicates
438 * to stop one subdivision before that (leaving a checkerboard pattern
442 generate_terrain (int start, int end, int final)
445 int ip, jp, in, jn; /* prev, next values */
448 assert (start < end);
449 assert (start >= 0 && start < TERRAIN_LENGTH);
450 assert (end >= 0 && end < TERRAIN_LENGTH);
452 diff = end - start + 1;
454 terrain[end][0] = random_elevation();
455 terrain[end][TERRAIN_BREADTH/2] = random_elevation();
457 for (w= diff/2, l=TERRAIN_BREADTH/4;
458 w >= final || l >= final; w /= 2, l /= 2) {
460 if (w<1) w=1; if (l<1) l=1;
462 for (i=start+w-1; i < end; i += (w*2)) {
463 ip = i-w; MODULO(ip, TERRAIN_LENGTH);
464 in = i+w; MODULO(in, TERRAIN_LENGTH);
465 for (j=l-1; j < TERRAIN_BREADTH; j += (l*2)) {
466 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
467 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
469 generate_terrain_value (terrain[ip][jp], terrain[in][jp],
470 terrain[ip][jn], terrain[in][jn], w);
474 for (i=start+(w*2)-1; i < end; i += (w*2)) {
475 ip = i-w; MODULO(ip, TERRAIN_LENGTH);
476 in = i+w; MODULO(in, TERRAIN_LENGTH);
477 for (j=l-1; j < TERRAIN_BREADTH; j += (l*2)) {
478 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
479 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
481 generate_terrain_value (terrain[ip][j], terrain[in][j],
482 terrain[i][jp], terrain[i][jn], w);
486 for (i=start+w-1; i < end; i += (w*2)) {
487 ip = i-w; MODULO(ip, TERRAIN_LENGTH);
488 in = i+w; MODULO(in, TERRAIN_LENGTH);
489 for (j=2*l-1; j < TERRAIN_BREADTH; j += (l*2)) {
490 jp = j-1; MODULO(jp, TERRAIN_BREADTH);
491 jn = j+1; MODULO(jn, TERRAIN_BREADTH);
493 generate_terrain_value (terrain[ip][j], terrain[in][j],
494 terrain[i][jp], terrain[i][jn], w);
501 * double generate_curvature_value (v1, v2, w)
503 * generate curvature value near the average of v1 and v2, with perturbation
507 generate_curvature_value (double v1, double v2, int w)
509 double sum, avg, diff, ret;
512 assert (!isnan(v1) && !isnan(v2));
517 diff = MIN(v1 - avg, v2 - avg);
519 rval = (int)diff * w;
520 if (rval == 0.0) return avg;
522 ret = (avg -((double)rval)/500.0 + ((double)RAND(rval))/1000.0);
524 assert (!isnan(ret));
530 * generate_curves (start, end)
532 * generate xcurvature[i], ycurvature[i], zcurvature[i] and wideness[i]
533 * between start and end inclusive
536 generate_curves (int start, int end)
538 int i, diff, ii, in, ip, w;
540 assert (start < end);
542 diff = end - start + 1; MODULO (diff, TERRAIN_LENGTH);
544 if (random() % 100 == 0)
545 xcurvature[end] = 30 * random_curvature();
546 else if (random() % 10 == 0)
547 xcurvature[end] = 20 * random_curvature();
549 xcurvature[end] = 10 * random_curvature();
551 if (random() % 50 == 0)
552 ycurvature[end] = 20 * random_curvature();
553 else if (random() % 25 == 0)
554 ycurvature[end] = 30 * random_curvature();
556 ycurvature[end] = 10 * random_curvature();
558 if (random() % 3 == 0)
559 zcurvature[end] = random_twist();
562 generate_curvature_value (zcurvature[end], random_twist(), 1);
565 wideness[end] = random_wideness();
568 generate_curvature_value (wideness[end], random_wideness(), 1);
570 for (w=diff/2; w >= 1; w /= 2) {
571 for (i=start+w-1; i < end; i+=(w*2)) {
572 ii = i; MODULO (ii, TERRAIN_LENGTH);
573 ip = i-w; MODULO (ip, TERRAIN_LENGTH);
574 in = i+w; MODULO (in, TERRAIN_LENGTH);
576 generate_curvature_value (xcurvature[ip], xcurvature[in], w);
578 generate_curvature_value (ycurvature[ip], ycurvature[in], w);
580 generate_curvature_value (zcurvature[ip], zcurvature[in], w);
582 generate_curvature_value (wideness[ip], wideness[in], w);
590 * choose a random bonus and perform its state transition
595 static int jamming=0;
601 nearest -= 2; MODULO(nearest, TERRAIN_LENGTH);
605 if (psychedelic_flag) change_colors();
607 switch (random() % 7) {
608 case 0: /* switch to or from wireframe */
609 wire_bonus = (wire_bonus?0:300);
611 case 1: /* speedup */
620 case 4: /* look backwards / look forwards */
621 flipped_at = nearest;
623 backwards_bonus = (backwards_bonus?0:10);
628 case 6: /* jam against the bonus a few times; deja vu! */
629 nearest -= 2; MODULO(nearest, TERRAIN_LENGTH);
641 * check if a bonus has been passed in the last frame, and handle it
646 int i, ii, start, end;
648 if (!bonuses_flag) return;
651 start = nearest; end = nearest + (int)floor(step);
653 end = nearest; start = nearest + (int)floor(step);
657 start += TERRAIN_LENGTH/4;
658 end += TERRAIN_LENGTH/4;
661 for (i=start; i < end; i++) {
662 ii = i; MODULO(ii, TERRAIN_LENGTH);
663 if (bonuses[ii] == 1) do_bonus ();
668 * decrement_bonuses (double time_per_frame)
670 * decrement timers associated with bonuses
673 decrement_bonuses (double time_per_frame)
675 if (!bonuses_flag) return;
677 if (bonus_bright > 0) bonus_bright-=4;
678 if (wire_bonus > 0) wire_bonus--;
679 if (speed_bonus > 0) speed_bonus -= 2.0;
681 if (spin_bonus > 10) spin_bonus -= (int)(step*13.7);
682 else if (spin_bonus < -10) spin_bonus += (int)(step*11.3);
684 if (backwards_bonus > 1) backwards_bonus--;
685 else if (backwards_bonus == 1) {
686 nearest += 2*(MAX(flipped_at, nearest) - MIN(flipped_at,nearest));
687 MODULO(nearest, TERRAIN_LENGTH);
694 * set_bonuses (start, end)
696 * choose if to and where to set a bonus between i=start and i=end inclusive
699 set_bonuses (int start, int end)
703 if (!bonuses_flag) return;
705 assert (start < end);
709 for (i=start; i <= end; i++) {
710 ii = i; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
713 if (random() % 4 == 0) {
714 i = start + RAND(diff-3);
715 ii = i; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
716 bonuses[ii] = 2; /* marker */
717 ii = i+3; if (ii>=TERRAIN_LENGTH) ii -= TERRAIN_LENGTH;
718 bonuses[ii] = 1; /* real thing */
723 * regenerate_terrain ()
725 * regenerate a portion of the terrain map of length TERRAIN_LENGTH/4 iff
726 * we just passed between two quarters of the terrain.
728 * choose the kind of terrain to produce, produce it and wrap the tunnel
731 regenerate_terrain (void)
736 passed = nearest % (TERRAIN_LENGTH/4);
739 (speed > 0.0 && passed > (int)step) ||
740 (speed < 0.0 && (TERRAIN_LENGTH/4)-passed > (int)fabs(step))) {
745 end = nearest - passed - 1; MODULO(end, TERRAIN_LENGTH);
746 start = end - TERRAIN_LENGTH/4 + 1; MODULO(start, TERRAIN_LENGTH);
748 if (DEBUG_FLAG) printf ("Regenerating [%d - %d]\n", start, end);
750 set_bonuses (start, end);
752 switch (random() % 64) {
755 generate_terrain (start, end, 1);
756 generate_smooth (start,
757 start + TERRAIN_LENGTH/8 + (random() % TERRAIN_LENGTH/8));
760 generate_smooth (start, end);
761 generate_terrain (start, end, 4); break;
763 generate_smooth (start, end);
764 generate_terrain (start, end, 2); break;
766 generate_terrain (start, end, 1);
769 if (random() % 16 == 0) {
770 generate_straight (start, end);
772 generate_curves (start, end);
775 wrap_tunnel (start, end);
781 * initialise the terrain map for the beginning of the demo
788 for (i=0; i < TERRAIN_LENGTH; i++) {
789 for (j=0; j < TERRAIN_BREADTH; j++) {
794 terrain[TERRAIN_LENGTH-1][0] = - (random() % 300);
795 terrain[TERRAIN_LENGTH-1][TERRAIN_BREADTH/2] = - (random() % 300);
797 generate_smooth (0, TERRAIN_LENGTH-1);
798 generate_terrain (0, TERRAIN_LENGTH/4 -1, 4);
799 generate_terrain (TERRAIN_LENGTH/4, TERRAIN_LENGTH/2 -1, 2);
800 generate_terrain (TERRAIN_LENGTH/2, 3*TERRAIN_LENGTH/4 -1, 1);
801 generate_smooth (3*TERRAIN_LENGTH/4, TERRAIN_LENGTH-1);
807 * initialise the curvatures and wideness for the beginning of the demo.
814 for (i=0; i < TERRAIN_LENGTH-1; i++) {
820 xcurvature[TERRAIN_LENGTH-1] = random_curvature();
821 ycurvature[TERRAIN_LENGTH-1] = random_curvature();
822 zcurvature[TERRAIN_LENGTH-1] = random_twist();
824 generate_straight (0, TERRAIN_LENGTH/4-1);
825 generate_curves (TERRAIN_LENGTH/4, TERRAIN_LENGTH/2-1);
826 generate_curves (TERRAIN_LENGTH/2, 3*TERRAIN_LENGTH/4-1);
827 generate_straight (3*TERRAIN_LENGTH/4, TERRAIN_LENGTH-1);
832 * render_quads (dpy, d, t, dt, i)
834 * renders the quadrilaterals from perspective depth t to t+dt.
835 * i is passed as a hint, where i corresponds to t as asserted.
838 render_quads (Display * dpy, Drawable d, int t, int dt, int i)
845 assert (i == (nearest - (t + dt) + TERRAIN_LENGTH) % TERRAIN_LENGTH);
847 in = i + 1; MODULO(in, TERRAIN_LENGTH);
849 for (j=0; j < TERRAIN_BREADTH; j+=dt) {
850 t2 = t+dt; if (t2 >= TERRAIN_LENGTH) t2 -= TERRAIN_LENGTH;
851 j2 = j+dt; if (j2 >= TERRAIN_BREADTH) j2 -= TERRAIN_BREADTH;
852 points[0].x = xvals[t][j]; points[0].y = yvals[t][j];
853 points[1].x = xvals[t2][j]; points[1].y = yvals[t2][j];
854 points[2].x = xvals[t2][j2]; points[2].y = yvals[t2][j2];
855 points[3].x = xvals[t][j2]; points[3].y = yvals[t][j2];
857 index = bonus_bright + ncolors/3 +
858 t*(t*INTERP + pindex) * ncolors /
859 (3*TERRAIN_LENGTH*TERRAIN_PDIST);
861 index += (int)((points[0].y - points[3].y) / 8);
862 index += (int)((worldx[i][j] - worldx[in][j]) / 40);
863 index += (int)((terrain[in][j] - terrain[i][j]) / 100);
865 if (be_wormy && psychedelic_flag) index += ncolors/4;
867 index = MIN (index, ncolors-1);
868 index = MAX (index, 0);
871 XSetClipMask (dpy, bonus_gcs[index], None);
875 if (bonuses[i]) gc = bonus_gcs[index];
876 else gc = ground_gcs[index];
877 XDrawLines (dpy, d, gc, points, 4, CoordModeOrigin);
880 gc = bonus_gcs[index];
881 else if ((direction>0 && j < TERRAIN_BREADTH/8) ||
882 (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8-1) ||
883 (direction < 0 && j > 3*TERRAIN_BREADTH/8-1 &&
884 j < TERRAIN_BREADTH/2) ||
885 terrain[i][j] == STEEL_ELEVATION ||
886 wideness[in] - wideness[i] > 200)
887 gc = ground_gcs[index];
889 gc = wall_gcs[index];
891 XFillPolygon (dpy, d, gc, points, 4, Nonconvex, CoordModeOrigin);
897 * render_pentagons (dpy, d, t, dt, i)
899 * renders the pentagons from perspective depth t to t+dt.
900 * i is passed as a hint, where i corresponds to t as asserted.
903 render_pentagons (Display *dpy, Drawable d, int t, int dt, int i)
905 int j, t2, j2, j3, in;
910 assert (i == (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH);
912 in = i + 1; MODULO(in, TERRAIN_LENGTH);
914 for (j=0; j < TERRAIN_BREADTH; j+=dt*2) {
915 t2 = t+(dt*2); if (t2 >= TERRAIN_LENGTH) t2 -= TERRAIN_LENGTH;
916 j2 = j+dt; if (j2 >= TERRAIN_BREADTH) j2 -= TERRAIN_BREADTH;
917 j3 = j+dt+dt; if (j3 >= TERRAIN_BREADTH) j3 -= TERRAIN_BREADTH;
918 points[0].x = xvals[t][j]; points[0].y = yvals[t][j];
919 points[1].x = xvals[t2][j]; points[1].y = yvals[t2][j];
920 points[2].x = xvals[t2][j2]; points[2].y = yvals[t2][j2];
921 points[3].x = xvals[t2][j3]; points[3].y = yvals[t2][j3];
922 points[4].x = xvals[t][j3]; points[4].y = yvals[t][j3];
924 index = bonus_bright + ncolors/3 +
925 t*(t*INTERP + pindex) * ncolors /
926 (3*TERRAIN_LENGTH*TERRAIN_PDIST);
928 index += (int)((points[0].y - points[3].y) / 8);
929 index += (int)((worldx[i][j] - worldx[in][j]) / 40);
930 index += (int)((terrain[in][j] - terrain[i][j]) / 100);
932 if (be_wormy && psychedelic_flag) index += ncolors/4;
934 index = MIN (index, ncolors-1);
935 index = MAX (index, 0);
938 XSetClipMask (dpy, bonus_gcs[index], None);
942 if (bonuses[i]) gc = bonus_gcs[index];
943 else gc = ground_gcs[index];
944 XDrawLines (dpy, d, gc, points, 5, CoordModeOrigin);
947 gc = bonus_gcs[index];
948 else if (j < TERRAIN_BREADTH/8 ||
949 (j > TERRAIN_BREADTH/8 && j < 3*TERRAIN_BREADTH/8-1) ||
950 terrain[i][j] == STEEL_ELEVATION ||
951 wideness[in] - wideness[i] > 200)
952 gc = ground_gcs[index];
954 gc = wall_gcs[index];
956 XFillPolygon (dpy, d, gc, points, 5, Complex, CoordModeOrigin);
962 * render_block (dpy, d, gc, t)
964 * render a filled polygon at perspective depth t using the given GC
967 render_block (Display * dpy, Drawable d, GC gc, int t)
971 XPoint erase_points[TERRAIN_BREADTH/2];
973 for (i=0; i < TERRAIN_BREADTH/2; i++) {
974 erase_points[i].x = xvals[t][i*2];
975 erase_points[i].y = yvals[t][i*2];
978 XFillPolygon (dpy, d, gc, erase_points,
979 TERRAIN_BREADTH/2, Complex, CoordModeOrigin);
983 * regenerate_stars_mask (dpy, t)
985 * regenerate the clip mask 'stars_mask' for drawing the bonus stars at
986 * random positions within the bounding box at depth t
989 regenerate_stars_mask (Display * dpy, int t)
991 int i, w, h, a, b, l1, l2;
992 const int lim = width*TERRAIN_LENGTH/(300*(TERRAIN_LENGTH-t));
994 w = maxx[t] - minx[t];
995 h = maxy[t] - miny[t];
997 if (w<6||h<6) return;
999 XFillRectangle (dpy, stars_mask, stars_erase_gc,
1000 0, 0, width, height);
1002 l1 = (t>3*TERRAIN_LENGTH/4?2:1);
1003 l2 = (t>7*TERRAIN_LENGTH/8?2:1);
1005 for (i=0; i < lim; i++) {
1006 a = RAND(w); b = RAND(h);
1007 XDrawLine (dpy, stars_mask, stars_gc,
1008 minx[t]+a-l1, miny[t]+b, minx[t]+a+l1, miny[t]+b);
1009 XDrawLine (dpy, stars_mask, stars_gc,
1010 minx[t]+a, miny[t]+b-l1, minx[t]+a, miny[t]+b+l1);
1012 for (i=0; i < lim; i++) {
1013 a = RAND(w); b = RAND(h);
1014 XDrawLine (dpy, stars_mask, stars_gc,
1015 minx[t]+a-l2, miny[t]+b, minx[t]+a+l2, miny[t]+b);
1016 XDrawLine (dpy, stars_mask, stars_gc,
1017 minx[t]+a, miny[t]+b-l2, minx[t]+a, miny[t]+b+l2);
1022 * render_bonus_block (dpy, d, t, i)
1024 * draw the bonus stars at depth t.
1025 * i is passed as a hint, where i corresponds to t as asserted.
1028 render_bonus_block (Display * dpy, Drawable d, int t, int i)
1032 assert (i == (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH);
1034 if (!bonuses[i] || wireframe) return;
1036 regenerate_stars_mask (dpy, t);
1038 bt = t * nr_bonus_colors / (2*TERRAIN_LENGTH);
1040 XSetClipMask (dpy, bonus_gcs[bt], stars_mask);
1042 render_block (dpy, d, bonus_gcs[bt], t);
1049 int max_minx=0, min_maxx=width, max_miny=0, min_maxy=height;
1051 for (t=TERRAIN_LENGTH-1; t > 0; t--) {
1052 max_minx = MAX (max_minx, minx[t]);
1053 min_maxx = MIN (min_maxx, maxx[t]);
1054 max_miny = MAX (max_miny, miny[t]);
1055 min_maxy = MIN (min_maxy, maxy[t]);
1057 if (max_miny >= min_maxy || max_minx >= min_maxx) break;
1064 * render_speedmine (dpy, d)
1066 * render the current frame.
1069 render_speedmine (Display * dpy, Drawable d)
1071 int t, i=nearest, dt=1;
1074 assert (nearest >= 0 && nearest < TERRAIN_LENGTH);
1076 if (be_wormy || wireframe) {
1077 XFillRectangle (dpy, d, erase_gc, 0, 0, width, height);
1080 for (t=0; t < TERRAIN_LENGTH/4; t+=dt) {
1081 render_bonus_block (dpy, d, t, i);
1082 i -= dt; MODULO(i, TERRAIN_LENGTH);
1083 render_quads (dpy, d, t, dt, i);
1086 assert (t == TERRAIN_LENGTH/4);
1088 t = MAX(begin_at(), TERRAIN_LENGTH/4);
1089 /*t = TERRAIN_LENGTH/4; dt = 2; */
1090 dt = (t >= 3*TERRAIN_LENGTH/4 ? 1 : 2);
1091 i = (nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH;
1092 render_block (dpy, d, tunnelend_gc, t);
1097 if (t == TERRAIN_LENGTH/4)
1098 render_pentagons (dpy, d, t, dt, i);
1100 for (; t < 3*TERRAIN_LENGTH/4; t+=dt) {
1101 render_bonus_block (dpy, d, t, i);
1102 i -= dt; MODULO(i, TERRAIN_LENGTH);
1103 render_quads (dpy, d, t, dt, i);
1108 for (; t < TERRAIN_LENGTH-(1+(pindex<INTERP/2)); t+=dt) {
1109 render_bonus_block (dpy, d, t, i);
1110 i -= dt; MODULO(i, TERRAIN_LENGTH);
1113 if (wireframe) {assert (t == 3*TERRAIN_LENGTH/4);}
1115 if (t == 3*TERRAIN_LENGTH/4)
1116 render_pentagons (dpy, d, t, dt, i);
1118 for (; t < TERRAIN_LENGTH-(1+(pindex<INTERP/2)); t+=dt) {
1119 render_bonus_block (dpy, d, t, i);
1120 i -= dt; MODULO(i, TERRAIN_LENGTH);
1121 render_quads (dpy, d, t, dt, i);
1125 /* Draw crosshair */
1126 if (crosshair_flag) {
1127 gc = (wireframe ? bonus_gcs[nr_bonus_colors/2] : erase_gc);
1128 XFillRectangle (dpy, d, gc,
1129 width/2+(xoffset)-8, height/2+(yoffset*2)-1, 16, 3);
1130 XFillRectangle (dpy, d, gc,
1131 width/2+(xoffset)-1, height/2+(yoffset*2)-8, 3, 16);
1139 * move to the position for the next frame, and modify the state variables
1140 * nearest, pindex, pos, speed
1148 dpos = SIGN3(pos) * floor(fabs(pos));
1150 pindex += SIGN3(effective_speed) + INTERP;
1151 while (pindex >= INTERP) {
1155 while (pindex < 0) {
1160 nearest += dpos; MODULO(nearest, TERRAIN_LENGTH);
1164 accel = thrust + ycurvature[nearest] * gravity;
1166 if (speed > maxspeed) speed = maxspeed;
1167 if (speed < -maxspeed) speed = -maxspeed;
1171 * speedmine (dpy, window)
1173 * do everything required for one frame of the demo
1176 speedmine (Display *dpy, Window window)
1178 double elapsed, time_per_frame = 0.04;
1180 regenerate_terrain ();
1184 render_speedmine (dpy, dbuf);
1185 XCopyArea (dpy, dbuf, window, draw_gc, 0, 0, width, height, 0, 0);
1187 #if HAVE_GETTIMEOFDAY
1188 fps_end = get_time();
1192 if (fps_end > fps_start + 0.5) {
1193 elapsed = fps_end - fps_start;
1194 fps_start = get_time();
1196 time_per_frame = elapsed / nframes - delay*1e-6;
1197 fps = nframes / elapsed;
1199 printf ("%f s elapsed\t%3f s/frame\t%.1f FPS\n", elapsed,
1200 time_per_frame, fps);
1202 step = effective_speed * elapsed;
1207 time_per_frame = 0.04;
1208 step = effective_speed;
1213 decrement_bonuses (time_per_frame);
1219 * speedmine_color_ramp (dpy, cmap, gcs, colors, ncolors, s1, s2, v1, v2)
1221 * generate a color ramp of up to *ncolors between randomly chosen hues,
1222 * varying from saturation s1 to s2 and value v1 to v2, placing the colors
1223 * in 'colors' and creating corresponding GCs in 'gcs'.
1225 * The number of colors actually allocated is returned in ncolors.
1228 speedmine_color_ramp (Display * dpy, Colormap cmap, GC *gcs, XColor * colors,
1229 int *ncolors, double s1, double s2, double v1, double v2)
1233 unsigned long flags;
1236 assert (*ncolors >= 0);
1237 assert (s1 >= 0.0 && s1 <= 1.0 && v1 >= 0.0 && v2 <= 1.0);
1239 if (psychedelic_flag) {
1240 h1 = RAND(360); h2 = (h1 + 180) % 360;
1242 h1 = h2 = RAND(360);
1245 make_color_ramp (dpy, cmap, h1, s1, v1, h2, s2, v2,
1246 colors, ncolors, False, True, False);
1248 flags = GCForeground;
1249 for (i=0; i < *ncolors; i++) {
1250 gcv.foreground = colors[i].pixel;
1251 gcs[i] = XCreateGC (dpy, dbuf, flags, &gcv);
1259 * perform the color changing bonus. New colors are allocated for the
1260 * walls and bonuses, and if the 'psychedelic' option is set then new
1261 * colors are also chosen for the ground.
1264 change_colors (void)
1268 if (psychedelic_flag) {
1269 free_colors (display, cmap, bonus_colors, nr_bonus_colors);
1270 free_colors (display, cmap, wall_colors, nr_wall_colors);
1271 free_colors (display, cmap, ground_colors, nr_ground_colors);
1272 ncolors = MAX_COLORS;
1276 speedmine_color_ramp (display, cmap, ground_gcs, ground_colors,
1277 &ncolors, 0.0, 0.8, 0.0, 0.9);
1278 nr_ground_colors = ncolors;
1280 free_colors (display, cmap, bonus_colors, nr_bonus_colors);
1281 free_colors (display, cmap, wall_colors, nr_wall_colors);
1282 ncolors = nr_ground_colors;
1287 speedmine_color_ramp (display, cmap, wall_gcs, wall_colors, &ncolors,
1289 nr_wall_colors = ncolors;
1291 speedmine_color_ramp (display, cmap, bonus_gcs, bonus_colors, &ncolors,
1292 0.6, 0.9, 0.4, 1.0);
1293 nr_bonus_colors = ncolors;
1297 * init_psychedelic_colors (dpy, window, cmap)
1299 * initialise a psychedelic colormap
1302 init_psychedelic_colors (Display * dpy, Window window, Colormap cmap)
1306 gcv.foreground = get_pixel_resource ("tunnelend", "TunnelEnd", dpy, cmap);
1307 tunnelend_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1309 ncolors = MAX_COLORS;
1311 speedmine_color_ramp (dpy, cmap, ground_gcs, ground_colors, &ncolors,
1312 0.0, 0.8, 0.0, 0.9);
1313 nr_ground_colors = ncolors;
1315 speedmine_color_ramp (dpy, cmap, wall_gcs, wall_colors, &ncolors,
1316 0.0, 0.6, 0.0, 0.9);
1317 nr_wall_colors = ncolors;
1319 speedmine_color_ramp (dpy, cmap, bonus_gcs, bonus_colors, &ncolors,
1320 0.6, 0.9, 0.4, 1.0);
1321 nr_bonus_colors = ncolors;
1325 * init_colors (dpy, window, cmap)
1327 * initialise a normal colormap
1330 init_colors (Display * dpy, Window window, Colormap cmap)
1335 double s1, s2, v1, v2;
1336 unsigned long flags;
1339 gcv.foreground = get_pixel_resource ("tunnelend", "TunnelEnd", dpy, cmap);
1340 tunnelend_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1342 ncolors = MAX_COLORS;
1344 dark.pixel = get_pixel_resource ("darkground", "DarkGround", dpy, cmap);
1345 XQueryColor (dpy, cmap, &dark);
1347 light.pixel = get_pixel_resource ("lightground", "LightGround", dpy, cmap);
1348 XQueryColor (dpy, cmap, &light);
1350 rgb_to_hsv (dark.red, dark.green, dark.blue, &h1, &s1, &v1);
1351 rgb_to_hsv (light.red, light.green, light.blue, &h2, &s2, &v2);
1352 make_color_ramp (dpy, cmap, h1, s1, v1, h2, s2, v2,
1353 ground_colors, &ncolors, False, True, False);
1354 nr_ground_colors = ncolors;
1356 flags = GCForeground;
1357 for (i=0; i < ncolors; i++) {
1358 gcv.foreground = ground_colors[i].pixel;
1359 ground_gcs[i] = XCreateGC (dpy, dbuf, flags, &gcv);
1362 speedmine_color_ramp (dpy, cmap, wall_gcs, wall_colors, &ncolors,
1363 0.0, 0.6, 0.0, 0.9);
1364 nr_wall_colors = ncolors;
1366 speedmine_color_ramp (dpy, cmap, bonus_gcs, bonus_colors, &ncolors,
1367 0.6, 0.9, 0.4, 1.0);
1368 nr_bonus_colors = ncolors;
1374 * print out average FPS stats for the demo
1379 if (total_nframes >= 1)
1380 printf ("Rendered %d frames averaging %f FPS\n", total_nframes,
1381 total_nframes / get_time());
1385 * init_speedmine (dpy, window)
1387 * initialise the demo
1390 init_speedmine (Display *dpy, Window window)
1393 XWindowAttributes xgwa;
1400 XGetWindowAttributes (dpy, window, &xgwa);
1401 cmap = xgwa.colormap;
1403 height = xgwa.height;
1405 verbose_flag = get_boolean_resource ("verbose", "Boolean");
1407 dbuf = XCreatePixmap (dpy, window, width, height, xgwa.depth);
1408 stars_mask = XCreatePixmap (dpy, window, width, height, 1);
1410 gcv.foreground = default_fg_pixel =
1411 get_pixel_resource ("foreground", "Foreground", dpy, cmap);
1412 draw_gc = XCreateGC (dpy, window, GCForeground, &gcv);
1413 stars_gc = XCreateGC (dpy, stars_mask, GCForeground, &gcv);
1415 gcv.foreground = get_pixel_resource ("background", "Background", dpy, cmap);
1416 erase_gc = XCreateGC (dpy, dbuf, GCForeground, &gcv);
1417 stars_erase_gc = XCreateGC (dpy, stars_mask, GCForeground, &gcv);
1419 wire_flag = get_boolean_resource ("wire", "Boolean");
1421 psychedelic_flag = get_boolean_resource ("psychedelic", "Boolean");
1423 delay = get_integer_resource("delay", "Integer");
1425 smoothness = get_integer_resource("smoothness", "Integer");
1426 if (smoothness < 1) smoothness = 1;
1428 maxspeed = get_float_resource("maxspeed", "Float");
1430 maxspeed = fabs(maxspeed);
1432 thrust = get_float_resource("thrust", "Float");
1435 gravity = get_float_resource("gravity", "Float");
1436 gravity *= 0.002/9.8;
1438 vertigo = get_float_resource("vertigo", "Float");
1441 curviness = get_float_resource("curviness", "Float");
1444 twistiness = get_float_resource("twistiness", "Float");
1445 twistiness *= 0.125;
1447 terrain_flag = get_boolean_resource ("terrain", "Boolean");
1448 widening_flag = get_boolean_resource ("widening", "Boolean");
1449 bumps_flag = get_boolean_resource ("bumps", "Boolean");
1450 bonuses_flag = get_boolean_resource ("bonuses", "Boolean");
1451 crosshair_flag = get_boolean_resource ("crosshair", "Boolean");
1453 be_wormy = get_boolean_resource ("worm", "Boolean");
1462 psychedelic_flag = True;
1463 crosshair_flag = False;
1466 if (psychedelic_flag) init_psychedelic_colors (dpy, window, cmap);
1467 else init_colors (dpy, window, cmap);
1469 for (i=0; i<ROTS; i++) {
1470 th = M_PI * 2.0 * i / ROTS;
1471 costab[i] = cos(th);
1472 sintab[i] = sin(th);
1475 wide = random_wideness();
1477 for (i=0; i < TERRAIN_LENGTH; i++) {
1484 wrap_tunnel (0, TERRAIN_LENGTH-1);
1486 if (DEBUG_FLAG || verbose_flag) atexit(print_stats);
1488 step = effective_speed;
1490 #ifdef HAVE_GETTIMEOFDAY
1498 * Down the speedmine, you'll find speed
1499 * to satisfy your moving needs;
1500 * So if you're looking for a blast
1501 * then hit the speedmine, really fast.
1505 * Speedworm likes to choke and spit
1506 * and chase his tail, and dance a bit
1507 * he really is a funky friend;
1508 * he's made of speed from end to end.
1511 char *progclass = "Speedmine";
1513 char *defaults [] = {
1517 ".background: black",
1518 ".foreground: white",
1519 "*darkground: #101010",
1520 "*lightground: #a0a0a0",
1521 "*tunnelend: #000000",
1535 "*psychedelic: False",
1539 XrmOptionDescRec options [] = {
1540 { "-verbose", ".verbose", XrmoptionNoArg, "True"},
1541 { "-worm", ".worm", XrmoptionNoArg, "True"},
1542 { "-wire", ".wire", XrmoptionNoArg, "True"},
1543 { "-nowire", ".wire", XrmoptionNoArg, "False"},
1544 { "-darkground", ".darkground", XrmoptionSepArg, 0 },
1545 { "-lightground", ".lightground", XrmoptionSepArg, 0 },
1546 { "-tunnelend", ".tunnelend", XrmoptionSepArg, 0 },
1547 { "-delay", ".delay", XrmoptionSepArg, 0 },
1548 { "-maxspeed", ".maxspeed", XrmoptionSepArg, 0 },
1549 { "-thrust", ".thrust", XrmoptionSepArg, 0 },
1550 { "-gravity", ".gravity", XrmoptionSepArg, 0 },
1551 { "-vertigo", ".vertigo", XrmoptionSepArg, 0 },
1552 { "-terrain", ".terrain", XrmoptionNoArg, "True"},
1553 { "-noterrain", ".terrain", XrmoptionNoArg, "False"},
1554 { "-smoothness", ".smoothness", XrmoptionSepArg, 0 },
1555 { "-curviness", ".curviness", XrmoptionSepArg, 0 },
1556 { "-twistiness", ".twistiness", XrmoptionSepArg, 0 },
1557 { "-widening", ".widening", XrmoptionNoArg, "True"},
1558 { "-nowidening", ".widening", XrmoptionNoArg, "False"},
1559 { "-bumps", ".bumps", XrmoptionNoArg, "True"},
1560 { "-nobumps", ".bumps", XrmoptionNoArg, "False"},
1561 { "-bonuses", ".bonuses", XrmoptionNoArg, "True"},
1562 { "-nobonuses", ".bonuses", XrmoptionNoArg, "False"},
1563 { "-crosshair", ".crosshair", XrmoptionNoArg, "True"},
1564 { "-nocrosshair", ".crosshair", XrmoptionNoArg, "False"},
1565 { "-psychedelic", ".psychedelic", XrmoptionNoArg, "True"},
1566 { "-nopsychedelic", ".psychedelic", XrmoptionNoArg, "False"},
1572 screenhack (Display *dpy, Window window)
1578 init_speedmine (dpy, window);
1581 speedmine (dpy, window);
1583 screenhack_handle_events (dpy);
1584 if (delay) usleep(delay);