X-Git-Url: http://git.hungrycats.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=hacks%2Fpacman.c;h=ae1b029bc9b71fb767e64f4eca57795aeda07e7e;hb=39809ded547bdbb08207d3e514950425215b4410;hp=226a3cf333dff49bbe56ac90a4006578076a1f27;hpb=f8cf5ac7b2f53510f80a0eaf286a25298be17bfe;p=xscreensaver diff --git a/hacks/pacman.c b/hacks/pacman.c index 226a3cf3..ae1b029b 100644 --- a/hacks/pacman.c +++ b/hacks/pacman.c @@ -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,7 +55,7 @@ static const char sccsid[] = "@(#)pacman.c 5.00 2000/11/01 xlockmore"; # define UNIFORM_COLORS # define BRIGHT_COLORS -# define reshape_pacman 0 +# define release_pacman 0 # define pacman_handle_event 0 # include "xlockmore.h" /* in xscreensaver distribution */ # include @@ -108,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; @@ -115,6 +115,7 @@ static const struct { 0, 1}, { 1, 0}, { 0, -1}}; +#endif #ifdef DISABLE_INTERACTIVE ENTRYPOINT ModeSpecOpt pacman_opts = { @@ -131,7 +132,7 @@ static XrmOptionDescRec opts[] = { }; static argtype vars[] = { - {&trackmouse, "trackmouse", "TrackMouse", DEF_TRACKMOUSE, t_Bool} + {&pacman_trackmouse, "trackmouse", "TrackMouse", DEF_TRACKMOUSE, t_Bool} }; static OptionStruct desc[] = { @@ -148,26 +149,28 @@ 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; */ + "free_pacman", /* *free_name; */ &pacman_opts, /* *msopts */ 10000, 4, 1, 0, 64, 1.0, "", "Shows Pacman(tm)", 0, NULL }; #endif -Bool trackmouse; -pacmangamestruct *pacmangames = (pacmangamestruct *) NULL; +Bool pacman_trackmouse; +pacmangamestruct *pacman_games = (pacmangamestruct *) NULL; static void repopulate (ModeInfo * mi); static void drawlevel (ModeInfo * mi); -static void -free_pacman (Display * display, pacmangamestruct * pp) +ENTRYPOINT void +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) { @@ -201,10 +204,10 @@ free_pacman (Display * display, pacmangamestruct * pp) static void reset_level (ModeInfo * mi, int n, int pac_init) { - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; unsigned int ghost; - MI_CLEARWINDOW (mi); + XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi)); drawlevel (mi); pp->gamestate = GHOST_DANGER; @@ -251,9 +254,9 @@ reset_level (ModeInfo * mi, int n, int pac_init) pp->ghosts[ghost].delta.y = 0; pp->ghosts[ghost].flash_scared = False; pp->ghosts[ghost].wait_pos = False; - ghost_update (pp, &(pp->ghosts[ghost])); + pacman_ghost_update (pp, &(pp->ghosts[ghost])); } - pac_update (mi, pp, &(pp->pacman)); + pacman_update (mi, pp, &(pp->pacman)); } static int @@ -311,9 +314,9 @@ check_death (ModeInfo * mi, pacmangamestruct * pp) static void repopulate (ModeInfo * mi) { - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; pp->pacman.deaths = 0; - reset_level (mi, createnewlevel (pp), True); + reset_level (mi, pacman_createnewlevel (pp), True); check_death (mi, pp); } @@ -322,7 +325,7 @@ static void setwallcolor (ModeInfo * mi) { Display *display = MI_DISPLAY (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; if (MI_NPIXELS (mi) > 2) XSetForeground (display, pp->stippledGC, MI_PIXEL (mi, BLUE)); @@ -335,7 +338,7 @@ static void setdotcolor (ModeInfo * mi) { Display *display = MI_DISPLAY (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; XSetForeground (display, pp->stippledGC, MI_WHITE_PIXEL (mi)); } @@ -344,7 +347,7 @@ static void cleardotcolor (ModeInfo * mi) { Display *display = MI_DISPLAY (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; XSetForeground (display, pp->stippledGC, MI_BLACK_PIXEL (mi)); } @@ -355,7 +358,7 @@ draw_position (ModeInfo * mi, int x, int y, int color) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; XFontStruct *font = NULL; char *f_name = "-*-utopia-*-r-*-*-*-600-*-*-p-*-*-*"; char *s = NULL; @@ -378,7 +381,7 @@ draw_number (ModeInfo * mi, int x, int y, int num, int color) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; XFontStruct *font = NULL; char *f_name = "-*-utopia-*-r-*-*-*-600-*-*-p-*-*-*"; char *s = NULL; @@ -406,7 +409,7 @@ draw_grid (ModeInfo * mi) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; int h = MI_HEIGHT (mi); int w = MI_WIDTH (mi); int y = 0; @@ -430,7 +433,7 @@ draw_string (ModeInfo * mi, int x, int y, char *s, int color) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; XFontStruct *font = NULL; char *f_name = "-*-utopia-*-r-*-*-*-600-*-*-p-*-*-*"; @@ -482,7 +485,7 @@ print_ghost_stats (ModeInfo *mi, ghoststruct *g , int ghost_num) static void print_pac_stats ( ModeInfo *mi, pacmanstruct *pac ) { - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; char s[1024]; sprintf (s, "Pacman, Deaths: %d", pac->deaths ); switch ( pac->aistate ){ @@ -516,7 +519,7 @@ print_pac_stats ( ModeInfo *mi, pacmanstruct *pac ) static void dot_rc_to_pixel (ModeInfo * mi, int *x, int *y) { - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; *x = (pp->xs * *x) + (pp->xs / 2) - (pp->xs > 32 ? (pp->xs / 16) : 1) + pp->xb; *y = (pp->ys * *y) + @@ -529,7 +532,7 @@ dot_rc_to_pixel (ModeInfo * mi, int *x, int *y) static void dot_width_height (ModeInfo *mi, int *w, int *h) { - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; if (pp->xs > 32){ *w = *h = (pp->xs / 32 ); }else { @@ -603,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); @@ -713,7 +716,7 @@ drawlevelblock (ModeInfo * mi, pacmangamestruct * pp, static void drawlevel (ModeInfo * mi) { - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; unsigned int x, y; for (y = 0; y < LEVHEIGHT; y++) @@ -745,7 +748,7 @@ draw_pacman_sprite (ModeInfo * mi) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; unsigned int dir = 0; int old_mask_dir = 0; int old_mask_mouth = 0; @@ -851,7 +854,7 @@ draw_ghost_sprite (ModeInfo * mi, const unsigned ghost) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; #define MAX_WAG_COUNT 50 unsigned int dir = 0; unsigned int fs = 0; /*flash scared*/ @@ -954,17 +957,13 @@ draw_pacman_sprite (ModeInfo * mi) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; - unsigned int dir; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; 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) { @@ -1003,7 +1002,7 @@ draw_ghost_sprite (ModeInfo * mi, const unsigned ghost) { Display *display = MI_DISPLAY (mi); Window window = MI_WINDOW (mi); - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; pp->ghosts[ghost].cf = pp->ghosts[ghost].col * pp->xs + pp->ghosts[ghost].delta.x * @@ -1059,7 +1058,7 @@ ghost_over (ModeInfo * mi, int x, int y) { int ghost = 0; int ret = False; - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; dot_rc_to_pixel (mi, &x, &y); for (ghost = 0; ghost < pp->nghosts; ghost++) { if ((pp->ghosts[ghost].cf <= x @@ -1078,11 +1077,11 @@ static void flash_bonus_dots (ModeInfo * mi) { #define MAX_FLASH_COUNT 25 - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; int i, x, y; for (i = 0; i < NUM_BONUS_DOTS; i++) { - if (!bonus_dot_eaten (pp, i)) { - bonus_dot_pos (pp, i, &x, &y); + if (!pacman_bonus_dot_eaten (pp, i)) { + pacman_bonus_dot_pos (pp, i, &x, &y); if (ghost_over (mi, x, y)) continue; if (pp->bd_on) @@ -1105,10 +1104,10 @@ ate_bonus_dot (ModeInfo * mi) */ unsigned int ret = 0; int idx = 0; - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; - if (is_bonus_dot (pp, pp->pacman.col, pp->pacman.row, &idx)) { - ret = !bonus_dot_eaten (pp, idx); - eat_bonus_dot (pp, idx); + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; + if (pacman_is_bonus_dot (pp, pp->pacman.col, pp->pacman.row, &idx)) { + ret = !pacman_bonus_dot_eaten (pp, idx); + pacman_eat_bonus_dot (pp, idx); } return ret; } @@ -1117,7 +1116,7 @@ static void ghost_scared (ModeInfo * mi) { unsigned int ghost; - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; for (ghost = 0; ghost < pp->nghosts; ghost++) { if (pp->ghosts[ghost].aistate == goingin || pp->ghosts[ghost].aistate == goingout || @@ -1133,7 +1132,7 @@ static void ghost_not_scared (ModeInfo * mi) { unsigned int ghost; - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; for (ghost = 0; ghost < pp->nghosts; ghost++){ if (pp->ghosts[ghost].aistate == goingin || pp->ghosts[ghost].aistate == goingout || @@ -1149,7 +1148,7 @@ static void ghost_flash_scared (ModeInfo * mi) { unsigned int ghost; - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; for (ghost = 0; ghost < pp->nghosts; ghost++) pp->ghosts[ghost].flash_scared = !pp->ghosts[ghost].flash_scared; } @@ -1162,7 +1161,7 @@ pacman_tick (ModeInfo * mi) #define START_FLASH 200 #define FLASH_COUNT 25 - pacmangamestruct *pp = &pacmangames[MI_SCREEN (mi)]; + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; unsigned int ghost; #if 0 draw_grid (mi); @@ -1264,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; @@ -1480,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; @@ -1491,13 +1490,8 @@ init_pacman (ModeInfo * mi) int dir, mouth; #endif - if (pacmangames == NULL) { - if ((pacmangames = (pacmangamestruct *) - calloc ((size_t) MI_NUM_SCREENS (mi), - sizeof (pacmangamestruct))) == NULL) - return; - } - pp = &pacmangames[MI_SCREEN (mi)]; + MI_INIT (mi, pacman_games); + pp = &pacman_games[MI_SCREEN (mi)]; pp->width = (unsigned short) MI_WIDTH (mi); pp->height = (unsigned short) MI_HEIGHT (mi); @@ -1525,9 +1519,15 @@ init_pacman (ModeInfo * mi) if (size == 0 || MINGRIDSIZE * size > (int) pp->width || MINGRIDSIZE * size > (int) pp->height) { - - pp->ys = pp->xs = MAX (MIN (pp->width / LEVWIDTH, - pp->height / LEVHEIGHT), 1); + double scale = MIN (pp->width / LEVWIDTH, pp->height / LEVHEIGHT); + + if (pp->width > pp->height * 5 || /* weird window aspect ratio */ + pp->height > pp->width * 5) + scale = 0.8 * (pp->width / pp->height + ? pp->width / (double) pp->height + : pp->height / (double) pp->width); + pp->ys = MAX (scale, 1); + pp->xs = pp->ys; } else { if (size < -MINSIZE) @@ -1545,6 +1545,7 @@ init_pacman (ModeInfo * mi) pp->xs = pp->ys; } + pp->wallwidth = (unsigned int) (pp->xs + pp->ys) >> 4; if (pp->wallwidth < 1) pp->wallwidth = 1; @@ -1566,12 +1567,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 @@ -1582,7 +1583,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; } @@ -1590,7 +1591,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; } @@ -1599,7 +1600,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; } @@ -1637,7 +1638,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; @@ -1645,7 +1646,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; @@ -1655,7 +1656,7 @@ init_pacman (ModeInfo * mi) GCForeground | GCBackground, &gcv)) == None) { XFreeGC (display, fg_gc); - free_pacman (display, pp); + free_pacman (mi); return; } XFillRectangle (display, @@ -1692,13 +1693,13 @@ init_pacman (ModeInfo * mi) if ((pp->ghosts = (ghoststruct *) calloc ((size_t) pp->nghosts, sizeof (ghoststruct))) == NULL) { - free_pacman (display, pp); + free_pacman (mi); return; } pp->pacman.mouthstage = MAXMOUTH - 1; - MI_CLEARWINDOW (mi); + XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi)); repopulate (mi); } @@ -1710,9 +1711,9 @@ draw_pacman (ModeInfo * mi) unsigned int g; pacmangamestruct *pp; - if (pacmangames == NULL) + if (pacman_games == NULL) return; - pp = &pacmangames[MI_SCREEN (mi)]; + pp = &pacman_games[MI_SCREEN (mi)]; if (pp->ghosts == NULL) return; @@ -1722,7 +1723,7 @@ draw_pacman (ModeInfo * mi) pp->pacman.delta.y += pp->pacman.err.y != 0 ? pp->incy : 0; if (pp->pacman.delta.x >= pp->xs && pp->pacman.delta.y >= pp->ys) { - pac_update (mi, pp, &(pp->pacman)); + pacman_update (mi, pp, &(pp->pacman)); check_death (mi, pp); pp->pacman.delta.x = pp->incx; pp->pacman.delta.y = pp->incy; @@ -1741,7 +1742,7 @@ draw_pacman (ModeInfo * mi) if (pp->ghosts[g].delta.x >= pp->xs && pp->ghosts[g].delta.y >= pp->ys) { - ghost_update (pp, &(pp->ghosts[g])); + pacman_ghost_update (pp, &(pp->ghosts[g])); pp->ghosts[g].delta.x = pp->incx; pp->ghosts[g].delta.y = pp->incy; } @@ -1754,26 +1755,27 @@ draw_pacman (ModeInfo * mi) pacman_tick (mi); } -/* Releases resources. */ +#ifndef STANDALONE +/* Refresh current level. */ ENTRYPOINT void -release_pacman (ModeInfo * mi) +refresh_pacman (ModeInfo * mi) { - if (pacmangames != NULL) { - int screen; - - for (screen = 0; screen < MI_NUM_SCREENS (mi); screen++) - free_pacman (MI_DISPLAY (mi), &pacmangames[screen]); - free (pacmangames); - pacmangames = (pacmangamestruct *) NULL; - } + drawlevel (mi); + pacman_tick (mi); } +#endif -/* Refresh current level. */ ENTRYPOINT void -refresh_pacman (ModeInfo * mi) +reshape_pacman(ModeInfo * mi, int width, int height) { + pacmangamestruct *pp = &pacman_games[MI_SCREEN (mi)]; + pp->width = width; + pp->height = height; + pp->xb = (pp->width - pp->ncols * pp->xs) >> 1; + pp->yb = (pp->height - pp->nrows * pp->ys) >> 1; + XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi)); + /* repopulate (mi); */ drawlevel (mi); - pacman_tick (mi); } #ifndef STANDALONE @@ -1781,7 +1783,7 @@ refresh_pacman (ModeInfo * mi) ENTRYPOINT void change_pacman (ModeInfo * mi) { - MI_CLEARWINDOW (mi); + XClearWindow (MI_DISPLAY(mi), MI_WINDOW(mi)); repopulate (mi); } #endif /* !STANDALONE */