root/OpenSceneGraph/trunk/examples/osgdirectinput/osgdirectinput.cpp @ 12529

Revision 12529, 4.3 kB (checked in by robert, 3 years ago)

Replaced .osg with .osgt file usage

  • Property svn:eol-style set to native
Line 
1/* OpenSceneGraph example, osgdirectinput.
2*
3*  Permission is hereby granted, free of charge, to any person obtaining a copy
4*  of this software and associated documentation files (the "Software"), to deal
5*  in the Software without restriction, including without limitation the rights
6*  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7*  copies of the Software, and to permit persons to whom the Software is
8*  furnished to do so, subject to the following conditions:
9*
10*  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
11*  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
12*  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
13*  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
14*  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
15*  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
16*  THE SOFTWARE.
17*/
18
19#include <osgDB/ReadFile>
20#include <osgGA/StateSetManipulator>
21#include <osgViewer/api/Win32/GraphicsWindowWin32>
22#include <osgViewer/Viewer>
23#include <osgViewer/ViewerEventHandlers>
24#include <iostream>
25#include "DirectInputRegistry"
26
27class CustomViewer : public osgViewer::Viewer
28{
29public:
30    CustomViewer() : osgViewer::Viewer() {}
31    virtual ~CustomViewer() {}
32   
33    virtual void eventTraversal()
34    {
35        DirectInputRegistry::instance()->updateState( _eventQueue.get() );
36        osgViewer::Viewer::eventTraversal();
37    }
38   
39protected:
40    virtual void viewerInit()
41    {
42        osgViewer::GraphicsWindowWin32* windowWin32 =
43            dynamic_cast<osgViewer::GraphicsWindowWin32*>( _camera->getGraphicsContext() );
44        if ( windowWin32 )
45        {
46            HWND hwnd = windowWin32->getHWND();
47            DirectInputRegistry::instance()->initKeyboard( hwnd );
48            //DirectInputRegistry::instance()->initMouse( hwnd );
49            DirectInputRegistry::instance()->initJoystick( hwnd );
50        }
51        osgViewer::Viewer::viewerInit();
52    }
53};
54
55class JoystickHandler : public osgGA::GUIEventHandler
56{
57public:
58    JoystickHandler() {}
59   
60    bool handle( const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa )
61    {
62        switch ( ea.getEventType() )
63        {
64        case osgGA::GUIEventAdapter::KEYDOWN:
65            std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " down ***" << std::endl;
66            break;
67        case osgGA::GUIEventAdapter::KEYUP:
68            std::cout << "*** Key 0x" << std::hex << ea.getKey() << std::dec << " up ***" << std::endl;
69            break;
70        case osgGA::GUIEventAdapter::USER:
71            {
72                const JoystickEvent* event = dynamic_cast<const JoystickEvent*>( ea.getUserData() );
73                if ( !event ) break;
74               
75                const DIJOYSTATE2& js = event->_js;
76                for ( unsigned int i=0; i<128; ++i )
77                {
78                    if ( js.rgbButtons[i] )
79                        std::cout << "*** Joystick Btn" << i << " = " << (int)js.rgbButtons[i] << std::endl;
80                }
81               
82                if ( js.lX==0x0000 ) std::cout << "*** Joystick X-" << std::endl;
83                else if ( js.lX==0xffff ) std::cout << "*** Joystick X+" << std::endl;
84               
85                if ( js.lY==0 ) std::cout << "*** Joystick Y-" << std::endl;
86                else if ( js.lY==0xffff ) std::cout << "*** Joystick Y+" << std::endl;
87            }
88            return true;
89        default:
90            break;
91        }
92        return false;
93    }
94};
95
96int main( int argc, char** argv )
97{
98    osg::ArgumentParser arguments( &argc, argv );
99    osg::Node* model = osgDB::readNodeFiles( arguments );
100    if ( !model ) model = osgDB::readNodeFile( "cow.osgt" );
101    if ( !model )
102    {
103        std::cout << arguments.getApplicationName() <<": No data loaded" << std::endl;
104        return 1;
105    }
106   
107    CustomViewer viewer;
108    viewer.addEventHandler( new JoystickHandler );
109    viewer.addEventHandler( new osgViewer::StatsHandler );
110    viewer.addEventHandler( new osgViewer::WindowSizeHandler );
111    viewer.addEventHandler( new osgGA::StateSetManipulator(viewer.getCamera()->getOrCreateStateSet()) );
112    viewer.setSceneData( model );
113    viewer.setUpViewInWindow( 250, 50, 800, 600 );
114    return viewer.run();
115}
Note: See TracBrowser for help on using the browser.