home *** CD-ROM | disk | FTP | other *** search
/ The Best of Select: Games 3 / cd.iso / os2 / pmgnuchs / hittest.c < prev    next >
Text File  |  1994-01-25  |  3KB  |  105 lines

  1. //
  2. //  Copyright (C) 1986, 1987, 1988, 1989, 1990 Free Software Foundation, Inc.
  3. //  Copyright (c) 1988, 1989, 1990  John Stanback
  4. //
  5. //  Project:    OS/2 PM Port of GNU CHESS 3.1 (PmChess)
  6. //
  7. //  Version:    1990-11-17
  8. //
  9. //   Module:    Hittest Support Logic (Hittest.c)
  10. //
  11. //   Porter:    Ported to Windows 3.0 by Darly Baker
  12. //
  13. //   Porter:    Ported to OS/2 1.2+ by Kent Cedola
  14. //
  15. //   System:    OS2 1.2 using Microsoft C 6.0
  16. //
  17. //  Remarks:    This code converted from Windows to PM using a straight port
  18. //              method with some editing improvements.  Based on ideas and
  19. //              code segments of Charles Petzold from artices in Microsoft
  20. //              Systems Journal.
  21. //
  22. //  License:
  23. //
  24. //    CHESS is distributed in the hope that it will be useful, but WITHOUT ANY
  25. //    WARRANTY.  No author or distributor accepts responsibility to anyone for
  26. //    the consequences of using it or for whether it serves any particular
  27. //    purpose or works at all, unless he says so in writing.  Refer to the
  28. //    CHESS General Public License for full details.
  29. //
  30. //    Everyone is granted permission to copy, modify and redistribute CHESS,
  31. //    but only under the conditions described in the CHESS General Public
  32. //    License.  A copy of this license is supposed to have been given to you
  33. //    along with CHESS so you can know your rights and responsibilities.  It
  34. //    should be in a file named COPYING.  Among other things, the copyright
  35. //    notice and this notice must be preserved on all copies.
  36. //
  37.  
  38. #define INCL_DOS
  39. #define INCL_PM
  40. #include <OS2.h>
  41. #include <Stdio.h>
  42. #include "PmChess.h"
  43. #include "Defs.h"
  44.  
  45.  
  46.  
  47. static int deltay;
  48. static HRGN hitrgn[8];
  49.  
  50. void InitHitTest ( void )
  51.   {
  52.   POINTL ptls[4];
  53.   POINTL toppt, botpt;
  54.   int i;
  55.  
  56.  
  57.    for (i=0; i<8; i++) {
  58.       QuerySqOrigin ( i, 0, ptls+0 );
  59.       //QuerySqOrigin ( i, 8, ptls+1 );
  60.       QuerySqOrigin ( i+1, 8, ptls+1 );
  61.       //QuerySqOrigin ( i+1, 0, ptls+3 );
  62.       hitrgn[i] = GpiCreateRegion(hpsClient, 1, (PRECTL)ptls);
  63.    }
  64.       QuerySqOrigin ( 0, 0, &botpt );
  65.       QuerySqOrigin ( 0, 8, &toppt );
  66.       deltay = botpt.y-toppt.y;
  67.  }
  68.  
  69. void Hittest_Destructor (VOID)
  70. {
  71.    int i;
  72.    for (i=0; i<8; i++)
  73.       GpiDestroyRegion(hpsClient, hitrgn[i] );
  74. }
  75.  
  76. static int HorzHitTest ( int x, int y)
  77. {
  78.    POINTL ptl;
  79.    int i;
  80.  
  81.    for ( i=0; i<8; i++) {
  82.       ptl.x = x;
  83.       ptl.y = y;
  84.       if ( GpiPtInRegion (hpsClient, hitrgn[i], &ptl) ) return i;
  85.    }
  86.    return -1;
  87. }
  88.  
  89. int HitTest ( int x, int y)
  90. {
  91.    int xsq, ysq;
  92.    POINTL sq00;
  93.  
  94.    xsq = HorzHitTest ( x, y );
  95.    if (xsq==-1) return -1;
  96.  
  97.    QuerySqOrigin ( 0,0, &sq00);
  98.  
  99.    if ( y > sq00.y ) return -1;
  100.    if ( y < (sq00.y-deltay) ) return -1;
  101.  
  102.    ysq = 7 - (y - (sq00.y-deltay) ) / (deltay/8);
  103.    return ( ysq*8 + xsq );
  104. }
  105.