Win32 Help

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
Zeroth
Posts: 17
Joined: Wed Jul 02, 2008 11:21 pm

Win32 Help

Post by Zeroth »

As the title of the topic suggests, my query concerns Win32 and not the engine itself:

Code: Select all

#include <irrlicht.h>
#ifndef _IRR_WINDOWS_
#error Windows Only
#else
#include <windows.h> 
#include "Game.h"

using namespace irr;

#pragma comment(lib, "irrlicht.lib")

#define FRAME_RATE 5

HWND hWnd;
Game* myGame;

static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
		
	case WM_CREATE:
		return 0;
		
	case WM_CLOSE:
		PostQuitMessage(0);
		return 0;
		
	case WM_DESTROY:
		PostQuitMessage(0);
		return 0;
		
	case WM_KEYDOWN:
		myGame->handleKeypress(wParam, 1);
		switch ( wParam )
		{
			case VK_ESCAPE:
				PostQuitMessage(0);
				return 0;
		}
		return 0;
	
	default:
		return DefWindowProc( hWnd, message, wParam, lParam );
			
	}
}



int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpre, LPSTR cmd, int cc)
{
	MSG msg;
	BOOL quit = FALSE;
	const char* Win32ClassName = "Game";
	float timeLast = GetTickCount();
	float currentTime;

	WNDCLASSEX wcex;
	wcex.cbSize			= sizeof(WNDCLASSEX);
	wcex.style			= CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc	= (WNDPROC)WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= DLGWINDOWEXTRA;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= NULL;
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= (HBRUSH)(COLOR_WINDOW);
	wcex.lpszMenuName	= 0;
	wcex.lpszClassName	= Win32ClassName;
	wcex.hIconSm		= 0;

	RegisterClassEx(&wcex);

	DWORD style = WS_SYSMENU | WS_BORDER | WS_CAPTION |
		WS_CLIPCHILDREN | WS_CLIPSIBLINGS | WS_MAXIMIZEBOX | WS_MINIMIZEBOX | WS_SIZEBOX;

	int windowWidth = 1024;
	int windowHeight = 768;

	hWnd = CreateWindow( Win32ClassName, "Game",
		style, 0, 0, windowWidth, windowHeight,
		NULL, NULL, hInstance, NULL);

	RECT clientRect;
	GetClientRect(hWnd, &clientRect);
	windowWidth = clientRect.right;
	windowHeight = clientRect.bottom;

	HWND hIrrlichtWindow = CreateWindow("BUTTON", "", WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
			0, 0, windowWidth, windowHeight, hWnd, NULL, hInstance, NULL);


	// IRRLICHT INIT -

	irr::SIrrlichtCreationParameters param;
	param.WindowId = reinterpret_cast<void*>(hIrrlichtWindow); 
	param.DriverType = video::EDT_DIRECT3D9;

	irr::IrrlichtDevice* device = irr::createDeviceEx(param);

	irr::scene::ISceneManager* smgr = device->getSceneManager();
	video::IVideoDriver* driver = device->getVideoDriver();

	//
	
	myGame = new Game(device, driver, smgr);
	myGame->loadLevel();
	myGame->initialiseGameplay();

	ShowWindow(hWnd , SW_SHOW);
	UpdateWindow(hWnd);
	
	
	while(true)
	{
		currentTime = GetTickCount();
					
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);

			if (msg.message == WM_QUIT)
				break;
		}
			
		if(currentTime-timeLast > FRAME_RATE)
		{
			myGame->update(currentTime-timeLast/FRAME_RATE);
			myGame->draw();
			
			timeLast = currentTime;
		}
		
	}
	
	delete(myGame);
	device->closeDevice();
	device->drop();

	return 0;
}
#endif // if windows
My problem is that the Callback function 'WndProc', doesn't seem to work at all. No user input is registered, not even the Escape key which _should_ close the application. I can't see where I've gone wrong given that I've (obviously) virtually copied the Win32 example provided with the engine. Any ideas?
RoBaTte
Posts: 10
Joined: Thu Jun 07, 2007 2:34 pm

Post by RoBaTte »

Hi,

I assume that in Game::draw() you call something like

Code: Select all

device->run()
Dont call it. It will let irrlichts internal WNDPROC dispatch thhe messages.

But you have to call

Code: Select all

device->getTimer()->tick();
in every loop, because device->run() wont do it for you anymore ;)
dlangdev
Posts: 1324
Joined: Tue Aug 07, 2007 7:28 pm
Location: Beaverton OR
Contact:

Post by dlangdev »

you might want to check out the file CIrrDeviceWin32.cpp.

look into that code and figure out how RegisterClassEx() can be modded so you can pass your WndProc in there.

maybe you can daisy chain them if you can code it, tho.
Image
Post Reply