bool CAnimationManager::LoadBinary(std::string szFilename)
{
	ifstream in;
	in.open(szFilename, ios_base::in | ios_base::binary);
	if(!in.is_open())
	{
		cout << "Failed to load Animation(from binary file)" << endl;
		return false;
	}

	//file is open
	if(in.good())
	{
		int nAnimCount, nFrameCount;
		std::vector<CAnimation*> list;
		CAnimation* anim;
		CFrame* frame;
		//Animation Count
		in.read((char *)&nAnimCount, sizeof(int));
		for(int i = 0; i < nAnimCount; i++)
		{
			anim = new CAnimation();
			//Frame Count
			in.read((char *)&nFrameCount, sizeof(int));
			for(int j = 0; j < nFrameCount; j++)
			{
			    //Anchor Point
				int nAPointX, nAPointY;
                in.read((char *)&nAPointX, sizeof(int));
                in.read((char *)&nAPointY, sizeof(int));
                //Draw Rect
				int nLeft, nTop, nRight, nBottom;
                in.read((char *)&nLeft, sizeof(int));
				in.read((char *)&nTop, sizeof(int));
				in.read((char *)&nRight, sizeof(int));
				in.read((char *)&nBottom, sizeof(int));
                //Collision Rect
                int nCLeft, nCTop, nCRight, nCBottom;
                in.read((char *)&nCLeft, sizeof(int));
				in.read((char *)&nCTop, sizeof(int));
				in.read((char *)&nCRight, sizeof(int));
				in.read((char *)&nCBottom, sizeof(int));
                //Duration
				float fDuration;
                in.read((char *)&fDuration, sizeof(float));
                //Trigger
				int nTrigLength;
				std::string szTriggerID;
                in.read((char *)&nTrigLength, sizeof(int));
                for (int h = 0; h < nTrigLength; h++) 
                {
					char szletter;
                    in.read((char *)&szletter, sizeof(char));
					szTriggerID.push_back(szletter);
                }
				frame = new CFrame(fDuration, szTriggerID,
					               nAPointX - nRight, nAPointY - nBottom,
					               nTop, nLeft, nBottom, nRight,
								   nCTop, nCLeft, nCBottom, nCRight);
				anim->AddFrame(frame);
			}//end frame loop
			//Path & Filename
			int nPathFileLength;
			std::string szPathFilename;
            in.read((char *)&nPathFileLength, sizeof(int));
            for (int n = 0; n < nPathFileLength; n++)
            {
                char szletter;
                in.read((char *)&szletter, sizeof(char));
			    szPathFilename.push_back(szletter);
            }
            //Filename 
            int nFileLength;
			std::string szFilename;
            in.read((char *)&nFileLength, sizeof(int));
            for (int k = 0; k < nFileLength; k++)
            {
                char szletter;
                in.read((char *)&szletter, sizeof(char));
			    szFilename.push_back(szletter);
            }
            //Name
            int nNameLength;
			std::string szName;
            in.read((char *)&nNameLength, sizeof(int));
            for (int k = 0; k < nNameLength; k++)
            {
                char szletter;
                in.read((char *)&szletter, sizeof(char));
			    szName.push_back(szletter);
            }
			anim->SetName(szName);
            //ImageID
			int nImageID; //-useless
            in.read((char *)&nImageID, sizeof(int));
            //Speed
			float fSpeed;
            in.read((char *)&fSpeed, sizeof(float));
			anim->SetSpeed(fSpeed);
            //Looping
			bool bIsLooping;
            in.read((char *)&bIsLooping, sizeof(bool));
			anim->SetLooping(bIsLooping);
            //Reversed
            bool bIsReversed;
            in.read((char *)&bIsReversed, sizeof(bool));
			anim->SetReversed(bIsReversed);
			list.push_back(anim);
		}//end animation loop
		//add them to the main list - in reverse order
		for(int size = (list.size() - 1); size >= 0; size--)
		{
			AddAnimation(list[size]);
		}
		
		in.close();
		return true;
	}
	return false;
}
CAnimation* CAnimationManager::LoadSingleAnimation(string szFileName, string szImageName)
{

    CAnimation* Anim = new CAnimation();

    TiXmlDocument doc;
    // Adding Paths to filename for animations
    string szPath = "resource/animations/";
    szPath += szFileName;

    string szImagePath = "resource/graphics/";
    szImagePath += szImageName;

    do
    {
        if( doc.LoadFile(szPath.c_str() ) == false )
            break;

        TiXmlElement* pRoot = doc.RootElement();

        if( !pRoot )
            break;

        TiXmlElement* pAnim = pRoot->FirstChildElement("Animation");

        if( pAnim == NULL )
            break;


        while( pAnim )
        {

            TiXmlElement* pNewAnim = pAnim;

            //***************Animation IsLooping**********************//
            if( pNewAnim->Attribute("IsLooping") == NULL )
                break;

            int dIsLooping = 0;

            pNewAnim->Attribute("IsLooping", &dIsLooping);

            if( dIsLooping != 0 )
                Anim->SetIsLooping(true);
            else
                Anim->SetIsLooping(false);


            Anim->SetImageID(CSGD_TextureManager::GetInstance()->LoadTexture(szImagePath.c_str() ) );

            //********************Animation Name*************************//

            if( pNewAnim->Attribute("Name") == NULL )
                break;

            const char* szName;

            szName = pNewAnim->Attribute("Name");
            Anim->SetName(szName);

            TiXmlElement* pFrame = pNewAnim->FirstChildElement("Frames");

            if( pFrame == NULL )
                break;

            // Animation Frames
            while( pFrame )
            {
                CFrame* Frame = new CFrame();
                TiXmlElement* pNewFrame = pFrame;
                ////*****************Setting Height*********************************//

                if( pNewFrame->Attribute("Height") == NULL )
                    break;

                double dHeight = 0.0;
                pNewFrame->Attribute("Height",&dHeight);
                Frame->SetHeight((int)dHeight);

                ////*******************Setting Width*******************************//

                if( pNewFrame->Attribute("Width") == NULL )
                    break;

                double dWidth = 0.0;
                pNewFrame->Attribute("Width",&dWidth);
                Frame->SetWidth((int)dWidth);

                //*****************Setting Anchor X*********************************//

                if( pNewFrame->Attribute("AnchorX") == NULL)
                    break;

                double dAnchorX = 0.0;
                pNewFrame->Attribute("AnchorX",&dAnchorX);
                Frame->SetAnchorX((int)dAnchorX);

                ////*******************Setting Anchor Y*******************************//

                if( pNewFrame->Attribute("AnchorY") == NULL )
                    break;

                double dAnchorY = 0.0;
                pNewFrame->Attribute("AnchorY",&dAnchorY);
                Frame->SetanchorY((int)dAnchorY);

                //*****************Setting Draw X*********************************//

                if( pNewFrame->Attribute("FrameX") == NULL)
                    break;

                double dDrawX = 0.0;
                pNewFrame->Attribute("FrameX",&dDrawX);
                Frame->SetDrawX((int)dDrawX);

                //*******************Setting Draw Y*******************************//

                if( pNewFrame->Attribute("FrameY") == NULL )
                    break;

                double dDrawY = 0.0;
                pNewFrame->Attribute("FrameY",&dDrawY);
                Frame->SetDrawY((int)dDrawY);


                //*******************Setting Duration of Frame*********************//
                if ( pNewFrame->Attribute("Duration") == NULL )
                    break;

                double dDuration = 0.0;

                pNewFrame->Attribute("Duration",&dDuration);
                Frame->SetDuration((float)dDuration);

                //*********************Setting Event**********************************//
                if( pNewFrame->Attribute("Event") == NULL)
                {

                }
                else
                {

                    const char* szEvent = NULL;
                    char buffer[128] = {0};

                    szEvent = pNewFrame->Attribute("Event");

                    strcpy_s(buffer,_countof(buffer),szEvent);
                    Frame->SetEvent(buffer);
                }

                //*********************Add frame to vector and go to next*****************//

                Anim->m_vecFrames.push_back(Frame);

                pFrame = pFrame->NextSiblingElement("Frames");
            }

            //*******************Add animation to vector and continue*******************//

            pAnim = pAnim->NextSiblingElement("Animation");

        }
        break;

    } while(true);

    return Anim;
}
bool CAnimationManager::LoadXML(std::string szFilename)
{
	TiXmlDocument doc;
	if( doc.LoadFile(szFilename.c_str()) == false)
	{
		cout << "Failed to load Animation(from XML file)" << endl;
		return false;
	}

	TiXmlElement* pRoot = doc.RootElement();
	if(!pRoot)
		return false;

	TiXmlElement* pAnims = pRoot->FirstChildElement("CAnimation");
	TiXmlElement* pFrames, *pData, *pDataChild;
	std::vector<CAnimation*> list;
	CAnimation* anim;
	CFrame* frame;
	bool isSingle = false;
	//Check to see if the file has many animations
	if(!pAnims)
	{
		//The file must contain only 1 animation
		//problem is here
		pAnims = pRoot;
		isSingle = true;
	}

	if(pAnims)
	{
		//we have the CAnimation Node
		while(pAnims)
		{
			anim = new CAnimation();
			//list of frames
			pFrames = pAnims->FirstChildElement("frames");
			if(pFrames)
			{
				//CFrame Node
				pFrames = pFrames->FirstChildElement("CFrame");
				while(pFrames)
				{
					std::string x, y;
					std::string width, height;
					std::string dur;
					int anchorx, anchory;
					int nx, ny, nwidth, nheight;
					int cx, cy, cwidth, cheight;
					float fdur;
					pData = pFrames;
					//Anchor Point
					pDataChild = pData->FirstChildElement("PAnchor");
					pDataChild = pDataChild->FirstChildElement("X");
					x = pDataChild->GetText();
					anchorx = atoi(x.c_str());
					pDataChild = pData->FirstChildElement("PAnchor");
					pDataChild = pDataChild->FirstChildElement("Y");
					y = pDataChild->GetText();
					anchory = atoi(y.c_str());
					//Draw Rect
					pDataChild = pData->FirstChildElement("RDrawRect");
					pDataChild = pDataChild->FirstChildElement("X");
					x = pDataChild->GetText();
					nx = atoi(x.c_str()); 
					pDataChild = pData->FirstChildElement("RDrawRect");
					pDataChild = pDataChild->FirstChildElement("Y");
					y = pDataChild->GetText();
					ny = atoi(y.c_str()); 
					pDataChild = pData->FirstChildElement("RDrawRect");
					pDataChild = pDataChild->FirstChildElement("Width");
					width = pDataChild->GetText();
					nwidth = atoi(width.c_str());
					pDataChild = pData->FirstChildElement("RDrawRect");
					pDataChild = pDataChild->FirstChildElement("Height");
					height = pDataChild->GetText();
					nheight = atoi(height.c_str());
					//Collision Rect
					pDataChild = pData->FirstChildElement("RCollisionRect");
					pDataChild = pDataChild->FirstChildElement("X");
					x = pDataChild->GetText();
					cx = atoi(x.c_str()); 
					pDataChild = pData->FirstChildElement("RCollisionRect");
					pDataChild = pDataChild->FirstChildElement("Y");
					y = pDataChild->GetText();
					cy = atoi(y.c_str()); 
					pDataChild = pData->FirstChildElement("RCollisionRect");
					pDataChild = pDataChild->FirstChildElement("Width");
					width = pDataChild->GetText();
					cwidth = atoi(width.c_str());
					pDataChild = pData->FirstChildElement("RCollisionRect");
					pDataChild = pDataChild->FirstChildElement("Height");
					height = pDataChild->GetText();
					cheight = atoi(height.c_str());
					//Duration
					pData = pFrames;
					pDataChild = pData->FirstChildElement("FDuration");
					dur = pDataChild->GetText();
					fdur = (float)atof(dur.c_str()); 
					//TriggerID
					std::string name;
					pDataChild = pData->FirstChildElement("SzTriggerID");
					if(pDataChild->GetText())
						name = pDataChild->GetText();
					//Add Frame
					frame = new CFrame(fdur, name, 
						anchorx - (nx + nwidth), anchory - (ny + nheight),
						ny, nx, (ny + nheight), (nx + nwidth),
						cy, cx, (cy + cheight), (cx + cwidth));
					anim->AddFrame(frame);
					pFrames = pFrames->NextSiblingElement("CFrame");
				}//end CFrame	
			}//end list of frames
			std::string speed;
			float fspeed;
			pData = pAnims;
			//ImageID
			//pAnims->FirstAttribute();
			//Animation name
			//pData->FirstChildElement("SzName")->GetText();
			std::string name = pData->FirstChildElement("SzName")->GetText();
			anim->SetName(name);
			//Filename
			//pData->NextSibling("SzFilename");
			//Filename&Path
			//pData->NextSibling("SzFilenameWithPath");
			//Speed
			speed = pAnims->FirstChildElement("FSpeed")->GetText();
			fspeed = (float)atof(speed.c_str());
			anim->SetSpeed(fspeed);
			//IsPlaying

			//IsLooping
			std::string text;
			text = pAnims->FirstChildElement("BIsLooping")->GetText();
			if(text == "true")
				anim->SetLooping(true);
			else if(text == "false")
				anim->SetLooping(false);

			//IsReversed
			text = pAnims->FirstChildElement("BIsReversed")->GetText();
			if(text == "true")
				anim->SetReversed(true);
			else if(text == "false")
				anim->SetReversed(false);

			list.push_back(anim);
			//to next Animation in file
			pAnims = pAnims->NextSiblingElement("CAnimation");

		}//End CAnimation
		//add them to the main list - in reverse order
		for(int size = (list.size() - 1); size >= 0; size--)
		{
			AddAnimation(list[size]);
		}
		return true;
	}

	return false;
}
int CAnimationManager::LoadAnimation(const char* szFileName, int* pnFirstAnmID, int* pnLastAnmID)
{
	if(pnFirstAnmID)
		*pnFirstAnmID = -1;
	if(pnLastAnmID)
		*pnLastAnmID = -1;

	TiXmlDocument doc;
	if(doc.LoadFile(szFileName) == false)
		return false;

	TiXmlElement* pRoot = doc.RootElement();

	if(!pRoot)
		return false;
	string filepath;
	filepath.append("resource/");

	const char* pDir;
	if(pRoot->Attribute("FileName"))
	{
		pDir = pRoot->Attribute("FileName");
		char buffer[128] = {0};
		strcpy_s(buffer,_countof(buffer),pDir);
		filepath.append(pDir);
	}
	int nAnimationSheet = TEX_MNG->LoadTexture(filepath.c_str(),D3DCOLOR_XRGB(255,0,255));
	
	TiXmlElement* pAnimation = pRoot->FirstChildElement("Animation");
	while(pAnimation)
	{

		CAnimation* tempAnimation = new CAnimation;
		int nTemp = 0;
		double dTemp = 0.0;
		const char* pName;
		if(pAnimation->Attribute("name"))
		{
			pName = pAnimation->Attribute("name");
			char buffer[128] = {0};
			strcpy_s(buffer,_countof(buffer),pName);
			tempAnimation->SetName(buffer);
		}

		if(pAnimation->QueryIntAttribute("looping",&nTemp) != TIXML_NO_ATTRIBUTE)
		{
			tempAnimation->SetIsLooping((nTemp==1)?true:false);
		}

		if(pAnimation->QueryIntAttribute("oslating",&nTemp) != TIXML_NO_ATTRIBUTE)
		{
			tempAnimation->SetIsOscillating((nTemp==1)?true:false);
		}
		if(pAnimation->QueryIntAttribute("playing",&nTemp) != TIXML_NO_ATTRIBUTE)
		{
			tempAnimation->SetIsPlaying((nTemp==1)?true:false);
		}
		if(pAnimation->QueryDoubleAttribute("speed",&dTemp) != TIXML_NO_ATTRIBUTE)
		{
			tempAnimation->SetSpeed(dTemp);
		}
		TiXmlElement* pFrame = pAnimation->FirstChildElement("Frame");
		while (pFrame)
		{
			CFrame tempFrame;
			if(pFrame->QueryDoubleAttribute("duration",&dTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempFrame.SetDuration(dTemp);
			}

			const char* pEvent;
			if(pFrame->Attribute("event"))
			{
				pEvent = pFrame->Attribute("event");
				tempFrame.SetEvent(pEvent);
			}

			//	Get the draw rect
			TiXmlElement* pDrawRect = pFrame->FirstChildElement("DrawRect");

			RectD tempDrawRect;
			if(pDrawRect->QueryIntAttribute("x",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempDrawRect.left = nTemp;
			}
			if(pDrawRect->QueryIntAttribute("y",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempDrawRect.top = nTemp;
			}
			if(pDrawRect->QueryIntAttribute("width",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempDrawRect.right = tempDrawRect.left + nTemp;
			}
			if(pDrawRect->QueryIntAttribute("height",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempDrawRect.bottom = tempDrawRect.top + nTemp;
			}
			//	Set the Draw Rect
			tempFrame.SetDrawRect(tempDrawRect);

			//	Get the collsion Rect
			TiXmlElement* pCollisionRect = pDrawRect->NextSiblingElement("CollisionRect");

			RectD tempCollisionRect;
			if(pCollisionRect->QueryIntAttribute("x",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempCollisionRect.left = nTemp;
			}
			if(pCollisionRect->QueryIntAttribute("y",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempCollisionRect.top = nTemp;
			}
			if(pCollisionRect->QueryIntAttribute("width",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempCollisionRect.right = tempCollisionRect.left + nTemp;
				tempCollisionRect.width = nTemp;
			}
			if(pCollisionRect->QueryIntAttribute("height",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempCollisionRect.bottom = tempCollisionRect.top + nTemp;
				tempCollisionRect.height = nTemp;
			}
			//	Set the collision Rect
			tempFrame.SetCollisionRect(tempCollisionRect);
			//	Get the anchor
			TiXmlElement* pAnchor = pDrawRect->NextSiblingElement("Anchor");

			Point tempAnchor;

			if(pAnchor->QueryIntAttribute("x",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempAnchor.x = nTemp;
			}
			if(pAnchor->QueryIntAttribute("y",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempAnchor.y = nTemp;
			}
			tempFrame.SetAnchorPoint(tempAnchor);

			TiXmlElement* pWeapon = pAnchor->NextSiblingElement("WeaponCollisionRect");
			RectD tempWeaponCollisionRect;
			if(pWeapon->QueryIntAttribute("x",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempWeaponCollisionRect.left = nTemp;
			}
			if(pWeapon->QueryIntAttribute("y",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempWeaponCollisionRect.top = nTemp;
			}
			if(pWeapon->QueryIntAttribute("width",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempWeaponCollisionRect.right = tempWeaponCollisionRect.left + nTemp;
			}
			if(pWeapon->QueryIntAttribute("height",&nTemp) != TIXML_NO_ATTRIBUTE)
			{
				tempWeaponCollisionRect.bottom = tempWeaponCollisionRect.top + nTemp;
			}
			tempFrame.SetWeaponCollisionRect(tempWeaponCollisionRect);
			//Point tempWeaponAnchor;
			//if(pWeapon->QueryIntAttribute("x",&nTemp) != TIXML_NO_ATTRIBUTE)
			//{
			//	tempWeaponAnchor.x = nTemp;
			//}
			//if(pWeapon->QueryIntAttribute("y",&nTemp) != TIXML_NO_ATTRIBUTE)
			//{
			//	tempWeaponAnchor.y = nTemp;
			//}
			//tempFrame.SetWeaponAnchorPoint(tempWeaponAnchor);

			//float tempWeaponAngle;
			//if(pWeapon->QueryDoubleAttribute("angle",&dTemp) != TIXML_NO_ATTRIBUTE)
			//{
			//	tempWeaponAngle = dTemp;
			//}
			//tempFrame.SetWeaponAngle(tempWeaponAngle);
			

			tempAnimation->GetFrames()->push_back(tempFrame);
			pFrame = pFrame->NextSiblingElement("Frame");
		}
		tempAnimation->SetImageId(nAnimationSheet);
		m_Animations.push_back(tempAnimation);

		// Set the right values to be returned
		if(pnFirstAnmID)
		{
			if(*pnFirstAnmID == -1)
				*pnFirstAnmID = m_Animations.size() - 1;
		}
		if(pnLastAnmID)
		{
			*pnLastAnmID = m_Animations.size() - 1;
		}

		pAnimation = pAnimation->NextSiblingElement("Animation");
	}
	return 0;
}