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>
37 #if defined(TARGET_X11)
78 KeyCode northKeyCode[3];
79 KeyCode eastKeyCode[3];
80 KeyCode southKeyCode[3];
81 KeyCode westKeyCode[3];
82 KeyCode fireKeyCode[3];
83 KeyCode escKeyCode[1];
85 char play[SAMPLE_MAX];
87 static int sound_pid = -1;
88 int sound_pipe[2] = { -1, -1 }; /* for communication */
89 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
90 long sound_length[SAMPLE_MAX]; /* length of sound data */
92 static Screen *defaultScreen;
93 static Visual *defaultVisual;
94 static Colormap defaultColourmap;
95 static Window defaultRootWindow;
96 static unsigned int screenDepth;
97 static unsigned int screenWidth;
98 static unsigned int screenHeight;
99 static unsigned long screenBlackPixel;
100 static unsigned long screenWhitePixel;
103 static XSizeHints sizeHints;
104 static XSetWindowAttributes setWindowAttributes;
105 static XWMHints wmHints;
106 static XVisualInfo visualInfo;
108 static XGCValues gcValues;
110 static Colormap privateColourmap;
111 static Cursor cursor;
112 static XColor *privateColours;
113 static unsigned char *privateFlags;
114 static int privateNumColours;
116 static XColor redColour;
117 static XColor whiteColour;
122 static Bitmap *pcxBitmaps[4];
123 static Bitmap *pcxBitmapsX2[4];
127 static Pixmap xpmPixmaps[4];
128 static Pixmap xpmBitmaps[4];
129 static XpmAttributes xpmAttributes[4];
130 static int xpmGot[4];
133 static int xpmAllocColourFunc(Display *, Colormap, char *, XColor *, void *);
134 static int xpmFreeColoursFunc(Display *, Colormap, unsigned long *, int, void *);
136 static KeyCode keycodes[16];
139 static const char *xpmNames[4] =
149 static const char *pcxNames[4] =
158 static const int xpmCloseness[4] = { 10000, 10000, 40000, 50000 };
159 static const KeySym keysyms[16] =
161 XK_Up, XK_KP_Up, XK_r, /* north */
162 XK_Right, XK_KP_Right, XK_g, /* east */
163 XK_Down, XK_KP_Down, XK_f, /* south */
164 XK_Left, XK_KP_Left, XK_d, /* west */
165 XK_Shift_L, XK_Control_R, XK_space, /* fire */
166 XK_Escape /* escape */
168 static const char *sound_names[SAMPLE_MAX] =
170 "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
171 "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
172 "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
173 "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
174 "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
175 "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
178 static const int sound_volume[SAMPLE_MAX] =
180 20,100,100,100,100,20,20,100,100,100,
181 50,100,100,100,100,100,100,100,100,100,
182 100,20,100,100,100,100,100,20,100,100,
187 static void xdebug(char *msg)
190 XSync(display, False);
191 printf("EM DEBUG: %s\n", msg);
198 char name[MAXNAME+2];
206 display = XOpenDisplay(arg_display);
209 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
210 XDisplayName(arg_display), "failed to open display",
216 defaultScreen = DefaultScreenOfDisplay(display);
217 defaultVisual = DefaultVisualOfScreen(defaultScreen);
218 defaultColourmap = DefaultColormapOfScreen(defaultScreen);
219 defaultRootWindow = RootWindowOfScreen(defaultScreen);
220 screenDepth = DefaultDepthOfScreen(defaultScreen);
221 screenWidth = WidthOfScreen(defaultScreen);
222 screenHeight = HeightOfScreen(defaultScreen);
223 screenBlackPixel = BlackPixelOfScreen(defaultScreen);
224 screenWhitePixel = WhitePixelOfScreen(defaultScreen);
229 visualInfo.visualid = XVisualIDFromVisual(defaultVisual);
230 dummyptr = XGetVisualInfo(display, VisualIDMask, &visualInfo, &dummyint);
233 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
234 XDisplayName(arg_display), "failed to get visual info",
239 memcpy(&visualInfo, dummyptr, sizeof(visualInfo));
242 if (visualInfo.class != PseudoColor)
244 fprintf(stderr, "%s: \"%s\": %s\n", progname, XDisplayName(arg_display),
245 "private colourmap only supported for pseudocolour display");
249 privateColourmap = XCreateColormap(display, defaultRootWindow,
250 defaultVisual, AllocAll);
251 if (privateColourmap == 0)
253 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
254 XDisplayName(arg_display), "failed to create colourmap",
259 privateNumColours = visualInfo.colormap_size;
261 privateColours = malloc(privateNumColours * sizeof(XColor));
262 if (privateColours == 0)
264 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed",
265 privateNumColours * sizeof(XColor), strerror(errno));
269 for (dummyint = 0; dummyint < privateNumColours; dummyint++)
270 privateColours[dummyint].pixel = dummyint;
272 XQueryColors(display, defaultColourmap, privateColours, privateNumColours);
273 XStoreColors(display, privateColourmap, privateColours, privateNumColours);
275 privateFlags = malloc(privateNumColours);
276 if (privateFlags == 0)
278 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed",
279 privateNumColours, strerror(errno));
283 memset(privateFlags, 0, privateNumColours);
285 /* first two entries (black and white) are already allocated */
290 sizeHints.flags = PSize | PMinSize | PMaxSize;
291 sizeHints.width = 20 * TILEX;
292 sizeHints.height = 12 * TILEY + SCOREY;
293 sizeHints.min_width = sizeHints.max_width = sizeHints.width;
294 sizeHints.min_height = sizeHints.max_height = sizeHints.height;
298 dummyint = XWMGeometry(display, XScreenNumberOfScreen(defaultScreen),
299 arg_geometry, 0, 2, &sizeHints, &sizeHints.x,
300 &sizeHints.y, &dummyint, &dummyint,
301 &sizeHints.win_gravity);
302 if (dummyint & (XValue | YValue))
303 sizeHints.flags |= USPosition | PWinGravity;
306 xwindow = XCreateWindow(display, defaultRootWindow, sizeHints.x, sizeHints.y,
307 sizeHints.width, sizeHints.height, 2, screenDepth,
308 InputOutput, CopyFromParent, 0, 0);
311 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
312 XDisplayName(arg_display), "failed to open window",
317 setWindowAttributes.background_pixel = screenBlackPixel;
318 setWindowAttributes.border_pixel = screenWhitePixel;
319 setWindowAttributes.backing_store = NotUseful;
320 setWindowAttributes.override_redirect = False;
321 setWindowAttributes.event_mask =
322 KeyPressMask | EnterWindowMask | LeaveWindowMask | ExposureMask;
323 setWindowAttributes.colormap =
324 privateColourmap ? privateColourmap : defaultColourmap;
325 XChangeWindowAttributes(display, xwindow,
326 CWBackPixel | CWBorderPixel | CWBackingStore |
327 CWOverrideRedirect | CWEventMask | CWColormap,
328 &setWindowAttributes);
330 XStoreName(display, xwindow, "Emerald Mine");
332 wmHints.flags = InputHint | StateHint;
333 wmHints.input = True;
334 wmHints.initial_state = NormalState;
335 XSetWMHints(display, xwindow, &wmHints);
337 XSetWMNormalHints(display, xwindow, &sizeHints);
339 deleteAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
340 XSetWMProtocols(display, xwindow, &deleteAtom, 1);
342 cursor = XCreateFontCursor(display, XC_trek);
343 if (cursor) XDefineCursor(display, xwindow, cursor);
345 XMapWindow(display, xwindow);
349 for (i = 0; i < 4; i++)
354 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_GFX_DIR,
359 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, xpmNames[i]);
362 if (name[MAXNAME]) snprintf_overflow("read graphics/ files");
364 xpmAttributes[i].valuemask =
365 XpmColormap | XpmReturnAllocPixels | XpmExactColors | XpmCloseness |
366 XpmAllocColor | XpmFreeColors;
367 xpmAttributes[i].colormap =
368 privateColourmap ? privateColourmap : defaultColourmap;
369 xpmAttributes[i].exactColors = False;
370 xpmAttributes[i].closeness = xpmCloseness[i];
371 xpmAttributes[i].alloc_color = xpmAllocColourFunc;
372 xpmAttributes[i].free_colors = xpmFreeColoursFunc;
373 dummyint = XpmReadFileToPixmap(display, xwindow, name, &xpmPixmaps[i],
374 &xpmBitmaps[i], &xpmAttributes[i]);
377 fprintf(stderr, "%s: \"%s\": \"%s\": %s: %s: %s\n", progname,
378 XDisplayName(arg_display), name, "failed to read xpm",
379 XpmGetErrorString(dummyint), strerror(errno));
387 for (i = 0; i < 4; i++)
390 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, pcxNames[i]);
393 snprintf_overflow("read graphics/ files");
395 if ((pcxBitmaps[i] = LoadImage(name)) == NULL)
397 printf("::: LoadImage() failed for file '%s'\n", name);
402 objBitmap = pcxBitmaps[0];
403 botBitmap = pcxBitmaps[1];
404 sprBitmap = pcxBitmaps[2];
405 ttlBitmap = pcxBitmaps[3];
408 objPixmap = xpmPixmaps[0];
409 botPixmap = xpmPixmaps[1];
410 sprPixmap = xpmPixmaps[2];
411 ttlPixmap = xpmPixmaps[3];
412 objmaskBitmap = xpmBitmaps[0];
413 botmaskBitmap = xpmBitmaps[1];
414 sprmaskBitmap = xpmBitmaps[2];
415 ttlmaskBitmap = xpmBitmaps[3];
417 objPixmap = pcxBitmaps[0]->drawable;
418 botPixmap = pcxBitmaps[1]->drawable;
419 sprPixmap = pcxBitmaps[2]->drawable;
420 ttlPixmap = pcxBitmaps[3]->drawable;
421 objmaskBitmap = pcxBitmaps[0]->clip_mask;
422 botmaskBitmap = pcxBitmaps[1]->clip_mask;
423 sprmaskBitmap = pcxBitmaps[2]->clip_mask;
424 ttlmaskBitmap = pcxBitmaps[3]->clip_mask;
428 for (i = 0; i < 4; i++)
432 if ((clip_mask = XCreatePixmap(display, window->drawable,
433 pcxBitmaps[i]->width * 2,
434 pcxBitmaps[i]->height * 2, 1))
437 printf("::: cannot create clip mask");
443 XGCValues clip_gc_values;
444 unsigned long clip_gc_valuemask;
447 clip_gc_values.graphics_exposures = False;
448 clip_gc_valuemask = GCGraphicsExposures;
449 if ((gc = XCreateGC(display, clip_mask,
450 clip_gc_valuemask, &clip_gc_values)) == None)
452 printf("X CreateGC failed\n");
456 XFillRectangle(display, clip_mask, gc, 0, 0,
457 pcxBitmaps[i]->width * 2,
458 pcxBitmaps[i]->height * 2);
465 int src_width = pcxBitmaps[i]->width;
466 int src_height = pcxBitmaps[i]->height;
469 printf("::: %d, %d [%ld -> %ld (%ld)]\n",
470 src_width, src_height,
472 src_ptr + src_width * src_height * 1,
473 src_width * src_height * 1);
477 for (i = src_ptr; i < src_ptr + src_width * src_height * 1; i++)
488 pcxBitmapsX2[i] = ZoomBitmap(pcxBitmaps[i],
489 pcxBitmaps[i]->width * ZOOM_FACTOR,
490 pcxBitmaps[i]->height * ZOOM_FACTOR);
492 pcxBitmapsX2[i] = pcxBitmaps[i];
497 printf("::: CREATING NEW CLIPMASKS ...\n");
500 clip_mask = Pixmap_to_Mask(pcxBitmapsX2[i]->drawable,
501 pcxBitmapsX2[i]->width,
502 pcxBitmapsX2[i]->height);
505 printf("::: CREATING NEW CLIPMASKS DONE\n");
508 pcxBitmapsX2[i]->clip_mask = clip_mask;
510 /* add clip mask GC */
512 XGCValues clip_gc_values;
513 unsigned long clip_gc_valuemask;
515 clip_gc_values.graphics_exposures = False;
516 clip_gc_values.clip_mask = pcxBitmapsX2[i]->clip_mask;
517 clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
518 pcxBitmapsX2[i]->stored_clip_gc = XCreateGC(display,
525 printf("::: %ld, %ld, %ld, %ld, %ld, %ld, %ld\n",
527 pcxBitmaps[i]->clip_mask, pcxBitmapsX2[i]->clip_mask,
528 pcxBitmaps[i]->width, pcxBitmaps[i]->height,
529 pcxBitmapsX2[i]->width, pcxBitmapsX2[i]->height);
533 ZoomPixmap(display, pcxBitmaps[i]->gc,
534 pcxBitmaps[i]->clip_mask, pcxBitmapsX2[i]->clip_mask,
535 pcxBitmaps[i]->width, pcxBitmaps[i]->height,
536 pcxBitmapsX2[i]->width, pcxBitmapsX2[i]->height);
542 objBitmap = pcxBitmapsX2[0];
543 botBitmap = pcxBitmapsX2[1];
544 sprBitmap = pcxBitmapsX2[2];
545 ttlBitmap = pcxBitmapsX2[3];
547 objPixmap = pcxBitmapsX2[0]->drawable;
548 botPixmap = pcxBitmapsX2[1]->drawable;
549 sprPixmap = pcxBitmapsX2[2]->drawable;
550 ttlPixmap = pcxBitmapsX2[3]->drawable;
552 objmaskBitmap = pcxBitmapsX2[0]->clip_mask;
553 botmaskBitmap = pcxBitmapsX2[1]->clip_mask;
554 sprmaskBitmap = pcxBitmapsX2[2]->clip_mask;
555 ttlmaskBitmap = pcxBitmapsX2[3]->clip_mask;
560 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
562 scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
565 screenPixmap = XCreatePixmap(display, xwindow,
566 MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
568 if (screenPixmap == 0)
570 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
571 XDisplayName(arg_display), "failed to create pixmap",
576 scorePixmap = XCreatePixmap(display, xwindow, 20 * TILEX, SCOREY,
578 if (scorePixmap == 0)
580 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
581 XDisplayName(arg_display), "failed to create pixmap",
587 screenBitmap->drawable = screenPixmap;
588 scoreBitmap->drawable = scorePixmap;
591 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
592 if (spriteBitmap == 0)
594 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
595 XDisplayName(arg_display), "failed to create pixmap",
600 redColour.pixel = screenWhitePixel;
601 whiteColour.pixel = screenBlackPixel;
602 gotRed = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
604 "red", &redColour, 0) > 0);
605 gotWhite = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
607 "white", &whiteColour, 0) > 0);
609 gcValues.graphics_exposures = False;
610 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
613 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
614 XDisplayName(arg_display), "failed to create graphics context",
619 gcValues.graphics_exposures = False;
620 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
623 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
624 XDisplayName(arg_display), "failed to create graphics context",
630 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
631 gcValues.graphics_exposures = False;
632 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
636 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
637 XDisplayName(arg_display), "failed to create graphics context",
642 gcValues.foreground = redColour.pixel;
643 gcValues.background = whiteColour.pixel;
644 gcValues.line_style = LineDoubleDash;
645 gcValues.graphics_exposures = False;
646 antsGC = XCreateGC(display, screenPixmap,
647 GCForeground | GCBackground | GCLineStyle |
652 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
653 XDisplayName(arg_display), "failed to create graphics context",
658 for (i = 0; i < 16; i++)
659 keycodes[i] = XKeysymToKeycode(display, keysyms[i]);
661 for (i = 0; i < 3; i++)
662 northKeyCode[i] = keycodes[i + 0];
663 for (i = 0; i < 3; i++)
664 eastKeyCode[i] = keycodes[i + 3];
665 for (i = 0; i < 3; i++)
666 southKeyCode[i] = keycodes[i + 6];
667 for (i = 0; i < 3; i++)
668 westKeyCode[i] = keycodes[i + 9];
669 for (i = 0; i < 3; i++)
670 fireKeyCode[i] = keycodes[i + 12];
671 for (i = 0; i < 1; i++)
672 escKeyCode[i] = keycodes[i + 15];
674 /* ----------------------------------------------------------------- */
678 Bitmap *bm = pcxBitmaps[2];
679 Pixmap clip_mask = bm->clip_mask;
684 int width = bm->width;
685 int height = bm->height;
690 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
691 width, height, AllPlanes, ZPixmap);
692 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
693 width, height, AllPlanes, ZPixmap);
696 if (src_ximage == NULL)
698 printf("src_ximage failed\n");
702 if (dst_ximage == NULL)
704 printf("dst_ximage failed\n");
708 printf("::: DISPLAY CLIP MASK ...\n");
710 for (x=0; x<width; x++)
712 for (y=0; y<height; y++)
714 unsigned long pixel = XGetPixel(src_ximage, x, y);
716 if (pixel != BlackPixel(display, screen))
717 pixel = WhitePixel(display, screen);
719 XPutPixel(dst_ximage, x, y, pixel);
723 printf("::: DISPLAY CLIP MASK NOW\n");
725 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
726 0, 13 * TILEY, width, height);
728 printf("::: DISPLAY CLIP MASK DONE\n");
733 /* ----------------------------------------------------------------- */
739 XGCValues clip_gc_values;
740 unsigned long clip_gc_valuemask;
746 GC gc = pcxBitmaps[ii]->stored_clip_gc;
748 GC gc = pcxBitmaps[ii]->gc;
751 Pixmap src_pixmap = pcxBitmaps[ii]->clip_mask;
752 Pixmap dst_pixmap = pcxBitmapsX2[ii]->clip_mask;
753 int src_width = pcxBitmaps[ii]->width;
754 int src_height = pcxBitmaps[ii]->height;
755 int dst_width = pcxBitmapsX2[ii]->width;
756 int dst_height = pcxBitmapsX2[ii]->height;
758 XImage *src_ximage, *dst_ximage;
759 byte *src_ptr, *dst_ptr;
764 boolean scale_down = (src_width > dst_width);
767 int zoom_factor = src_width / dst_width; /* currently very limited! */
769 int row_skip, col_skip;
771 printf("::: %d\n", scale_down);
773 xdebug("::: ZOOM STEP 1");
775 clip_gc_values.graphics_exposures = False;
776 clip_gc_valuemask = GCGraphicsExposures;
777 if ((gc = XCreateGC(display, pcxBitmaps[ii]->clip_mask,
778 clip_gc_valuemask, &clip_gc_values)) == None)
780 printf("XCreateGC failed\n");
784 xdebug("::: ZOOM STEP 2");
788 zoom_factor = src_width / dst_width;
790 /* adjust source image size to integer multiple of destination size */
791 src_width = dst_width * zoom_factor;
792 src_height = dst_height * zoom_factor;
796 zoom_factor = dst_width / src_width;
798 /* no adjustment needed when scaling up (some pixels may be left blank) */
801 /* copy source pixmap to temporary image */
802 if ((src_ximage = XGetImage(display, src_pixmap, 0, 0, src_width, src_height,
803 AllPlanes, ZPixmap)) == NULL)
804 Error(ERR_EXIT, "ZoomPixmap(): XGetImage() failed");
806 bits_per_pixel = src_ximage->bits_per_pixel;
807 bytes_per_pixel = (bits_per_pixel + 7) / 8;
809 printf("::: bits_per_pixel == %d\n", bits_per_pixel);
811 if ((dst_ximage = XCreateImage(display, visual, src_ximage->depth, ZPixmap,
812 0, NULL, dst_width, dst_height,
813 8, dst_width * bytes_per_pixel)) == NULL)
814 Error(ERR_EXIT, "ZoomPixmap(): XC reateImage() failed");
817 checked_malloc(dst_width * dst_height * bytes_per_pixel);
818 dst_ximage->byte_order = src_ximage->byte_order;
820 src_ptr = (byte *)src_ximage->data;
821 dst_ptr = (byte *)dst_ximage->data;
825 col_skip = (zoom_factor - 1) * bytes_per_pixel;
826 row_skip = col_skip * src_width;
828 /* scale image down by scaling factor 'zoom_factor' */
829 for (y = 0; y < src_height; y += zoom_factor, src_ptr += row_skip)
830 for (x = 0; x < src_width; x += zoom_factor, src_ptr += col_skip)
831 for (i = 0; i < bytes_per_pixel; i++)
832 *dst_ptr++ = *src_ptr++;
836 row_skip = src_width * bytes_per_pixel;
839 printf("::: %d, %d -> %d, %d [%d / %d]\n[%ld -> %ld (%ld)] [%ld -> %ld (%ld)]\n",
840 src_width, src_height,
841 dst_width, dst_height,
842 zoom_factor, bytes_per_pixel,
844 src_ptr + src_width * src_height * bytes_per_pixel,
845 src_width * src_height * bytes_per_pixel,
847 dst_ptr + dst_width * dst_height * bytes_per_pixel,
848 dst_width * dst_height * bytes_per_pixel);
854 for (i = 0; i < src_width * src_height * bytes_per_pixel;
857 byte x = *(byte *)(src_ptr + i);
859 printf("::: %d ...\n", i);
867 /* scale image up by scaling factor 'zoom_factor' */
868 for (y = 0; y < src_height; y++)
870 for (yy = 0; yy < zoom_factor; yy++)
876 printf("::: [%d -> %ld / %ld]\n", y, src_ptr, dst_ptr);
879 for (x = 0; x < src_width; x++)
881 for (xx = 0; xx < zoom_factor; xx++)
882 for (i = 0; i < bytes_per_pixel; i++)
886 printf("::: %d\n", *(src_ptr + i));
889 *dst_ptr++ = *(src_ptr + i);
901 xdebug("::: ZOOM STEP 9");
903 /* copy scaled image to destination pixmap */
904 XPutImage(display, dst_pixmap, gc, dst_ximage, 0, 0, 0, 0,
905 dst_width, dst_height);
907 /* free temporary images */
908 X11DestroyImage(src_ximage);
909 X11DestroyImage(dst_ximage);
912 xdebug("::: ZOOM DONE");
917 /* ----------------------------------------------------------------- */
921 Bitmap *bm = pcxBitmapsX2[2];
922 Pixmap clip_mask = bm->clip_mask;
927 int width = bm->width;
928 int height = bm->height;
933 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
934 width, height, AllPlanes, ZPixmap);
935 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
936 width, height, AllPlanes, ZPixmap);
939 if (src_ximage == NULL)
941 printf("src_ximage failed\n");
945 if (dst_ximage == NULL)
947 printf("dst_ximage failed\n");
951 printf("::: DISPLAY CLIP MASK ...\n");
953 for (x=0; x<width; x++)
955 for (y=0; y<height; y++)
957 unsigned long pixel = XGetPixel(src_ximage, x, y);
959 if (pixel != BlackPixel(display, screen))
960 pixel = WhitePixel(display, screen);
962 XPutPixel(dst_ximage, x, y, pixel);
966 printf("::: DISPLAY CLIP MASK NOW\n");
968 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
969 0, 13 * TILEY + height, width, height);
971 printf("::: DISPLAY CLIP MASK DONE\n");
976 /* ----------------------------------------------------------------- */
982 printf("::: GET IMAGE ...\n");
984 dst_ximage = XGetImage(display, xwindow, 0, 0,
985 16, 16, AllPlanes, ZPixmap);
986 if (dst_ximage == NULL)
988 printf("dst_ximage failed\n");
992 printf("::: PUT IMAGE ...\n");
994 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
995 0, 13 * TILEY, 10, 10);
997 printf("::: PUT IMAGE DONE\n");
1001 /* ----------------------------------------------------------------- */
1003 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
1004 if (arg_silence == 0)
1006 for (i = 0; i < SAMPLE_MAX; i++)
1012 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
1017 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
1020 if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
1022 if (read_sample(name, &sound_data[i], &sound_length[i]))
1027 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
1028 stop = sound_data[i] + sound_length[i];
1029 for (ptr = sound_data[i]; ptr < stop; ptr++)
1030 *ptr = (*ptr * mult) / 65536;
1034 if (pipe(sound_pipe) == -1)
1036 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
1042 if (sound_pid == -1)
1044 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
1049 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
1051 _exit(sound_thread());
1053 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
1056 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
1061 void close_all(void)
1065 if (sound_pid != -1)
1067 kill(sound_pid, SIGTERM);
1068 waitpid(sound_pid, 0, 0);
1071 if (sound_pipe[0] != -1)
1072 close(sound_pipe[0]);
1073 if (sound_pipe[1] != -1)
1074 close(sound_pipe[1]);
1076 for (i = 0; i < SAMPLE_MAX; i++)
1078 free(sound_data[i]);
1081 for (i = 0; i < 4; i++)
1083 XFreePixmap(display, xpmPixmaps[i]);
1084 for (i = 0; i < 4; i++)
1086 XFreePixmap(display, xpmBitmaps[i]);
1087 for (i = 0; i < 4; i++)
1090 xpmFreeColoursFunc(display, xpmAttributes[i].colormap,
1091 xpmAttributes[i].alloc_pixels,
1092 xpmAttributes[i].nalloc_pixels, 0);
1093 XpmFreeAttributes(&xpmAttributes[i]);
1098 xpmFreeColoursFunc(display,
1099 privateColourmap ? privateColourmap : defaultColourmap,
1100 &redColour.pixel, 1, 0);
1102 xpmFreeColoursFunc(display,
1103 privateColourmap ? privateColourmap : defaultColourmap,
1104 &whiteColour.pixel, 1, 0);
1107 XFreeGC(display, screenGC);
1109 XFreeGC(display, scoreGC);
1111 XFreeGC(display, spriteGC);
1113 XFreeGC(display, antsGC);
1115 XFreePixmap(display, screenPixmap);
1117 XFreePixmap(display, scorePixmap);
1119 XFreePixmap(display, spriteBitmap);
1121 XDestroyWindow(display, xwindow);
1123 XFreeCursor(display, cursor);
1124 if (privateColourmap)
1125 XFreeColormap(display, privateColourmap);
1127 free(privateColours);
1132 XCloseDisplay(display);
1136 /* ---------------------------------------------------------------------- */
1138 void sound_play(void)
1140 if (sound_pipe[1] != -1)
1142 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
1144 fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
1147 if (sound_pipe[0] != -1)
1149 close(sound_pipe[0]);
1153 if (sound_pipe[1] != -1)
1155 close(sound_pipe[1]);
1161 memset(play, 0, sizeof(play));
1164 /* ---------------------------------------------------------------------- */
1166 static int xpmAllocColourFunc(Display *display, Colormap colourmap,
1167 char *colourname, XColor *xcolour, void *closure)
1174 if (XParseColor(display, colourmap, colourname, xcolour) == 0)
1175 return(-1); /* invalid name */
1176 if (colourmap != privateColourmap)
1177 return(XAllocColor(display, colourmap, xcolour) != 0);
1179 /* first try to find an exact match */
1181 for (i = 0; i < privateNumColours; i++)
1183 if (privateColours[i].red == xcolour->red &&
1184 privateColours[i].green == xcolour->green &&
1185 privateColours[i].blue == xcolour->blue)
1191 privateFlags[match] = 1;
1192 xcolour->pixel = privateColours[match].pixel;
1196 /* then find an unallocated colour that is close to what we want */
1199 for (i = 0; i < privateNumColours; i++)
1201 if (privateFlags[i])
1202 continue; /* skip if it is already allocated */
1204 r = (privateColours[i].red - xcolour->red) / 256;
1205 g = (privateColours[i].green - xcolour->green) / 256;
1206 b = (privateColours[i].blue - xcolour->blue) / 256;
1207 sum = r * r + g * g + b * b;
1218 privateFlags[match] = 1;
1219 privateColours[match].red = xcolour->red;
1220 privateColours[match].green = xcolour->green;
1221 privateColours[match].blue = xcolour->blue;
1222 XStoreColor(display, colourmap, &privateColours[match]);
1223 xcolour->pixel = privateColours[match].pixel;
1224 return(1); /* found a close match */
1227 /* if all else fails, just find the closest colour and return it */
1230 for (i = 0; i < privateNumColours; i++)
1232 r = (privateColours[i].red - xcolour->red) / 256;
1233 g = (privateColours[i].green - xcolour->green) / 256;
1234 b = (privateColours[i].blue - xcolour->blue) / 256;
1235 sum = r * r + g * g + b * b;
1246 xcolour->red = privateColours[match].red;
1247 xcolour->green = privateColours[match].green;
1248 xcolour->blue = privateColours[match].blue;
1249 xcolour->pixel = privateColours[match].pixel;
1250 return(1); /* best we could do */
1253 return(0); /* still didnt find one, give up */
1256 static int xpmFreeColoursFunc(Display *display, Colormap colourmap,
1257 unsigned long *pixels, int npixels,
1260 if (colourmap != privateColourmap)
1261 XFreeColors(display, colourmap, pixels, npixels, 0);
1263 return(1); /* non-zero for success */