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

Go to the documentation of this file.
00001 
00002 //Define this to make OIS use dynamic linking imports:
00003 #define OIS_DYNAMIC_LIB
00004 
00005 #include "OIS.h"
00006 
00007 #include <iostream>
00008 #include <vector>
00009 #include <sstream>
00010 
00012 #if defined OIS_WIN32_PLATFORM
00013 #  define WIN32_LEAN_AND_MEAN
00014 #  include "windows.h"
00015 #  include "resource.h"
00016    LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
00019 #elif defined OIS_LINUX_PLATFORM
00020 #  include <X11/Xlib.h>
00021    void checkX11Events();
00022 #endif
00024 using namespace OIS;
00025 
00026 //-- Some local prototypes --//
00027 void doStartup();
00028 
00029 //-- Some hacky globals --//
00030 bool appRunning = true; //Global Exit Flag
00031 
00032 InputManager *g_InputManager = 0;
00033 Keyboard *g_kb  = 0;            //Keyboard Device
00034 JoyStick* g_joys[4] = {0,0,0,0};   //This demo supports up to 4 controllers
00035 ForceFeedback* g_ff[4] = {0,0,0,0};//Array to hold ff interface for each joy
00036 
00038 #if defined OIS_WIN32_PLATFORM
00039   HWND hWnd = 0;
00040 #elif defined OIS_LINUX_PLATFORM
00041   Display *xDisp = 0;
00042   Window xWin = 0;
00043 #endif
00044 
00046 class EventHandler : public KeyListener, public JoyStickListener
00047 {
00048         bool configuringFF;
00049 public:
00050         EventHandler() : configuringFF(false) {}
00051         ~EventHandler() {}
00052 
00053         bool keyPressed( const KeyEvent &arg )
00054         {
00055                 if( arg.key == KC_ESCAPE || arg.key == KC_Q )
00056                         appRunning =false;
00057 
00058                 return true;
00059         }
00060 
00061         bool keyReleased( const KeyEvent &arg )
00062         {
00063                 return true;
00064         }
00065 
00066         bool buttonPressed( const JoyStickEvent &arg, int button )
00067         {
00068                 return true;
00069         }
00070         bool buttonReleased( const JoyStickEvent &arg, int button )
00071         {
00072                 return true;
00073         }
00074         bool axisMoved( const JoyStickEvent &arg, int axis )
00075         {
00076                 return true;
00077         }
00078         bool povMoved( const JoyStickEvent &arg, int pov )
00079         {
00080                 return true;
00081         }
00082 
00083         void displayHelp()
00084         {
00085                 std::cout << "\n\tEsc or Q <-----> Exit App"
00086                                   << "\n\tH        <-----> This Help Menu"
00087                                   << "\n\tF        <-----> Create an FF Effect\n";
00088         }
00089 };
00090 
00091 //Create a global instance
00092 EventHandler handler;
00093 
00094 int main()
00095 {
00096         try
00097         {
00098                 doStartup();
00099                 std::cout << "\nThis is a simple command line Force Feedback testing demo..."
00100                         << "\nAll connected joystick type devices will be created and if FF "
00101                         << "Support is found, it will be availiable.\n** Hit 'q' or ESC to exit. Or 'h' for help **\n";
00102 
00103                 //Main polling loop
00104                 while(appRunning)
00105                 {
00106                         //This fires off buffered events for keyboards
00107                         g_kb->capture();
00108                         //This fires off buffered events for each joystick we have
00109                         for( int i = 0; i < 4 ; ++i )
00110                                 if( g_joys[i] ) g_joys[i]->capture();
00111 
00112                         //Throttle down CPU usage & handle OS events
00113                         #if defined OIS_WIN32_PLATFORM
00114                           Sleep( 30 );
00115                           MSG  msg;
00116                           while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
00117                           {
00118                                   TranslateMessage( &msg );
00119                                   DispatchMessage( &msg );
00120                           }
00121                         #elif defined OIS_LINUX_PLATFORM
00122                           checkX11Events();
00123                           usleep( 300 );
00124                         #endif
00125                 }
00126         }
00127         catch( const Exception &ex )
00128         {
00129                 #if defined OIS_WIN32_PLATFORM
00130                   MessageBox(0, ex.eText, "Exception Raised!", MB_OK);
00131                 #else
00132                   std::cout << "\nOIS Exception Caught!\n" << "\t" << ex.eText << "[Line " << ex.eLine << " in " << ex.eFile << "]\nExiting App\n";
00133                 #endif
00134         }
00135 
00136         if( g_InputManager )
00137         {
00138                 g_InputManager->destroyInputObject( g_kb );
00139 
00140                 for(int i = 0; i < 4; ++i)
00141                         g_InputManager->destroyInputObject( g_joys[i] );
00142 
00143                 InputManager::destroyInputSystem(g_InputManager);
00144         }
00145 
00146         #if defined OIS_LINUX_PLATFORM
00147           // Be nice to X and clean up the x window
00148           XDestroyWindow(xDisp, xWin);
00149           XCloseDisplay(xDisp);
00150         #elif defined OIS_WIN32_PLATFORM && _DEBUG
00151           system("pause");
00152         #endif
00153 
00154         return 0;
00155 }
00156 
00157 void doStartup()
00158 {
00159         ParamList pl;
00160 
00161         #if defined OIS_WIN32_PLATFORM
00162           //Create a capture window for Input Grabbing
00163           hWnd = CreateDialog( 0, MAKEINTRESOURCE(IDD_DIALOG1), 0,(DLGPROC)DlgProc);
00164           if( hWnd == NULL )
00165                 OIS_EXCEPT(E_General, "Failed to create Win32 Window Dialog!");
00166 
00167           ShowWindow(hWnd, SW_SHOW);
00168 
00169           std::ostringstream wnd;
00170           wnd << (size_t)hWnd; 
00171 
00172           pl.insert(std::make_pair( std::string("WINDOW"), wnd.str() ));
00173         #elif defined OIS_LINUX_PLATFORM
00174           //Connects to default X window
00175           if( !(xDisp = XOpenDisplay(0)) )
00176                   OIS_EXCEPT(E_General, "Error opening X!");
00177           //Create a window
00178           xWin = XCreateSimpleWindow(xDisp,DefaultRootWindow(xDisp), 0,0, 100,100, 0, 0, 0);
00179           //bind our connection to that window
00180           XMapWindow(xDisp, xWin);
00181           //Select what events we want to listen to locally
00182           XSelectInput(xDisp, xWin, StructureNotifyMask);
00183           XEvent evtent;
00184           //Wait for Window to show up
00185           do  { XNextEvent(xDisp, &evtent); } while(evtent.type != MapNotify);
00186 
00187           std::ostringstream wnd;
00188           wnd << xWin;
00189 
00190           pl.insert(std::make_pair(std::string("WINDOW"), wnd.str()));
00191         #endif
00192 
00193         //Do the creation
00194         g_InputManager = InputManager::createInputSystem(pl);
00195         InputManager &im = *g_InputManager;
00196         std::cout << "Input Manager (" << im.inputSystemName() << ") Created..\n";
00197 
00198         //Creat a simple keyboard
00199         g_kb = (Keyboard*)im.createInputObject( OISKeyboard, true );
00200         g_kb->setEventCallback( &handler );
00201 
00202         //This demo only uses at max 4 joys
00203         int numSticks = (im.numJoysticks() > 4) ? 4 : im.numJoysticks();
00204         bool ffFound = false;
00205         for( int i = 0; i < numSticks; ++i ) 
00206         {
00207                 //Create the stick
00208                 g_joys[i] = (JoyStick*)im.createInputObject( OISJoyStick, true );
00209                 g_joys[i]->setEventCallback( &handler );
00210                 //Check for FF, if so, dump info
00211                 g_ff[i] = (ForceFeedback*)g_joys[i]->queryInterface( Interface::ForceFeedback );
00212                 if( g_ff[i] )
00213                 {
00214                         ffFound = true;
00215                         std::cout << "Created buffered joystick **with ForceFeedback** support.\n";
00216                         const ForceFeedback::SupportedEffectList &list = g_ff[i]->getSupportedEffects();
00217                         ForceFeedback::SupportedEffectList::const_iterator i = list.begin(),
00218                                 e = list.end();
00219                         for( ; i != e; ++i)
00220                                 std::cout << "Force =  " << i->first << " Type = " << i->second << std::endl;
00221                 }
00222                 else
00223                         std::cout << "Joystick **without** FF support\n";
00224         }
00225         if( ffFound == false )
00226                 OIS_EXCEPT( E_General, "At least one Force Feedback device is required for this demo.\n\n" );
00227 }
00228 
00229 #if defined OIS_WIN32_PLATFORM
00230 LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00231 {
00232         return FALSE;
00233 }
00234 #endif
00235 
00236 #if defined OIS_LINUX_PLATFORM
00237 //This is just here to show that you still recieve x11 events, as the lib only needs mouse/key events
00238 void checkX11Events()
00239 {
00240         XEvent event;
00241 
00242         //Poll x11 for events
00243         while( XPending(xDisp) > 0 )
00244         {
00245                 XNextEvent(xDisp, &event);
00246         }
00247 }
00248 #endif

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