Home | History | Annotate | Download | only in SdlDemo
      1 #define WIN32_LEAN_AND_MEAN
      2 #include <windows.h>
      3 #include <iostream>
      4 #include <vector>
      5 #include <sstream>
      6 #include <OIS.h>
      7 #include <SDL.h>
      8 #include "resource.h"
      9 
     10 LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam );
     11 void initSDL();
     12 void destroySDL();
     13 void initOIS();
     14 void destroyOIS();
     15 
     16 void OutputMessage( const std::string& message );
     17 
     18 //Fun Globals ;-)
     19 HWND hWnd = 0, hOut = 0, hDisp = 0;
     20 bool appRunning = true;
     21 
     22 using namespace OIS;
     23 
     24 //////////// Common Event handler class ////////
     25 class EventHandler : public KeyListener, public MouseListener
     26 {
     27 public:
     28 	EventHandler() {}
     29 	~EventHandler() {}
     30 	bool keyPressed( const KeyEvent &arg ) {
     31 		std::ostringstream ss;
     32 		ss << "KeyPressed {" << arg.key	<< ", " << ((Keyboard*)(arg.device))->getAsString(arg.key)
     33 			<< "} || Text (" << (arg.text > 0 ? (char)arg.text : '?') << ")";
     34 		OutputMessage(ss.str());
     35 		return true;
     36 	}
     37 	bool keyReleased( const KeyEvent &arg ) {
     38 		if( arg.key == KC_ESCAPE || arg.key == KC_Q )
     39 		{
     40 			appRunning = false;
     41 			return false;
     42 		}
     43 		std::ostringstream ss;
     44 		ss << "KeyReleased (" << arg.key << ")";
     45 		OutputMessage(ss.str());
     46 		return true;
     47 	}
     48 	bool mouseMoved( const MouseEvent &arg ) {
     49 		const MouseState& s = arg.state;
     50 		std::ostringstream ss;
     51 		ss << "MouseMoved: Abs("
     52 		  << s.abX << ", " << s.abY << ", " << s.abZ << ") Rel("
     53 		  << s.relX << ", " << s.relY << ", " << s.relZ << ")";
     54 		OutputMessage(ss.str());
     55 		return true;
     56 	}
     57 	bool mousePressed( const MouseEvent &arg, MouseButtonID id ) {
     58 		std::ostringstream ss;
     59 		ss << "MousePressed: " << id;
     60 		OutputMessage(ss.str());
     61 		return true;
     62 	}
     63 	bool mouseReleased( const MouseEvent &arg, MouseButtonID id ) {
     64 		std::ostringstream ss;
     65 		ss << "MouseReleased: " << id;
     66 		OutputMessage(ss.str());
     67 		return true;
     68 	}
     69 };
     70 
     71 //More Fun Globals ;-)
     72 EventHandler gHandler;
     73 Mouse* gMouse = 0;
     74 Keyboard* gKeyboard = 0;
     75 
     76 //---------------------------------------------------------------------------------//
     77 INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT )
     78 {
     79 	//Create a capture window for Input Grabbing
     80 	hWnd = CreateDialog( 0, MAKEINTRESOURCE(IDD_MAIN_WINDOW), 0,(DLGPROC)DlgProc);
     81 	if( hWnd == 0 ) exit(-1);
     82 	ShowWindow(hWnd, SW_SHOW);
     83 
     84 	hOut = GetDlgItem(hWnd, IDC_OUTPUT);
     85 	if(hOut == 0) exit(-1);
     86 
     87 	hDisp = GetDlgItem(hWnd, IDC_SDL_WIN);
     88 	if(hDisp == 0) exit(-1);
     89 
     90 	OutputMessage("Initialising Demo Application...");
     91 
     92 	try
     93 	{
     94 		initSDL();
     95 		initOIS();
     96 	}
     97 	catch(...)
     98 	{
     99 		appRunning = false;
    100 	}
    101 
    102 	while(appRunning)
    103 	{
    104 		Sleep( 30 );
    105 		MSG  msg;
    106 		while( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
    107 		{
    108 			if( msg.message == WM_QUIT )
    109 				appRunning = false;
    110 
    111 			TranslateMessage( &msg );
    112 			DispatchMessage( &msg );
    113 		}
    114 
    115 		if( gKeyboard )
    116 		{
    117 			gKeyboard->capture();
    118 			if( gKeyboard->buffered() == false )
    119 				if( gKeyboard->isKeyDown( KC_ESCAPE ) )
    120 					appRunning = false;
    121 		}
    122 
    123 		if( gMouse )
    124 		{
    125 			gMouse->capture();
    126 		}
    127 	}
    128 
    129 	destroyOIS();
    130 	destroySDL();
    131 	return 0;
    132 }
    133 
    134 //---------------------------------------------------------------------------------//
    135 void initSDL()
    136 {
    137 	OutputMessage("Initialising SDL...");
    138 	//I cannot get embedding functioning :/
    139 	//std::ostringstream ss;
    140 	//ss << "SDL_WINDOWID=" << hDisp;
    141 	//_putenv(ss.str().c_str());
    142 	//_putenv("SDL_VIDEODRIVER=windib");
    143 	RECT r;
    144 	GetWindowRect(hDisp, &r);
    145 
    146 	if( SDL_Init(SDL_INIT_VIDEO) < 0 )
    147 		throw("Error!");
    148 	SDL_Surface *screen = SDL_SetVideoMode( r.right-r.left, r.bottom-r.top, 32, SDL_HWSURFACE );
    149 
    150 	//SDL_Surface *screen = SDL_SetVideoMode( r.right-r.left, r.bottom-r.top, 0, 0 );
    151 	//SetWindowPos(hDisp, 0, r.left, r.top, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
    152 	OutputMessage("Success!");
    153 }
    154 
    155 //---------------------------------------------------------------------------------//
    156 void destroySDL()
    157 {
    158 	SDL_Quit();
    159 }
    160 
    161 //---------------------------------------------------------------------------------//
    162 void initOIS()
    163 {
    164 	OutputMessage("Initialising OIS...");
    165 	InputManager *im = InputManager::createInputSystem(ParamList());
    166 
    167 	gKeyboard = static_cast<Keyboard*>(im->createInputObject(OISKeyboard, false));
    168 	gKeyboard->setEventCallback( &gHandler );
    169 
    170 	gMouse = static_cast<Mouse*>(im->createInputObject(OISMouse, false));
    171 	gMouse->setEventCallback( &gHandler );
    172 
    173 	std::ostringstream temp;
    174 	unsigned int v = im->getVersionNumber();
    175 	temp << "Success! >> " << "Version: " << (v>>16 ) << "." << ((v>>8) & 0x000000FF)
    176 		<< "." << (v & 0x000000FF) << " >> Release Name: "
    177 		<< im->getVersionName() << " >> Platform: " << im->inputSystemName();
    178 	OutputMessage(temp.str());
    179 	OutputMessage("");
    180 	OutputMessage("***************************************************************");
    181 	OutputMessage("TIP!: Keep the external SDL window active to recieve events");
    182 	OutputMessage("TIP!: Git Escape in buffered or unbuffered to quit");
    183 	OutputMessage("***************************************************************");
    184 }
    185 
    186 //---------------------------------------------------------------------------------//
    187 void destroyOIS()
    188 {
    189 	if( InputManager::getSingletonPtr() )
    190 	{
    191 		InputManager::getSingletonPtr()->destroyInputObject(gKeyboard);
    192 		InputManager::destroyInputSystem();
    193 	}
    194 }
    195 
    196 //---------------------------------------------------------------------------------//
    197 LRESULT DlgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
    198 {
    199 	int wmId = LOWORD(wParam), wmEvent = HIWORD(wParam);
    200 
    201 	switch(uMsg)
    202 	{
    203 		case WM_CLOSE:
    204 			PostQuitMessage(0);
    205 			return TRUE;
    206 		case WM_COMMAND:
    207 		{
    208 			switch(wmId)
    209 			{
    210 			case ID_EXIT:
    211 				PostQuitMessage(0);
    212 				return TRUE;
    213 			case IDC_BUFF_KEYS:
    214 			{
    215 				gKeyboard->setBuffered( !gKeyboard->buffered() );
    216 				std::ostringstream temp;
    217 				temp << "** Setting Keyboard buffered Mode to: " << (gKeyboard->buffered() ? "Buffered" : "Unbuffered");
    218 				OutputMessage(temp.str());
    219 				return FALSE;
    220 			}
    221 			case IDC_BUFF_MOUSE:
    222 			{
    223 				gMouse->setBuffered( !gMouse->buffered() );
    224 				std::ostringstream temp;
    225 				temp << "** Setting Mouse buffered Mode to: " << (gMouse->buffered() ? "Buffered" : "Unbuffered");
    226 				OutputMessage(temp.str());
    227 				return FALSE;
    228 			}
    229 			default: break;
    230 			}
    231 		}
    232 	}
    233 
    234 	return FALSE;
    235 }
    236 
    237 //---------------------------------------------------------------------------------//
    238 void OutputMessage( const std::string& message )
    239 {
    240 	static std::ostringstream buff;
    241 	buff << message << "\r\n";
    242 	SendMessage(hOut, WM_SETTEXT, 0, (LPARAM)buff.str().c_str());
    243 }
    244