Code: Select all
#include <irrlicht.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
class MyEventReceiver : public IEventReceiver
{
public:
// 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 == 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];
}
MyEventReceiver()
{
for (u32 i=0; i<KEY_KEY_CODES_COUNT; ++i)
KeyIsDown[i] = false;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main(int argc, char** argv)
{
MyEventReceiver receiver;
//Create the main device for the game
IrrlichtDevice *device =
createDevice(EDT_OPENGL, dimension2d<s32>(640, 480), 16,
false, false, false, 0);
//Set the window caption
device->setWindowCaption(L"Psychic Demo");
//Create all of the environments for the game
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
IGUIEnvironment* guienv = device->getGUIEnvironment();
//Create the head
ISceneNode* head = smgr->addSphereSceneNode();
//Create the body
ISceneNode* body = smgr->addSphereSceneNode();
//Create one hand
ISceneNode* hand1 = smgr->addSphereSceneNode();
//Create the other hand
ISceneNode* hand2 = smgr->addSphereSceneNode();
//Create one foot
ISceneNode* foot1 = smgr->addSphereSceneNode();
//Create the other foot
ISceneNode* foot2 = smgr->addSphereSceneNode();
//Test cube
ISceneNode* cube = smgr->addCubeSceneNode();
cube->setPosition(vector3df(30, 0, 0));
//Set stuff for head
if (head)
{
head->setMaterialFlag(EMF_LIGHTING, true);
head->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
head->setScale(vector3df(0.8, 0.8, 0.8));
head->setPosition(vector3df(0,0,0));
head->setDebugDataVisible(EDS_FULL);
}
//Set stuff for the body
if (body)
{
body->setParent(head);
body->setMaterialFlag(EMF_LIGHTING, true);
body->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
body->setScale(vector3df(1, 1.8, 1));
body->setPosition(head->getPosition() + vector3df(0, -15, 0));
}
//Stuff for hand1
if (hand1)
{
hand1->setParent(head);
hand1->setMaterialFlag(EMF_LIGHTING, true);
hand1->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
hand1->setScale(vector3df(0.5, 0.5, 0.5));
hand1->setPosition(head->getPosition() + vector3df(0, -16, -8));
}
//Stuff for hand2
if (hand2)
{
hand2->setParent(head);
hand2->setMaterialFlag(EMF_LIGHTING, true);
hand2->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
hand2->setScale(vector3df(0.5, 0.5, 0.5));
hand2->setPosition(head->getPosition() + vector3df(0, -16, 8));
}
//Stuff for foot1
if (foot1)
{
foot1->setParent(head);
foot1->setMaterialFlag(EMF_LIGHTING, true);
foot1->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
foot1->setScale(vector3df(0.5, 0.5, 0.5));
foot1->setPosition(head->getPosition() + vector3df(0, -27, 6));
}
//Stuff for foot2
if (foot2)
{
foot2->setParent(head);
foot2->setMaterialFlag(EMF_LIGHTING, true);
foot2->setMaterialTexture(0, driver->getTexture("./textures/dummy.bmp"));
foot2->setScale(vector3df(0.5, 0.5, 0.5));
foot2->setPosition(head->getPosition() + vector3df(0, -27, -6));
}
//Create the camera
ICameraSceneNode* camera = smgr->addCameraSceneNode(0, vector3df(-50, 0, 0), vector3df(head->getPosition()));
camera->setParent(head);
camera->setTarget(head->getPosition());
//camera->setPosition(vector3df(-50, 0, 0));
//Hide or show the mouse cursor
device->getCursorControl()->setVisible(true);
while(device->run())
{
//Detect mouse key presses
if (receiver.IsKeyDown(KEY_KEY_W))
{
head->setPosition(head->getPosition() + vector3df(0, 0, 1));
}
if (receiver.IsKeyDown(KEY_KEY_S))
{
head->setPosition(head->getPosition() + vector3df(0, 0, -1));
}
if (receiver.IsKeyDown(KEY_KEY_A))
{
head->setRotation(head->getRotation() + vector3df(1, 0, 0));
}
if (receiver.IsKeyDown(KEY_KEY_D))
{
head->setRotation(head->getRotation() + vector3df(1, 0, 0));
}
camera->setTarget(head->getPosition());
driver->beginScene(true, true, SColor(0,200,200,200));
smgr->drawAll();
guienv->drawAll();
driver->endScene();
}
device->drop();
return 0;
}