Code: Select all
#include <irrlicht.h>
#include <stdlib.h> /* srand, rand */
#include <time.h>
#include <math.h>
using namespace irr;
using namespace core;
using namespace scene;
using namespace video;
using namespace io;
using namespace gui;
#ifdef _MSC_VER
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(linker, "/subsystem:windows /ENTRY:mainCRTStartup")
#endif
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;
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()
{
MyEventReceiver keyReciever;
IrrlichtDevice *device = createDevice(video::EDT_OPENGL, dimension2d<u32>(1280, 720), 32, false, false, false, &keyReciever);
if (!device)
return 1;
device->setWindowCaption(L"Voxel type world generator");
device->getCursorControl()->setVisible(false);
IVideoDriver* driver = device->getVideoDriver();
ISceneManager* smgr = device->getSceneManager();
//Control Camera with WASD
SKeyMap controlKeyMap[5];
controlKeyMap[0].Action = EKA_MOVE_FORWARD;
controlKeyMap[0].KeyCode = KEY_KEY_W;
controlKeyMap[1].Action = EKA_STRAFE_LEFT;
controlKeyMap[1].KeyCode = KEY_KEY_A;
controlKeyMap[2].Action = EKA_MOVE_BACKWARD;
controlKeyMap[2].KeyCode = KEY_KEY_S;
controlKeyMap[3].Action = EKA_STRAFE_RIGHT;
controlKeyMap[3].KeyCode = KEY_KEY_D;
controlKeyMap[4].Action = EKA_JUMP_UP;
controlKeyMap[4].KeyCode = KEY_KEY_F;
ICameraSceneNode* myCam = smgr->addCameraSceneNodeFPS(0, 50, .2, -1, controlKeyMap, 5);
myCam->setPosition(vector3df(-50, 0, 0));
//This is our map file and heightmap
IMeshSceneNode* map[50][50];
int heightmap[5][5] = { { 0, 1, 2, 3, 4 }, { 0, 0, 1, 2, 3 }, { 1, 0, 0, 1, 2 }, { 2, 1, 0, 0, 1 }, { 3, 2, 1, 1, 0 } };
//This will generate our cubes
ITriangleSelector* sel;
int size = 10;
srand(time(NULL));
for (int y = 0; y < sizeof(map[0]) / sizeof(*map[0]); y++){
for (int x = 0; x < sizeof(map) / sizeof(*map); x++){
map[x][y] = smgr->addCubeSceneNode(10);
map[x][y]->setPosition(vector3df(x*size, heightmap[x/10][y/10]*5, y*size));
map[x][y]->setMaterialTexture(0, driver->getTexture("C:/Users/Edward/Pictures/grass.jpg"));
map[x][y]->setMaterialFlag(EMF_LIGHTING, false);
//Should add our bounds?
sel = smgr->createOctreeTriangleSelector(map[x][y]->getMesh(), map[x][y]);
map[x][y]->setTriangleSelector(sel);
}
}
scene::ISceneNodeAnimator* anim = smgr->createCollisionResponseAnimator(
sel, myCam, vector3df(30, 40, 30),
vector3df(0, 0, 0),//vector3df(0, -30, 0),//Gravity
vector3df(0, 50, 0));
myCam->addAnimator(anim);
while (device->run())
{
driver->beginScene(true, true, SColor(255, 255, 0, 0));
if (keyReciever.IsKeyDown(KEY_SPACE)){}
smgr->drawAll();
driver->endScene();
}
device->drop();
return 0;
}