home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xinvaders / spacers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-05-03  |  3.4 KB  |  167 lines

  1. /* 
  2. Copyright notice:
  3.  
  4. This is mine.  I'm only letting you use it.  Period.  Feel free to rip off
  5. any of the code you see fit, but have the courtesy to give me credit.
  6. Otherwise great hairy beasties will rip your eyes out and eat your flesh
  7. when you least expect it.
  8.  
  9. Jonny Goldman <jonathan@think.com>
  10.  
  11. Wed May  8 1991
  12. */
  13.  
  14. /* spacers.c - handle movement, etc. of the little space ships. */
  15.  
  16. #include "vaders.h"
  17.  
  18. Boolean spacer_shown;
  19.  
  20. #define SPACERY 0
  21. #define SPACERV 2*scale
  22.  
  23. typedef struct _SpacerRec {
  24.   int x;            /* Location. */
  25.   int width, height;        /* box of this rock. */
  26.   int score;            /* value of this guy */
  27.   XImage *shape_image;        /* an XImage for the spaceship */
  28. } SpacerRec, *Spacer;
  29.  
  30. SpacerRec spacerrec;
  31.  
  32. Spacer spacer = &spacerrec;
  33.  
  34. #define SpacerNearPoint(spacer, x, y)    \
  35.   ((spacer)->x <= (x) && (x) < (spacer)->x + (spacer)->width  && \
  36.    y <= SPACERY + (spacer)->height && y > SPACERY)
  37.  
  38. int spacer_counter;        /* number of steps to wait for new spacer */
  39.  
  40. int showing_sexplosion = FALSE;
  41.  
  42. /* now the code */
  43.  
  44. void PaintSpacer(gc)
  45.      GC gc;
  46. {
  47.     XPutImage(dpy, gamewindow, gc, spacer->shape_image,
  48.           0, 0, spacer->x, SPACERY, spacer->width, spacer->height);
  49. }
  50.  
  51.  
  52. void ShowSexplosion(gc)
  53. GC gc;
  54. {
  55.   char score[5];
  56.  
  57.   sprintf(score,"%3d", spacer->score);
  58.   XDrawString(dpy, gamewindow, gc, spacer->x, SPACERY+spacer->height, score, 3);
  59.  
  60. }
  61.  
  62. /*
  63.  * Destroy the Spacer, much like the ship.
  64.  */
  65.  
  66. static void DestroySpacer()
  67. {
  68.   score += spacer->score;
  69.   PaintScore();
  70.  
  71.   if(!paused) {
  72.     PaintSpacer(backgc);
  73.     ShowSexplosion(spacergc);
  74.     if (spacertimerid)
  75.       XtRemoveTimeOut(spacertimerid);
  76.     XtAddTimeOut(1000, MoveSpacer, (Opaque) MoveSpacer);
  77.     showing_sexplosion = TRUE;
  78.     spacer_shown = FALSE;
  79.   }
  80. }
  81.  
  82. Boolean ShotHitsSpacer(x, y)
  83.      int x, y;
  84. {
  85.   if(spacer_shown) {
  86.     if (SpacerNearPoint(spacer, x, y)) {
  87.       DestroySpacer();
  88.       return TRUE;
  89.     }
  90.   }
  91.   return FALSE;
  92. }
  93.  
  94.  
  95.  
  96. void MakeSpacer()
  97. {
  98.   spacer_shown = TRUE;
  99.  
  100.   spacer->x=0;
  101.   spacer->score = 50*(random()%6+1);
  102.   PaintSpacer(spacergc);
  103. }
  104.   
  105. /*ARGSUSED*/
  106. void MoveSpacer(closure, id)
  107.      Opaque closure;
  108.      XtIntervalId id;
  109. {
  110.   if (closure != (Opaque) MoveSpacer) return;
  111.   spacertimerid = XtAddTimeOut(spacerwait, MoveSpacer, (Opaque) MoveSpacer);
  112.   if (!paused) {
  113.     if (showing_sexplosion) {
  114.       showing_sexplosion = FALSE;
  115.       ShowSexplosion(backgc);
  116.       spacer_shown = FALSE;
  117.       spacer_counter = 1000;
  118.       return;
  119.     }
  120.     if (spacer_shown) {
  121.       PaintSpacer(backgc);
  122.       spacer->x += SPACERV;
  123.       if (spacer->x < gamewidth-spacer->width) {
  124.     PaintSpacer(spacergc);
  125.       } else {
  126.     spacer_shown = FALSE;
  127.     spacer_counter = 1000;
  128.       }
  129.     } else
  130.       if (spacer_counter-- == 0) MakeSpacer();
  131.   }
  132. }
  133.  
  134.  
  135. #include "spacer1.bit"
  136. #include "spacer2.bit"
  137.  
  138. int ReadSpacerImages()
  139. {
  140.   spacer->width = (scale == 1) ? spacer1_width : spacer2_width;
  141.   spacer->height = (scale == 1) ? spacer1_height : spacer2_height;
  142.  
  143.   spacer->shape_image = XCreateImage(dpy,
  144.                      DefaultVisual(dpy, DefaultScreen(dpy)),
  145.                      1,
  146.                      XYBitmap,
  147.                      0,
  148.                      (scale == 1) ? spacer1_bits : spacer2_bits,
  149.                      spacer->width, spacer->height,
  150.                      8, 0);
  151.  
  152.   spacer->shape_image->bitmap_bit_order = LSBFirst;
  153.   spacer->shape_image->byte_order = LSBFirst;
  154.  
  155.   return BitmapSuccess;
  156. }
  157.  
  158. void InitSpacers()
  159. {
  160.   if(ReadSpacerImages()!= BitmapSuccess) {
  161.     fprintf(stderr, "Error reading Spacer image\n");
  162.     exit(10);
  163.   }
  164.  
  165.   spacertimerid = NULL;
  166. }
  167.