home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / games / volume2 / mines.shr / boardsw.c < prev    next >
C/C++ Source or Header  |  1987-11-20  |  5KB  |  238 lines

  1. /*
  2.  * handle the board subwindow 
  3.  *
  4.  * Copyright (c) 1987 Tom Anderson; 20831 Frank Waters Road;
  5.  * Stanwood, WA  98282.   All rights reserved.
  6.  */
  7. static char copyright[] = "Copyright 1987 Tom Anderson";
  8.  
  9. #include <stdio.h>
  10. #include <suntool/tool_hs.h>
  11. #include <suntool/panel.h>
  12. #include <suntool/gfxsw.h>
  13. #include <sunwindow/win_cursor.h>
  14. #include <sys/resource.h>
  15. #include <sys/ioctl.h>
  16. #include <strings.h>
  17.  
  18. #include "mines.h"
  19.  
  20. /*
  21.  * cross-hairs cursor
  22.  */
  23. static short CrosshairsImage[] = {
  24. #include "crosshairs.cursor"
  25. };
  26. DEFINE_CURSOR_FROM_IMAGE(CrosshairsCursor, 8, 8, PIX_SRC^PIX_DST, CrosshairsImage);
  27.  
  28. /*
  29.  * square pixrects
  30.  */
  31. unsigned short WhiteSquareImage[] = {
  32. #include "whiteSquare.icon"
  33. };
  34. mpr_static(WhiteSquarePR, 64, 64, 1, WhiteSquareImage);
  35.  
  36. unsigned short BlackSquareImage[] = {
  37. #include "blackSquare.icon"
  38. };
  39. mpr_static(BlackSquarePR, 64, 64, 1, BlackSquareImage);
  40.  
  41. unsigned short PlayerSquareImage[] = {
  42. #include "playerSquare.icon"
  43. };
  44. mpr_static(PlayerSquarePR, 64, 64, 1, PlayerSquareImage);
  45.  
  46. unsigned short MineSquareImage[] = {
  47. #include "mineSquare.icon"
  48. };
  49. mpr_static(MineSquarePR, 64, 64, 1, MineSquareImage);
  50.  
  51. unsigned short SafeSquareImage[] = {
  52. #include "safeSquare.icon"
  53. };
  54. mpr_static(SafeSquarePR, 64, 64, 1, SafeSquareImage);
  55.  
  56. /* board subwindow handles */
  57. struct toolsw * BoardSW;
  58. struct gfxsubwindow * Board;
  59.  
  60. /* square sizes */
  61. int SquareWidth = 32, SquareHeight = 32;
  62. /* icon offsets */
  63. struct pr_pos IconOffset = { 32, 32 };
  64.  
  65. /*
  66.  * board sigwinch handler 
  67.  */
  68. /*ARGSUSED*/
  69. boardSigwinch(sw)
  70.     caddr_t sw;
  71. {
  72.     gfxsw_interpretesigwinch(Board);
  73.     gfxsw_handlesigwinch(Board);
  74.     if (Board->gfx_flags & GFX_RESTART) {
  75.     Board->gfx_flags &= ~ GFX_RESTART;
  76.     DrawBoard();
  77.     }
  78. }
  79.  
  80. /*
  81.  * map a mouse coordinate to a board coordinate
  82.  */
  83. void
  84. mapMouseToBoard(mlocp, blocp)
  85.     struct pr_pos * mlocp;
  86.     BoardCoordinate * blocp;
  87. {
  88.     blocp->x = mlocp->x / (SquareWidth-1);
  89.     blocp->y = mlocp->y / (SquareHeight-1);
  90. }
  91.  
  92. /* 
  93.  * map a board coordinate to a mouse coordinate
  94.  */
  95. void
  96. mapBoardToMouse(blocp, mlocp)
  97.     BoardCoordinate * blocp;
  98.     struct pr_pos * mlocp;
  99. {
  100.     mlocp->x = blocp->x * (SquareWidth-1) - 1;
  101.     mlocp->y = blocp->y * (SquareHeight-1) - 1;
  102. }
  103.  
  104. /*
  105.  * board select() handler 
  106.  */
  107. /*ARGSUSED*/
  108. boardSelected(nullsw, ibits, obits, ebits, timer)
  109.     caddr_t * nullsw;
  110.     int * ibits, * obits, * ebits;
  111.     struct timeval ** timer;
  112. {
  113.     struct inputevent ie;
  114.     struct pr_pos mloc;
  115.     BoardCoordinate bloc;
  116.  
  117.     /*
  118.      * read the input event
  119.      */
  120.     if (input_readevent(BoardSW->ts_windowfd, &ie) == -1) {
  121.     perror("input failed");
  122.     abort();
  123.     }
  124.     if (win_inputposevent(&ie)) { 
  125.     switch(ie.ie_code) {
  126.     /*
  127.      * if it is an attempt to move
  128.      */
  129.     case MS_LEFT:
  130.         mloc.x = ie.ie_locx; mloc.y = ie.ie_locy;
  131.         mapMouseToBoard(&mloc, &bloc);
  132.         DoMove(&bloc);
  133.         break;
  134.     /*
  135.      * else if he is toggling a square's unsafe marking 
  136.      */
  137.     case MS_MIDDLE:
  138.         mloc.x = ie.ie_locx; mloc.y = ie.ie_locy;
  139.         mapMouseToBoard(&mloc, &bloc);
  140.         MarkSquare(&bloc, FALSE);
  141.         break;
  142.     /*
  143.      * else if he is toggling a square's safe marking 
  144.      */
  145.     case MS_RIGHT:
  146.         mloc.x = ie.ie_locx; mloc.y = ie.ie_locy;
  147.         mapMouseToBoard(&mloc, &bloc);
  148.         MarkSquare(&bloc, TRUE);
  149.         break;
  150.     }
  151.     }
  152.     * ibits = * obits = * ebits = 0;
  153. }
  154.  
  155. /*
  156.  * initialize the board subwindow
  157.  */
  158. void
  159. InitBoardSW()
  160. {
  161.     struct inputmask mask;
  162.     register unsigned int i;
  163.  
  164.     /*
  165.      * initialize the subwindow
  166.      */
  167.     if ((BoardSW = gfxsw_createtoolsubwindow(MinesTool, "",
  168.     TOOL_SWEXTENDTOEDGE, 
  169.     /* playing surface    +  victim area */
  170.     (SquareHeight-1) * SIDE_SIZE,
  171.     NULL)) == NULL) 
  172.     {
  173.     fprintf(stderr, "Can't create board subwindow\n");
  174.     exit(1);
  175.     }
  176.     Board = (struct gfxsubwindow *) BoardSW->ts_data;
  177.     gfxsw_getretained(Board); 
  178.     BoardSW->ts_io.tio_handlesigwinch = boardSigwinch;
  179.     BoardSW->ts_io.tio_selected = boardSelected;
  180.     input_imnull(&mask);
  181.     win_setinputcodebit(&mask, MS_LEFT);
  182.     win_setinputcodebit(&mask, MS_MIDDLE);
  183.     win_setinputcodebit(&mask, MS_RIGHT);
  184.     win_setinputcodebit(&mask, LOC_MOVEWHILEBUTDOWN);
  185.     win_setinputcodebit(&mask, LOC_WINEXIT);
  186.     mask.im_flags |= IM_NEGEVENT;
  187.     win_setinputmask(BoardSW->ts_windowfd, &mask, NULL, WIN_NULLLINK);
  188.     win_setcursor(BoardSW->ts_windowfd, &CrosshairsCursor);
  189. }
  190.  
  191. /*
  192.  * draw a square
  193.  */
  194. void
  195. DrawSquare(bloc)
  196.     BoardCoordinate * bloc;
  197. {
  198.     struct pr_pos mloc;
  199.     struct pixrect * pr;
  200.     Square * sqp = GetSquare(bloc);
  201.  
  202.     /* determine which pixrect to paint the square with */
  203.     if (sqp->occupied)
  204.     pr = &PlayerSquarePR;
  205.     else if (sqp->traversed)
  206.     pr = &BlackSquarePR;
  207.     else if ( ! GameOver && sqp->unsafe || GameOver && sqp->mined)
  208.     pr = &MineSquarePR;
  209.     else if ( ! GameOver && sqp->safe)
  210.     pr = &SafeSquarePR;
  211.     else 
  212.     pr = &WhiteSquarePR;
  213.     /* paint the square */
  214.     mapBoardToMouse(bloc, &mloc);
  215.     pw_rop(Board->gfx_pixwin, 
  216.     mloc.x, mloc.y, SquareWidth, SquareHeight, PIX_SRC, pr, IconOffset.x, IconOffset.y);
  217. }
  218.  
  219. /*
  220.  * draw the playing surface and victim area
  221.  */
  222. void
  223. DrawBoard()
  224. {
  225.     BoardCoordinate bloc;
  226.  
  227.     /* clear the board area */
  228.     pw_rop(Board->gfx_pixwin,
  229.     0, 0, Board->gfx_rect.r_width, Board->gfx_rect.r_height,
  230.     PIX_CLR, (struct pixrect *) 0, 0, 0);
  231.     /* draw the playing area */
  232.     for (bloc.x = 0 ; bloc.x < SIDE_SIZE ; bloc.x++) {
  233.     for (bloc.y = 0 ; bloc.y < SIDE_SIZE ; bloc.y++) {
  234.         DrawSquare(&bloc);
  235.     }
  236.     }
  237. }
  238.