1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
11 #include <X11/Intrinsic.h>
12 #include <X11/keysymdef.h>
16 #include <X11/Xutil.h>
17 #include <X11/Xatom.h>
18 #include <X11/keysym.h>
19 #include <X11/cursorfont.h>
21 #include <sys/types.h>
30 #include "../libgame/platform.h"
31 #include "../libgame/libgame.h"
38 #if defined(TARGET_X11)
79 KeyCode northKeyCode[3];
80 KeyCode eastKeyCode[3];
81 KeyCode southKeyCode[3];
82 KeyCode westKeyCode[3];
83 KeyCode fireKeyCode[3];
84 KeyCode escKeyCode[1];
86 char play[SAMPLE_MAX];
88 static int sound_pid = -1;
89 int sound_pipe[2] = { -1, -1 }; /* for communication */
90 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
91 long sound_length[SAMPLE_MAX]; /* length of sound data */
93 static Screen *defaultScreen;
94 static Visual *defaultVisual;
95 static Colormap defaultColourmap;
96 static Window defaultRootWindow;
97 static unsigned int screenDepth;
98 static unsigned int screenWidth;
99 static unsigned int screenHeight;
100 static unsigned long screenBlackPixel;
101 static unsigned long screenWhitePixel;
104 static XSizeHints sizeHints;
105 static XSetWindowAttributes setWindowAttributes;
106 static XWMHints wmHints;
107 static XVisualInfo visualInfo;
109 static XGCValues gcValues;
111 static Colormap privateColourmap;
112 static Cursor cursor;
113 static XColor *privateColours;
114 static unsigned char *privateFlags;
115 static int privateNumColours;
117 static XColor redColour;
118 static XColor whiteColour;
123 static Bitmap *pcxBitmaps[4];
124 static Bitmap *pcxBitmapsX2[4];
128 static Pixmap xpmPixmaps[4];
129 static Pixmap xpmBitmaps[4];
130 static XpmAttributes xpmAttributes[4];
131 static int xpmGot[4];
134 static int xpmAllocColourFunc(Display *, Colormap, char *, XColor *, void *);
135 static int xpmFreeColoursFunc(Display *, Colormap, unsigned long *, int, void *);
137 static KeyCode keycodes[16];
140 static const char *xpmNames[4] =
150 static const char *pcxNames[4] =
159 static const int xpmCloseness[4] = { 10000, 10000, 40000, 50000 };
160 static const KeySym keysyms[16] =
162 XK_Up, XK_KP_Up, XK_r, /* north */
163 XK_Right, XK_KP_Right, XK_g, /* east */
164 XK_Down, XK_KP_Down, XK_f, /* south */
165 XK_Left, XK_KP_Left, XK_d, /* west */
166 XK_Shift_L, XK_Control_R, XK_space, /* fire */
167 XK_Escape /* escape */
169 static const char *sound_names[SAMPLE_MAX] =
171 "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
172 "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
173 "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
174 "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
175 "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
176 "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
179 static const int sound_volume[SAMPLE_MAX] =
181 20,100,100,100,100,20,20,100,100,100,
182 50,100,100,100,100,100,100,100,100,100,
183 100,20,100,100,100,100,100,20,100,100,
188 static void xdebug(char *msg)
191 XSync(display, False);
192 printf("EM DEBUG: %s\n", msg);
199 char name[MAXNAME+2];
207 display = XOpenDisplay(arg_display);
210 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
211 XDisplayName(arg_display), "failed to open display",
217 defaultScreen = DefaultScreenOfDisplay(display);
218 defaultVisual = DefaultVisualOfScreen(defaultScreen);
219 defaultColourmap = DefaultColormapOfScreen(defaultScreen);
220 defaultRootWindow = RootWindowOfScreen(defaultScreen);
221 screenDepth = DefaultDepthOfScreen(defaultScreen);
222 screenWidth = WidthOfScreen(defaultScreen);
223 screenHeight = HeightOfScreen(defaultScreen);
224 screenBlackPixel = BlackPixelOfScreen(defaultScreen);
225 screenWhitePixel = WhitePixelOfScreen(defaultScreen);
230 visualInfo.visualid = XVisualIDFromVisual(defaultVisual);
231 dummyptr = XGetVisualInfo(display, VisualIDMask, &visualInfo, &dummyint);
234 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
235 XDisplayName(arg_display), "failed to get visual info",
240 memcpy(&visualInfo, dummyptr, sizeof(visualInfo));
243 if (visualInfo.class != PseudoColor)
245 fprintf(stderr, "%s: \"%s\": %s\n", progname, XDisplayName(arg_display),
246 "private colourmap only supported for pseudocolour display");
250 privateColourmap = XCreateColormap(display, defaultRootWindow,
251 defaultVisual, AllocAll);
252 if (privateColourmap == 0)
254 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
255 XDisplayName(arg_display), "failed to create colourmap",
260 privateNumColours = visualInfo.colormap_size;
262 privateColours = malloc(privateNumColours * sizeof(XColor));
263 if (privateColours == 0)
265 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed",
266 privateNumColours * sizeof(XColor), strerror(errno));
270 for (dummyint = 0; dummyint < privateNumColours; dummyint++)
271 privateColours[dummyint].pixel = dummyint;
273 XQueryColors(display, defaultColourmap, privateColours, privateNumColours);
274 XStoreColors(display, privateColourmap, privateColours, privateNumColours);
276 privateFlags = malloc(privateNumColours);
277 if (privateFlags == 0)
279 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed",
280 privateNumColours, strerror(errno));
284 memset(privateFlags, 0, privateNumColours);
286 /* first two entries (black and white) are already allocated */
291 sizeHints.flags = PSize | PMinSize | PMaxSize;
292 sizeHints.width = 20 * TILEX;
293 sizeHints.height = 12 * TILEY + SCOREY;
294 sizeHints.min_width = sizeHints.max_width = sizeHints.width;
295 sizeHints.min_height = sizeHints.max_height = sizeHints.height;
299 dummyint = XWMGeometry(display, XScreenNumberOfScreen(defaultScreen),
300 arg_geometry, 0, 2, &sizeHints, &sizeHints.x,
301 &sizeHints.y, &dummyint, &dummyint,
302 &sizeHints.win_gravity);
303 if (dummyint & (XValue | YValue))
304 sizeHints.flags |= USPosition | PWinGravity;
307 xwindow = XCreateWindow(display, defaultRootWindow, sizeHints.x, sizeHints.y,
308 sizeHints.width, sizeHints.height, 2, screenDepth,
309 InputOutput, CopyFromParent, 0, 0);
312 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
313 XDisplayName(arg_display), "failed to open window",
318 setWindowAttributes.background_pixel = screenBlackPixel;
319 setWindowAttributes.border_pixel = screenWhitePixel;
320 setWindowAttributes.backing_store = NotUseful;
321 setWindowAttributes.override_redirect = False;
322 setWindowAttributes.event_mask =
323 KeyPressMask | EnterWindowMask | LeaveWindowMask | ExposureMask;
324 setWindowAttributes.colormap =
325 privateColourmap ? privateColourmap : defaultColourmap;
326 XChangeWindowAttributes(display, xwindow,
327 CWBackPixel | CWBorderPixel | CWBackingStore |
328 CWOverrideRedirect | CWEventMask | CWColormap,
329 &setWindowAttributes);
331 XStoreName(display, xwindow, "Emerald Mine");
333 wmHints.flags = InputHint | StateHint;
334 wmHints.input = True;
335 wmHints.initial_state = NormalState;
336 XSetWMHints(display, xwindow, &wmHints);
338 XSetWMNormalHints(display, xwindow, &sizeHints);
340 deleteAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
341 XSetWMProtocols(display, xwindow, &deleteAtom, 1);
343 cursor = XCreateFontCursor(display, XC_trek);
344 if (cursor) XDefineCursor(display, xwindow, cursor);
346 XMapWindow(display, xwindow);
350 for (i = 0; i < 4; i++)
355 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_GFX_DIR,
360 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, xpmNames[i]);
363 if (name[MAXNAME]) snprintf_overflow("read graphics/ files");
365 xpmAttributes[i].valuemask =
366 XpmColormap | XpmReturnAllocPixels | XpmExactColors | XpmCloseness |
367 XpmAllocColor | XpmFreeColors;
368 xpmAttributes[i].colormap =
369 privateColourmap ? privateColourmap : defaultColourmap;
370 xpmAttributes[i].exactColors = False;
371 xpmAttributes[i].closeness = xpmCloseness[i];
372 xpmAttributes[i].alloc_color = xpmAllocColourFunc;
373 xpmAttributes[i].free_colors = xpmFreeColoursFunc;
374 dummyint = XpmReadFileToPixmap(display, xwindow, name, &xpmPixmaps[i],
375 &xpmBitmaps[i], &xpmAttributes[i]);
378 fprintf(stderr, "%s: \"%s\": \"%s\": %s: %s: %s\n", progname,
379 XDisplayName(arg_display), name, "failed to read xpm",
380 XpmGetErrorString(dummyint), strerror(errno));
388 for (i = 0; i < 4; i++)
391 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, pcxNames[i]);
394 snprintf_overflow("read graphics/ files");
396 if ((pcxBitmaps[i] = LoadImage(name)) == NULL)
398 printf("::: LoadImage() failed for file '%s'\n", name);
403 objBitmap = pcxBitmaps[0];
404 botBitmap = pcxBitmaps[1];
405 sprBitmap = pcxBitmaps[2];
406 ttlBitmap = pcxBitmaps[3];
409 objPixmap = xpmPixmaps[0];
410 botPixmap = xpmPixmaps[1];
411 sprPixmap = xpmPixmaps[2];
412 ttlPixmap = xpmPixmaps[3];
413 objmaskBitmap = xpmBitmaps[0];
414 botmaskBitmap = xpmBitmaps[1];
415 sprmaskBitmap = xpmBitmaps[2];
416 ttlmaskBitmap = xpmBitmaps[3];
418 objPixmap = pcxBitmaps[0]->drawable;
419 botPixmap = pcxBitmaps[1]->drawable;
420 sprPixmap = pcxBitmaps[2]->drawable;
421 ttlPixmap = pcxBitmaps[3]->drawable;
422 objmaskBitmap = pcxBitmaps[0]->clip_mask;
423 botmaskBitmap = pcxBitmaps[1]->clip_mask;
424 sprmaskBitmap = pcxBitmaps[2]->clip_mask;
425 ttlmaskBitmap = pcxBitmaps[3]->clip_mask;
429 for (i = 0; i < 4; i++)
433 if ((clip_mask = XCreatePixmap(display, window->drawable,
434 pcxBitmaps[i]->width * 2,
435 pcxBitmaps[i]->height * 2, 1))
438 printf("::: cannot create clip mask");
444 XGCValues clip_gc_values;
445 unsigned long clip_gc_valuemask;
448 clip_gc_values.graphics_exposures = False;
449 clip_gc_valuemask = GCGraphicsExposures;
450 if ((gc = XCreateGC(display, clip_mask,
451 clip_gc_valuemask, &clip_gc_values)) == None)
453 printf("X CreateGC failed\n");
457 XFillRectangle(display, clip_mask, gc, 0, 0,
458 pcxBitmaps[i]->width * 2,
459 pcxBitmaps[i]->height * 2);
466 int src_width = pcxBitmaps[i]->width;
467 int src_height = pcxBitmaps[i]->height;
470 printf("::: %d, %d [%ld -> %ld (%ld)]\n",
471 src_width, src_height,
473 src_ptr + src_width * src_height * 1,
474 src_width * src_height * 1);
478 for (i = src_ptr; i < src_ptr + src_width * src_height * 1; i++)
489 pcxBitmapsX2[i] = ZoomBitmap(pcxBitmaps[i],
490 pcxBitmaps[i]->width * ZOOM_FACTOR,
491 pcxBitmaps[i]->height * ZOOM_FACTOR);
493 pcxBitmapsX2[i] = pcxBitmaps[i];
498 printf("::: CREATING NEW CLIPMASKS ...\n");
501 clip_mask = Pixmap_to_Mask(pcxBitmapsX2[i]->drawable,
502 pcxBitmapsX2[i]->width,
503 pcxBitmapsX2[i]->height);
506 printf("::: CREATING NEW CLIPMASKS DONE\n");
509 pcxBitmapsX2[i]->clip_mask = clip_mask;
511 /* add clip mask GC */
513 XGCValues clip_gc_values;
514 unsigned long clip_gc_valuemask;
516 clip_gc_values.graphics_exposures = False;
517 clip_gc_values.clip_mask = pcxBitmapsX2[i]->clip_mask;
518 clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
519 pcxBitmapsX2[i]->stored_clip_gc = XCreateGC(display,
526 printf("::: %ld, %ld, %ld, %ld, %ld, %ld, %ld\n",
528 pcxBitmaps[i]->clip_mask, pcxBitmapsX2[i]->clip_mask,
529 pcxBitmaps[i]->width, pcxBitmaps[i]->height,
530 pcxBitmapsX2[i]->width, pcxBitmapsX2[i]->height);
534 ZoomPixmap(display, pcxBitmaps[i]->gc,
535 pcxBitmaps[i]->clip_mask, pcxBitmapsX2[i]->clip_mask,
536 pcxBitmaps[i]->width, pcxBitmaps[i]->height,
537 pcxBitmapsX2[i]->width, pcxBitmapsX2[i]->height);
543 objBitmap = pcxBitmapsX2[0];
544 botBitmap = pcxBitmapsX2[1];
545 sprBitmap = pcxBitmapsX2[2];
546 ttlBitmap = pcxBitmapsX2[3];
548 objPixmap = pcxBitmapsX2[0]->drawable;
549 botPixmap = pcxBitmapsX2[1]->drawable;
550 sprPixmap = pcxBitmapsX2[2]->drawable;
551 ttlPixmap = pcxBitmapsX2[3]->drawable;
553 objmaskBitmap = pcxBitmapsX2[0]->clip_mask;
554 botmaskBitmap = pcxBitmapsX2[1]->clip_mask;
555 sprmaskBitmap = pcxBitmapsX2[2]->clip_mask;
556 ttlmaskBitmap = pcxBitmapsX2[3]->clip_mask;
561 screenBitmap = CreateBitmap(22 * TILEX, 14 * TILEY, DEFAULT_DEPTH);
562 scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
565 screenPixmap = XCreatePixmap(display, xwindow, 22 * TILEX, 14 * TILEY,
567 if (screenPixmap == 0)
569 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
570 XDisplayName(arg_display), "failed to create pixmap",
575 scorePixmap = XCreatePixmap(display, xwindow, 20 * TILEX, SCOREY,
577 if (scorePixmap == 0)
579 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
580 XDisplayName(arg_display), "failed to create pixmap",
586 screenBitmap->drawable = screenPixmap;
587 scoreBitmap->drawable = scorePixmap;
590 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
591 if (spriteBitmap == 0)
593 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
594 XDisplayName(arg_display), "failed to create pixmap",
599 redColour.pixel = screenWhitePixel;
600 whiteColour.pixel = screenBlackPixel;
601 gotRed = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
603 "red", &redColour, 0) > 0);
604 gotWhite = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
606 "white", &whiteColour, 0) > 0);
608 gcValues.graphics_exposures = False;
609 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
612 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
613 XDisplayName(arg_display), "failed to create graphics context",
618 gcValues.graphics_exposures = False;
619 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
622 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
623 XDisplayName(arg_display), "failed to create graphics context",
629 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
630 gcValues.graphics_exposures = False;
631 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
635 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
636 XDisplayName(arg_display), "failed to create graphics context",
641 gcValues.foreground = redColour.pixel;
642 gcValues.background = whiteColour.pixel;
643 gcValues.line_style = LineDoubleDash;
644 gcValues.graphics_exposures = False;
645 antsGC = XCreateGC(display, screenPixmap,
646 GCForeground | GCBackground | GCLineStyle |
651 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
652 XDisplayName(arg_display), "failed to create graphics context",
657 for (i = 0; i < 16; i++)
658 keycodes[i] = XKeysymToKeycode(display, keysyms[i]);
660 for (i = 0; i < 3; i++)
661 northKeyCode[i] = keycodes[i + 0];
662 for (i = 0; i < 3; i++)
663 eastKeyCode[i] = keycodes[i + 3];
664 for (i = 0; i < 3; i++)
665 southKeyCode[i] = keycodes[i + 6];
666 for (i = 0; i < 3; i++)
667 westKeyCode[i] = keycodes[i + 9];
668 for (i = 0; i < 3; i++)
669 fireKeyCode[i] = keycodes[i + 12];
670 for (i = 0; i < 1; i++)
671 escKeyCode[i] = keycodes[i + 15];
673 /* ----------------------------------------------------------------- */
677 Bitmap *bm = pcxBitmaps[2];
678 Pixmap clip_mask = bm->clip_mask;
683 int width = bm->width;
684 int height = bm->height;
689 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
690 width, height, AllPlanes, ZPixmap);
691 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
692 width, height, AllPlanes, ZPixmap);
695 if (src_ximage == NULL)
697 printf("src_ximage failed\n");
701 if (dst_ximage == NULL)
703 printf("dst_ximage failed\n");
707 printf("::: DISPLAY CLIP MASK ...\n");
709 for (x=0; x<width; x++)
711 for (y=0; y<height; y++)
713 unsigned long pixel = XGetPixel(src_ximage, x, y);
715 if (pixel != BlackPixel(display, screen))
716 pixel = WhitePixel(display, screen);
718 XPutPixel(dst_ximage, x, y, pixel);
722 printf("::: DISPLAY CLIP MASK NOW\n");
724 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
725 0, 13 * TILEY, width, height);
727 printf("::: DISPLAY CLIP MASK DONE\n");
732 /* ----------------------------------------------------------------- */
738 XGCValues clip_gc_values;
739 unsigned long clip_gc_valuemask;
745 GC gc = pcxBitmaps[ii]->stored_clip_gc;
747 GC gc = pcxBitmaps[ii]->gc;
750 Pixmap src_pixmap = pcxBitmaps[ii]->clip_mask;
751 Pixmap dst_pixmap = pcxBitmapsX2[ii]->clip_mask;
752 int src_width = pcxBitmaps[ii]->width;
753 int src_height = pcxBitmaps[ii]->height;
754 int dst_width = pcxBitmapsX2[ii]->width;
755 int dst_height = pcxBitmapsX2[ii]->height;
757 XImage *src_ximage, *dst_ximage;
758 byte *src_ptr, *dst_ptr;
763 boolean scale_down = (src_width > dst_width);
766 int zoom_factor = src_width / dst_width; /* currently very limited! */
768 int row_skip, col_skip;
770 printf("::: %d\n", scale_down);
772 xdebug("::: ZOOM STEP 1");
774 clip_gc_values.graphics_exposures = False;
775 clip_gc_valuemask = GCGraphicsExposures;
776 if ((gc = XCreateGC(display, pcxBitmaps[ii]->clip_mask,
777 clip_gc_valuemask, &clip_gc_values)) == None)
779 printf("XCreateGC failed\n");
783 xdebug("::: ZOOM STEP 2");
787 zoom_factor = src_width / dst_width;
789 /* adjust source image size to integer multiple of destination size */
790 src_width = dst_width * zoom_factor;
791 src_height = dst_height * zoom_factor;
795 zoom_factor = dst_width / src_width;
797 /* no adjustment needed when scaling up (some pixels may be left blank) */
800 /* copy source pixmap to temporary image */
801 if ((src_ximage = XGetImage(display, src_pixmap, 0, 0, src_width, src_height,
802 AllPlanes, ZPixmap)) == NULL)
803 Error(ERR_EXIT, "ZoomPixmap(): XGetImage() failed");
805 bits_per_pixel = src_ximage->bits_per_pixel;
806 bytes_per_pixel = (bits_per_pixel + 7) / 8;
808 printf("::: bits_per_pixel == %d\n", bits_per_pixel);
810 if ((dst_ximage = XCreateImage(display, visual, src_ximage->depth, ZPixmap,
811 0, NULL, dst_width, dst_height,
812 8, dst_width * bytes_per_pixel)) == NULL)
813 Error(ERR_EXIT, "ZoomPixmap(): XC reateImage() failed");
816 checked_malloc(dst_width * dst_height * bytes_per_pixel);
817 dst_ximage->byte_order = src_ximage->byte_order;
819 src_ptr = (byte *)src_ximage->data;
820 dst_ptr = (byte *)dst_ximage->data;
824 col_skip = (zoom_factor - 1) * bytes_per_pixel;
825 row_skip = col_skip * src_width;
827 /* scale image down by scaling factor 'zoom_factor' */
828 for (y = 0; y < src_height; y += zoom_factor, src_ptr += row_skip)
829 for (x = 0; x < src_width; x += zoom_factor, src_ptr += col_skip)
830 for (i = 0; i < bytes_per_pixel; i++)
831 *dst_ptr++ = *src_ptr++;
835 row_skip = src_width * bytes_per_pixel;
838 printf("::: %d, %d -> %d, %d [%d / %d]\n[%ld -> %ld (%ld)] [%ld -> %ld (%ld)]\n",
839 src_width, src_height,
840 dst_width, dst_height,
841 zoom_factor, bytes_per_pixel,
843 src_ptr + src_width * src_height * bytes_per_pixel,
844 src_width * src_height * bytes_per_pixel,
846 dst_ptr + dst_width * dst_height * bytes_per_pixel,
847 dst_width * dst_height * bytes_per_pixel);
853 for (i = 0; i < src_width * src_height * bytes_per_pixel;
856 byte x = *(byte *)(src_ptr + i);
858 printf("::: %d ...\n", i);
866 /* scale image up by scaling factor 'zoom_factor' */
867 for (y = 0; y < src_height; y++)
869 for (yy = 0; yy < zoom_factor; yy++)
875 printf("::: [%d -> %ld / %ld]\n", y, src_ptr, dst_ptr);
878 for (x = 0; x < src_width; x++)
880 for (xx = 0; xx < zoom_factor; xx++)
881 for (i = 0; i < bytes_per_pixel; i++)
885 printf("::: %d\n", *(src_ptr + i));
888 *dst_ptr++ = *(src_ptr + i);
900 xdebug("::: ZOOM STEP 9");
902 /* copy scaled image to destination pixmap */
903 XPutImage(display, dst_pixmap, gc, dst_ximage, 0, 0, 0, 0,
904 dst_width, dst_height);
906 /* free temporary images */
907 X11DestroyImage(src_ximage);
908 X11DestroyImage(dst_ximage);
911 xdebug("::: ZOOM DONE");
916 /* ----------------------------------------------------------------- */
920 Bitmap *bm = pcxBitmapsX2[2];
921 Pixmap clip_mask = bm->clip_mask;
926 int width = bm->width;
927 int height = bm->height;
932 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
933 width, height, AllPlanes, ZPixmap);
934 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
935 width, height, AllPlanes, ZPixmap);
938 if (src_ximage == NULL)
940 printf("src_ximage failed\n");
944 if (dst_ximage == NULL)
946 printf("dst_ximage failed\n");
950 printf("::: DISPLAY CLIP MASK ...\n");
952 for (x=0; x<width; x++)
954 for (y=0; y<height; y++)
956 unsigned long pixel = XGetPixel(src_ximage, x, y);
958 if (pixel != BlackPixel(display, screen))
959 pixel = WhitePixel(display, screen);
961 XPutPixel(dst_ximage, x, y, pixel);
965 printf("::: DISPLAY CLIP MASK NOW\n");
967 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
968 0, 13 * TILEY + height, width, height);
970 printf("::: DISPLAY CLIP MASK DONE\n");
975 /* ----------------------------------------------------------------- */
981 printf("::: GET IMAGE ...\n");
983 dst_ximage = XGetImage(display, xwindow, 0, 0,
984 16, 16, AllPlanes, ZPixmap);
985 if (dst_ximage == NULL)
987 printf("dst_ximage failed\n");
991 printf("::: PUT IMAGE ...\n");
993 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
994 0, 13 * TILEY, 10, 10);
996 printf("::: PUT IMAGE DONE\n");
1000 /* ----------------------------------------------------------------- */
1002 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
1003 if (arg_silence == 0)
1005 for (i = 0; i < SAMPLE_MAX; i++)
1011 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
1016 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
1019 if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
1021 if (read_sample(name, &sound_data[i], &sound_length[i]))
1026 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
1027 stop = sound_data[i] + sound_length[i];
1028 for (ptr = sound_data[i]; ptr < stop; ptr++)
1029 *ptr = (*ptr * mult) / 65536;
1033 if (pipe(sound_pipe) == -1)
1035 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
1041 if (sound_pid == -1)
1043 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
1048 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
1050 _exit(sound_thread());
1052 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
1055 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
1060 void close_all(void)
1064 if (sound_pid != -1)
1066 kill(sound_pid, SIGTERM);
1067 waitpid(sound_pid, 0, 0);
1070 if (sound_pipe[0] != -1)
1071 close(sound_pipe[0]);
1072 if (sound_pipe[1] != -1)
1073 close(sound_pipe[1]);
1075 for (i = 0; i < SAMPLE_MAX; i++)
1077 free(sound_data[i]);
1080 for (i = 0; i < 4; i++)
1082 XFreePixmap(display, xpmPixmaps[i]);
1083 for (i = 0; i < 4; i++)
1085 XFreePixmap(display, xpmBitmaps[i]);
1086 for (i = 0; i < 4; i++)
1089 xpmFreeColoursFunc(display, xpmAttributes[i].colormap,
1090 xpmAttributes[i].alloc_pixels,
1091 xpmAttributes[i].nalloc_pixels, 0);
1092 XpmFreeAttributes(&xpmAttributes[i]);
1097 xpmFreeColoursFunc(display,
1098 privateColourmap ? privateColourmap : defaultColourmap,
1099 &redColour.pixel, 1, 0);
1101 xpmFreeColoursFunc(display,
1102 privateColourmap ? privateColourmap : defaultColourmap,
1103 &whiteColour.pixel, 1, 0);
1106 XFreeGC(display, screenGC);
1108 XFreeGC(display, scoreGC);
1110 XFreeGC(display, spriteGC);
1112 XFreeGC(display, antsGC);
1114 XFreePixmap(display, screenPixmap);
1116 XFreePixmap(display, scorePixmap);
1118 XFreePixmap(display, spriteBitmap);
1120 XDestroyWindow(display, xwindow);
1122 XFreeCursor(display, cursor);
1123 if (privateColourmap)
1124 XFreeColormap(display, privateColourmap);
1126 free(privateColours);
1131 XCloseDisplay(display);
1135 /* ---------------------------------------------------------------------- */
1137 void sound_play(void)
1139 if (sound_pipe[1] != -1)
1141 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
1143 fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
1146 if (sound_pipe[0] != -1)
1148 close(sound_pipe[0]);
1152 if (sound_pipe[1] != -1)
1154 close(sound_pipe[1]);
1160 memset(play, 0, sizeof(play));
1163 /* ---------------------------------------------------------------------- */
1165 static int xpmAllocColourFunc(Display *display, Colormap colourmap,
1166 char *colourname, XColor *xcolour, void *closure)
1173 if (XParseColor(display, colourmap, colourname, xcolour) == 0)
1174 return(-1); /* invalid name */
1175 if (colourmap != privateColourmap)
1176 return(XAllocColor(display, colourmap, xcolour) != 0);
1178 /* first try to find an exact match */
1180 for (i = 0; i < privateNumColours; i++)
1182 if (privateColours[i].red == xcolour->red &&
1183 privateColours[i].green == xcolour->green &&
1184 privateColours[i].blue == xcolour->blue)
1190 privateFlags[match] = 1;
1191 xcolour->pixel = privateColours[match].pixel;
1195 /* then find an unallocated colour that is close to what we want */
1198 for (i = 0; i < privateNumColours; i++)
1200 if (privateFlags[i])
1201 continue; /* skip if it is already allocated */
1203 r = (privateColours[i].red - xcolour->red) / 256;
1204 g = (privateColours[i].green - xcolour->green) / 256;
1205 b = (privateColours[i].blue - xcolour->blue) / 256;
1206 sum = r * r + g * g + b * b;
1217 privateFlags[match] = 1;
1218 privateColours[match].red = xcolour->red;
1219 privateColours[match].green = xcolour->green;
1220 privateColours[match].blue = xcolour->blue;
1221 XStoreColor(display, colourmap, &privateColours[match]);
1222 xcolour->pixel = privateColours[match].pixel;
1223 return(1); /* found a close match */
1226 /* if all else fails, just find the closest colour and return it */
1229 for (i = 0; i < privateNumColours; i++)
1231 r = (privateColours[i].red - xcolour->red) / 256;
1232 g = (privateColours[i].green - xcolour->green) / 256;
1233 b = (privateColours[i].blue - xcolour->blue) / 256;
1234 sum = r * r + g * g + b * b;
1245 xcolour->red = privateColours[match].red;
1246 xcolour->green = privateColours[match].green;
1247 xcolour->blue = privateColours[match].blue;
1248 xcolour->pixel = privateColours[match].pixel;
1249 return(1); /* best we could do */
1252 return(0); /* still didnt find one, give up */
1255 static int xpmFreeColoursFunc(Display *display, Colormap colourmap,
1256 unsigned long *pixels, int npixels,
1259 if (colourmap != privateColourmap)
1260 XFreeColors(display, colourmap, pixels, npixels, 0);
1262 return(1); /* non-zero for success */