00001 #define WIN32_LEAN_AND_MEAN
00002 #include <windows.h>
00003 #include <iostream>
00004 #include <vector>
00005 #include <sstream>
00006 #include <OIS.h>
00007 #include <SDL.h>
00008 #include "resource.h"
00009
00010 LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
00011 void initSDL();
00012 void destroySDL();
00013 void initOIS();
00014 void destroyOIS();
00015
00016 void OutputMessage( const std::string& message );
00017
00018
00019 HWND hWnd = 0, hOut = 0, hDisp = 0;
00020 bool appRunning = true;
00021
00022 using namespace OIS;
00023
00025 class EventHandler : public KeyListener, public MouseListener
00026 {
00027 public:
00028 EventHandler() {}
00029 ~EventHandler() {}
00030 bool keyPressed( const KeyEvent &arg ) {
00031 std::ostringstream ss;
00032 ss << "KeyPressed {" << arg.key << ", " << ((Keyboard*)(arg.device))->getAsString(arg.key)
00033 << "} || Text (" << (arg.text > 0 ? (char)arg.text : '?') << ")";
00034 OutputMessage(ss.str());
00035 return true;
00036 }
00037 bool keyReleased( const KeyEvent &arg ) {
00038 if( arg.key == KC_ESCAPE || arg.key == KC_Q )
00039 {
00040 appRunning = false;
00041 return false;
00042 }
00043 std::ostringstream ss;
00044 ss << "KeyReleased (" << arg.key << ")";
00045 OutputMessage(ss.str());
00046 return true;
00047 }
00048 bool mouseMoved( const MouseEvent &arg ) {
00049 const MouseState& s = arg.state;
00050 std::ostringstream ss;
00051 ss << "MouseMoved: Abs("
00052 << s.abX << ", " << s.abY << ", " << s.abZ << ") Rel("
00053 << s.relX << ", " << s.relY << ", " << s.relZ << ")";
00054 OutputMessage(ss.str());
00055 return true;
00056 }
00057 bool mousePressed( const MouseEvent &arg, MouseButtonID id ) {
00058 std::ostringstream ss;
00059 ss << "MousePressed: " << id;
00060 OutputMessage(ss.str());
00061 return true;
00062 }
00063 bool mouseReleased( const MouseEvent &arg, MouseButtonID id ) {
00064 std::ostringstream ss;
00065 ss << "MouseReleased: " << id;
00066 OutputMessage(ss.str());
00067 return true;
00068 }
00069 };
00070
00071
00072 EventHandler gHandler;
00073 Mouse* gMouse = 0;
00074 Keyboard* gKeyboard = 0;
00075
00076
00077 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
00078 {
00079
00080 hWnd = CreateDialog( 0, MAKEINTRESOURCE(IDD_MAIN_WINDOW), 0,(DLGPROC)DlgProc);
00081 if( hWnd == 0 ) exit(-1);
00082 ShowWindow(hWnd, SW_SHOW);
00083
00084 hOut = GetDlgItem(hWnd, IDC_OUTPUT);
00085 if(hOut == 0) exit(-1);
00086
00087 hDisp = GetDlgItem(hWnd, IDC_SDL_WIN);
00088 if(hDisp == 0) exit(-1);
00089
00090 OutputMessage("Initialising Demo Application...");
00091
00092 try
00093 {
00094 initSDL();
00095 initOIS();
00096 }
00097 catch(...)
00098 {
00099 appRunning = false;
00100 }
00101
00102 while(appRunning)
00103 {
00104 Sleep( 30 );
00105 MSG msg;
00106 while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
00107 {
00108 if( msg.message == WM_QUIT )
00109 appRunning = false;
00110
00111 TranslateMessage( &msg );
00112 DispatchMessage( &msg );
00113 }
00114
00115 if( gKeyboard )
00116 {
00117 gKeyboard->capture();
00118 if( gKeyboard->buffered() == false )
00119 if( gKeyboard->isKeyDown( KC_ESCAPE ) )
00120 appRunning = false;
00121 }
00122
00123 if( gMouse )
00124 {
00125 gMouse->capture();
00126 }
00127 }
00128
00129 destroyOIS();
00130 destroySDL();
00131 return 0;
00132 }
00133
00134
00135 void initSDL()
00136 {
00137 OutputMessage("Initialising SDL...");
00138
00139
00140
00141
00142
00143 RECT r;
00144 GetWindowRect(hDisp, &r);
00145
00146 if( SDL_Init(SDL_INIT_VIDEO) < 0 )
00147 throw("Error!");
00148 SDL_Surface *screen = SDL_SetVideoMode( r.right-r.left, r.bottom-r.top, 32, SDL_HWSURFACE );
00149
00150
00151
00152 OutputMessage("Success!");
00153 }
00154
00155
00156 void destroySDL()
00157 {
00158 SDL_Quit();
00159 }
00160
00161
00162 void initOIS()
00163 {
00164 OutputMessage("Initialising OIS...");
00165 InputManager *im = InputManager::createInputSystem(ParamList());
00166
00167 gKeyboard = static_cast<Keyboard*>(im->createInputObject(OISKeyboard, false));
00168 gKeyboard->setEventCallback( &gHandler );
00169
00170 gMouse = static_cast<Mouse*>(im->createInputObject(OISMouse, false));
00171 gMouse->setEventCallback( &gHandler );
00172
00173 std::ostringstream temp;
00174 unsigned int v = im->getVersionNumber();
00175 temp << "Success! >> " << "Version: " << (v>>16 ) << "." << ((v>>8) & 0x000000FF)
00176 << "." << (v & 0x000000FF) << " >> Release Name: "
00177 << im->getVersionName() << " >> Platform: " << im->inputSystemName();
00178 OutputMessage(temp.str());
00179 OutputMessage("");
00180 OutputMessage("***************************************************************");
00181 OutputMessage("TIP!: Keep the external SDL window active to recieve events");
00182 OutputMessage("TIP!: Git Escape in buffered or unbuffered to quit");
00183 OutputMessage("***************************************************************");
00184 }
00185
00186
00187 void destroyOIS()
00188 {
00189 if( InputManager::getSingletonPtr() )
00190 {
00191 InputManager::getSingletonPtr()->destroyInputObject(gKeyboard);
00192 InputManager::destroyInputSystem();
00193 }
00194 }
00195
00196
00197 LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
00198 {
00199 int wmId = LOWORD(wParam), wmEvent = HIWORD(wParam);
00200
00201 switch(uMsg)
00202 {
00203 case WM_CLOSE:
00204 PostQuitMessage(0);
00205 return TRUE;
00206 case WM_COMMAND:
00207 {
00208 switch(wmId)
00209 {
00210 case ID_EXIT:
00211 PostQuitMessage(0);
00212 return TRUE;
00213 case IDC_BUFF_KEYS:
00214 {
00215 gKeyboard->setBuffered( !gKeyboard->buffered() );
00216 std::ostringstream temp;
00217 temp << "** Setting Keyboard buffered Mode to: " << (gKeyboard->buffered() ? "Buffered" : "Unbuffered");
00218 OutputMessage(temp.str());
00219 return FALSE;
00220 }
00221 case IDC_BUFF_MOUSE:
00222 {
00223 gMouse->setBuffered( !gMouse->buffered() );
00224 std::ostringstream temp;
00225 temp << "** Setting Mouse buffered Mode to: " << (gMouse->buffered() ? "Buffered" : "Unbuffered");
00226 OutputMessage(temp.str());
00227 return FALSE;
00228 }
00229 default: break;
00230 }
00231 }
00232 }
00233
00234 return FALSE;
00235 }
00236
00237
00238 void OutputMessage( const std::string& message )
00239 {
00240 static std::ostringstream buff;
00241 buff << message << "\r\n";
00242 SendMessage(hOut, WM_SETTEXT, 0, (LPARAM)buff.str().c_str());
00243 }