Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
/* our Keyboard reciever class*/
class MyEventReceiver : public IEventReceiver
{
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
public:
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
// This is the one method that we have to implement
virtual bool OnEvent(const SEvent& event)
{
// Remember whether each key is down or up
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];
}
};
void Main()
{
/* createDevice() has 7 parameters:
- deviceType: Type of the device. This can currently be the Null-device,
one of the two software renderers, D3D8, D3D9, or OpenGL. In this
example we use EDT_SOFTWARE, but to try out, you might want to
change it to EDT_BURNINGSVIDEO, EDT_NULL, EDT_DIRECT3D8,
EDT_DIRECT3D9, or EDT_OPENGL.
- windowSize: Size of the Window or screen in FullScreenMode to be
created. In this example we use 640x480.
- bits: Amount of color bits per pixel. This should be 16 or 32. The
parameter is often ignored when running in windowed mode.
- fullscreen: Specifies if we want the device to run in fullscreen mode
or not.
- stencilbuffer: Specifies if we want to use the stencil buffer (for
drawing shadows).
- vsync: Specifies if we want to have vsync enabled, this is only useful
in fullscreen mode.
- eventReceiver: An object to receive events. */
MyEventReceiver receiver;
IrrlichtDevice *device =createDevice( video::EDT_DIRECT3D9, dimension2d<u32>(1024, 768), 32,
false, false, false, &receiver);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
/*
Set the caption of the window to some text. Note that there is an
'L' in front of the string. The Irrlicht Engine uses wide character
strings when displaying text.
*/
device->setWindowCaption(L"Irrlicht Engine Test");
/*load a mesh for Main Persona*/
IAnimatedMesh* meshMP = smgr->getMesh(".../Dat/sydney.md2");
/*Create a Node for Main Persona*/
IAnimatedMeshSceneNode* nodeMP = smgr->addAnimatedMeshSceneNode( meshMP );
/*Set Materials and Animation for Main Persona Node*/
nodeMP->setMaterialFlag(EMF_LIGHTING, false);
//nodeMP->nodeMP->setMD2Animation(scene::EMAT_STAND);
nodeMP->setMaterialTexture( 0, driver->getTexture(".../Dat/sydney.bmp") );
/*Add a Camera*/
smgr->addCameraSceneNode(0,vector3df(0,250,0), vector3df(0,0,0));
const float speed=25.0f;
core::vector3df nodeMPCurPosition;
core::vector3df nodeMPNewPosition;
while(device->run())
{
nodeMPCurPosition = nodeMP->getPosition();
if(receiver.IsKeyDown(irr::KEY_KEY_W)){
nodeMPNewPosition=nodeMPCurPosition;
nodeMPNewPosition.X+=speed;
nodeMP->setMD2Animation(scene::EMAT_RUN);
scene::ISceneNodeAnimator* animMPRun =
smgr->createFlyStraightAnimator(nodeMPCurPosition,
nodeMPNewPosition, 1000, false);
nodeMP->addAnimator(animMPRun);
animMPRun->drop();
nodeMP->setPosition(nodeMPNewPosition);
}
if(receiver.IsKeyDown(irr::KEY_KEY_S)){
nodeMPNewPosition=nodeMPCurPosition;
nodeMPNewPosition.X-=speed;
nodeMP->setMD2Animation(scene::EMAT_RUN);
scene::ISceneNodeAnimator* animMPRun =
smgr->createFlyStraightAnimator(nodeMPCurPosition,
nodeMPNewPosition, 1000, false);
nodeMP->addAnimator(animMPRun);
animMPRun->drop();
nodeMP->setPosition(nodeMPNewPosition);
}
if(receiver.IsKeyDown(irr::KEY_KEY_A)){
nodeMPNewPosition=nodeMPCurPosition;
nodeMPNewPosition.Z+=speed;
nodeMP->setMD2Animation(scene::EMAT_RUN);
scene::ISceneNodeAnimator* animMPRun =
smgr->createFlyStraightAnimator(nodeMPCurPosition,
nodeMPNewPosition, 1000, false);
nodeMP->addAnimator(animMPRun);
animMPRun->drop();
nodeMP->setPosition(nodeMPNewPosition);
}
if(receiver.IsKeyDown(irr::KEY_KEY_D))
{
nodeMPNewPosition=nodeMPCurPosition;
nodeMPNewPosition.Z-=speed;
nodeMP->setMD2Animation(scene::EMAT_RUN);
scene::ISceneNodeAnimator* animMPRun =
smgr->createFlyStraightAnimator(nodeMPCurPosition,
nodeMPNewPosition, 1000, false);
nodeMP->addAnimator(animMPRun);
animMPRun->drop();
}
driver->beginScene(true, true, SColor(255,0,255,0));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
device->drop();
return ;
}