#include <irrlicht.h>
#include <IPhysics.h>
#pragma comment(lib, "Irrlicht.lib")
#pragma comment(lib, "IPhysics.lib")

CPhysics physics;
ISceneManager* smgr;
IVideoDriver* driver;
ICameraSceneNode* cam;

class EventReceiver : public IEventReceiver
{
public:
	bool OnEvent(SEvent event)
	{
		if(event.EventType == EET_MOUSE_INPUT_EVENT)
		{
			if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
			{
				//Throw test cube from camera's position
				f32 size,mass,velocity;
				size = 10;
				mass = 10;
				velocity = 5;
						
				SPhysicsPrimitive cube;

				cube.primitiveType = EPT_CUBE;
				cube.bodyType = EBT_DYNAMIC;
				cube.mass = mass;
				cube.node = smgr->addCubeSceneNode(size);
				cube.node->setMaterialFlag(EMF_LIGHTING, false);
				cube.node->setMaterialTexture(0, driver->getTexture("../../Media/crate.jpg"));
				cube.size = vector3df(size, size, size);

				IPhysicsEntity* entity = physics.addEntity(&cube);

				entity->setPosition(cam->getPosition());

				vector3df vel = (cam->getTarget() - cam->getPosition()).normalize() * velocity;
				entity->setVelocity(vel);

			}
			else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
			{
				//Throw test sphere from camera's position
				f32 size,mass,velocity;
				size = 10;
				mass = 10;
				velocity = 5;

				SPhysicsPrimitive sphere;

				sphere.primitiveType = EPT_SPHERE;
				sphere.bodyType = EBT_DYNAMIC;
				sphere.mass = mass;
				sphere.node = smgr->addSphereSceneNode(size);
				sphere.node->setMaterialFlag(EMF_LIGHTING, false);
				sphere.size = vector3df(size,size,size);

				IPhysicsEntity* entity = physics.addEntity(&sphere);

				entity->setPosition(cam->getPosition());

				vector3df vel = (cam->getTarget() - cam->getPosition()).normalize() * velocity;
				entity->setVelocity(vel);
			}
		} 

		return false;
	}
};

int main()
{
	// start Irrlicht
	IrrlichtDevice* device = createDevice(EDT_DIRECT3D9);

	// pointers to driver, scenemanager
	driver = device->getVideoDriver();
	smgr = device->getSceneManager();

	// create an FPS camera
	cam = smgr->addCameraSceneNodeFPS();

	// start iphysics
	physics.init(device);
	
	EventReceiver receiver;
	device->setEventReceiver(&receiver);

	// create terrain from heightmap
	ITerrainSceneNode* terrainNode = smgr->addTerrainSceneNode("../../media/heightmap.bmp");
	terrainNode->setMaterialTexture(0, driver->getTexture("../../media/terrain_texture.jpg"));
	terrainNode->setMaterialTexture(1, driver->getTexture("../../media/terrain_detail.jpg"));
	terrainNode->setMaterialType(EMT_DETAIL_MAP);
	terrainNode->scaleTexture(1.0f, 10.0f);
	terrainNode->setMaterialFlag(EMF_LIGHTING, false);

	// create SPhysicsTerrain
	SPhysicsTerrain terrain;
	terrain.node = terrainNode;
	terrain.scale = vector3df(20.0f, 2.0f, 20.0f);

	// create Terrain entity
	IPhysicsEntity* terrainEntity = physics.addEntity(&terrain);

	stringw caption;
	while(device->run())
	{
		driver->beginScene(true, true, SColor(0,100,100,100));
		
		caption = driver->getFPS();

		device->setWindowCaption(caption.c_str());

		physics.update();

		smgr->drawAll();

		driver->endScene();

	}

	physics.close();

	return 0;
}