E:/Download/ois-1.0RC1/demos/OISConsole.cpp

Go to the documentation of this file.
00001 
00002 #include "OISInputManager.h"
00003 #include "OISException.h"
00004 #include "OISKeyboard.h"
00005 #include "OISMouse.h"
00006 #include "OISJoyStick.h"
00007 #include "OISEvents.h"
00008 
00009 //Advanced Usage
00010 #include "OISForceFeedback.h"
00011 
00012 #include <iostream>
00013 #include <vector>
00014 #include <sstream>
00015 
00017 #if defined OIS_WIN32_PLATFORM
00018 #  define WIN32_LEAN_AND_MEAN
00019 #  include "windows.h"
00020 #  include "resource.h"
00021    LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
00024 #elif defined OIS_LINUX_PLATFORM
00025 #  include <X11/Xlib.h>
00026    void checkX11Events();
00027 #endif
00029 using namespace OIS;
00030 
00031 //-- Some local prototypes --//
00032 void doStartup();
00033 void handleNonBufferedKeys();
00034 void handleNonBufferedMouse();
00035 void handleNonBufferedJoy( JoyStick* js );
00036 
00037 //-- Some hacky globals --//
00038 bool appRunning = true; //Global Exit Flag
00039 
00040 InputManager *g_InputManager = 0;
00041 Keyboard *g_kb  = 0;            //Keyboard Device
00042 Mouse    *g_m   = 0;            //Mouse Device
00043 JoyStick* g_joys[4] = {0,0,0,0};   //This demo supports up to 4 controllers
00044 ForceFeedback* g_ff[4] = {0,0,0,0};//Array to hold ff interface for each joy
00045 
00047 #if defined OIS_WIN32_PLATFORM
00048   HWND hWnd = 0;
00049 #elif defined OIS_LINUX_PLATFORM
00050   Display *xDisp = 0;
00051   Window xWin = 0;
00052 #endif
00053 
00055 class EventHandler : public KeyListener, public MouseListener, public JoyStickListener
00056 {
00057 public:
00058         EventHandler() {}
00059         ~EventHandler() {}
00060         bool keyPressed( const KeyEvent &arg ) {
00061                 std::cout << "\nKeyPressed {" << arg.key
00062                         << ", " << ((Keyboard*)(arg.device))->getAsString(arg.key)
00063                         << "} || Character (" << (char)arg.text << ")" << std::endl;
00064                 return true;
00065         }
00066         bool keyReleased( const KeyEvent &arg ) {
00067                 if( arg.key == KC_ESCAPE || arg.key == KC_Q )
00068                         appRunning = false;
00069                 return true;
00070         }
00071         bool mouseMoved( const MouseEvent &arg ) {
00072                 const OIS::MouseState& s = arg.state;
00073                 std::cout << "\nMouseMoved: Abs("
00074                                   << s.X.abs << ", " << s.Y.abs << ", " << s.Z.abs << ") Rel("
00075                                   << s.X.rel << ", " << s.Y.rel << ", " << s.Z.rel << ")";
00076                 return true;
00077         }
00078         bool mousePressed( const MouseEvent &arg, MouseButtonID id ) {
00079                 std::cout << "\nMousePressed: " << id;
00080                 return true;
00081         }
00082         bool mouseReleased( const MouseEvent &arg, MouseButtonID id ) {
00083                 std::cout << "\nMouseReleased: " << id;
00084                 return true;
00085         }
00086         bool buttonPressed( const JoyStickEvent &arg, int button ) {
00087                 std::cout << "\nJoy ButtonPressed: " << button;
00088                 return true;
00089         }
00090         bool buttonReleased( const JoyStickEvent &arg, int button ) {
00091                 return true;
00092         }
00093         bool axisMoved( const JoyStickEvent &arg, int axis )
00094         {
00095                 //Provide a little dead zone
00096                 if( arg.state.mAxes[axis].abs > 2500 || arg.state.mAxes[axis].abs < -2500 )
00097                         std::cout << "\nJoy Axis #: " << axis << " Value: " << arg.state.mAxes[axis].abs;
00098                 return true;
00099         }
00100         bool povMoved( const JoyStickEvent &arg, int pov )
00101         {
00102                 std::cout << "\nJoy POV" << pov << " ";
00103 
00104                 if( arg.state.mPOV[pov].direction & Pov::North ) //Going up
00105                         std::cout << "North";
00106                 else if( arg.state.mPOV[pov].direction & Pov::South ) //Going down
00107                         std::cout << "South";
00108 
00109                 if( arg.state.mPOV[pov].direction & Pov::East ) //Going right
00110                         std::cout << "East";
00111                 else if( arg.state.mPOV[pov].direction & Pov::West ) //Going left
00112                         std::cout << "West";
00113 
00114                 if( arg.state.mPOV[pov].direction == Pov::Centered ) //stopped/centered out
00115                         std::cout << "Centered";
00116                 return true;
00117         }
00118 };
00119 
00120 //Create a global instance
00121 EventHandler handler;
00122 
00123 int main()
00124 {
00125         std::cout << "\n\n*** OIS Console Demo App is starting up... *** \n";
00126         try
00127         {
00128                 doStartup();
00129                 std::cout << "\nStartup done... Hit 'q' or ESC to exit (or joy button 1)\n\n";
00130 
00131                 while(appRunning)
00132                 {
00133                         //Throttle down CPU usage
00134                         #if defined OIS_WIN32_PLATFORM
00135                           Sleep( 30 );
00136                           MSG  msg;
00137                           while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
00138                           {
00139                                 TranslateMessage( &msg );
00140                                 DispatchMessage( &msg );
00141                           }
00142                         #elif defined OIS_LINUX_PLATFORM
00143                           checkX11Events();
00144                           usleep( 500 );
00145                         #endif
00146 
00147                         if( g_kb )
00148                         {
00149                                 g_kb->capture();
00150                                 if( !g_kb->buffered() )
00151                                         handleNonBufferedKeys();
00152                         }
00153 
00154                         if( g_m )
00155                         {
00156                                 g_m->capture();
00157                                 if( !g_m->buffered() )
00158                                         handleNonBufferedMouse();
00159                         }
00160 
00161                         for( int i = 0; i < 4 ; ++i )
00162                         {
00163                                 if( g_joys[i] )
00164                                 {
00165                                         g_joys[i]->capture();
00166                                         if( !g_joys[i]->buffered() )
00167                                                 handleNonBufferedJoy( g_joys[i] );
00168                                 }
00169                         }
00170                 }
00171         }
00172         catch( const Exception &ex )
00173         {
00174                 #if defined OIS_WIN32_PLATFORM
00175                   MessageBox( NULL, ex.eText, "An exception has occured!", MB_OK |
00176                                 MB_ICONERROR | MB_TASKMODAL);
00177                 #else
00178                   std::cout << "\nOIS Exception Caught!\n" << "\t" << ex.eText << "[Line "
00179                         << ex.eLine << " in " << ex.eFile << "]\nExiting App";
00180                 #endif
00181         }
00182 
00183         if( g_InputManager )
00184         {
00185                 g_InputManager->destroyInputObject( g_kb );
00186                 g_InputManager->destroyInputObject( g_m );
00187 
00188                 for(int i = 0; i < 4; ++i)
00189                         g_InputManager->destroyInputObject( g_joys[i] );
00190 
00191                 InputManager::destroyInputSystem(g_InputManager);
00192         }
00193 
00194 #if defined OIS_LINUX_PLATFORM
00195         // Be nice to X and clean up the x window
00196         XDestroyWindow(xDisp, xWin);
00197         XCloseDisplay(xDisp);
00198 #endif
00199 
00200         std::cout << "\n\nGoodbye\n\n";
00201         return 0;
00202 }
00203 
00204 void doStartup()
00205 {
00206         ParamList pl;
00207 
00208 #if defined OIS_WIN32_PLATFORM
00209         //Create a capture window for Input Grabbing
00210         hWnd = CreateDialog( 0, MAKEINTRESOURCE(IDD_DIALOG1), 0,(DLGPROC)DlgProc);
00211         if( hWnd == NULL )
00212                 OIS_EXCEPT(E_General, "Failed to create Win32 Window Dialog!");
00213 
00214         ShowWindow(hWnd, SW_SHOW);
00215 
00216         std::ostringstream wnd;
00217         wnd << (size_t)hWnd;
00218 
00219         pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));
00220 
00221         //Default mode is foreground exclusive..but, we want to show mouse - so nonexclusive
00222         pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_FOREGROUND" )));
00223         pl.insert(std::make_pair(std::string("w32_mouse"), std::string("DISCL_NONEXCLUSIVE")));
00224 #elif defined OIS_LINUX_PLATFORM
00225         //Connects to default X window
00226         if( !(xDisp = XOpenDisplay(0)) )
00227                 OIS_EXCEPT(E_General, "Error opening X!");
00228         //Create a window
00229         xWin = XCreateSimpleWindow(xDisp,DefaultRootWindow(xDisp), 0,0, 100,100, 0, 0, 0);
00230         //bind our connection to that window
00231         XMapWindow(xDisp, xWin);
00232         //Select what events we want to listen to locally
00233         XSelectInput(xDisp, xWin, StructureNotifyMask);
00234         XEvent evtent;
00235         do
00236         {
00237                 XNextEvent(xDisp, &evtent);
00238         } while(evtent.type != MapNotify);
00239 
00240         std::ostringstream wnd;
00241         wnd << xWin;
00242 
00243         pl.insert(std::make_pair(std::string("WINDOW"), wnd.str()));
00244 
00245         //For this demo, show mouse and do not grab (confine to window)
00246 //      pl.insert(std::make_pair(std::string("x11_mouse_grab"), std::string("false")));
00247 //      pl.insert(std::make_pair(std::string("x11_mouse_hide"), std::string("false")));
00248 #endif
00249 
00250         //This never returns null.. it will raise an exception on errors
00251         g_InputManager = InputManager::createInputSystem(pl);
00252 
00253         unsigned int v = g_InputManager->getVersionNumber();
00254         std::cout << "OIS Version: " << (v>>16 ) << "." << ((v>>8) & 0x000000FF) << "." << (v & 0x000000FF)
00255                 << "\n\tRelease Name: " << g_InputManager->getVersionName()
00256                 << "\n\tPlatform: " << g_InputManager->inputSystemName()
00257                 << "\n\tNumber of Mice: " << g_InputManager->numMice()
00258                 << "\n\tNumber of Keyboards: " << g_InputManager->numKeyBoards()
00259                 << "\n\tNumber of Joys/Pads = " << g_InputManager->numJoysticks();
00260 
00261         if( g_InputManager->numKeyBoards() > 0 )
00262         {
00263                 g_kb = (Keyboard*)g_InputManager->createInputObject( OISKeyboard, true );
00264                 std::cout << "\n\nCreated buffered keyboard";
00265                 g_kb->setEventCallback( &handler );
00266         }
00267 
00268         if( g_InputManager->numMice() > 0 )
00269         {
00270                 g_m = (Mouse*)g_InputManager->createInputObject( OISMouse, true );
00271                 std::cout << "\nCreated buffered mouse";
00272                 g_m->setEventCallback( &handler );
00273 
00274                 const MouseState &ms = g_m->getMouseState();
00275                 ms.width = 100;
00276                 ms.height = 100;
00277         }
00278 
00279         //This demo only uses at max 4 joys
00280         int numSticks = g_InputManager->numJoysticks();
00281         if( numSticks > 4 )     numSticks = 4;
00282 
00283         for( int i = 0; i < numSticks; ++i )
00284         {
00285                 g_joys[i] = (JoyStick*)g_InputManager->createInputObject( OISJoyStick, true );
00286                 g_joys[i]->setEventCallback( &handler );
00287 
00288                 g_ff[i] = (ForceFeedback*)g_joys[i]->queryInterface( Interface::ForceFeedback );
00289                 if( g_ff[i] )
00290                 {
00291                         std::cout << "\nCreated buffered joystick with ForceFeedback support.\n";
00292                         //Dump out all the supported effects:
00293                         const ForceFeedback::SupportedEffectList &list = g_ff[i]->getSupportedEffects();
00294                         ForceFeedback::SupportedEffectList::const_iterator i = list.begin(),
00295                                 e = list.end();
00296                         for( ; i != e; ++i)
00297                                 std::cout << "Force =  " << i->first << " Type = " << i->second << std::endl;
00298                 }
00299                 else
00300                         std::cout << "\nCreated buffered joystick. **without** FF support";
00301         }
00302 }
00303 
00304 void handleNonBufferedKeys()
00305 {
00306         if( g_kb->isKeyDown( KC_ESCAPE ) || g_kb->isKeyDown( KC_Q ) )
00307                 appRunning = false;
00308 
00309         if( g_kb->isModifierDown(Keyboard::Shift) )
00310                 std::cout << "Shift is down..\n";
00311         if( g_kb->isModifierDown(Keyboard::Alt) )
00312                 std::cout << "Alt is down..\n";
00313         if( g_kb->isModifierDown(Keyboard::Ctrl) )
00314                 std::cout << "Ctrl is down..\n";
00315 }
00316 
00317 void handleNonBufferedMouse()
00318 {
00319         //Just dump the current mouse state
00320         const MouseState &ms = g_m->getMouseState();
00321         std::cout << "\nMouse: Abs(" << ms.X.abs << " " << ms.Y.abs << " " << ms.Z.abs
00322                 << ") B: " << ms.buttons << " Rel(" << ms.X.rel << " " << ms.Y.rel << " " << ms.Z.rel << ")";
00323 }
00324 
00325 void handleNonBufferedJoy( JoyStick* js )
00326 {
00327         //Just dump the current joy state
00328         const JoyStickState &joy = js->getJoyStickState();
00329         std::cout << "\nJoyStick: button bits: " << joy.buttons;
00330         for( int i = 0; i < joy.mAxes.size(); ++i )
00331                 std::cout << "\nAxis " << i << " X: " << joy.mAxes[i].abs;
00332 }
00333 
00334 #if defined OIS_WIN32_PLATFORM
00335 LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00336 {
00337         return FALSE;
00338 }
00339 #endif
00340 
00341 #if defined OIS_LINUX_PLATFORM
00342 //This is just here to show that you still recieve x11 events, as the lib only needs mouse/key events
00343 void checkX11Events()
00344 {
00345         XEvent event;
00346 
00347         //Poll x11 for events (keyboard and mouse events are caught here)
00348         while( XPending(xDisp) > 0 )
00349         {
00350                 XNextEvent(xDisp, &event);
00351                 //Handle Resize events
00352                 if( event.type == ConfigureNotify )
00353                 {
00354                         if( g_m )
00355                         {
00356                                 const MouseState &ms = g_m->getMouseState();
00357                                 ms.width = event.xconfigure.width;
00358                                 ms.height = event.xconfigure.height;
00359                         }
00360                 }
00361                 else if( event.type == DestroyNotify )
00362                 {
00363                         std::cout << "Exiting...\n";
00364                         appRunning = false;
00365                 }
00366                 else
00367                         std::cout << "\nUnknown X Event: " << event.type << std::endl;
00368         }
00369 }
00370 #endif

Generated on Sat Dec 1 20:13:51 2007 for OIS by  doxygen 1.5.4