From http://www.jwz.org/xscreensaver/xscreensaver-5.22.tar.gz
[xscreensaver] / hacks / speedmine.c
index 312e51604d7f109ec88d29c9dbd5e49205f34e1e..81c468686760db0eb49c289fc04d204385d54ac0 100644 (file)
@@ -110,6 +110,8 @@ struct state {
 
     Pixmap dbuf, stars_mask;
     Colormap cmap;
+    Visual *visual;
+    Screen *screen;
     unsigned int default_fg_pixel;
     GC draw_gc, erase_gc, tunnelend_gc, stars_gc, stars_erase_gc;
 
@@ -881,6 +883,7 @@ render_quads (struct state *st, Drawable d, int t, int dt, int i)
                }
                if (st->be_wormy && st->psychedelic_flag) index += st->ncolors/4;
 
+        if (st->ncolors > MAX_COLORS) abort();
                index = MIN (index, st->ncolors-1);
                index = MAX (index, 0);
 
@@ -1104,7 +1107,7 @@ render_speedmine (struct state *st, Drawable d)
        } else {
                t = MAX(begin_at(st), TERRAIN_LENGTH/4);
                /*t = TERRAIN_LENGTH/4; dt = 2; */
-               dt = (t >= 3*TERRAIN_LENGTH/4 ? 1 : 2);
+               /*dt = (t >= 3*TERRAIN_LENGTH/4 ? 1 : 2);*/
                i = (st->nearest -t + TERRAIN_LENGTH) % TERRAIN_LENGTH;
                render_block (st, d, st->tunnelend_gc, t);
        }
@@ -1259,13 +1262,14 @@ speedmine_color_ramp (struct state *st, GC *gcs, XColor * colors,
                h1 = h2 = RAND(360);
        }
 
-       make_color_ramp (st->dpy, st->cmap, 
+       make_color_ramp (st->screen, st->visual, st->cmap, 
                      h1, s1, v1, h2, s2, v2,
                                     colors, ncolors, False, True, False);
 
        flags = GCForeground;
        for (i=0; i < *ncolors; i++) {
                gcv.foreground = colors[i].pixel;
+        if (gcs[i]) XFreeGC (st->dpy, gcs[i]);
                gcs[i] = XCreateGC (st->dpy, st->dbuf, flags, &gcv);
        }
 
@@ -1284,28 +1288,29 @@ change_colors (struct state *st)
        double s1, s2;
 
        if (st->psychedelic_flag) {
-               free_colors (st->dpy, st->cmap, st->bonus_colors, st->nr_bonus_colors);
-               free_colors (st->dpy, st->cmap, st->wall_colors, st->nr_wall_colors);
-               free_colors (st->dpy, st->cmap, st->ground_colors, st->nr_ground_colors);
-               st->ncolors = MAX_COLORS;
-
+               free_colors (st->screen, st->cmap, st->bonus_colors, st->nr_bonus_colors);
+               free_colors (st->screen, st->cmap, st->wall_colors, st->nr_wall_colors);
+               free_colors (st->screen, st->cmap, st->ground_colors, st->nr_ground_colors);
                s1 = 0.4; s2 = 0.9;
 
+               st->ncolors = MAX_COLORS;
                speedmine_color_ramp (st, st->ground_gcs, st->ground_colors,
                                                          &st->ncolors, 0.0, 0.8, 0.0, 0.9);
                st->nr_ground_colors = st->ncolors;
        } else {
-               free_colors (st->dpy, st->cmap, st->bonus_colors, st->nr_bonus_colors);
-               free_colors (st->dpy, st->cmap, st->wall_colors, st->nr_wall_colors);
+               free_colors (st->screen, st->cmap, st->bonus_colors, st->nr_bonus_colors);
+               free_colors (st->screen, st->cmap, st->wall_colors, st->nr_wall_colors);
                st->ncolors = st->nr_ground_colors;
 
                s1 = 0.0; s2 = 0.6;
        }
 
+    st->ncolors = MAX_COLORS;
     speedmine_color_ramp (st, st->wall_gcs, st->wall_colors, &st->ncolors,
                                                  s1, s2, 0.0, 0.9);
     st->nr_wall_colors = st->ncolors;
 
+    st->ncolors = MAX_COLORS;
     speedmine_color_ramp (st, st->bonus_gcs, st->bonus_colors, &st->ncolors,
                                                  0.6, 0.9, 0.4, 1.0);
        st->nr_bonus_colors = st->ncolors;
@@ -1325,15 +1330,16 @@ init_psychedelic_colors (struct state *st)
   st->tunnelend_gc = XCreateGC (st->dpy, st->window, GCForeground, &gcv);
 
   st->ncolors = MAX_COLORS;
-
   speedmine_color_ramp (st, st->ground_gcs, st->ground_colors, &st->ncolors,
                                                0.0, 0.8, 0.0, 0.9);
   st->nr_ground_colors = st->ncolors;
 
+  st->ncolors = MAX_COLORS;
   speedmine_color_ramp (st, st->wall_gcs, st->wall_colors, &st->ncolors,
                                                0.0, 0.6, 0.0, 0.9);
   st->nr_wall_colors = st->ncolors;
 
+  st->ncolors = MAX_COLORS;
   speedmine_color_ramp (st, st->bonus_gcs, st->bonus_colors, &st->ncolors,
                                                0.6, 0.9, 0.4, 1.0);
   st->nr_bonus_colors = st->ncolors;
@@ -1367,7 +1373,8 @@ init_colors (struct state *st)
 
   rgb_to_hsv (dark.red, dark.green, dark.blue, &h1, &s1, &v1);
   rgb_to_hsv (light.red, light.green, light.blue, &h2, &s2, &v2);
-  make_color_ramp (st->dpy, st->cmap, h1, s1, v1, h2, s2, v2,
+  make_color_ramp (st->screen, st->visual, st->cmap,
+                   h1, s1, v1, h2, s2, v2,
                                  st->ground_colors, &st->ncolors, False, True, False);
   st->nr_ground_colors = st->ncolors;
 
@@ -1377,10 +1384,12 @@ init_colors (struct state *st)
        st->ground_gcs[i] = XCreateGC (st->dpy, st->dbuf, flags, &gcv);
   }
 
+  st->ncolors = MAX_COLORS;
   speedmine_color_ramp (st, st->wall_gcs, st->wall_colors, &st->ncolors,
                                                0.0, 0.6, 0.0, 0.9);
   st->nr_wall_colors = st->ncolors;
 
+  st->ncolors = MAX_COLORS;
   speedmine_color_ramp (st, st->bonus_gcs, st->bonus_colors, &st->ncolors,
                                                0.6, 0.9, 0.4, 1.0);
   st->nr_bonus_colors = st->ncolors;
@@ -1426,6 +1435,8 @@ speedmine_init (Display *dpy, Window window)
 
   XGetWindowAttributes (st->dpy, st->window, &xgwa);
   st->cmap = xgwa.colormap;
+  st->visual = xgwa.visual;
+  st->screen = xgwa.screen;
   st->width = xgwa.width;
   st->height = xgwa.height;
 
@@ -1532,6 +1543,16 @@ static void
 speedmine_reshape (Display *dpy, Window window, void *closure, 
                  unsigned int w, unsigned int h)
 {
+  struct state *st = (struct state *) closure;
+  st->width = w;
+  st->height = h;
+  if (st->dbuf != st->window) {
+      XWindowAttributes xgwa;
+      XGetWindowAttributes (st->dpy, st->window, &xgwa);
+      XFreePixmap (dpy, st->dbuf);
+      st->dbuf = XCreatePixmap (st->dpy, st->window, 
+                                st->width, st->height, xgwa.depth);
+  }
 }
 
 static Bool
@@ -1622,7 +1643,7 @@ static XrmOptionDescRec speedmine_options [] = {
 };
 
 
-XSCREENSAVER_MODULE ("Speedmine", speedmine)
+XSCREENSAVER_MODULE ("SpeedMine", speedmine)
 
 /* vim: ts=4
  */