MFC-Eventreceiver

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
sebi707
Posts: 18
Joined: Tue Feb 20, 2007 4:19 pm

MFC-Eventreceiver

Post by sebi707 »

hi i want to use the irrlicht engine inside a MFC-Project but the eventreceiver is not working. i found this post here:

http://irrlicht.sourceforge.net/phpBB2/ ... hlight=mfc

but i don't know how to send the input handling that MFC provides to my event receiver.
vizzy
Posts: 10
Joined: Mon May 21, 2007 2:55 pm
Location: Dresden, Germany

Post by vizzy »

...you must overwrite the WindowProc function:
sample:

Code: Select all

// Für MFC abgeleiteter EVentReceiver
LRESULT CI3DEditView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	// TODO: Add your specialized code here and/or call the base class

	LRESULT result=CView::WindowProc(message, wParam, lParam);

	HWND hWnd=this->GetSafeHwnd();

	#ifndef WM_MOUSEWHEEL
	#define WM_MOUSEWHEEL 0x020A
	#endif
	#ifndef WHEEL_DELTA
	#define WHEEL_DELTA 120
	#endif

	irr::CIrrDeviceWin32* dev = 0;

	irr::SEvent event;
	SEnvMapper* envm = 0;

	switch (message)
	{
		case WM_ERASEBKGND:
		return 0;

		case WM_SETCURSOR:
		envm = getEnvMapperFromHWnd(hWnd);
		if (envm && !envm->irrDev->getWin32CursorControl()->isVisible())
		{
			SetCursor(NULL);
			return 0;
		}
		break;

		case WM_MOUSEWHEEL:
			event.EventType = irr::EET_MOUSE_INPUT_EVENT;
			event.MouseInput.Wheel = (float)((short)HIWORD(wParam)) / (float)WHEEL_DELTA;
			event.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;

			POINT p; // fixed by jox
			p.x = 0; p.y = 0;
			ClientToScreen(&p);
			event.MouseInput.X = LOWORD(lParam) - p.x;
			event.MouseInput.Y = HIWORD(lParam) - p.y;

			dev = getDeviceFromHWnd(hWnd);
			if (dev)
				dev->postEventFromUser(event);
		break;
sebi707
Posts: 18
Joined: Tue Feb 20, 2007 4:19 pm

Post by sebi707 »

i get problems with this lines

Code: Select all

   irr::CIrrDeviceWin32* dev = 0;
   irr::SEvent event;
   SEnvMapper* envm = 0; 
CIrrDeviceWin32 is not an element of 'irr' is says
vizzy
Posts: 10
Joined: Mon May 21, 2007 2:55 pm
Location: Dresden, Germany

Post by vizzy »

...just include the "cirrdevicewin32.h" , e.g. #include "c:\bla\irrlichdll\cirrdevicewin32.h"
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

Code not optimized or even good enough for prod, though.

Code: Select all

LRESULT CAppDialog01View::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{

	// TODO: Add your specialized code here and/or call the base class
	#ifndef WM_MOUSEWHEEL
	#define WM_MOUSEWHEEL 0x020A
	#endif
	#ifndef WHEEL_DELTA
	#define WHEEL_DELTA 120
	#endif
	
	m_aHwnd = this->m_hWnd;
	HWND hWnd=this->GetSafeHwnd(); 
	if ( !hWnd ) 
	{
		hWnd=this->m_hWnd; 
	}
	hWnd=m_aHwnd;
	CAppDialog01View* dev = 0;
	irr::SEvent evnt;
	SEnvMapper* envm = 0;

	BYTE allKeys[256];

	static irr::s32 ClickCount=0;
	if (GetCapture() != (CWnd*)hWnd && ClickCount > 0)
		ClickCount = 0;

	switch (message)
	{
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			//BeginPaint(hWnd, &ps);
			//EndPaint(hWnd, &ps);
			BeginPaint(&ps);
			EndPaint(&ps);
		}
		return 0;

	case WM_ERASEBKGND:
		return 0;

	case WM_SETCURSOR:
		envm = getEnvMapperFromHWnd(hWnd);
		//if (envm && !envm->irrDev->getWin32CursorControl()->isVisible())
		//{
		//	SetCursor(NULL);
		//	return 0;
		//}
		break;

	case WM_MOUSEWHEEL:
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Wheel = ((irr::f32)((short)HIWORD(wParam))) / (irr::f32)WHEEL_DELTA;
		evnt.MouseInput.Event = irr::EMIE_MOUSE_WHEEL;

		POINT p; // fixed by jox
		p.x = 0; p.y = 0;
		//ClientToScreen(hWnd, &p);
		ClientToScreen(&p);
		evnt.MouseInput.X = LOWORD(lParam) - p.x;
		evnt.MouseInput.Y = HIWORD(lParam) - p.y;

		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		break;

	case WM_LBUTTONDOWN:
		ClickCount++;
		SetCapture();
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_LMOUSE_PRESSED_DOWN;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		return 0;

	case WM_LBUTTONUP:
		ClickCount--;
		if (ClickCount<1)
		{
			ClickCount=0;
			ReleaseCapture();
		}
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_LMOUSE_LEFT_UP;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);


		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		return 0;

	case WM_RBUTTONDOWN:
		ClickCount++;
		SetCapture();
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_RMOUSE_PRESSED_DOWN;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		return 0;

	case WM_RBUTTONUP:
		ClickCount--;
		if (ClickCount<1)
		{
			ClickCount=0;
			ReleaseCapture();
		}
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_RMOUSE_LEFT_UP;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		return 0;

	case WM_MBUTTONDOWN:
		ClickCount++;
		SetCapture();
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_MMOUSE_PRESSED_DOWN;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		return 0;

	case WM_MBUTTONUP:
		ClickCount--;
		if (ClickCount<1)
		{
			ClickCount=0;
			ReleaseCapture();
		}
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_MMOUSE_LEFT_UP;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//dev = getDeviceFromHWnd(hWnd);
		//if (dev)
		//	dev->postEventFromUser(evnt);
		return 0;

	case WM_MOUSEMOVE:
		evnt.EventType = irr::EET_MOUSE_INPUT_EVENT;
		evnt.MouseInput.Event = irr::EMIE_MOUSE_MOVED;
		evnt.MouseInput.X = (short)LOWORD(lParam);
		evnt.MouseInput.Y = (short)HIWORD(lParam);
		dev = getDeviceFromHWnd(hWnd);
		UserReceiver = irrDevice->getEventReceiver();
		if (UserReceiver)
			absorbed = UserReceiver->OnEvent(evnt);
		absorbed = smgr->postEventFromUser(evnt);

		//if (dev)
		//	dev->postEventFromUser(evnt);
		//return 0;

	case WM_KEYDOWN:
	case WM_KEYUP:
		{
			evnt.EventType = irr::EET_KEY_INPUT_EVENT;
			evnt.KeyInput.Key = (irr::EKEY_CODE)wParam;
			evnt.KeyInput.PressedDown = (message==WM_KEYDOWN);
			dev = getDeviceFromHWnd(hWnd);

			WORD KeyAsc=0;
			GetKeyboardState(allKeys);
			ToAscii((UINT)wParam,(UINT)lParam,allKeys,&KeyAsc,0);

			evnt.KeyInput.Shift = ((allKeys[VK_SHIFT] & 0x80)!=0);
			evnt.KeyInput.Control = ((allKeys[VK_CONTROL] & 0x80)!=0);
			evnt.KeyInput.Char = (KeyAsc & 0x00ff); //KeyAsc >= 0 ? KeyAsc : 0;
			UserReceiver = irrDevice->getEventReceiver();
			if (UserReceiver)
				absorbed = UserReceiver->OnEvent(evnt);
			absorbed = smgr->postEventFromUser(evnt);

			//if (dev)
			//	dev->postEventFromUser(evnt);

			return 0;
		}

	case WM_SIZE:
		{
			// resize
			dev = getDeviceFromHWnd(hWnd);
			//if (dev)
			//	dev->OnResized();
		}
		return 0;

	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;

	case WM_SYSCOMMAND:
		// prevent screensaver or monitor powersave mode from starting
		if ((wParam & 0xFFF0) == SC_SCREENSAVE ||
			(wParam & 0xFFF0) == SC_MONITORPOWER)
			return 0;
		break;
	}

	return 	CView::WindowProc(message, wParam, lParam);

}
Image
Image
yellowfever13
Posts: 34
Joined: Thu Aug 17, 2006 10:32 pm

help

Post by yellowfever13 »

I am also trying to get events( mayacamera doesnt work, buttons etc. ) from an MFC project ( CWinApp ).

Ive read this thread and others and still dont understand what to put in WindowProc. I see the above about attaching the CIrrDeviceWin32.h file and using the text above. But that header file wants many others that arent included in the Irrlicht package. I found a couple on the internet, but I couldnt find them all. This seems awefully complicated to add this. Otherwise Irrlicht seems to be working fine in my MFC app. I put the run loop in my overridden OnIdle().

The only problem is my camera gets no input and I also need to add some buttons.

Thanks!

UPDATE: Ok so I realized that I'm an idiot and found the files in the source. I had many errors, fixed most, but these remain...

error C2065: 'SEnvMapper' : undeclared identifier
error C2065: 'envm' : undeclared identifier
error C3861: 'getEnvMapperFromHWnd': identifier not found
error C2227: left of '->irrDev' must point to class/struct/union/generic type
pe''
error C2227: left of '->getWin32CursorControl' must point to class/struct/union/generic type
error C2227: left of '->isVisible' must point to class/struct/union/generic type
error C3861: 'getDeviceFromHWnd': identifier not found
Fashizzle
Post Reply