Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members   Related Pages  

OgreWin32Input.cpp

Go to the documentation of this file.
00001 /*
00002 -----------------------------------------------------------------------------
00003 This source file is part of OGRE
00004     (Object-oriented Graphics Rendering Engine)
00005 For the latest info, see http://www.ogre3d.org/
00006 
00007 Copyright © 2000-2002 The OGRE Team
00008 Also see acknowledgements in Readme.html
00009 
00010 This program is free software; you can redistribute it and/or modify it under
00011 the terms of the GNU Lesser General Public License as published by the Free Software
00012 Foundation; either version 2 of the License, or (at your option) any later
00013 version.
00014 
00015 This program is distributed in the hope that it will be useful, but WITHOUT
00016 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
00017 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
00018 
00019 You should have received a copy of the GNU Lesser General Public License along with
00020 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
00021 Place - Suite 330, Boston, MA 02111-1307, USA, or go to
00022 http://www.gnu.org/copyleft/lesser.txt.
00023 -----------------------------------------------------------------------------
00024 */
00025 
00026 #include "OgreWin32Input.h"
00027 #ifdef DX7INPUTONLY
00028 
00029 #include "OgreRenderWindow.h"
00030 #include "OgreLogManager.h"
00031 #include "OgreException.h"
00032 #include "OgreRoot.h"
00033 #include "OgreRenderSystem.h"
00034 
00035 #define DINPUT_BUFFERSIZE  16
00036 
00037 namespace Ogre {
00038     //-----------------------------------------------------------------------
00039     Win32Input::Win32Input() :
00040         InputReader()
00041     {
00042         mlpDI = 0;
00043         mlpDIKeyboard = 0;
00044         mlpDIMouse = 0;
00045 
00046         memset(mKeyboardBuffer,0,256);
00047 
00048 
00049     }
00050     //-----------------------------------------------------------------------
00051     Win32Input::~Win32Input()
00052     {
00053         // Shutdown
00054         if (mlpDIKeyboard)
00055         {
00056             mlpDIKeyboard->Unacquire();
00057             mlpDIKeyboard->Release();
00058             mlpDIKeyboard = 0;
00059         }
00060         if (mlpDIMouse)
00061         {
00062             mlpDIMouse->Unacquire();
00063             mlpDIMouse->Release();
00064             mlpDIMouse = 0;
00065         }
00066         if (mlpDI)
00067         {
00068             mlpDI->Release();
00069             mlpDI = 0;
00070         }
00071 
00072     }
00073 
00074     //-----------------------------------------------------------------------
00075     void Win32Input::initialise(RenderWindow* pWindow, bool useKeyboard, bool useMouse, bool useGameController)
00076     {
00077         HRESULT hr;
00078 
00079         LogManager::getSingleton().logMessage("Win32Input: DirectInput Activation Starts");
00080 
00081         // Get HINST
00082         HINSTANCE hInst = GetModuleHandle("OgrePlatform.dll");
00083 
00084 
00085         // Get HWND
00086         HWND hWnd;
00087         //pWindow->getCustomAttribute("HWND", &hWnd);
00088         // Decouple from Win32Window
00089         hWnd = GetActiveWindow();
00090 
00091         mHWnd = hWnd;
00092         RECT rect;
00093         GetClientRect(mHWnd, &rect);
00094         mMouseCenterX = (rect.right - rect.left) / 2;
00095         mMouseCenterY = (rect.bottom - rect.top) / 2;
00096         POINT p;
00097         p.x = mMouseCenterX;
00098         p.y = mMouseCenterY;
00099         ClientToScreen(mHWnd, &p);
00100         SetCursorPos(p.x, p.y);
00101         // hide cursor
00102         ShowCursor(FALSE);
00103 
00104 
00105 
00106         // Create direct input
00107         hr = DirectInputCreateEx(hInst, DIRECTINPUT_VERSION,
00108             IID_IDirectInput7, (void**)&mlpDI, NULL);
00109 
00110         if (FAILED(hr))
00111             throw Exception(hr, "Unable to initialise DirectInput.",
00112                 "Win32Input - initialise");
00113 
00114         if (useKeyboard)
00115         {
00116             LogManager::getSingleton().logMessage("Win32Input: Establishing keyboard input.");
00117 
00118             // Create keyboard device
00119             hr = mlpDI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
00120                 (void**)&mlpDIKeyboard, NULL);
00121 
00122 
00123             if (FAILED(hr))
00124                 throw Exception(hr, "Unable to create DirectInput keyboard device.",
00125                     "Win32Input - initialise");
00126 
00127             // Set data format
00128             hr = mlpDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
00129             if (FAILED(hr))
00130                 throw Exception(hr, "Unable to set DirectInput keyboard device data format.",
00131                     "Win32Input - initialise");
00132 
00133             // Make the window grab keyboard behaviour when foreground
00134             // NB Keyboard is never exclusive
00135             hr = mlpDIKeyboard->SetCooperativeLevel(hWnd,
00136                        DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
00137             if (FAILED(hr))
00138                 throw Exception(hr, "Unable to set DirectInput keyboard device co-operative level.",
00139                     "Win32Input - initialise");
00140 
00141 
00142             // Acquire input
00143             hr = mlpDIKeyboard->Acquire();
00144             if (FAILED(hr))
00145                 throw Exception(hr, "Unable to set aquire DirectInput keyboard device.",
00146                     "Win32Input - initialise");
00147 
00148             LogManager::getSingleton().logMessage("Win32Input: Keyboard input established.");
00149         }
00150         if (useMouse)
00151         {
00152             /* don't use DI
00153             LogManager::getSingleton().logMessage("Win32Input: Establishing mouse input.");
00154 
00155             // Create mouse device
00156             hr = mlpDI->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice7,
00157                 (void**)&mlpDIMouse, NULL);
00158 
00159 
00160             if (FAILED(hr))
00161                 throw Exception(hr, "Unable to create DirectInput mouse device.",
00162                     "Win32Input - initialise");
00163 
00164             // Set data format
00165             hr = mlpDIMouse->SetDataFormat(&c_dfDIMouse);
00166             if (FAILED(hr))
00167                 throw Exception(hr, "Unable to set DirectInput mouse device data format.",
00168                     "Win32Input - initialise");
00169 
00170             // Make the window grab mouse behaviour when foreground
00171             hr = mlpDIMouse->SetCooperativeLevel(hWnd,
00172                        DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
00173             if (FAILED(hr))
00174                 throw Exception(hr, "Unable to set DirectInput mouse device co-operative level.",
00175                     "Win32Input - initialise");
00176 
00177             // Acquire input
00178             hr = mlpDIKeyboard->Acquire();
00179             if (FAILED(hr))
00180                 throw Exception(hr, "Unable to set aquire DirectInput mouse device.",
00181                     "Win32Input - initialise");
00182 
00183             LogManager::getSingleton().logMessage("Win32Input: Mouse input established.");
00184             */
00185 
00186         }
00187 
00188 
00189         LogManager::getSingleton().logMessage("Win32Input: DirectInput OK.(**** YES !!!, the old version using DX7****)");
00190 
00191     }
00192 
00193     //-----------------------------------------------------------------------
00194     void Win32Input::capture(void)
00195     {
00196 
00197         HRESULT  hr;
00198 
00199         // Get keyboard state
00200         hr = mlpDIKeyboard->GetDeviceState(256,(LPVOID)&mKeyboardBuffer);
00201         if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
00202         {
00203             hr = mlpDIKeyboard->Acquire();
00204             if (hr == DIERR_OTHERAPPHASPRIO)
00205             {
00206                 hr = 0;
00207             }
00208             else
00209             {
00210                 hr = mlpDIKeyboard->GetDeviceState(256,(LPVOID)&mKeyboardBuffer);
00211             }
00212         }
00213         else if (hr == DIERR_OTHERAPPHASPRIO)
00214         {
00215             // We've gone into the background - ignore
00216             hr = 0;
00217         }
00218         else if (hr == DIERR_NOTINITIALIZED)
00219         {
00220             hr = 0;
00221         }
00222         else if (hr == E_PENDING)
00223         {
00224             hr = 0;
00225         }
00226         else if (FAILED(hr))
00227         {
00228             // Ignore for now
00229             // TODO - sort this out
00230             hr = 0;
00231         }
00232 
00233         /*
00234         DIMOUSESTATE diMouseState;
00235 
00236         if (mlpDIMouse)
00237         {
00238             hr = mlpDIMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&diMouseState);
00239             if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
00240             {
00241                 hr = mlpDIMouse->Acquire();
00242                 if (hr == DIERR_OTHERAPPHASPRIO)
00243                 {
00244                     hr = 0;
00245                 }
00246                 else
00247                 {
00248                     hr = mlpDIKeyboard->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&diMouseState);
00249                 }
00250             }
00251             else if (hr == DIERR_OTHERAPPHASPRIO)
00252             {
00253                 // We've gone into the background - ignore
00254                 hr = 0;
00255             }
00256             else if (hr == DIERR_NOTINITIALIZED)
00257             {
00258                 hr = 0;
00259             }
00260             else if (hr == E_PENDING)
00261             {
00262                 hr = 0;
00263             }
00264             else if (FAILED(hr))
00265             {
00266                 // Ignore for now
00267                 // TODO - sort this out
00268                 hr = 0;
00269             }
00270             else
00271             {
00272                 mMouseRelX = diMouseState.lX;
00273                 mMouseRelY = diMouseState.lY;
00274 
00275             }
00276         }
00277         */
00278         /*
00279             Only update mouse position if the window has the focus
00280          */
00281         if( mHWnd == GetForegroundWindow() )
00282         {
00283             POINT p;
00284             GetCursorPos(&p);
00285             ScreenToClient(mHWnd,&p);
00286             mMouseX = (Real)p.x;
00287             mMouseY = (Real)p.y;
00288             p.x = mMouseCenterX;
00289             p.y = mMouseCenterY;
00290             ClientToScreen(mHWnd, &p);
00291             if( IsWindowVisible( mHWnd ) )
00292             SetCursorPos(p.x, p.y);
00293         }
00294     }
00295     //-----------------------------------------------------------------------
00296     bool Win32Input::isKeyDownImmediate(KeyCode kc) const 
00297     {
00298         if (mKeyboardBuffer[kc] & 0x80)
00299         {
00300             return true;
00301         }
00302         else
00303         {
00304             return false;
00305         }
00306     }
00307 
00308     //-----------------------------------------------------------------------
00309     long Win32Input::getMouseRelX(void) const 
00310     {
00311         return (long)(mMouseX - mMouseCenterX);
00312     }
00313     //-----------------------------------------------------------------------
00314     long Win32Input::getMouseRelY(void) const 
00315     {
00316         return (long)(mMouseY - mMouseCenterY);
00317     }
00318 
00319 
00320 } // namespace
00321 #endif

Copyright © 2002-2003 by The OGRE Team
Last modified Sun Nov 28 19:48:51 2004