#include <irrlicht.h>
#include <iostream>


#include "HardwareSkinCallback.h"

#pragma comment(lib,"irrlicht.lib")

using namespace irr;
using namespace scene;
using namespace video;
using namespace io;
using namespace core;

// Yey for globals... =/
IrrlichtDevice* device = 0;


int main()
{
	
	
	char i = 0;
	char j = 0;
	char k = 0;

	printf("Dx or Ogl?\n1.Dx\n2.Ogl\n");
	std::cin >> i;

	//printf("Use ATI Technique?\n1.Yes\n2.No\n");
	//std::cin >> use_ati;

	printf("Activate the hardware skinning?\n1.Yes\n2.No\n3.Disable skinning\n");
	std::cin >> j;

	printf("Enable Lighting?\n1.Yes\n2.No\n");
	std::cin >> k;

	if(i == '1')
		device = createDevice(EDT_DIRECT3D9);
	else
		device = createDevice(EDT_OPENGL);  

	ISceneManager* smgr = device->getSceneManager();
	IVideoDriver* driver = device->getVideoDriver();

	IAnimatedMeshSceneNode* dwarf = smgr->addAnimatedMeshSceneNode(smgr->getMesh("dwarf.x"));
	ISkinnedMesh* mesh = (ISkinnedMesh*)dwarf->getMesh();
	dwarf->setScale(vector3df(1,1,1));
	if (j=='3')
		mesh->setHardwareSkinning(true);
	
	if (k=='1') 
		dwarf->setMaterialFlag(EMF_LIGHTING, true);
	else
		dwarf->setMaterialFlag(EMF_LIGHTING, false);

	if (j == '1')
		HWSkinCB::getInstance()->setupNode(device,dwarf,17);

	for(int z=0; z<6; z++)
	{
		for(int x=0; x<=6; x++)
		{
			if (x!=0 || z!=0)
			{
				ISceneNode* dwarfClone = dwarf->clone();
				dwarf->setPosition(vector3df(x*50,0,z*50));
			}
		}
	}

	ICameraSceneNode* cammy = smgr->addCameraSceneNode();
	
	
	cammy->setTarget(vector3df(125,32,125));
	cammy->addAnimator(smgr->createFlyCircleAnimator(vector3df(125,100,125),200,0.0002f));

	ILightSceneNode* light = smgr->addLightSceneNode(0,vector3df(0,0,0));
	light->addAnimator(smgr->createFlyCircleAnimator(vector3df(125,180,125),100,0.001f));
	light->getLightData().DiffuseColor = SColorf(0.5f,0.5f,0.0f,0.0f);

	light = smgr->addLightSceneNode(0,vector3df(0.0f,0.0f,0.0f));
	light->addAnimator(smgr->createFlyCircleAnimator(vector3df(125,180,125),100.0f,-0.001f));
	light->getLightData().DiffuseColor = SColorf(0.0f,1.0f,0.0f,0.0f);

	light = smgr->addLightSceneNode(0,vector3df(0.0f,0.0f,0.0f));
	light->addAnimator(smgr->createFlyCircleAnimator(vector3df(125,180,125),80.0f,0.001f));
	light->getLightData().DiffuseColor = SColorf(0.0f,0.5f,1.0f,0.0f);

	light = smgr->addLightSceneNode(0,vector3df(0.0f,0.0f,0.0f));
	light->addAnimator(smgr->createFlyCircleAnimator(vector3df(125,180,125),60.0f,-0.002f));
	light->getLightData().DiffuseColor = SColorf(0.8f,0.2f,0.0f,0.0f);
	
	int lastFPS=0;
	while(device->run())
	{
		driver->beginScene(true,true,SColor(255,0,0,200));
		smgr->drawAll();
		driver->endScene();
		 // display frames per second in window title
		int fps = driver->getFPS();
		if (lastFPS != fps)
		{
			core::stringw str = L"Hardware Skinning example";
			
			if (j=='2')
				str += "(Hardware Skinning is disabled)";

			str += " FPS:";
			str += fps;

			device->setWindowCaption(str.c_str());
			lastFPS = fps;
		}
	}
	device->closeDevice();
	device->drop();
	return 0;
};