home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / x / volume4 / xrobots / part01 / graphics.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-06-04  |  11.8 KB  |  418 lines

  1. /*
  2.  * graphics.c  --  xrobots v1.0
  3.  */
  4.  
  5. #include <X11/X.h>
  6. #include <X11/Intrinsic.h>
  7. #include <X11/Shell.h>
  8. #include "graphics.h"
  9. #include "game.h"
  10. #include "bitmaps.h"
  11.  
  12. static Pixmap playerP, dead_playerP, robotP, heapP, dotP;
  13. static Cursor upC, up_rightC, rightC, down_rightC, downC, 
  14.               down_leftC, leftC, up_leftC, stayC, cant_goC, thumbsC;
  15.  
  16. #define NUM_TMP_CURSOR_PIXMAPS 11
  17. static Pixmap tmp_pixmap[NUM_TMP_CURSOR_PIXMAPS]; 
  18.  
  19. /*----------------------------------------------------------------------*/
  20.  
  21. void
  22. init_pixmaps(top_shell)
  23.   Widget top_shell;
  24. {
  25. /*
  26.  * Let's make some pixmaps and some cursors.
  27.  * And then let's set the iconpixmap.
  28.  */
  29.   Pixmap tmpP;
  30.   XColor fgcolor,bgcolor;
  31.   Arg arg;
  32.  
  33.   playerP = XCreateBitmapFromData(display,playfield, 
  34.         Image_player_bits, Image_player_width, Image_player_height);
  35.  
  36.   dead_playerP = XCreateBitmapFromData(display,playfield, 
  37.         Image_player_dead_bits, Image_player_dead_width, 
  38.         Image_player_dead_height);
  39.  
  40.   robotP  = XCreateBitmapFromData(display,playfield, 
  41.         Image_robot_bits, Image_robot_width, Image_robot_height);
  42.  
  43.   heapP   = XCreateBitmapFromData(display,playfield, 
  44.         Image_heap_bits, Image_heap_width, Image_heap_height);
  45.  
  46.   dotP    = XCreateBitmapFromData(display,playfield, 
  47.         dot_bits, dot_width, dot_height);
  48.  
  49. /*- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  50.  
  51.   fgcolor.pixel = fg;
  52.   bgcolor.pixel = bg;
  53.   fgcolor.flags =  DoRed | DoGreen | DoBlue;
  54.   bgcolor.flags =  DoRed | DoGreen | DoBlue;
  55.   XQueryColor(display,DefaultColormapOfScreen(XtScreen(playfield_widget)), &fgcolor);
  56.   XQueryColor(display,DefaultColormapOfScreen(XtScreen(playfield_widget)), &bgcolor);
  57.  
  58.  
  59.   tmp_pixmap[0] = 
  60.   tmpP = XCreateBitmapFromData(display,playfield, up_bits, 
  61.                                   up_width, up_height);
  62.   upC  = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  63.  
  64.   tmp_pixmap[1] = 
  65.   tmpP = XCreateBitmapFromData(display,playfield, up_right_bits, 
  66.                                   up_right_width, up_right_height);
  67.   up_rightC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  68.  
  69.   tmp_pixmap[2] = 
  70.   tmpP = XCreateBitmapFromData(display,playfield, right_bits, 
  71.                                   right_width, right_height);
  72.   rightC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  73.  
  74.   tmp_pixmap[3] = 
  75.   tmpP = XCreateBitmapFromData(display,playfield, down_right_bits, 
  76.                                   down_right_width, down_right_height);
  77.   down_rightC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  78.  
  79.   tmp_pixmap[4] = 
  80.   tmpP = XCreateBitmapFromData(display,playfield, down_bits, 
  81.                                   down_width, down_height);
  82.   downC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  83.  
  84.   tmp_pixmap[5] = 
  85.   tmpP = XCreateBitmapFromData(display,playfield, down_left_bits, 
  86.                                   down_left_width, down_left_height);
  87.   down_leftC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  88.  
  89.   tmp_pixmap[6] = 
  90.   tmpP = XCreateBitmapFromData(display,playfield, left_bits, 
  91.                                   left_width, left_height);
  92.   leftC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  93.  
  94.   tmp_pixmap[7] = 
  95.   tmpP = XCreateBitmapFromData(display,playfield, up_left_bits, 
  96.                                   up_left_width, up_left_height);
  97.   up_leftC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  98.  
  99.   tmp_pixmap[8] = 
  100.   tmpP = XCreateBitmapFromData(display,playfield, stay_bits, 
  101.                                   stay_width, stay_height);
  102.   stayC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  103.  
  104.   tmp_pixmap[9] = 
  105.   tmpP = XCreateBitmapFromData(display,playfield, cant_go_bits, 
  106.                                   cant_go_width, cant_go_height);
  107.   cant_goC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,8,8);
  108.  
  109.   tmp_pixmap[10] = 
  110.   tmpP = XCreateBitmapFromData(display,playfield, thumbs_down_bits, 
  111.                                   thumbs_down_width, thumbs_down_height);
  112.   thumbsC = XCreatePixmapCursor(display, tmpP, tmpP, &fgcolor,&bgcolor,
  113.                  thumbs_down_x_hot,thumbs_down_y_hot);
  114.  
  115.   XtSetArg(arg,XtNiconPixmap,robotP);
  116.   XtSetValues(top_shell,&arg,1);
  117. }
  118.  
  119. /*----------------------------------------------------------------------*/
  120.  
  121. static void
  122. display_ok_move(x,y)
  123.   int x,y;
  124. {
  125.  
  126.   if(can_go(x,y))
  127.             /* show the icon for a good move */
  128.     XCopyPlane(display,dotP,playfield,gc,0,0,
  129.                dot_width,dot_height,
  130.                pos_to_coord(x),pos_to_coord(y),1);
  131.   else
  132.             /* or erase any previous dross */
  133.     if( INXRANGE(x) && INYRANGE(y) && (robot_array[x][y] == EMPTY))
  134.       XClearArea(display, playfield, 
  135.                  pos_to_coord(x), pos_to_coord(y),
  136.                  CELLSIZE, CELLSIZE, False);
  137. }
  138.  
  139. void
  140. display_possible_moves()
  141. {
  142.   if(!spiffy) return;
  143.   display_ok_move( human_x-1,human_y-1 );
  144.   display_ok_move( human_x  ,human_y-1 );
  145.   display_ok_move( human_x+1,human_y-1 );
  146.   display_ok_move( human_x-1,human_y   );
  147.  
  148.   display_ok_move( human_x+1,human_y   );
  149.   display_ok_move( human_x-1,human_y+1 );
  150.   display_ok_move( human_x  ,human_y+1 );
  151.   display_ok_move( human_x+1,human_y+1 );
  152. }
  153.  
  154.  
  155. void
  156. display_level()
  157. {
  158. /* Naive refresh algorithm.... */
  159.   int x,y;
  160.  
  161.   XClearWindow(display,playfield);
  162.   if(game_active)
  163.     XCopyPlane(display,playerP,playfield,gc,0,0,
  164.      Image_player_width,Image_player_height,
  165.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  166.   else
  167.     XCopyPlane(display,dead_playerP,playfield,gc,0,0,
  168.      Image_player_dead_width,Image_player_dead_height,
  169.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  170.  
  171.   for(y=0;y<MAXY;y++)
  172.   {
  173.     for(x=0;x<MAXX;x++)
  174.         switch (robot_array[x][y])
  175.         {
  176.           case ROBOT:
  177.             XCopyPlane(display,robotP,playfield,gc,0,0,
  178.                        Image_robot_width,Image_robot_height,
  179.                        pos_to_coord(x),pos_to_coord(y),1);
  180.             break;
  181.  
  182.           case HEAP:  
  183.             XCopyPlane(display,heapP,playfield,gc,0,0,
  184.                        Image_heap_width,Image_heap_height,
  185.                        pos_to_coord(x),pos_to_coord(y),1);
  186.             break;
  187.     }
  188.   }
  189. }
  190.  
  191.  
  192. void
  193. show_movement()
  194. {
  195. /* 
  196.  * draw just the changes.
  197.  */
  198.   int x,y;
  199.  
  200.   /* force a redraw of the area surrounding last position */
  201.   for(x=last_human_x-1;x<last_human_x+2;x++)
  202.     for(y=last_human_y-1;y<last_human_y+2;y++)
  203.       if( INXRANGE(x) && INYRANGE(y) )
  204.         robot_array_bak[x][y] = REDRAW;  
  205.  
  206.   /* 
  207.    * This is a little hacked up because the player isn't really in the 
  208.    * robot array.  Assigning the backup array to EMPTY avoids
  209.    * drawing over the player.
  210.    */
  211.   robot_array_bak[human_x][human_y] = EMPTY;   
  212.  
  213.   XCopyPlane(display,playerP,playfield,gc,0,0,
  214.         Image_player_width,Image_player_height,
  215.         pos_to_coord(human_x),pos_to_coord(human_y),1);
  216.  
  217.   for(y=0;y<MAXY;y++)
  218.   {
  219.     for(x=0;x<MAXX;x++)
  220.       if(robot_array[x][y] != robot_array_bak[x][y])
  221.         switch (robot_array[x][y])
  222.         {
  223.           case ROBOT:
  224.             XCopyPlane(display,robotP,playfield,gc,0,0,
  225.                        Image_robot_width,Image_robot_height,
  226.                        pos_to_coord(x),pos_to_coord(y),1);
  227.             break;
  228.  
  229.           case HEAP:
  230.             XCopyPlane(display,heapP,playfield,gc,0,0,
  231.                        Image_heap_width,Image_heap_height,
  232.                        pos_to_coord(x),pos_to_coord(y),1);
  233.             break;
  234.           default:
  235.             XClearArea(display, playfield, 
  236.                        pos_to_coord(x), pos_to_coord(y),
  237.                        CELLSIZE, CELLSIZE, False);
  238.     }
  239.   }
  240. }
  241.  
  242.  
  243.  
  244. /*ARGSUSED*/
  245. XtEventHandler
  246. redisplay_level(w, closure, event)
  247.   Widget w;
  248.   caddr_t closure;
  249.   XExposeEvent *event;
  250. {
  251. /* refresh entire screen -- e.g. expose events.  Just a little naive. */
  252.   display_level();
  253.   display_possible_moves();
  254. }
  255.  
  256.  
  257. void
  258. update_pointer(direction)
  259.   int direction;
  260. {
  261. /*
  262.  * set the cursor to the appropriate one.
  263.  */
  264.   int tmp_human_x = human_x, tmp_human_y = human_y;
  265.  
  266.   if(direction & UP)    tmp_human_y--;
  267.   if(direction & DOWN)  tmp_human_y++;
  268.   if(direction & LEFT)  tmp_human_x--;
  269.   if(direction & RIGHT) tmp_human_x++;
  270.  
  271.   if(!can_go(tmp_human_x,tmp_human_y)) {
  272.     XDefineCursor(display,playfield,cant_goC);
  273.     return;
  274.     }
  275.   if(direction&LEFT)
  276.     if(direction&UP)
  277.       XDefineCursor(display,playfield,up_leftC);
  278.     else 
  279.       if(direction&DOWN)
  280.           XDefineCursor(display,playfield,down_leftC);
  281.       else
  282.         XDefineCursor(display,playfield,leftC);
  283.  
  284.    if(direction&RIGHT)
  285.     if(direction&UP)
  286.       XDefineCursor(display,playfield,up_rightC);
  287.     else 
  288.       if(direction&DOWN)
  289.           XDefineCursor(display,playfield,down_rightC);
  290.       else
  291.         XDefineCursor(display,playfield,rightC);
  292.  
  293.   if(direction == UP)
  294.         XDefineCursor(display,playfield,upC);
  295.   if(direction == DOWN)
  296.         XDefineCursor(display,playfield,downC);
  297.   if(direction == STILL)
  298.         XDefineCursor(display,playfield,stayC);
  299.     
  300.  
  301. }
  302.  
  303.  
  304. /*----------------------------------------------------------------------*/
  305.  
  306. void free_pixmaps()
  307. {
  308.   int i;
  309.  
  310.   XFreeCursor(display,upC);
  311.   XFreeCursor(display,up_rightC);
  312.   XFreeCursor(display,rightC);
  313.   XFreeCursor(display,down_rightC);
  314.   XFreeCursor(display,downC);
  315.   XFreeCursor(display,down_leftC);
  316.   XFreeCursor(display,leftC); 
  317.   XFreeCursor(display,up_leftC); 
  318.   XFreeCursor(display,stayC);
  319.   XFreeCursor(display,thumbsC);
  320.   XFreeCursor(display,cant_goC);
  321.   for(i=0;i++;i<NUM_TMP_CURSOR_PIXMAPS) 
  322.     XFreePixmap(display,tmp_pixmap[i]);
  323.  
  324.   XFreePixmap(display,playerP);
  325.   XFreePixmap(display,dead_playerP);
  326.   XFreePixmap(display,robotP);
  327.   XFreePixmap(display,heapP);
  328.   XFreePixmap(display,dotP);
  329.  
  330. }
  331.  
  332.  
  333.  
  334. /*----------------------------------------------------------------------*/
  335.  
  336. void
  337. do_death()
  338. {
  339.   XDefineCursor(display,playfield,thumbsC);
  340.   XCopyPlane(display,dead_playerP,playfield,gc,0,0,
  341.      Image_player_dead_width,Image_player_dead_height,
  342.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  343. }
  344.  
  345. /*----------------------------------------------------------------------*/
  346.  
  347. void
  348. show_teleport()
  349. {
  350. /* 
  351.  * This may need to be slowed down (or changed) if you have a fast server.
  352.  * Looks okay on a sun 3/50.
  353.  */
  354. unsigned int i;
  355.  
  356.   if(!spiffy) return;
  357.  
  358.   for(i=100;i>0;i-=25)
  359.     XDrawArc(display,playfield,gc,
  360.       pos_to_coord(human_x)+(CELLSIZE/2)-(i/2)-2,
  361.       pos_to_coord(human_y)+(CELLSIZE/2)-(i/2)-2,
  362.       i,i,0,0);
  363. }
  364.  
  365. /*----------------------------------------------------------------------*/
  366.  
  367. static void
  368. do_sonic(agc)
  369. GC agc;
  370. {
  371. /* 
  372.  * do some effects for the sonic screwdriver...
  373.  */
  374. #define SPACING 3
  375. int center_x = pos_to_coord(human_x)+(CELLSIZE/2)-2,
  376.     center_y = pos_to_coord(human_y)+(CELLSIZE/2)-2;
  377. int i;
  378.  
  379.   if(!spiffy) return;
  380.  
  381.   for(i=pos_to_coord(human_x-1);i<pos_to_coord(human_x+2);i+=SPACING) {
  382.     XDrawLine(display,playfield,agc,center_x,center_y,i,pos_to_coord(human_y-1));
  383.     XCopyPlane(display,playerP,playfield,gc,0,0,
  384.      Image_player_width,Image_player_height,
  385.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  386.   }
  387.  
  388.   for(i=pos_to_coord(human_y-1);i<pos_to_coord(human_y+2);i+=SPACING) {
  389.     XDrawLine(display,playfield,agc,center_x,center_y,pos_to_coord(human_x+2),i);
  390.     XCopyPlane(display,playerP,playfield,gc,0,0,
  391.      Image_player_width,Image_player_height,
  392.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  393.   }
  394.  
  395.   for(i=pos_to_coord(human_x+2);i>pos_to_coord(human_x-1);i-=SPACING) {
  396.     XDrawLine(display,playfield,agc,center_x,center_y,i,pos_to_coord(human_y+2));
  397.     XCopyPlane(display,playerP,playfield,gc,0,0,
  398.      Image_player_width,Image_player_height,
  399.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  400.   }
  401.  
  402.   for(i=pos_to_coord(human_y+2);i>pos_to_coord(human_y-1);i-=SPACING) {
  403.     XDrawLine(display,playfield,agc,center_x,center_y,pos_to_coord(human_x-1),i);
  404.     XCopyPlane(display,playerP,playfield,gc,0,0,
  405.      Image_player_width,Image_player_height,
  406.      pos_to_coord(human_x),pos_to_coord(human_y),1);
  407.   }
  408. }
  409.  
  410.  
  411. void
  412. show_sonic()
  413. {
  414.   do_sonic(gc);        /* do it, */
  415.   do_sonic(cleargc);    /* then erase it. */
  416. }
  417.  
  418.