/* $Id: loadbmp.c,v 1.1 1996/05/04 19:08:18 yoneyama Exp yoneyama $ */ /************************************************************************ * SameGame for X Window (sxsame) * * Copyleft (c) 1994-1996 Software Research Academy * ************************************************************************/ #include #include #include #include #include #include "sxsame.h" int getpixel( #if NeedFunctionPrototypes Display *d, Colormap cmap, int c1 #endif ); unsigned short fgetushort( #if NeedFunctionPrototypes FILE *fp #endif ); unsigned long fgetulong( #if NeedFunctionPrototypes FILE *fp #endif ); static unsigned char *tmpcols; static int pcolf; int LoadBMPSameData(d, cmap, game, bmpname) /* return: number of colors */ Display *d; Colormap cmap; SameGame *game; char *bmpname; { FILE *fp; int c, c1,c2; int width, height, planes, ncol; int *col; char fname[MAXPATHLEN]; Pixmap w; GC gc; int i, j, k; if (MAXPATHLEN <= strlen(bmpname) ) { fprintf(stderr,"Error: BMP file name too long.\n"); return -1; } strcpy(fname,bmpname); fp = fopen(fname,"rb"); if(fp == NULL) { if (MAXPATHLEN <= snprintf(fname, MAXPATHLEN, "%s/%s", SAME_DIR, bmpname)) { fprintf(stderr,"Error: BMP file name too long.\n"); return -1; } fp = fopen(fname, "r"); if(fp == NULL) { fprintf(stderr,"Error: can't open BMP file '%s'.\n", bmpname); return(-1); } } c1 = fgetc(fp); c2 = fgetc(fp); if(c1 != 'B' && c2 != 'M') { fprintf(stderr,"Error: not BMP format file '%s'.\n", fname); return(0); } fgetulong(fp); fgetushort(fp); fgetushort(fp); fgetulong(fp); fgetulong(fp); width=fgetulong(fp); height=fgetulong(fp); planes=fgetushort(fp); if(width != 32*6 || height != 64 || planes != 1) { fprintf(stderr, "Error: not supported data format or size.\n"); return(-1); } ncol = 1 << fgetushort(fp); fgetulong(fp); fgetulong(fp); fgetulong(fp); fgetulong(fp); fgetulong(fp); fgetulong(fp); tmpcols = (unsigned char *)malloc(ncol*4); if(tmpcols == NULL) { fprintf(stderr,"Error: can't allocate memory.\n"); return(-1); } col = (int *)malloc(ncol*sizeof(int)); if(col == NULL) { fprintf(stderr,"Error: can't allocate memory.\n"); return(-1); } for(i=0;i> (7-k))&1; c2 = getpixel(d, cmap, c1); if(c2 == -1) return(-1); XSetForeground(d,gc,c2); XDrawPoint(d,w,gc,j + k, height-i-1); } j += 7; } else if(ncol > 16) { c2 = getpixel(d, cmap, c); if(c2 == -1) return(-1); XSetForeground(d,gc,c2); XDrawPoint(d,w,gc,j,height - i - 1); } else { c1 = (c >> 4) & 0x0f; c2 = getpixel(d, cmap, c1); if(c2 == -1) return(-1); XSetForeground(d,gc,c2); XDrawPoint(d,w,gc,j,height - i - 1); j++; c1 = c & 0x0f; c2 = getpixel(d, cmap, c1); if(c2 == -1) return(-1); XSetForeground(d,gc,c2); XDrawPoint(d,w,gc,j,height-i-1); } } } fclose(fp); free(tmpcols); free(col); XSetFunction(d, gc, GXcopy); game->bit_width = 32; game->bit_height = 32; game->bit_box = 0; game->bit_pat[0] = 1; game->bit_pat[1] = 1; game->bit_pat[2] = 0; for(i=0;i<5;i++) { game->bits_xpm[0][i] = XCreatePixmap(d, RootWindow(d, 0), 32, 32, DefaultDepth(d, 0)); XCopyArea(d, w, game->bits_xpm[0][i], gc, 32*(i+1), 0, 32, 32, 0, 0); } for(i=0;i<5;i++) { game->bits_xpm[1][i] = XCreatePixmap(d, RootWindow(d, 0), 32, 32, DefaultDepth(d, 0)); XCopyArea(d, w, game->bits_xpm[1][i], gc, 32*(i+1), 32, 32, 32, 0, 0); } game->bg_xpm = XCreatePixmap(d, RootWindow(d, 0), 32, 32, DefaultDepth(d, 0)); XCopyArea(d, w, game->bg_xpm, gc, 0, 0, 32, 32, 0, 0); /* game->frame_xpm = XCreatePixmap(d, RootWindow(d, 0), 32, 32, DefaultDepth(d, 0)); XCopyArea(d, w, game->frame_xpm, gc, 0, 32, 32, 32, 0, 0); */ return(ncol); } int getpixel(d,cmap,c1) Display *d; Colormap cmap; int c1; { XColor acolor,tmpcolor; int c2; int i; unsigned long tstd, tmpd = 0xffffffff; unsigned long tmppixel = 0; if(tmpcols[c1*4+3] != 0) { c2 = tmpcols[c1*4]*256 + tmpcols[c1*4+1]; } else { acolor.blue = tmpcols[c1*4]<<8; acolor.green = tmpcols[c1*4+1]<<8; acolor.red = tmpcols[c1*4+2]<<8; if(XAllocColor(d, cmap, &acolor) == False) { if(pcolf == 0) { fprintf(stderr,"Warning: can't allocate color.\n"); fprintf(stderr,"(use similar colors...)\n"); } pcolf = 1; for(i=0;i<(1<>8)) * (tmpcols[c1*4] - (tmpcolor.blue>>8)) + (tmpcols[c1*4+1] - (tmpcolor.green>>8)) * (tmpcols[c1*4+1] - (tmpcolor.green>>8)) + (tmpcols[c1*4+2] - (tmpcolor.red>>8)) * (tmpcols[c1*4+2] - (tmpcolor.red>>8)); if(tstd < tmpd) { tmpd = tstd; tmppixel = i; if(tstd == 0) break; } } c2 = tmppixel; tmpcols[c1*4+3] = 1; tmpcols[c1*4] = (c2>>8)&0xff; tmpcols[c1*4+1] = c2 & 0xff; return(tmppixel); } c2 = acolor.pixel; tmpcols[c1*4+3] = 1; tmpcols[c1*4] = (c2>>8)&0xff; tmpcols[c1*4+1] = c2 & 0xff; } return(c2); } unsigned short fgetushort(fp) FILE *fp; { return((unsigned short)(fgetc(fp) + fgetc(fp)*256)); } unsigned long fgetulong(fp) FILE *fp; { unsigned long lo; lo = (unsigned long)fgetc(fp); lo += ((unsigned long)fgetc(fp))*256L; lo += ((unsigned long)fgetc(fp))*65536L; lo += ((unsigned long)fgetc(fp))*65536L*256L; return(lo); }