home *** CD-ROM | disk | FTP | other *** search
/ NEXT Generation 27 / NEXT27.iso / pc / demos / emperor / dx3.exe / SDK / SAMPLES / IKLOWNS / CGINPUT.CPP < prev    next >
C/C++ Source or Header  |  1996-08-28  |  5KB  |  185 lines

  1. /*===========================================================================*\
  2. |
  3. |  File:        cginput.cpp
  4. |
  5. |  Description: 
  6. |       
  7. |-----------------------------------------------------------------------------
  8. |
  9. |  Copyright (C) 1995-1996 Microsoft Corporation.  All Rights Reserved.
  10. |
  11. |  Written by Moss Bay Engineering, Inc. under contract to Microsoft Corporation
  12. |
  13. \*===========================================================================*/
  14.  
  15. /**************************************************************************
  16.  
  17.     (C) Copyright 1995-1996 Microsoft Corp.  All rights reserved.
  18.  
  19.     You have a royalty-free right to use, modify, reproduce and 
  20.     distribute the Sample Files (and/or any modified version) in 
  21.     any way you find useful, provided that you agree that 
  22.     Microsoft has no warranty obligations or liability for any 
  23.     Sample Application Files which are modified. 
  24.  
  25.     we do not recomend you base your game on IKlowns, start with one of
  26.     the other simpler sample apps in the GDK
  27.  
  28.  **************************************************************************/
  29.  
  30. #include <windows.h>
  31. #include <mmsystem.h>
  32. #ifdef __WATCOMC__
  33. #include <mem.h>
  34. #else
  35. #include <memory.h>
  36. #endif
  37.  
  38. #include "cginput.h"
  39.  
  40. CGameInput::CGameInput (void)
  41. {
  42.     JOYINFO joypos;
  43.     memset(&caps, 0, sizeof(JOYCAPS) * 16);
  44.     memset(&joyThere, 0, sizeof(int) *  16);
  45.  
  46.     for (int x=0; x<QJoystick(); x++)
  47.     {
  48.         if (joyGetPos(JOYSTICKID1+x, &joypos) == JOYERR_NOERROR)
  49.         {
  50.             // there *is* a joystick 'x' installed!
  51.             ++joyThere[x];
  52.             joyGetDevCaps(JOYSTICKID1+x, &(caps[x]), sizeof(JOYCAPS));
  53.         }
  54.     }
  55.     Flush();
  56.     UpdateJoystick();
  57. }
  58.  
  59. CGameInput::~CGameInput(void)
  60. {
  61. }
  62.  
  63. void CGameInput::Flush(void)
  64. {
  65.     // Do things this way because, ahem... SetKeyboardState() doesn't appear
  66.     // to work on Win'95...
  67.     for (int x=0; x<256; x++)
  68.     {
  69.         GetKeyboard(x);
  70.     }
  71. }
  72.  
  73.  
  74. //--------------------------------------------------
  75.  
  76. int CGameInput::QKeyboard(void)
  77. {
  78.     // if there is a keyboard, returns 1 else 0
  79.     return(GetKeyboardType(1));
  80. }
  81.  
  82.  
  83. int CGameInput::QMouse(void)
  84. {
  85.     // if no mouse, 0 else number of buttons on mouse
  86.     return(GetSystemMetrics(SM_CMOUSEBUTTONS)); 
  87. }
  88.  
  89.  
  90. int CGameInput::QJoystick(void)
  91. {
  92.     // if no joystick(s), returns 0 else number of joysticks attached.
  93.     return(joyGetNumDevs());    
  94. }
  95.  
  96. int CGameInput::GetKeyboard(int key)
  97. {
  98.     // returns 0 if the key has been depressed, else returns 1 and sets key to code recd.
  99.     return (GetAsyncKeyState(key));
  100. }
  101.  
  102. int CGameInput::QKeyDepressed(int numkeys, int *keyarray)
  103. {
  104.     int x;
  105.  
  106.     // tells if keys in keyarray are currently depressed.  Returns 0 if not, 1 if all
  107.     if (!numkeys || !keyarray) 
  108.         return(0);
  109.  
  110.     for (x=0; x<numkeys ; x++)
  111.     {
  112.         // mask off top bit
  113.         if ((GetAsyncKeyState(keyarray[x])) == 0)
  114.             return(0);          
  115.     }
  116.     return(1);  
  117. }
  118.  
  119. int CGameInput::GetMouse(int& xpos, int&ypos, int& buttons)
  120. {
  121.     // returns 0 if no mouse action to report; else, 1 and fills in params
  122.     int button1, button2;
  123.     POINT pt;
  124.  
  125.     if (!GetCursorPos(&pt))
  126.         return(0);
  127.     
  128.     xpos = pt.x;
  129.     ypos = pt.y;
  130.     buttons = 0;
  131.  
  132.     button1 = GetAsyncKeyState(VK_LBUTTON);
  133.     button2 = GetAsyncKeyState(VK_RBUTTON);
  134.     if (button1)
  135.         buttons |= 1;
  136.     if (button2)
  137.         buttons |= 2;
  138.  
  139.     return(1);  
  140. }
  141.  
  142. int normalize (int val, int minval, int maxval)
  143. {
  144.     // error detection:
  145.     if ((maxval-minval) == 0)
  146.     {
  147.         return(0);      
  148.     }
  149.  
  150.     // zero-base:
  151.     val -= minval;
  152.  
  153.     // normalize to 0..200:
  154.     val = (200L * val) / (maxval-minval);
  155.  
  156.     // shift to -100 .. 100:
  157.     val -= 100;
  158.  
  159.     return(val);    
  160. }
  161.  
  162. void CGameInput::UpdateJoystick(void)
  163. {
  164.     for (int x=0; x<16; x++)
  165.     {
  166.         if (joyThere[x])
  167.             joyGetPos(JOYSTICKID1 + x, &(cached_joyinfo[x]));
  168.     }
  169. }
  170.  
  171. int CGameInput::GetJoystick(int joynum, JOYINFO * joypos)
  172. {
  173.     if ((joynum >= 16) || (joynum <= 0) || !joyThere[joynum-1])
  174.         return(0);      
  175.  
  176.     memcpy(joypos, &(cached_joyinfo[joynum-1]), sizeof(JOYINFO));
  177.  
  178.     // normalize the joypos to -100,0,100 scale....
  179.     joypos->wXpos = normalize(joypos->wXpos, caps[joynum-1].wXmin, caps[joynum-1].wXmax);
  180.     joypos->wYpos = normalize(joypos->wYpos, caps[joynum-1].wYmin, caps[joynum-1].wYmax);
  181.     joypos->wZpos = normalize(joypos->wZpos, caps[joynum-1].wZmin, caps[joynum-1].wZmax);
  182.  
  183.     return(1);  
  184. }
  185.