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(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
563 scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
566 screenPixmap = XCreatePixmap(display, xwindow,
567 MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
569 if (screenPixmap == 0)
571 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
572 XDisplayName(arg_display), "failed to create pixmap",
577 scorePixmap = XCreatePixmap(display, xwindow, 20 * TILEX, SCOREY,
579 if (scorePixmap == 0)
581 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
582 XDisplayName(arg_display), "failed to create pixmap",
588 screenBitmap->drawable = screenPixmap;
589 scoreBitmap->drawable = scorePixmap;
592 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
593 if (spriteBitmap == 0)
595 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
596 XDisplayName(arg_display), "failed to create pixmap",
601 redColour.pixel = screenWhitePixel;
602 whiteColour.pixel = screenBlackPixel;
603 gotRed = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
605 "red", &redColour, 0) > 0);
606 gotWhite = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
608 "white", &whiteColour, 0) > 0);
610 gcValues.graphics_exposures = False;
611 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
614 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
615 XDisplayName(arg_display), "failed to create graphics context",
620 gcValues.graphics_exposures = False;
621 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
624 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
625 XDisplayName(arg_display), "failed to create graphics context",
631 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
632 gcValues.graphics_exposures = False;
633 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
637 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
638 XDisplayName(arg_display), "failed to create graphics context",
643 gcValues.foreground = redColour.pixel;
644 gcValues.background = whiteColour.pixel;
645 gcValues.line_style = LineDoubleDash;
646 gcValues.graphics_exposures = False;
647 antsGC = XCreateGC(display, screenPixmap,
648 GCForeground | GCBackground | GCLineStyle |
653 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
654 XDisplayName(arg_display), "failed to create graphics context",
659 for (i = 0; i < 16; i++)
660 keycodes[i] = XKeysymToKeycode(display, keysyms[i]);
662 for (i = 0; i < 3; i++)
663 northKeyCode[i] = keycodes[i + 0];
664 for (i = 0; i < 3; i++)
665 eastKeyCode[i] = keycodes[i + 3];
666 for (i = 0; i < 3; i++)
667 southKeyCode[i] = keycodes[i + 6];
668 for (i = 0; i < 3; i++)
669 westKeyCode[i] = keycodes[i + 9];
670 for (i = 0; i < 3; i++)
671 fireKeyCode[i] = keycodes[i + 12];
672 for (i = 0; i < 1; i++)
673 escKeyCode[i] = keycodes[i + 15];
675 /* ----------------------------------------------------------------- */
679 Bitmap *bm = pcxBitmaps[2];
680 Pixmap clip_mask = bm->clip_mask;
685 int width = bm->width;
686 int height = bm->height;
691 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
692 width, height, AllPlanes, ZPixmap);
693 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
694 width, height, AllPlanes, ZPixmap);
697 if (src_ximage == NULL)
699 printf("src_ximage failed\n");
703 if (dst_ximage == NULL)
705 printf("dst_ximage failed\n");
709 printf("::: DISPLAY CLIP MASK ...\n");
711 for (x=0; x<width; x++)
713 for (y=0; y<height; y++)
715 unsigned long pixel = XGetPixel(src_ximage, x, y);
717 if (pixel != BlackPixel(display, screen))
718 pixel = WhitePixel(display, screen);
720 XPutPixel(dst_ximage, x, y, pixel);
724 printf("::: DISPLAY CLIP MASK NOW\n");
726 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
727 0, 13 * TILEY, width, height);
729 printf("::: DISPLAY CLIP MASK DONE\n");
734 /* ----------------------------------------------------------------- */
740 XGCValues clip_gc_values;
741 unsigned long clip_gc_valuemask;
747 GC gc = pcxBitmaps[ii]->stored_clip_gc;
749 GC gc = pcxBitmaps[ii]->gc;
752 Pixmap src_pixmap = pcxBitmaps[ii]->clip_mask;
753 Pixmap dst_pixmap = pcxBitmapsX2[ii]->clip_mask;
754 int src_width = pcxBitmaps[ii]->width;
755 int src_height = pcxBitmaps[ii]->height;
756 int dst_width = pcxBitmapsX2[ii]->width;
757 int dst_height = pcxBitmapsX2[ii]->height;
759 XImage *src_ximage, *dst_ximage;
760 byte *src_ptr, *dst_ptr;
765 boolean scale_down = (src_width > dst_width);
768 int zoom_factor = src_width / dst_width; /* currently very limited! */
770 int row_skip, col_skip;
772 printf("::: %d\n", scale_down);
774 xdebug("::: ZOOM STEP 1");
776 clip_gc_values.graphics_exposures = False;
777 clip_gc_valuemask = GCGraphicsExposures;
778 if ((gc = XCreateGC(display, pcxBitmaps[ii]->clip_mask,
779 clip_gc_valuemask, &clip_gc_values)) == None)
781 printf("XCreateGC failed\n");
785 xdebug("::: ZOOM STEP 2");
789 zoom_factor = src_width / dst_width;
791 /* adjust source image size to integer multiple of destination size */
792 src_width = dst_width * zoom_factor;
793 src_height = dst_height * zoom_factor;
797 zoom_factor = dst_width / src_width;
799 /* no adjustment needed when scaling up (some pixels may be left blank) */
802 /* copy source pixmap to temporary image */
803 if ((src_ximage = XGetImage(display, src_pixmap, 0, 0, src_width, src_height,
804 AllPlanes, ZPixmap)) == NULL)
805 Error(ERR_EXIT, "ZoomPixmap(): XGetImage() failed");
807 bits_per_pixel = src_ximage->bits_per_pixel;
808 bytes_per_pixel = (bits_per_pixel + 7) / 8;
810 printf("::: bits_per_pixel == %d\n", bits_per_pixel);
812 if ((dst_ximage = XCreateImage(display, visual, src_ximage->depth, ZPixmap,
813 0, NULL, dst_width, dst_height,
814 8, dst_width * bytes_per_pixel)) == NULL)
815 Error(ERR_EXIT, "ZoomPixmap(): XC reateImage() failed");
818 checked_malloc(dst_width * dst_height * bytes_per_pixel);
819 dst_ximage->byte_order = src_ximage->byte_order;
821 src_ptr = (byte *)src_ximage->data;
822 dst_ptr = (byte *)dst_ximage->data;
826 col_skip = (zoom_factor - 1) * bytes_per_pixel;
827 row_skip = col_skip * src_width;
829 /* scale image down by scaling factor 'zoom_factor' */
830 for (y = 0; y < src_height; y += zoom_factor, src_ptr += row_skip)
831 for (x = 0; x < src_width; x += zoom_factor, src_ptr += col_skip)
832 for (i = 0; i < bytes_per_pixel; i++)
833 *dst_ptr++ = *src_ptr++;
837 row_skip = src_width * bytes_per_pixel;
840 printf("::: %d, %d -> %d, %d [%d / %d]\n[%ld -> %ld (%ld)] [%ld -> %ld (%ld)]\n",
841 src_width, src_height,
842 dst_width, dst_height,
843 zoom_factor, bytes_per_pixel,
845 src_ptr + src_width * src_height * bytes_per_pixel,
846 src_width * src_height * bytes_per_pixel,
848 dst_ptr + dst_width * dst_height * bytes_per_pixel,
849 dst_width * dst_height * bytes_per_pixel);
855 for (i = 0; i < src_width * src_height * bytes_per_pixel;
858 byte x = *(byte *)(src_ptr + i);
860 printf("::: %d ...\n", i);
868 /* scale image up by scaling factor 'zoom_factor' */
869 for (y = 0; y < src_height; y++)
871 for (yy = 0; yy < zoom_factor; yy++)
877 printf("::: [%d -> %ld / %ld]\n", y, src_ptr, dst_ptr);
880 for (x = 0; x < src_width; x++)
882 for (xx = 0; xx < zoom_factor; xx++)
883 for (i = 0; i < bytes_per_pixel; i++)
887 printf("::: %d\n", *(src_ptr + i));
890 *dst_ptr++ = *(src_ptr + i);
902 xdebug("::: ZOOM STEP 9");
904 /* copy scaled image to destination pixmap */
905 XPutImage(display, dst_pixmap, gc, dst_ximage, 0, 0, 0, 0,
906 dst_width, dst_height);
908 /* free temporary images */
909 X11DestroyImage(src_ximage);
910 X11DestroyImage(dst_ximage);
913 xdebug("::: ZOOM DONE");
918 /* ----------------------------------------------------------------- */
922 Bitmap *bm = pcxBitmapsX2[2];
923 Pixmap clip_mask = bm->clip_mask;
928 int width = bm->width;
929 int height = bm->height;
934 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
935 width, height, AllPlanes, ZPixmap);
936 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
937 width, height, AllPlanes, ZPixmap);
940 if (src_ximage == NULL)
942 printf("src_ximage failed\n");
946 if (dst_ximage == NULL)
948 printf("dst_ximage failed\n");
952 printf("::: DISPLAY CLIP MASK ...\n");
954 for (x=0; x<width; x++)
956 for (y=0; y<height; y++)
958 unsigned long pixel = XGetPixel(src_ximage, x, y);
960 if (pixel != BlackPixel(display, screen))
961 pixel = WhitePixel(display, screen);
963 XPutPixel(dst_ximage, x, y, pixel);
967 printf("::: DISPLAY CLIP MASK NOW\n");
969 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
970 0, 13 * TILEY + height, width, height);
972 printf("::: DISPLAY CLIP MASK DONE\n");
977 /* ----------------------------------------------------------------- */
983 printf("::: GET IMAGE ...\n");
985 dst_ximage = XGetImage(display, xwindow, 0, 0,
986 16, 16, AllPlanes, ZPixmap);
987 if (dst_ximage == NULL)
989 printf("dst_ximage failed\n");
993 printf("::: PUT IMAGE ...\n");
995 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
996 0, 13 * TILEY, 10, 10);
998 printf("::: PUT IMAGE DONE\n");
1002 /* ----------------------------------------------------------------- */
1004 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
1005 if (arg_silence == 0)
1007 for (i = 0; i < SAMPLE_MAX; i++)
1013 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
1018 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
1021 if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
1023 if (read_sample(name, &sound_data[i], &sound_length[i]))
1028 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
1029 stop = sound_data[i] + sound_length[i];
1030 for (ptr = sound_data[i]; ptr < stop; ptr++)
1031 *ptr = (*ptr * mult) / 65536;
1035 if (pipe(sound_pipe) == -1)
1037 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
1043 if (sound_pid == -1)
1045 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
1050 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
1052 _exit(sound_thread());
1054 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
1057 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
1062 void close_all(void)
1066 if (sound_pid != -1)
1068 kill(sound_pid, SIGTERM);
1069 waitpid(sound_pid, 0, 0);
1072 if (sound_pipe[0] != -1)
1073 close(sound_pipe[0]);
1074 if (sound_pipe[1] != -1)
1075 close(sound_pipe[1]);
1077 for (i = 0; i < SAMPLE_MAX; i++)
1079 free(sound_data[i]);
1082 for (i = 0; i < 4; i++)
1084 XFreePixmap(display, xpmPixmaps[i]);
1085 for (i = 0; i < 4; i++)
1087 XFreePixmap(display, xpmBitmaps[i]);
1088 for (i = 0; i < 4; i++)
1091 xpmFreeColoursFunc(display, xpmAttributes[i].colormap,
1092 xpmAttributes[i].alloc_pixels,
1093 xpmAttributes[i].nalloc_pixels, 0);
1094 XpmFreeAttributes(&xpmAttributes[i]);
1099 xpmFreeColoursFunc(display,
1100 privateColourmap ? privateColourmap : defaultColourmap,
1101 &redColour.pixel, 1, 0);
1103 xpmFreeColoursFunc(display,
1104 privateColourmap ? privateColourmap : defaultColourmap,
1105 &whiteColour.pixel, 1, 0);
1108 XFreeGC(display, screenGC);
1110 XFreeGC(display, scoreGC);
1112 XFreeGC(display, spriteGC);
1114 XFreeGC(display, antsGC);
1116 XFreePixmap(display, screenPixmap);
1118 XFreePixmap(display, scorePixmap);
1120 XFreePixmap(display, spriteBitmap);
1122 XDestroyWindow(display, xwindow);
1124 XFreeCursor(display, cursor);
1125 if (privateColourmap)
1126 XFreeColormap(display, privateColourmap);
1128 free(privateColours);
1133 XCloseDisplay(display);
1137 /* ---------------------------------------------------------------------- */
1139 void sound_play(void)
1141 if (sound_pipe[1] != -1)
1143 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
1145 fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
1148 if (sound_pipe[0] != -1)
1150 close(sound_pipe[0]);
1154 if (sound_pipe[1] != -1)
1156 close(sound_pipe[1]);
1162 memset(play, 0, sizeof(play));
1165 /* ---------------------------------------------------------------------- */
1167 static int xpmAllocColourFunc(Display *display, Colormap colourmap,
1168 char *colourname, XColor *xcolour, void *closure)
1175 if (XParseColor(display, colourmap, colourname, xcolour) == 0)
1176 return(-1); /* invalid name */
1177 if (colourmap != privateColourmap)
1178 return(XAllocColor(display, colourmap, xcolour) != 0);
1180 /* first try to find an exact match */
1182 for (i = 0; i < privateNumColours; i++)
1184 if (privateColours[i].red == xcolour->red &&
1185 privateColours[i].green == xcolour->green &&
1186 privateColours[i].blue == xcolour->blue)
1192 privateFlags[match] = 1;
1193 xcolour->pixel = privateColours[match].pixel;
1197 /* then find an unallocated colour that is close to what we want */
1200 for (i = 0; i < privateNumColours; i++)
1202 if (privateFlags[i])
1203 continue; /* skip if it is already allocated */
1205 r = (privateColours[i].red - xcolour->red) / 256;
1206 g = (privateColours[i].green - xcolour->green) / 256;
1207 b = (privateColours[i].blue - xcolour->blue) / 256;
1208 sum = r * r + g * g + b * b;
1219 privateFlags[match] = 1;
1220 privateColours[match].red = xcolour->red;
1221 privateColours[match].green = xcolour->green;
1222 privateColours[match].blue = xcolour->blue;
1223 XStoreColor(display, colourmap, &privateColours[match]);
1224 xcolour->pixel = privateColours[match].pixel;
1225 return(1); /* found a close match */
1228 /* if all else fails, just find the closest colour and return it */
1231 for (i = 0; i < privateNumColours; i++)
1233 r = (privateColours[i].red - xcolour->red) / 256;
1234 g = (privateColours[i].green - xcolour->green) / 256;
1235 b = (privateColours[i].blue - xcolour->blue) / 256;
1236 sum = r * r + g * g + b * b;
1247 xcolour->red = privateColours[match].red;
1248 xcolour->green = privateColours[match].green;
1249 xcolour->blue = privateColours[match].blue;
1250 xcolour->pixel = privateColours[match].pixel;
1251 return(1); /* best we could do */
1254 return(0); /* still didnt find one, give up */
1257 static int xpmFreeColoursFunc(Display *display, Colormap colourmap,
1258 unsigned long *pixels, int npixels,
1261 if (colourmap != privateColourmap)
1262 XFreeColors(display, colourmap, pixels, npixels, 0);
1264 return(1); /* non-zero for success */