From http://www.jwz.org/xscreensaver/xscreensaver-5.37.tar.gz
[xscreensaver] / hacks / pacman.c
index 23fb3a1d4d0b743a43fb2e82e5abf0a0649b2a52..de3398ef93635cb6e85e7cd3c139f3869cd7af84 100644 (file)
@@ -1,9 +1,8 @@
 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
 /* pacman --- Mr. Pacman and his ghost friends */
 
-#if !defined( lint ) && !defined( SABER )
+#if 0
 static const char sccsid[] = "@(#)pacman.c     5.00 2000/11/01 xlockmore";
-
 #endif
 
 /*-
@@ -56,6 +55,7 @@ static const char sccsid[] = "@(#)pacman.c    5.00 2000/11/01 xlockmore";
 
 #      define UNIFORM_COLORS
 #      define BRIGHT_COLORS
+#      define release_pacman 0
 #   define pacman_handle_event 0
 #      include "xlockmore.h"   /* in xscreensaver distribution */
 #   include <assert.h>
@@ -107,6 +107,7 @@ static const char sccsid[] = "@(#)pacman.c  5.00 2000/11/01 xlockmore";
 # include "images/pacman/pacman-ds8.xpm"
 #endif
 
+#if 0
 static const struct
 {
     int dx, dy;
@@ -114,6 +115,7 @@ static const struct
                        {  0, 1},
                        {  1, 0},
                        {  0, -1}};
+#endif
 
 #ifdef DISABLE_INTERACTIVE
 ENTRYPOINT ModeSpecOpt pacman_opts = {
@@ -147,7 +149,7 @@ ModStruct pacman_description = {
     "pacman",                   /* *cmdline_arg; */
     "init_pacman",              /* *init_name; */
     "draw_pacman",              /* *callback_name; */
-    "release_pacman",           /* *release_name; */
+    (char *) NULL,              /* *release_name; */
     "refresh_pacman",           /* *refresh_name; */
     "change_pacman",            /* *change_name; */
     (char *) NULL,              /* *unused_name; */
@@ -165,8 +167,10 @@ static void drawlevel (ModeInfo * mi);
 
 
 static void
-free_pacman (Display * display, pacmangamestruct * pp)
+free_pacman (ModeInfo * mi)
 {
+    Display * display = MI_DISPLAY (mi);
+    pacmangamestruct * pp = &pacman_games[MI_SCREEN (mi)];
     int dir, mouth, i, j, k;
 
     if (pp->ghosts != NULL) {
@@ -602,9 +606,9 @@ drawlevelblock (ModeInfo * mi, pacmangamestruct * pp,
     if (pp->ys % 2 == 1)
         dy = -1;
 
-#ifndef HAVE_COCOA
+#ifndef HAVE_JWXYZ
     XSetFillStyle (display, pp->stippledGC, FillSolid);
-#endif /* !HAVE_COCOA */
+#endif /* !HAVE_JWXYZ */
     XSetLineAttributes (display, pp->stippledGC, pp->wallwidth,
                         LineSolid, CapRound, JoinMiter);
 
@@ -954,16 +958,12 @@ draw_pacman_sprite (ModeInfo * mi)
     Display *display = MI_DISPLAY (mi);
     Window window = MI_WINDOW (mi);
     pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)];
-    unsigned int dir;
 
     pp->pacman.cf = pp->pacman.col * pp->xs + pp->pacman.delta.x *
         pp->pacman.cfactor + pp->xb + pp->spritedx;
     pp->pacman.rf = pp->pacman.row * pp->ys + pp->pacman.delta.y *
         pp->pacman.rfactor + pp->yb + pp->spritedy;
 
-    dir = (ABS (pp->pacman.cfactor) * (2 - pp->pacman.cfactor) +
-           ABS (pp->pacman.rfactor) * (1 + pp->pacman.rfactor)) % 4;
-
     XSetForeground (display, pp->stippledGC, MI_BLACK_PIXEL (mi));
     if (pp->pacman.oldcf != NOWHERE && pp->pacman.oldrf != NOWHERE) {
 
@@ -1263,7 +1263,7 @@ scale_pixmap (Display ** dpy, GC gc, Pixmap source, int dwidth, int dheight)
     end = dheight * yscale;
     /* Scale height of temp into dest pixmap */
     for (i = 0; i <= end; i += yscale)
-        XCopyArea (*dpy, temp, dest, gc, 0, i, swidth, 1, 0, j++);
+        XCopyArea (*dpy, temp, dest, gc, 0, i, dwidth, 1, 0, j++);
 
     XFreePixmap (*dpy, temp);
     return (Pixmap) dest;
@@ -1479,7 +1479,7 @@ init_pacman (ModeInfo * mi)
 {
     Display *display = MI_DISPLAY (mi);
     Window window = MI_WINDOW (mi);
-    int size = MI_SIZE (mi);
+    long size = MI_SIZE (mi);
     pacmangamestruct *pp;
     XGCValues gcv;
     int i, j, k;
@@ -1490,12 +1490,7 @@ init_pacman (ModeInfo * mi)
     int dir, mouth;
 #endif
 
-    if (pacman_games == NULL) {
-        if ((pacman_games = (pacmangamestruct *)
-             calloc ((size_t) MI_NUM_SCREENS (mi),
-                     sizeof (pacmangamestruct))) == NULL)
-            return;
-    }
+    MI_INIT (mi, pacman_games, free_pacman);
     pp = &pacman_games[MI_SCREEN (mi)];
 
     pp->width = (unsigned short) MI_WIDTH (mi);
@@ -1565,12 +1560,12 @@ init_pacman (ModeInfo * mi)
         if ((pp->stippledGC = XCreateGC (display, window,
                                          GCForeground | GCBackground,
                                          &gcv)) == None) {
-            free_pacman (display, pp);
+            free_pacman (mi);
             return;
         }
     }
 
-#ifdef HAVE_COCOA
+#ifdef HAVE_JWXYZ
     jwxyz_XSetAntiAliasing (display, pp->stippledGC, False);
 #endif
 
@@ -1581,7 +1576,7 @@ init_pacman (ModeInfo * mi)
     if ((pp->ghostPixmap[0][0][0] = XCreatePixmap (display, window,
                                                    pp->spritexs, pp->spriteys,
                                                    1)) == None) {
-        free_pacman (display, pp);
+        free_pacman (mi);
         return;
     }
 
@@ -1589,7 +1584,7 @@ init_pacman (ModeInfo * mi)
     gcv.background = 1;
     if ((bg_gc = XCreateGC (display, pp->ghostPixmap[0][0][0],
                             GCForeground | GCBackground, &gcv)) == None) {
-        free_pacman (display, pp);
+        free_pacman (mi);
         return;
     }
 
@@ -1598,7 +1593,7 @@ init_pacman (ModeInfo * mi)
     if ((fg_gc = XCreateGC (display, pp->ghostPixmap[0][0][0],
                             GCForeground | GCBackground, &gcv)) == None) {
         XFreeGC (display, bg_gc);
-        free_pacman (display, pp);
+        free_pacman (mi);
         return;
     }
 
@@ -1636,7 +1631,7 @@ init_pacman (ModeInfo * mi)
             if ((pp->pacmanPixmap[dir][mouth] =
                  XCreatePixmap (display, MI_WINDOW (mi), pp->spritexs,
                                 pp->spriteys, 1)) == None) {
-                free_pacman (display, pp);
+                free_pacman (mi);
                 return;
             }
             gcv.foreground = 1;
@@ -1644,7 +1639,7 @@ init_pacman (ModeInfo * mi)
             if ((fg_gc = XCreateGC (display, pp->pacmanPixmap[dir][mouth],
                                     GCForeground | GCBackground,
                                     &gcv)) == None) {
-                free_pacman (display, pp);
+                free_pacman (mi);
                 return;
             }
             gcv.foreground = 0;
@@ -1654,7 +1649,7 @@ init_pacman (ModeInfo * mi)
                                     GCForeground |
                                     GCBackground, &gcv)) == None) {
                 XFreeGC (display, fg_gc);
-                free_pacman (display, pp);
+                free_pacman (mi);
                 return;
             }
             XFillRectangle (display,
@@ -1691,7 +1686,7 @@ init_pacman (ModeInfo * mi)
         if ((pp->ghosts = (ghoststruct *) calloc ((size_t) pp->nghosts,
                                                   sizeof (ghoststruct))) ==
             NULL) {
-            free_pacman (display, pp);
+            free_pacman (mi);
             return;
         }
 
@@ -1753,20 +1748,6 @@ draw_pacman (ModeInfo * mi)
     pacman_tick (mi);
 }
 
-/* Releases resources. */
-ENTRYPOINT void
-release_pacman (ModeInfo * mi)
-{
-    if (pacman_games != NULL) {
-        int screen;
-
-        for (screen = 0; screen < MI_NUM_SCREENS (mi); screen++)
-            free_pacman (MI_DISPLAY (mi), &pacman_games[screen]);
-        free (pacman_games);
-        pacman_games = (pacmangamestruct *) NULL;
-    }
-}
-
 /* Refresh current level. */
 ENTRYPOINT void
 refresh_pacman (ModeInfo * mi)
@@ -1779,8 +1760,10 @@ ENTRYPOINT void
 reshape_pacman(ModeInfo * mi, int width, int height)
 {
     pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)];
-    pp->width = width;
+    pp->width  = width;
     pp->height = height;
+    pp->xb = (pp->width  - pp->ncols * pp->xs) >> 1;
+    pp->yb = (pp->height - pp->nrows * pp->ys) >> 1;
     MI_CLEARWINDOW (mi);
     /* repopulate (mi); */
     drawlevel (mi);