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 #include "OgreWin32Input.h"
00026 #include "OgreRenderWindow.h"
00027 #include "OgreLogManager.h"
00028 #include "OgreException.h"
00029 #include "OgreRoot.h"
00030 #include "OgreRenderSystem.h"
00031 
00032 #define DINPUT_BUFFERSIZE  16
00033 
00034 namespace Ogre {
00035     //-----------------------------------------------------------------------
00036     Win32Input::Win32Input()
00037     {
00038         mlpDI = 0;
00039         mlpDIKeyboard = 0;
00040         mlpDIMouse = 0;
00041 
00042         memset(mKeyboardBuffer,0,256);
00043 
00044 
00045     }
00046     //-----------------------------------------------------------------------
00047     Win32Input::~Win32Input()
00048     {
00049         // Shutdown
00050         if (mlpDIKeyboard)
00051         {
00052             mlpDIKeyboard->Unacquire();
00053             mlpDIKeyboard->Release();
00054             mlpDIKeyboard = 0;
00055         }
00056         if (mlpDIMouse)
00057         {
00058             mlpDIMouse->Unacquire();
00059             mlpDIMouse->Release();
00060             mlpDIMouse = 0;
00061         }
00062         if (mlpDI)
00063         {
00064             mlpDI->Release();
00065             mlpDI = 0;
00066         }
00067 
00068     }
00069 
00070     //-----------------------------------------------------------------------
00071     void Win32Input::initialise(RenderWindow* pWindow, bool useKeyboard, bool useMouse, bool useGameController)
00072     {
00073         HRESULT hr;
00074 
00075         LogManager::getSingleton().logMessage("Win32Input: DirectInput Activation Starts");
00076 
00077         // Get HINST
00078         HINSTANCE hInst = GetModuleHandle("OgrePlatform.dll");
00079 
00080 
00081         // Get HWND
00082         HWND hWnd;
00083         //pWindow->getCustomAttribute("HWND", &hWnd);
00084         // Decouple from Win32Window
00085         hWnd = GetActiveWindow();
00086 
00087         mHWnd = hWnd;
00088         RECT rect;
00089         GetClientRect(mHWnd, &rect);
00090         mMouseCenterX = (rect.right - rect.left) / 2;
00091         mMouseCenterY = (rect.bottom - rect.top) / 2;
00092         POINT p;
00093         p.x = mMouseCenterX;
00094         p.y = mMouseCenterY;
00095         ClientToScreen(mHWnd, &p);
00096         SetCursorPos(p.x, p.y);
00097         // hide cursor
00098         ShowCursor(FALSE);
00099 
00100 
00101 
00102         // Create direct input
00103         hr = DirectInputCreateEx(hInst, DIRECTINPUT_VERSION,
00104             IID_IDirectInput7, (void**)&mlpDI, NULL);
00105 
00106         if (FAILED(hr))
00107             throw Exception(hr, "Unable to initialise DirectInput.",
00108                 "Win32Input - initialise");
00109 
00110         if (useKeyboard)
00111         {
00112             LogManager::getSingleton().logMessage("Win32Input: Establishing keyboard input.");
00113 
00114             // Create keyboard device
00115             hr = mlpDI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
00116                 (void**)&mlpDIKeyboard, NULL);
00117 
00118 
00119             if (FAILED(hr))
00120                 throw Exception(hr, "Unable to create DirectInput keyboard device.",
00121                     "Win32Input - initialise");
00122 
00123             // Set data format
00124             hr = mlpDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
00125             if (FAILED(hr))
00126                 throw Exception(hr, "Unable to set DirectInput keyboard device data format.",
00127                     "Win32Input - initialise");
00128 
00129             // Make the window grab keyboard behaviour when foreground
00130             // NB Keyboard is never exclusive
00131             hr = mlpDIKeyboard->SetCooperativeLevel(hWnd,
00132                        DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
00133             if (FAILED(hr))
00134                 throw Exception(hr, "Unable to set DirectInput keyboard device co-operative level.",
00135                     "Win32Input - initialise");
00136 
00137 
00138             // Acquire input
00139             hr = mlpDIKeyboard->Acquire();
00140             if (FAILED(hr))
00141                 throw Exception(hr, "Unable to set aquire DirectInput keyboard device.",
00142                     "Win32Input - initialise");
00143 
00144             LogManager::getSingleton().logMessage("Win32Input: Keyboard input established.");
00145         }
00146         if (useMouse)
00147         {
00148             /* don't use DI
00149             LogManager::getSingleton().logMessage("Win32Input: Establishing mouse input.");
00150 
00151             // Create mouse device
00152             hr = mlpDI->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice7,
00153                 (void**)&mlpDIMouse, NULL);
00154 
00155 
00156             if (FAILED(hr))
00157                 throw Exception(hr, "Unable to create DirectInput mouse device.",
00158                     "Win32Input - initialise");
00159 
00160             // Set data format
00161             hr = mlpDIMouse->SetDataFormat(&c_dfDIMouse);
00162             if (FAILED(hr))
00163                 throw Exception(hr, "Unable to set DirectInput mouse device data format.",
00164                     "Win32Input - initialise");
00165 
00166             // Make the window grab mouse behaviour when foreground
00167             hr = mlpDIMouse->SetCooperativeLevel(hWnd,
00168                        DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
00169             if (FAILED(hr))
00170                 throw Exception(hr, "Unable to set DirectInput mouse device co-operative level.",
00171                     "Win32Input - initialise");
00172 
00173             // Acquire input
00174             hr = mlpDIKeyboard->Acquire();
00175             if (FAILED(hr))
00176                 throw Exception(hr, "Unable to set aquire DirectInput mouse device.",
00177                     "Win32Input - initialise");
00178 
00179             LogManager::getSingleton().logMessage("Win32Input: Mouse input established.");
00180             */
00181 
00182         }
00183 
00184 
00185         LogManager::getSingleton().logMessage("Win32Input: DirectInput OK.");
00186 
00187     }
00188 
00189     //-----------------------------------------------------------------------
00190     void Win32Input::capture(void)
00191     {
00192 
00193         HRESULT  hr;
00194 
00195         // Get keyboard state
00196         hr = mlpDIKeyboard->GetDeviceState(256,(LPVOID)&mKeyboardBuffer);
00197         if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
00198         {
00199             hr = mlpDIKeyboard->Acquire();
00200             if (hr == DIERR_OTHERAPPHASPRIO)
00201             {
00202                 hr = 0;
00203             }
00204             else
00205             {
00206                 hr = mlpDIKeyboard->GetDeviceState(256,(LPVOID)&mKeyboardBuffer);
00207             }
00208         }
00209         else if (hr == DIERR_OTHERAPPHASPRIO)
00210         {
00211             // We've gone into the background - ignore
00212             hr = 0;
00213         }
00214         else if (hr == DIERR_NOTINITIALIZED)
00215         {
00216             hr = 0;
00217         }
00218         else if (hr == E_PENDING)
00219         {
00220             hr = 0;
00221         }
00222         else if (FAILED(hr))
00223         {
00224             // Ignore for now
00225             // TODO - sort this out
00226             hr = 0;
00227         }
00228 
00229         /*
00230         DIMOUSESTATE diMouseState;
00231 
00232         if (mlpDIMouse)
00233         {
00234             hr = mlpDIMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&diMouseState);
00235             if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
00236             {
00237                 hr = mlpDIMouse->Acquire();
00238                 if (hr == DIERR_OTHERAPPHASPRIO)
00239                 {
00240                     hr = 0;
00241                 }
00242                 else
00243                 {
00244                     hr = mlpDIKeyboard->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&diMouseState);
00245                 }
00246             }
00247             else if (hr == DIERR_OTHERAPPHASPRIO)
00248             {
00249                 // We've gone into the background - ignore
00250                 hr = 0;
00251             }
00252             else if (hr == DIERR_NOTINITIALIZED)
00253             {
00254                 hr = 0;
00255             }
00256             else if (hr == E_PENDING)
00257             {
00258                 hr = 0;
00259             }
00260             else if (FAILED(hr))
00261             {
00262                 // Ignore for now
00263                 // TODO - sort this out
00264                 hr = 0;
00265             }
00266             else
00267             {
00268                 mMouseRelX = diMouseState.lX;
00269                 mMouseRelY = diMouseState.lY;
00270 
00271             }
00272         }
00273         */
00274         /*
00275             Only update mouse position if the window has the focus
00276          */
00277         if( mHWnd == GetForegroundWindow() )
00278         {
00279             POINT p;
00280             GetCursorPos(&p);
00281             ScreenToClient(mHWnd,&p);
00282             mMouseX = (Real)p.x;
00283             mMouseY = (Real)p.y;
00284             p.x = mMouseCenterX;
00285             p.y = mMouseCenterY;
00286             ClientToScreen(mHWnd, &p);
00287             if( IsWindowVisible( mHWnd ) )
00288             SetCursorPos(p.x, p.y);
00289         }
00290     }
00291     //-----------------------------------------------------------------------
00292     bool Win32Input::isKeyDown(KeyCode kc)
00293     {
00294         if (mKeyboardBuffer[kc] & 0x80)
00295         {
00296             return true;
00297         }
00298         else
00299         {
00300             return false;
00301         }
00302     }
00303 
00304     //-----------------------------------------------------------------------
00305     int Win32Input::getMouseRelativeX(void)
00306     {
00307         return mMouseX - mMouseCenterX;
00308     }
00309     //-----------------------------------------------------------------------
00310     int Win32Input::getMouseRelativeY(void)
00311     {
00312         return mMouseY - mMouseCenterY;
00313     }
00314 
00315 
00316 } // namespace

Copyright © 2002-2003 by The OGRE Team
Last modified Fri May 14 23:22:55 2004