home *** CD-ROM | disk | FTP | other *** search
/ Power Bytes: Board & Strategy Games / PB2.ISO / chess / hittest.c < prev    next >
C/C++ Source or Header  |  1991-06-16  |  2KB  |  91 lines

  1. /*
  2.   C source for GNU CHESS
  3.  
  4.   Revision: 1990-09-30
  5.  
  6.   Modified by Daryl Baker for use in MS WINDOWS environment
  7.  
  8.   This file is part of CHESS.
  9.  
  10.   CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  11.   WARRANTY.  No author or distributor accepts responsibility to anyone for
  12.   the consequences of using it or for whether it serves any particular
  13.   purpose or works at all, unless he says so in writing.  Refer to the CHESS
  14.   General Public License for full details.
  15.  
  16.   Everyone is granted permission to copy, modify and redistribute CHESS, but
  17.   only under the conditions described in the CHESS General Public License.
  18.   A copy of this license is supposed to have been given to you along with
  19.   CHESS so you can know your rights and responsibilities.  It should be in a
  20.   file named COPYING.  Among other things, the copyright notice and this
  21.   notice must be preserved on all copies.
  22. */
  23.  
  24. #define NOATOM 
  25. #define NOCLIPBOARD
  26. #define NOCREATESTRUCT
  27. #define NOFONT
  28. #define NOSOUND
  29. #define NOWH
  30. #define NOCOMM
  31. #define NOKANJI
  32.  
  33. #include <windows.h>
  34. #include <stdio.h>
  35.  
  36. #include "defs.h"
  37.  
  38. static int deltay;
  39. static HRGN hitrgn[8];
  40.  
  41. void InitHitTest ( void )
  42. {
  43.    POINT ptls[4];
  44.    POINT toppt, botpt;
  45.    int i;
  46.    for (i=0; i<8; i++) {
  47.       QuerySqOrigin ( i, 0, ptls+0 );
  48.       QuerySqOrigin ( i, 8, ptls+1 );
  49.       QuerySqOrigin ( i+1, 8, ptls+2 );
  50.       QuerySqOrigin ( i+1, 0, ptls+3 );
  51.       hitrgn[i] = CreatePolygonRgn ( ptls, 4, WINDING);
  52.    }
  53.       QuerySqOrigin ( 0, 0, &botpt );
  54.       QuerySqOrigin ( 0, 8, &toppt );
  55.       deltay = botpt.y-toppt.y;
  56.  }
  57.  
  58. void Hittest_Destructor (VOID)
  59. {
  60.    int i;
  61.    for (i=0; i<8; i++)
  62.       DeleteObject ( hitrgn[i] );
  63. }
  64.  
  65. static int HorzHitTest ( int x, int y)
  66. {
  67.    int i;
  68.  
  69.    for ( i=0; i<8; i++) {
  70.       if ( PtInRegion ( hitrgn[i],x,y) ) return i;
  71.    }
  72.    return -1;
  73. }
  74.  
  75. int HitTest ( int x, int y)
  76. {
  77.    int xsq, ysq;
  78.    POINT sq00;
  79.  
  80.    xsq = HorzHitTest ( x, y );
  81.    if (xsq==-1) return -1;
  82.  
  83.    QuerySqOrigin ( 0,0, &sq00);
  84.  
  85.    if ( y > sq00.y ) return -1;
  86.    if ( y < (sq00.y-deltay) ) return -1;
  87.  
  88.    ysq = 7 - (y - (sq00.y-deltay) ) / (deltay/8);
  89.    return ( ysq*8 + xsq );
  90. }
  91.