Code: Select all
#include <irrlicht.h>
#include <iostream>
#include <cstdlib>
#include <conio.h>
#include <cmath>
using namespace std;
using namespace irr;
using namespace core;
using namespace video;
using namespace gui;
using namespace scene;
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 == irr::EET_KEY_INPUT_EVENT)
KeyIsDown[event.KeyInput.Key] = event.KeyInput.PressedDown;
// Remember the mouse state
if (event.EventType == irr::EET_MOUSE_INPUT_EVENT)
{
switch(event.MouseInput.Event)
{
case EMIE_LMOUSE_PRESSED_DOWN:
MouseState.LeftButtonDown = true;
break;
case EMIE_LMOUSE_LEFT_UP:
MouseState.LeftButtonDown = false;
break;
case EMIE_MOUSE_MOVED:
MouseState.Position.X = event.MouseInput.X;
MouseState.Position.Y = event.MouseInput.Y;
break;
default:
// We won't use the wheel
break;
}
}
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;
}
// We'll create a struct to record info on the mouse state
struct SMouseState
{
core::position2di Position;
bool LeftButtonDown;
SMouseState() : LeftButtonDown(false) { }
} MouseState;
const SMouseState & GetMouseState(void) const
{
return MouseState;
}
private:
// We use this array to store the current state of each key
bool KeyIsDown[KEY_KEY_CODES_COUNT];
};
int main()
{
MyEventReceiver receiver;
IrrlichtDevice *device = createDevice(video::EDT_SOFTWARE, dimension2d<u32>(640, 480), 16, false, false, false, &receiver);
IVideoDriver *driver = device->getVideoDriver();
ISceneManager *smgr = device->getSceneManager();
SMaterial material;
material.Lighting = false;
driver->setMaterial(material);
driver->setTransform(video::ETS_WORLD, core::matrix4());
vector3df position = vector3df(0, -40, 0);
ICameraSceneNode *camera = smgr->addCameraSceneNode(0, position, vector3df(0, 0, 0));
while (device->run())
{
driver->beginScene(true, true, SColor(255, 0, 0, 0));
if (receiver.IsKeyDown(irr::KEY_KEY_A))
position.X--;
if (receiver.IsKeyDown(irr::KEY_KEY_D))
position.X++;
if (receiver.IsKeyDown(irr::KEY_KEY_W))
position.Z--;
if (receiver.IsKeyDown(irr::KEY_KEY_S))
position.Z++;
u16 index[] = {0, 1};
camera->setPosition(position);
printf("x: %f y: %f z: %f\n", position.X, position.Y, position.Z);
driver->draw3DLine(vector3df(-100, 0, 0), vector3df(100, 0, 0), SColor(255, 255, 255, 255));
smgr->drawAll();
driver->endScene();
}
}