OpenSceneGraph/examples/osgdirectinput/osgdirectinput.cpp
Robert Osfield a345a04254 From Wang Rui, "I implemented a customized viewer event traversal here to read state
changes from the DirectInput devices and add events to the event
queue. I've tested with the keyboard and joystick supports. Because of
only having a very old 6-button gamepad, I can't do more experiments.
Hope this will bring more ideas to those who face similar problems,
especially simulation game designers. :)

I didn't map all DirectInput key values to GUIEventAdapter key
symbols. Users may add more in the buildKeyMap() function freely. The
mouse handling operations are also ignored, but will be easily
improved in the same way of creating keyboard and joystick devices.

Please add a line:

FIND_PACKAGE(DirectInput)

in the CMakeLists of root directory. And in the examples/CMakeLists.txt:

IF(DIRECTINPUT_FOUND)
   ADD_SUBDIRECTORY(osgdirectinput)
ENDIF(DIRECTINPUT_FOUND)

DirectX SDK 2009 is used here, but an older version like DX8 should
also work in my opinion.
"
2010-12-13 11:34:33 +00:00

116 lines
4.4 KiB
C++

/* OpenSceneGraph example, osgdirectinput.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include <osgDB/ReadFile>
#include <osgGA/StateSetManipulator>
#include <osgViewer/api/Win32/GraphicsWindowWin32>
#include <osgViewer/Viewer>
#include <osgViewer/ViewerEventHandlers>
#include <iostream>
#include "DirectInputRegistry"
class CustomViewer : public osgViewer::Viewer
{
public:
CustomViewer() : osgViewer::Viewer() {}
virtual ~CustomViewer() {}
virtual void eventTraversal()
{
DirectInputRegistry::instance()->updateState( _eventQueue.get() );
osgViewer::Viewer::eventTraversal();
}
protected:
virtual void viewerInit()
{
osgViewer::GraphicsWindowWin32* windowWin32 =
dynamic_cast<osgViewer::GraphicsWindowWin32*>( _camera->getGraphicsContext() );
if ( windowWin32 )
{
HWND hwnd = windowWin32->getHWND();
DirectInputRegistry::instance()->initKeyboard( hwnd );
//DirectInputRegistry::instance()->initMouse( hwnd );
DirectInputRegistry::instance()->initJoystick( hwnd );
}
osgViewer::Viewer::viewerInit();
}
};
class JoystickHandler : public osgGA::GUIEventHandler
{
public:
JoystickHandler() {}
bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
{
switch ( ea.getEventType() )
{
case osgGA::GUIEventAdapter::KEYDOWN:
std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " down ***" << std::endl;
break;
case osgGA::GUIEventAdapter::KEYUP:
std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " up ***" << std::endl;
break;
case osgGA::GUIEventAdapter::USER:
{
const JoystickEvent* event = dynamic_cast<const JoystickEvent*>( ea.getUserData() );
if ( !event ) break;
const DIJOYSTATE2& js = event->_js;
for ( unsigned int i=0; i<128; ++i )
{
if ( js.rgbButtons[i] )
std::cout << "*** Joystick Btn" << i << " = " << (int)js.rgbButtons[i] << std::endl;
}
if ( js.lX==0x0000 ) std::cout << "*** Joystick X-" << std::endl;
else if ( js.lX==0xffff ) std::cout << "*** Joystick X+" << std::endl;
if ( js.lY==0 ) std::cout << "*** Joystick Y-" << std::endl;
else if ( js.lY==0xffff ) std::cout << "*** Joystick Y+" << std::endl;
}
return true;
default:
break;
}
return false;
}
};
int main( int argc, char** argv )
{
osg::ArgumentParser arguments( &argc, argv );
osg::Node* model = osgDB::readNodeFiles( arguments );
if ( !model ) model = osgDB::readNodeFile( "cow.osg" );
if ( !model )
{
std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
return 1;
}
CustomViewer viewer;
viewer.addEventHandler( new JoystickHandler );
viewer.addEventHandler( new osgViewer::StatsHandler );
viewer.addEventHandler( new osgViewer::WindowSizeHandler );
viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
viewer.setSceneData( model );
viewer.setUpViewInWindow( 250, 50, 800, 600 );
return viewer.run();
}