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 #if 0 /* BITMAP CREATION STUFF */
389 for (i = 0; i < 4; i++)
392 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, pcxNames[i]);
395 snprintf_overflow("read graphics/ files");
397 if ((pcxBitmaps[i] = LoadImage(name)) == NULL)
399 printf("::: LoadImage() failed for file '%s'\n", name);
404 objBitmap = pcxBitmaps[0];
405 botBitmap = pcxBitmaps[1];
406 sprBitmap = pcxBitmaps[2];
407 ttlBitmap = pcxBitmaps[3];
410 objPixmap = xpmPixmaps[0];
411 botPixmap = xpmPixmaps[1];
412 sprPixmap = xpmPixmaps[2];
413 ttlPixmap = xpmPixmaps[3];
414 objmaskBitmap = xpmBitmaps[0];
415 botmaskBitmap = xpmBitmaps[1];
416 sprmaskBitmap = xpmBitmaps[2];
417 ttlmaskBitmap = xpmBitmaps[3];
419 objPixmap = pcxBitmaps[0]->drawable;
420 botPixmap = pcxBitmaps[1]->drawable;
421 sprPixmap = pcxBitmaps[2]->drawable;
422 ttlPixmap = pcxBitmaps[3]->drawable;
423 objmaskBitmap = pcxBitmaps[0]->clip_mask;
424 botmaskBitmap = pcxBitmaps[1]->clip_mask;
425 sprmaskBitmap = pcxBitmaps[2]->clip_mask;
426 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");
502 clip_mask = Pixmap_to_Mask(pcxBitmapsX2[i]->drawable,
503 pcxBitmapsX2[i]->width,
504 pcxBitmapsX2[i]->height);
508 printf("::: CREATING NEW CLIPMASKS DONE\n");
511 pcxBitmapsX2[i]->clip_mask = clip_mask;
513 /* add clip mask GC */
515 XGCValues clip_gc_values;
516 unsigned long clip_gc_valuemask;
518 clip_gc_values.graphics_exposures = False;
519 clip_gc_values.clip_mask = pcxBitmapsX2[i]->clip_mask;
520 clip_gc_valuemask = GCGraphicsExposures | GCClipMask;
521 pcxBitmapsX2[i]->stored_clip_gc = XCreateGC(display,
528 printf("::: %ld, %ld, %ld, %ld, %ld, %ld, %ld\n",
530 pcxBitmaps[i]->clip_mask, pcxBitmapsX2[i]->clip_mask,
531 pcxBitmaps[i]->width, pcxBitmaps[i]->height,
532 pcxBitmapsX2[i]->width, pcxBitmapsX2[i]->height);
536 ZoomPixmap(display, pcxBitmaps[i]->gc,
537 pcxBitmaps[i]->clip_mask, pcxBitmapsX2[i]->clip_mask,
538 pcxBitmaps[i]->width, pcxBitmaps[i]->height,
539 pcxBitmapsX2[i]->width, pcxBitmapsX2[i]->height);
545 #endif /* BITMAP CREATION STUFF */
548 SetBitmaps_EM(pcxBitmapsX2);
551 objBitmap = pcxBitmapsX2[0];
552 botBitmap = pcxBitmapsX2[1];
553 sprBitmap = pcxBitmapsX2[2];
554 ttlBitmap = pcxBitmapsX2[3];
556 objPixmap = pcxBitmapsX2[0]->drawable;
557 botPixmap = pcxBitmapsX2[1]->drawable;
558 sprPixmap = pcxBitmapsX2[2]->drawable;
559 ttlPixmap = pcxBitmapsX2[3]->drawable;
561 objmaskBitmap = pcxBitmapsX2[0]->clip_mask;
562 botmaskBitmap = pcxBitmapsX2[1]->clip_mask;
563 sprmaskBitmap = pcxBitmapsX2[2]->clip_mask;
564 ttlmaskBitmap = pcxBitmapsX2[3]->clip_mask;
568 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
570 scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
573 screenPixmap = XCreatePixmap(display, xwindow,
574 MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
576 if (screenPixmap == 0)
578 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
579 XDisplayName(arg_display), "failed to create pixmap",
584 scorePixmap = XCreatePixmap(display, xwindow, 20 * TILEX, SCOREY,
586 if (scorePixmap == 0)
588 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
589 XDisplayName(arg_display), "failed to create pixmap",
595 screenBitmap->drawable = screenPixmap;
596 scoreBitmap->drawable = scorePixmap;
599 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
600 if (spriteBitmap == 0)
602 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
603 XDisplayName(arg_display), "failed to create pixmap",
608 redColour.pixel = screenWhitePixel;
609 whiteColour.pixel = screenBlackPixel;
610 gotRed = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
612 "red", &redColour, 0) > 0);
613 gotWhite = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap :
615 "white", &whiteColour, 0) > 0);
617 gcValues.graphics_exposures = False;
618 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
621 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
622 XDisplayName(arg_display), "failed to create graphics context",
627 gcValues.graphics_exposures = False;
628 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
631 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
632 XDisplayName(arg_display), "failed to create graphics context",
638 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
639 gcValues.graphics_exposures = False;
640 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
644 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
645 XDisplayName(arg_display), "failed to create graphics context",
650 gcValues.foreground = redColour.pixel;
651 gcValues.background = whiteColour.pixel;
652 gcValues.line_style = LineDoubleDash;
653 gcValues.graphics_exposures = False;
654 antsGC = XCreateGC(display, screenPixmap,
655 GCForeground | GCBackground | GCLineStyle |
660 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
661 XDisplayName(arg_display), "failed to create graphics context",
666 for (i = 0; i < 16; i++)
667 keycodes[i] = XKeysymToKeycode(display, keysyms[i]);
669 for (i = 0; i < 3; i++)
670 northKeyCode[i] = keycodes[i + 0];
671 for (i = 0; i < 3; i++)
672 eastKeyCode[i] = keycodes[i + 3];
673 for (i = 0; i < 3; i++)
674 southKeyCode[i] = keycodes[i + 6];
675 for (i = 0; i < 3; i++)
676 westKeyCode[i] = keycodes[i + 9];
677 for (i = 0; i < 3; i++)
678 fireKeyCode[i] = keycodes[i + 12];
679 for (i = 0; i < 1; i++)
680 escKeyCode[i] = keycodes[i + 15];
682 /* ----------------------------------------------------------------- */
686 Bitmap *bm = pcxBitmaps[2];
687 Pixmap clip_mask = bm->clip_mask;
692 int width = bm->width;
693 int height = bm->height;
698 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
699 width, height, AllPlanes, ZPixmap);
700 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
701 width, height, AllPlanes, ZPixmap);
704 if (src_ximage == NULL)
706 printf("src_ximage failed\n");
710 if (dst_ximage == NULL)
712 printf("dst_ximage failed\n");
716 printf("::: DISPLAY CLIP MASK ...\n");
718 for (x=0; x<width; x++)
720 for (y=0; y<height; y++)
722 unsigned long pixel = XGetPixel(src_ximage, x, y);
724 if (pixel != BlackPixel(display, screen))
725 pixel = WhitePixel(display, screen);
727 XPutPixel(dst_ximage, x, y, pixel);
731 printf("::: DISPLAY CLIP MASK NOW\n");
733 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
734 0, 13 * TILEY, width, height);
736 printf("::: DISPLAY CLIP MASK DONE\n");
741 /* ----------------------------------------------------------------- */
747 XGCValues clip_gc_values;
748 unsigned long clip_gc_valuemask;
754 GC gc = pcxBitmaps[ii]->stored_clip_gc;
756 GC gc = pcxBitmaps[ii]->gc;
759 Pixmap src_pixmap = pcxBitmaps[ii]->clip_mask;
760 Pixmap dst_pixmap = pcxBitmapsX2[ii]->clip_mask;
761 int src_width = pcxBitmaps[ii]->width;
762 int src_height = pcxBitmaps[ii]->height;
763 int dst_width = pcxBitmapsX2[ii]->width;
764 int dst_height = pcxBitmapsX2[ii]->height;
766 XImage *src_ximage, *dst_ximage;
767 byte *src_ptr, *dst_ptr;
772 boolean scale_down = (src_width > dst_width);
775 int zoom_factor = src_width / dst_width; /* currently very limited! */
777 int row_skip, col_skip;
779 printf("::: %d\n", scale_down);
781 xdebug("::: ZOOM STEP 1");
783 clip_gc_values.graphics_exposures = False;
784 clip_gc_valuemask = GCGraphicsExposures;
785 if ((gc = XCreateGC(display, pcxBitmaps[ii]->clip_mask,
786 clip_gc_valuemask, &clip_gc_values)) == None)
788 printf("XCreateGC failed\n");
792 xdebug("::: ZOOM STEP 2");
796 zoom_factor = src_width / dst_width;
798 /* adjust source image size to integer multiple of destination size */
799 src_width = dst_width * zoom_factor;
800 src_height = dst_height * zoom_factor;
804 zoom_factor = dst_width / src_width;
806 /* no adjustment needed when scaling up (some pixels may be left blank) */
809 /* copy source pixmap to temporary image */
810 if ((src_ximage = XGetImage(display, src_pixmap, 0, 0, src_width, src_height,
811 AllPlanes, ZPixmap)) == NULL)
812 Error(ERR_EXIT, "ZoomPixmap(): XGetImage() failed");
814 bits_per_pixel = src_ximage->bits_per_pixel;
815 bytes_per_pixel = (bits_per_pixel + 7) / 8;
817 printf("::: bits_per_pixel == %d\n", bits_per_pixel);
819 if ((dst_ximage = XCreateImage(display, visual, src_ximage->depth, ZPixmap,
820 0, NULL, dst_width, dst_height,
821 8, dst_width * bytes_per_pixel)) == NULL)
822 Error(ERR_EXIT, "ZoomPixmap(): XC reateImage() failed");
825 checked_malloc(dst_width * dst_height * bytes_per_pixel);
826 dst_ximage->byte_order = src_ximage->byte_order;
828 src_ptr = (byte *)src_ximage->data;
829 dst_ptr = (byte *)dst_ximage->data;
833 col_skip = (zoom_factor - 1) * bytes_per_pixel;
834 row_skip = col_skip * src_width;
836 /* scale image down by scaling factor 'zoom_factor' */
837 for (y = 0; y < src_height; y += zoom_factor, src_ptr += row_skip)
838 for (x = 0; x < src_width; x += zoom_factor, src_ptr += col_skip)
839 for (i = 0; i < bytes_per_pixel; i++)
840 *dst_ptr++ = *src_ptr++;
844 row_skip = src_width * bytes_per_pixel;
847 printf("::: %d, %d -> %d, %d [%d / %d]\n[%ld -> %ld (%ld)] [%ld -> %ld (%ld)]\n",
848 src_width, src_height,
849 dst_width, dst_height,
850 zoom_factor, bytes_per_pixel,
852 src_ptr + src_width * src_height * bytes_per_pixel,
853 src_width * src_height * bytes_per_pixel,
855 dst_ptr + dst_width * dst_height * bytes_per_pixel,
856 dst_width * dst_height * bytes_per_pixel);
862 for (i = 0; i < src_width * src_height * bytes_per_pixel;
865 byte x = *(byte *)(src_ptr + i);
867 printf("::: %d ...\n", i);
875 /* scale image up by scaling factor 'zoom_factor' */
876 for (y = 0; y < src_height; y++)
878 for (yy = 0; yy < zoom_factor; yy++)
884 printf("::: [%d -> %ld / %ld]\n", y, src_ptr, dst_ptr);
887 for (x = 0; x < src_width; x++)
889 for (xx = 0; xx < zoom_factor; xx++)
890 for (i = 0; i < bytes_per_pixel; i++)
894 printf("::: %d\n", *(src_ptr + i));
897 *dst_ptr++ = *(src_ptr + i);
909 xdebug("::: ZOOM STEP 9");
911 /* copy scaled image to destination pixmap */
912 XPutImage(display, dst_pixmap, gc, dst_ximage, 0, 0, 0, 0,
913 dst_width, dst_height);
915 /* free temporary images */
916 X11DestroyImage(src_ximage);
917 X11DestroyImage(dst_ximage);
920 xdebug("::: ZOOM DONE");
925 /* ----------------------------------------------------------------- */
929 Bitmap *bm = pcxBitmapsX2[2];
930 Pixmap clip_mask = bm->clip_mask;
935 int width = bm->width;
936 int height = bm->height;
941 XImage *src_ximage = XGetImage(display, clip_mask, 0, 0,
942 width, height, AllPlanes, ZPixmap);
943 XImage *dst_ximage = XGetImage(display, xwindow, 0, 0,
944 width, height, AllPlanes, ZPixmap);
947 if (src_ximage == NULL)
949 printf("src_ximage failed\n");
953 if (dst_ximage == NULL)
955 printf("dst_ximage failed\n");
959 printf("::: DISPLAY CLIP MASK ...\n");
961 for (x=0; x<width; x++)
963 for (y=0; y<height; y++)
965 unsigned long pixel = XGetPixel(src_ximage, x, y);
967 if (pixel != BlackPixel(display, screen))
968 pixel = WhitePixel(display, screen);
970 XPutPixel(dst_ximage, x, y, pixel);
974 printf("::: DISPLAY CLIP MASK NOW\n");
976 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
977 0, 13 * TILEY + height, width, height);
979 printf("::: DISPLAY CLIP MASK DONE\n");
984 /* ----------------------------------------------------------------- */
990 printf("::: GET IMAGE ...\n");
992 dst_ximage = XGetImage(display, xwindow, 0, 0,
993 16, 16, AllPlanes, ZPixmap);
994 if (dst_ximage == NULL)
996 printf("dst_ximage failed\n");
1000 printf("::: PUT IMAGE ...\n");
1002 XPutImage(display, xwindow, screenGC, dst_ximage, 0, 0,
1003 0, 13 * TILEY, 10, 10);
1005 printf("::: PUT IMAGE DONE\n");
1009 /* ----------------------------------------------------------------- */
1011 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
1012 if (arg_silence == 0)
1014 for (i = 0; i < SAMPLE_MAX; i++)
1020 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
1025 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
1028 if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
1030 if (read_sample(name, &sound_data[i], &sound_length[i]))
1035 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
1036 stop = sound_data[i] + sound_length[i];
1037 for (ptr = sound_data[i]; ptr < stop; ptr++)
1038 *ptr = (*ptr * mult) / 65536;
1042 if (pipe(sound_pipe) == -1)
1044 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
1050 if (sound_pid == -1)
1052 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
1057 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
1059 _exit(sound_thread());
1061 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
1064 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
1069 void close_all(void)
1073 if (sound_pid != -1)
1075 kill(sound_pid, SIGTERM);
1076 waitpid(sound_pid, 0, 0);
1079 if (sound_pipe[0] != -1)
1080 close(sound_pipe[0]);
1081 if (sound_pipe[1] != -1)
1082 close(sound_pipe[1]);
1084 for (i = 0; i < SAMPLE_MAX; i++)
1086 free(sound_data[i]);
1089 for (i = 0; i < 4; i++)
1091 XFreePixmap(display, xpmPixmaps[i]);
1092 for (i = 0; i < 4; i++)
1094 XFreePixmap(display, xpmBitmaps[i]);
1095 for (i = 0; i < 4; i++)
1098 xpmFreeColoursFunc(display, xpmAttributes[i].colormap,
1099 xpmAttributes[i].alloc_pixels,
1100 xpmAttributes[i].nalloc_pixels, 0);
1101 XpmFreeAttributes(&xpmAttributes[i]);
1106 xpmFreeColoursFunc(display,
1107 privateColourmap ? privateColourmap : defaultColourmap,
1108 &redColour.pixel, 1, 0);
1110 xpmFreeColoursFunc(display,
1111 privateColourmap ? privateColourmap : defaultColourmap,
1112 &whiteColour.pixel, 1, 0);
1115 XFreeGC(display, screenGC);
1117 XFreeGC(display, scoreGC);
1119 XFreeGC(display, spriteGC);
1121 XFreeGC(display, antsGC);
1123 XFreePixmap(display, screenPixmap);
1125 XFreePixmap(display, scorePixmap);
1127 XFreePixmap(display, spriteBitmap);
1129 XDestroyWindow(display, xwindow);
1131 XFreeCursor(display, cursor);
1132 if (privateColourmap)
1133 XFreeColormap(display, privateColourmap);
1135 free(privateColours);
1140 XCloseDisplay(display);
1144 /* ---------------------------------------------------------------------- */
1146 void sound_play(void)
1148 if (sound_pipe[1] != -1)
1150 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
1152 fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
1155 if (sound_pipe[0] != -1)
1157 close(sound_pipe[0]);
1161 if (sound_pipe[1] != -1)
1163 close(sound_pipe[1]);
1169 memset(play, 0, sizeof(play));
1172 /* ---------------------------------------------------------------------- */
1174 static int xpmAllocColourFunc(Display *display, Colormap colourmap,
1175 char *colourname, XColor *xcolour, void *closure)
1182 if (XParseColor(display, colourmap, colourname, xcolour) == 0)
1183 return(-1); /* invalid name */
1184 if (colourmap != privateColourmap)
1185 return(XAllocColor(display, colourmap, xcolour) != 0);
1187 /* first try to find an exact match */
1189 for (i = 0; i < privateNumColours; i++)
1191 if (privateColours[i].red == xcolour->red &&
1192 privateColours[i].green == xcolour->green &&
1193 privateColours[i].blue == xcolour->blue)
1199 privateFlags[match] = 1;
1200 xcolour->pixel = privateColours[match].pixel;
1204 /* then find an unallocated colour that is close to what we want */
1207 for (i = 0; i < privateNumColours; i++)
1209 if (privateFlags[i])
1210 continue; /* skip if it is already allocated */
1212 r = (privateColours[i].red - xcolour->red) / 256;
1213 g = (privateColours[i].green - xcolour->green) / 256;
1214 b = (privateColours[i].blue - xcolour->blue) / 256;
1215 sum = r * r + g * g + b * b;
1226 privateFlags[match] = 1;
1227 privateColours[match].red = xcolour->red;
1228 privateColours[match].green = xcolour->green;
1229 privateColours[match].blue = xcolour->blue;
1230 XStoreColor(display, colourmap, &privateColours[match]);
1231 xcolour->pixel = privateColours[match].pixel;
1232 return(1); /* found a close match */
1235 /* if all else fails, just find the closest colour and return it */
1238 for (i = 0; i < privateNumColours; i++)
1240 r = (privateColours[i].red - xcolour->red) / 256;
1241 g = (privateColours[i].green - xcolour->green) / 256;
1242 b = (privateColours[i].blue - xcolour->blue) / 256;
1243 sum = r * r + g * g + b * b;
1254 xcolour->red = privateColours[match].red;
1255 xcolour->green = privateColours[match].green;
1256 xcolour->blue = privateColours[match].blue;
1257 xcolour->pixel = privateColours[match].pixel;
1258 return(1); /* best we could do */
1261 return(0); /* still didnt find one, give up */
1264 static int xpmFreeColoursFunc(Display *display, Colormap colourmap,
1265 unsigned long *pixels, int npixels,
1268 if (colourmap != privateColourmap)
1269 XFreeColors(display, colourmap, pixels, npixels, 0);
1271 return(1); /* non-zero for success */