what's wrong

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
anth

what's wrong

Post by anth »

It compiles and shows the rotating pyramid but the 'w' and 's' keys don't do anything (was takken from movement example)

Code: Select all

#include <stdio.h>
#include <wchar.h>
#include <irrlicht.h>

using namespace irr;

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

scene::ISceneNode* node = 0;
IrrlichtDevice* device = 0;


class MyEventReceiver : public IEventReceiver
{
public:
virtual bool OnEvent(SEvent event)
{

if (node != 0 && event.EventType == irr::EET_KEY_INPUT_EVENT&&
!event.KeyInput.PressedDown)
{
switch(event.KeyInput.Key)
{
case KEY_KEY_W:
case KEY_KEY_S:
{
core::vector3df v = node->getPosition();
v.Y += event.KeyInput.Key == KEY_KEY_W ? 5.0f : -5.0f;
node->setPosition(v);
}
return true;
}
}

return false;
}
};

class CSampleSceneNode : public scene::ISceneNode
{

core::aabbox3d<f32> Box;
video::S3DVertex Vertices[4];
video::SMaterial Material;

public:

CSampleSceneNode(scene::ISceneNode* parent, scene::ISceneManager* mgr, s32 id)
 : scene::ISceneNode(parent, mgr, id)
{ 
  Material.Wireframe = false;
  Material.Lighting = false;

  Vertices[0] = video::S3DVertex(0,0,10, 1,1,0,video::SColor(255,0,255,255),0,1);
  Vertices[1] = video::S3DVertex(10,0,-10, 1,0,0,video::SColor(255,255,0,255),1,1); 
  Vertices[2] = video::S3DVertex(0,20,0, 0,1,1,video::SColor(255,255,255,0),1,0);
  Vertices[3] = video::S3DVertex(-10,0,-10, 0,0,1,video::SColor(255,0,255,0),0,0);

  Box.reset(Vertices[0].Pos);
 		for (s32 i=1; i<4; ++i)
 			Box.addInternalPoint(Vertices[i].Pos);
}

virtual void OnPreRender()
{
  if (IsVisible)
    SceneManager->registerNodeForRendering(this);

  ISceneNode::OnPreRender();
}

virtual void render()
{
  u16 indices[] = { 0,2,3, 2,1,3, 1,0,3, 2,0,1 };
  video::IVideoDriver* driver = SceneManager->getVideoDriver();

  driver->setMaterial(Material);
  driver->setTransform(video::ETS_WORLD, AbsoluteTransformation);
  driver->drawIndexedTriangleList(&Vertices[0], 4, &indices[0], 4);
}

  virtual const core::aabbox3d<f32>& getBoundingBox() const
  {
    return Box;
  }

  virtual s32 getMaterialCount()
  {
    return 1;
  }

  virtual video::SMaterial& getMaterial(s32 i)
  {
    return Material;
  } 
};

int main()

{
  IrrlichtDevice *device =
       createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 16, false);

  device->setWindowCaption(L"Scene Node Demo");

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

  smgr->addCameraSceneNode(0, core::vector3df(0,-40,0), core::vector3df(0,0,0));

CSampleSceneNode *myNode = 
  new CSampleSceneNode(smgr->getRootSceneNode(), smgr, 666);

myNode->drop();

scene::ISceneNodeAnimator* anim = 
   smgr->createRotationAnimator(core::vector3df(0.8f, 0, 0.8f));

myNode->addAnimator(anim);
anim->drop();

  while(device->run())
  {
    driver->beginScene(true, true, video::SColor(0,0,0,0));

    smgr->drawAll();

    driver->endScene();
  }

device->drop();

return 0;

{
	MyEventReceiver receiver;

	device = createDevice(video::EDT_DIRECTX8, core::dimension2d<s32>(800, 600),
		16, false, false, &receiver);

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


	node = smgr->addTestSceneNode();
	node->setPosition(core::vector3df(0,0,30));
	node->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));

	scene::ISceneNode* n = smgr->addTestSceneNode();
	n->setPosition(core::vector3df(0,0,30));
	n->setMaterialTexture(0, driver->getTexture("../../media/t351sml.jpg"));

	scene::ISceneNodeAnimator* anim = smgr->createFlyCircleAnimator(
		core::vector3df(0,1,0), 20.0f);
	n->addAnimator(anim);
	anim->drop();

	scene::IAnimatedMeshSceneNode* anms = smgr->addAnimatedMeshSceneNode(
		smgr->getMesh("../../media/sydney.md2"));

	if (n)
	{
		anim = smgr->createFlyStraightAnimator(core::vector3df(100,0,60), 
			core::vector3df(-100,0,60), 10000, true);
		anms->addAnimator(anim);
		anim->drop();

		anms->setMaterialFlag(video::EMF_LIGHTING, false);
		anms->setFrameLoop(320, 360);
		anms->setAnimationSpeed(30);
		anms->setRotation(core::vector3df(0,180.0f,0));
		anms->setMaterialTexture(0, driver->getTexture("../../media/sydney.bmp"));
	}

	smgr->addCameraSceneNodeFPS(0, 100.0f, 100.0f);
	device->getCursorControl()->setVisible(false);

	int lastFPS = -1;

	while(device->run())
	{
		driver->beginScene(true, true, video::SColor(255,90,90,156));
		smgr->drawAll();
		driver->endScene();

		int fps = driver->getFPS();

		if (lastFPS != fps)
		{
			wchar_t tmp[1024];
			swprintf(tmp, 1024, L"Test(%s)(fps:%d)", 
				driver->getName(), fps);

			device->setWindowCaption(tmp);
			lastFPS = fps;
		}
	}

	device->drop();
	
	return 0;
}

}
cinek
Posts: 13
Joined: Sat Aug 07, 2004 3:00 pm
Location: Poland

Re: what's wrong

Post by cinek »

This line is wrong:

Code: Select all

  IrrlichtDevice *device =
       createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 16, false);
You should specify your event receiver as the 6th argument, like this:

Code: Select all

  IrrlichtDevice *device =
       createDevice(video::EDT_OPENGL, core::dimension2d<s32>(800, 600), 16, false, flase, &MyEventReceiver);
oh... and what's that block of code after the (first) "return 0;" in your main() function? :)
Cinek
Post Reply