Event Recievers & .irr Files

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
IrrNoob
Posts: 110
Joined: Sun Nov 16, 2008 8:01 pm
Location: Fort Collins, Us

Event Recievers & .irr Files

Post by IrrNoob »

I'm trying to add a event reciever to a project that uses .irr files
I used this code with the old projects using a terrain scene node:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:

	MyEventReceiver(scene::ISceneNode* terrain)
	{
			for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

	bool OnEvent(const SEvent& event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}
	
private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

Code: Select all

	// add terrain scene node
	scene::ITerrainSceneNode* terrain = smgr->addTerrainSceneNode( 
		"media/terrain-heightmap.bmp",
		0,										// parent node
		-1,										// node id
		core::vector3df(0.f, -1200.f, 0.f),		// position
		core::vector3df(0.f, 0.f, 0.f),			// rotation
		core::vector3df(40.f, 4.4f, 40.f),		// scale
		video::SColor ( 255, 255, 255, 255 ),	// vertexColor,
		5,										// maxLOD
		scene::ETPS_17,							// patchSize
		4										// smoothFactor
		);

Code: Select all

	// create event receiver
	MyEventReceiver receiver(terrain);
	device->setEventReceiver(&receiver);

Code: Select all

// in draw loop
     // add an ESCAPE key to exit
     if (receiver.IsKeyDown(irr::KEY_ESCAPE))
     {
       device->closeDevice(); 
       device->drop(); 
       exit(0); 
     } 
my problem that when I pass

Code: Select all

scene::ISceneNode* Irr = smgr->loadScene("media/game.irr");
I get this compile error:

Code: Select all

error C2440: 'initializing' : cannot convert from 'bool' to 'irr::scene::ISceneNode *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
How do I fix this?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Check the return type of the loadScene function and you should know the problem. Read example 15 to find solutions.
IrrNoob
Posts: 110
Joined: Sun Nov 16, 2008 8:01 pm
Location: Fort Collins, Us

Post by IrrNoob »

ok, I'm confused. I tried this instead:

Code: Select all

 bool * Irr = smgr->loadScene("media/game.irr");
And I get this error

Code: Select all

error C2440: 'initializing' : cannot convert from 'bool' to 'bool *'
        Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
So how do I fix this?
Am I supposed to do a reinterpret cast? If so How do I do that?
IrrNoob
Posts: 110
Joined: Sun Nov 16, 2008 8:01 pm
Location: Fort Collins, Us

Post by IrrNoob »

Code: Select all

bool Irr = smgr->loadScene("media/game.irr");
This seems to work for the exit function, but is this the correct way?
Seven
Posts: 1034
Joined: Mon Nov 14, 2005 2:03 pm

Post by Seven »

what does smgr->loadScene() return? Does it return a bool* or something else?
maroxe
Posts: 51
Joined: Wed Dec 10, 2008 1:52 pm

Post by maroxe »

a bool
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Yes, this is the correct way to handle the return value. However, you could have ignored this as well, because the important part is to initialize the Irr scene node pointer :idea:
IrrNoob
Posts: 110
Joined: Sun Nov 16, 2008 8:01 pm
Location: Fort Collins, Us

Post by IrrNoob »

Okay after adding some controls to adjust the speed of the camera based on which key is pressed I found that it does infact work:

Code: Select all

class MyEventReceiver : public IEventReceiver
{
public:

	MyEventReceiver(bool Irr)
	{
			for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
			KeyIsDown[i] = false;
	}

	bool OnEvent(const SEvent& event)
	{
		if (event.EventType == irr::EET_KEY_INPUT_EVENT)
			KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;

		return false;
	}

	// This is used to check whether a key is being held down
	virtual bool IsKeyDown(EKEY_CODE keyCode) const
	{
		return KeyIsDown[keyCode];
	}
	
private:
	// We use this array to store the current state of each key
	bool KeyIsDown[KEY_KEY_CODES_COUNT];
};

Code: Select all

    video::SMaterial material;
    material.Lighting = true;
    scene::IAnimatedMeshSceneNode* node = 0;
	material.setTexture(0, driver->getTexture("media/testGrid256X2.jpg"));

    scene::IAnimatedMesh* arm = smgr->getMesh("media/arm.md2");
    node = smgr->addAnimatedMeshSceneNode(arm);
    node->getMaterial(0) = material;



	// Add a light
   smgr->addLightSceneNode(0, core::vector3df(0,200,0),
		video::SColorf(1.0f,1.0f,1.0f,1.0f),
		600.0f);

	// load the scene
    bool Irr = smgr->loadScene("media/game.irr");

Code: Select all

	// add camera
    // CREATE WASD KEYMAP ARRAY 
    SKeyMap keyMapWASD[5]; 
    keyMapWASD[0].Action = EKA_MOVE_FORWARD; 
    keyMapWASD[0].KeyCode = KEY_KEY_W; 
    keyMapWASD[1].Action = EKA_MOVE_BACKWARD; 
    keyMapWASD[1].KeyCode = KEY_KEY_S; 
    keyMapWASD[2].Action = EKA_STRAFE_LEFT; 
    keyMapWASD[2].KeyCode = KEY_KEY_A; 
    keyMapWASD[3].Action = EKA_STRAFE_RIGHT; 
    keyMapWASD[3].KeyCode = KEY_KEY_D; 
    keyMapWASD[4].Action = EKA_JUMP_UP; 
    keyMapWASD[4].KeyCode = KEY_SPACE; 

    scene::ICameraSceneNode * camera = smgr->addCameraSceneNodeFPS(0,/*rotation*/50.0f,/*speed*/0.1f,0,keyMapWASD, 5, true, 0.7f);

	camera->setPosition(core::vector3df(3800,-350,7400));
	camera->setTarget(core::vector3df(4794,-686,3400));
	camera->setFarValue(12000.0f);
	camera->setNearValue(1.0f);
                camera->addChild(node);
                node->setPosition(core::vector3df(0,0,0));

Code: Select all

	// create event receiver
	MyEventReceiver receiver(Irr);
	device->setEventReceiver(&receiver);

Code: Select all

    u32 previous = device->getTimer()->getTime();

	while(device->run())
	if (device->isWindowActive())
	{
      const u32 current = device->getTimer()->getTime(); 
      const f32 elapsed = (current - previous) / 1000.f; // convert to fractional seconds 

      // the current time becomes the previous time for the next iteration 
      previous = current; 

      /* Check if key W or key S is being held down, and move the
	  camera forward or backward respectively.
	  */
      core::vector3df pmove(0, 0, 0);
    
      if(receiver.IsKeyDown(irr::KEY_KEY_W)) 
      { 
                                   
        pmove.Z += 20.f;

      } 
     
     if(receiver.IsKeyDown(irr::KEY_KEY_S)) 
     { 
        pmove.Z -= 10.f; 
     }              
    
      /* Check if key A or key D is being held down, and move the
      camera Left or Rigt respectively.
      */
	 
     if(receiver.IsKeyDown(irr::KEY_KEY_A)) 
     { 
       pmove.X -= 15.f;
     }              
    
     if(receiver.IsKeyDown(irr::KEY_KEY_D)) 
     { 
       pmove.X += 15.f; 
     }     
    
     // move the camera 
     core::vector3df pos( camera->getPosition() ); 

     // rotate the movement vector into parent coordinate system 
     camera->getRelativeTransformation().rotateVect(pmove); 

     //const core::vector3df velocity(60.f, 0.f, 30.f); // we can move faster forward than sideways 
     const f32 velocity = 20.f;

     // apply movement over elapsed time at provided velocity 
     pos += (pmove * (velocity * elapsed)); 

     // update the final position 
     camera->setPosition(pos); 

     // add an ESCAPE key to exit
     if (receiver.IsKeyDown(irr::KEY_ESCAPE))
     {
       device->closeDevice(); 
       device->drop(); 
       exit(0); 
     } 
Thanks for the quick responses and good help.
Last edited by IrrNoob on Tue Jan 06, 2009 12:00 am, edited 1 time in total.
IrrNoob
Posts: 110
Joined: Sun Nov 16, 2008 8:01 pm
Location: Fort Collins, Us

Post by IrrNoob »

hybrid wrote: you could have ignored this as well, because the important part is to initialize the Irr scene node pointer :idea:
I'm having trouble understanding this point.
Could you please show an example?
hybrid
Admin
Posts: 14143
Joined: Wed Apr 19, 2006 9:20 pm
Location: Oldenburg(Oldb), Germany
Contact:

Post by hybrid »

Well, since it already works you might not need it. Do you need to know which nodes have been loaded from the .irr file? I assumed this, because you assigned the return value to a pointer.
IrrNoob
Posts: 110
Joined: Sun Nov 16, 2008 8:01 pm
Location: Fort Collins, Us

Post by IrrNoob »

Yeah I would like to know how to do that.
Post Reply