#pragma once

#include <windows.h>
#include <dxfile.h>
#include "XFile.h"
#include "d3d9.h"
#include "d3dx9.h"

// A macro to quickly release and NULL a COM interface
#define XPReleaseCOM(x) { if(x) { x->Release(); x= NULL; } }

class XFileLoader
{
protected:
	// Functions called when parsing begins and end
	virtual BOOL BeginParse(void **Data) { return TRUE; }
	virtual BOOL EndParse(void **Data)   { return TRUE; }

	// Function called for every template found
	virtual BOOL ParseObject(ID3DXFileData *pDataObj, ID3DXFileData *pParentDataObj, DWORD Depth, void **Data, BOOL Reference)
	{
		return ParseChildObjects(pDataObj, Depth, Data, Reference);
	}

	// Function called to enumerate child templates
	BOOL ParseChildObjects(ID3DXFileData *pDataObj, DWORD Depth, void **Data, BOOL ForceReference = FALSE);


public:
	// Function to start parsing an .X file
	BOOL Parse(char *Filename, void **Data = NULL);

	// Functions to help retrieve template information
	const GUID *GetObjectGUID(ID3DXFileData *pDataObj);
	char *GetObjectName(ID3DXFileData *pDataObj);
	void *GetObjectData(ID3DXFileData *pDataObj, DWORD *Size);
};

