source: OGRE/trunk/ogrenew/PlatformManagers/Win32/src/OgreWin32Input.cpp @ 692

Revision 692, 10.0 KB checked in by mattausch, 19 years ago (diff)

adding ogre 1.2 and dependencies

Line 
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4    (Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2005 The OGRE Team
8Also see acknowledgements in Readme.html
9
10This program is free software; you can redistribute it and/or modify it under
11the terms of the GNU Lesser General Public License as published by the Free Software
12Foundation; either version 2 of the License, or (at your option) any later
13version.
14
15This program is distributed in the hope that it will be useful, but WITHOUT
16ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
18
19You should have received a copy of the GNU Lesser General Public License along with
20this program; if not, write to the Free Software Foundation, Inc., 59 Temple
21Place - Suite 330, Boston, MA 02111-1307, USA, or go to
22http://www.gnu.org/copyleft/lesser.txt.
23-----------------------------------------------------------------------------
24*/
25
26#include "OgreWin32Input.h"
27#ifndef OGRE_NO_DX_INPUT
28#ifdef DX7INPUTONLY
29
30#include "OgreRenderWindow.h"
31#include "OgreLogManager.h"
32#include "OgreException.h"
33#include "OgreRoot.h"
34#include "OgreRenderSystem.h"
35
36#define DINPUT_BUFFERSIZE  64
37
38namespace Ogre {
39    //-----------------------------------------------------------------------
40    Win32Input::Win32Input() :
41                InputReader()
42    {
43        mlpDI = 0;
44        mlpDIKeyboard = 0;
45        mlpDIMouse = 0;
46
47        memset(mKeyboardBuffer,0,256);
48
49
50    }
51    //-----------------------------------------------------------------------
52    Win32Input::~Win32Input()
53    {
54        // Shutdown
55        if (mlpDIKeyboard)
56        {
57            mlpDIKeyboard->Unacquire();
58            mlpDIKeyboard->Release();
59            mlpDIKeyboard = 0;
60        }
61        if (mlpDIMouse)
62        {
63            mlpDIMouse->Unacquire();
64            mlpDIMouse->Release();
65            mlpDIMouse = 0;
66        }
67        if (mlpDI)
68        {
69            mlpDI->Release();
70            mlpDI = 0;
71        }
72
73    }
74
75    //-----------------------------------------------------------------------
76    void Win32Input::initialise(RenderWindow* pWindow, bool useKeyboard, bool useMouse, bool useGameController)
77    {
78        HRESULT hr;
79
80        LogManager::getSingleton().logMessage("Win32Input: DirectInput Activation Starts");
81
82        // Get HINST
83        HINSTANCE hInst = GetModuleHandle(OGRE_PLATFORM_LIB);
84
85
86        // Get HWND
87        HWND hWnd;
88        //pWindow->getCustomAttribute("HWND", &hWnd);
89        // Decouple from Win32Window
90        hWnd = GetActiveWindow();
91
92        mHWnd = hWnd;
93        RECT rect;
94        GetClientRect(mHWnd, &rect);
95        mMouseCenterX = (rect.right - rect.left) / 2;
96        mMouseCenterY = (rect.bottom - rect.top) / 2;
97        POINT p;
98        p.x = mMouseCenterX;
99        p.y = mMouseCenterY;
100        ClientToScreen(mHWnd, &p);
101        SetCursorPos(p.x, p.y);
102        // hide cursor
103        ShowCursor(FALSE);
104
105
106
107        // Create direct input
108        hr = DirectInputCreateEx(hInst, DIRECTINPUT_VERSION,
109            IID_IDirectInput7, (void**)&mlpDI, NULL);
110
111        if (FAILED(hr))
112            throw Exception(hr, "Unable to initialise DirectInput.",
113                "Win32Input - initialise");
114
115        if (useKeyboard)
116        {
117            LogManager::getSingleton().logMessage("Win32Input: Establishing keyboard input.");
118
119            // Create keyboard device
120            hr = mlpDI->CreateDeviceEx(GUID_SysKeyboard, IID_IDirectInputDevice7,
121                (void**)&mlpDIKeyboard, NULL);
122
123
124            if (FAILED(hr))
125                throw Exception(hr, "Unable to create DirectInput keyboard device.",
126                    "Win32Input - initialise");
127
128            // Set data format
129            hr = mlpDIKeyboard->SetDataFormat(&c_dfDIKeyboard);
130            if (FAILED(hr))
131                throw Exception(hr, "Unable to set DirectInput keyboard device data format.",
132                    "Win32Input - initialise");
133
134            // Make the window grab keyboard behaviour when foreground
135            // NB Keyboard is never exclusive
136            hr = mlpDIKeyboard->SetCooperativeLevel(hWnd,
137                       DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
138            if (FAILED(hr))
139                throw Exception(hr, "Unable to set DirectInput keyboard device co-operative level.",
140                    "Win32Input - initialise");
141
142
143            // Acquire input
144            hr = mlpDIKeyboard->Acquire();
145            if (FAILED(hr))
146                throw Exception(hr, "Unable to set aquire DirectInput keyboard device.",
147                    "Win32Input - initialise");
148
149            LogManager::getSingleton().logMessage("Win32Input: Keyboard input established.");
150        }
151        if (useMouse)
152        {
153            /* don't use DI
154            LogManager::getSingleton().logMessage("Win32Input: Establishing mouse input.");
155
156            // Create mouse device
157            hr = mlpDI->CreateDeviceEx(GUID_SysMouse, IID_IDirectInputDevice7,
158                (void**)&mlpDIMouse, NULL);
159
160
161            if (FAILED(hr))
162                throw Exception(hr, "Unable to create DirectInput mouse device.",
163                    "Win32Input - initialise");
164
165            // Set data format
166            hr = mlpDIMouse->SetDataFormat(&c_dfDIMouse);
167            if (FAILED(hr))
168                throw Exception(hr, "Unable to set DirectInput mouse device data format.",
169                    "Win32Input - initialise");
170
171            // Make the window grab mouse behaviour when foreground
172            hr = mlpDIMouse->SetCooperativeLevel(hWnd,
173                       DISCL_BACKGROUND | DISCL_NONEXCLUSIVE);
174            if (FAILED(hr))
175                throw Exception(hr, "Unable to set DirectInput mouse device co-operative level.",
176                    "Win32Input - initialise");
177
178            // Acquire input
179            hr = mlpDIKeyboard->Acquire();
180            if (FAILED(hr))
181                throw Exception(hr, "Unable to set aquire DirectInput mouse device.",
182                    "Win32Input - initialise");
183
184            LogManager::getSingleton().logMessage("Win32Input: Mouse input established.");
185            */
186
187        }
188
189
190        LogManager::getSingleton().logMessage("Win32Input: DirectInput OK.(**** YES !!!, the old version using DX7****)");
191
192    }
193
194    //-----------------------------------------------------------------------
195    void Win32Input::capture(void)
196    {
197
198        HRESULT  hr;
199
200        // Get keyboard state
201        hr = mlpDIKeyboard->GetDeviceState(256,(LPVOID)&mKeyboardBuffer);
202        if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
203        {
204            hr = mlpDIKeyboard->Acquire();
205            if (hr == DIERR_OTHERAPPHASPRIO)
206            {
207                hr = 0;
208            }
209            else
210            {
211                hr = mlpDIKeyboard->GetDeviceState(256,(LPVOID)&mKeyboardBuffer);
212            }
213        }
214        else if (hr == DIERR_OTHERAPPHASPRIO)
215        {
216            // We've gone into the background - ignore
217            hr = 0;
218        }
219        else if (hr == DIERR_NOTINITIALIZED)
220        {
221            hr = 0;
222        }
223        else if (hr == E_PENDING)
224        {
225            hr = 0;
226        }
227        else if (FAILED(hr))
228        {
229            // Ignore for now
230            // TODO - sort this out
231            hr = 0;
232        }
233
234        /*
235        DIMOUSESTATE diMouseState;
236
237        if (mlpDIMouse)
238        {
239            hr = mlpDIMouse->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&diMouseState);
240            if (hr == DIERR_INPUTLOST || hr == DIERR_NOTACQUIRED)
241            {
242                hr = mlpDIMouse->Acquire();
243                if (hr == DIERR_OTHERAPPHASPRIO)
244                {
245                    hr = 0;
246                }
247                else
248                {
249                    hr = mlpDIKeyboard->GetDeviceState(sizeof(DIMOUSESTATE),(LPVOID)&diMouseState);
250                }
251            }
252            else if (hr == DIERR_OTHERAPPHASPRIO)
253            {
254                // We've gone into the background - ignore
255                hr = 0;
256            }
257            else if (hr == DIERR_NOTINITIALIZED)
258            {
259                hr = 0;
260            }
261            else if (hr == E_PENDING)
262            {
263                hr = 0;
264            }
265            else if (FAILED(hr))
266            {
267                // Ignore for now
268                // TODO - sort this out
269                hr = 0;
270            }
271            else
272            {
273                mMouseRelX = diMouseState.lX;
274                mMouseRelY = diMouseState.lY;
275
276            }
277        }
278        */
279        /*
280            Only update mouse position if the window has the focus
281         */
282        if( mHWnd == GetForegroundWindow() )
283        {
284            POINT p;
285            GetCursorPos(&p);
286            ScreenToClient(mHWnd,&p);
287            mMouseX = (Real)p.x;
288            mMouseY = (Real)p.y;
289            p.x = mMouseCenterX;
290            p.y = mMouseCenterY;
291            ClientToScreen(mHWnd, &p);
292            if( IsWindowVisible( mHWnd ) )
293            SetCursorPos(p.x, p.y);
294        }
295    }
296    //-----------------------------------------------------------------------
297    bool Win32Input::isKeyDownImmediate(KeyCode kc) const
298    {
299        if (mKeyboardBuffer[kc] & 0x80)
300        {
301            return true;
302        }
303        else
304        {
305            return false;
306        }
307    }
308
309    //-----------------------------------------------------------------------
310    long Win32Input::getMouseRelX(void) const
311    {
312        return (long)(mMouseX - mMouseCenterX);
313    }
314    //-----------------------------------------------------------------------
315    long Win32Input::getMouseRelY(void) const
316    {
317        return (long)(mMouseY - mMouseCenterY);
318    }
319
320
321} // namespace
322#endif
323#endif
Note: See TracBrowser for help on using the repository browser.