青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

天行健 君子當自強而不息

Using the .X File Format(8)

Constructing an .X Parser Class

So, you want to create a class to handle all aspects of parsing .X files, eh? Sounds great to me! In this .X file parser class, you can wrap up the Parse and ParseObject functions you saw earlier in this chapter, in the "Enumerating Data Objects" section. Use the code from those two functions and write the parser class to allow yourself to override the data object parsing functions, which will allow you to scan for specific objects.

Start the parser class with a simple definition and go from there.

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

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

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

Whoa! I know I said you should start with a simple definition, not what I've just shown here! Bear with me friends, because you'll quickly realize just how simple this class is going to be. So far, you have three functions in your new cXParser .X file parser class. You use these three functions (ParseObject, ParseChildObjects, and Parse) to process a single data object, scan for embedded child objects, and parse an entire file, respectively.

cXParser::Parse, which is the easiest of the functions, merely duplicates the code in the Parse function you used earlier in this chapter.

The second function, ParseObject, is your .X parser's workhorse. ParseObject is called for every single data object found in an .X file. You need to override the ParseObject function (a virtual function) for it to do something useful. As you can see from the ParseObject function prototype, there's a lot going on that'll need some explanation.

The first parameter for ParseObject is an IDirectXFileData object which, as you saw earlier in this chapter, represents the data object that is currently being enumerated. Inside your overridden function, you can access the object's data via the pDataObj pointer.

The second parameter, pParentDataObj (also an IDirectXFileData object), represents the parent (higher−level object) of the current data object that is being enumerated. This is provided in case you want to see whether the current object is a child of another object.

The Depth parameter measures the depth of the object in the hierarchy. The highest−level data objects are at a depth of 0, whereas child objects have their parent's depth plus one. As an example, I have shown a few Frame objects here, with their respective depths listed.

Frame RootFrame { // Depth = 0
  Frame ChildofRoot { // Depth = 1
    Frame ChildofChild { // Depth = 2
    }
  }

  Frame SiblingofRootChild { // Depth = 1
  }
}

Frame RootSibling { // Depth = 0
}

Data is the fourth parameter of ParseObject. It is a user−defined data pointer (or rather, a pointer to a data pointer) that you use to pass information to your parser functions. For example, you can create a data structure to contain all of the information you need.

typedef struct sDATA {
  long NumObjects;
  sDATA() { NumObjects = 0; }
} sDATA:

Note The depth of a data object is extremely useful for sorting hierarchies, such as frame hierarchies used in skeletal animation.

To pass an sDATA structure to your parsing functions, you instance it and use it during a call to cXParser::Parse, as shown here:
sDATA Data;

cXParser Parser;
Parser.Parse("test.x", (void**)&Data);

From then on, every time ParseObject is called you can cast an appropriate pointer to access your data structure.

BOOL cXParser::ParseObject(IDirectXFileData *pDataObj,
  IDirectXFileData *pParentDataObj,
  DWORD Depth,
  void **Data, BOOL Reference)
{
  cDATA *DataPtr = (sDATA*)*Data;
  DataPtr−>NumObjects++; // Increase object count

  return ParseChildObjects(pDataObj,Depth,Data,Reference);
}

I know I'm getting ahead of myself again by showing you some sample code for cXParser, so let's jump back to the fifth (and last) parameter for ParseObject−Reference. The Reference Boolean variable determines whether the data object being enumerated is referenced or instanced. You can use the Reference variable to determine whether you want to load a referenced object's data or wait for the object to be instanced. This is useful when it comes time to load animation data, which needs data object references rather than actual object instances.

Whew! With the ParseObject function set aside, you're left with the last of the trio of cXParser functions−ParseChildObjects. Thankfully, the ParseChildObjects function is easy−it merely enumerates any child data objects of the object you pass it. Typically, you call ParseChildObjects at the end of your ParseObject function, as I did in the last code bit.

You can see that you need to pass the current IDirectXFileData object, data object depth, data pointer, and reference flag to ParseChildObjects because it is responsible for increasing the depth and setting the parent data object as needed for the next call to ParseObject. If you don't want to parse any child data objects, however, you can skip the call to ParseChildObjects and return a TRUE or FALSE value.(TRUE forces enumeration to continue, whereas FALSE stops it.)

Now that the basics are in place, you need to expand on your parser class a bit. How about adding some functions to retrieve a data object's name, GUID, and data pointer, as well as inserting a couple of functions that are called before and after an .X file is parsed? Take a look at the following code to see what your new parser class should look like.

class cXParser
{
  
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(IDirectXFileData* xfile_data,
                             IDirectXFileData
* parent_xfile_data,        
                             DWORD  depth,  
                             
void** data, 
                             BOOL   is_ref)
    { 
      
return ParseChildObjects(xfile_data, depth, data, is_ref);
    }

    
// Function called to enumerate child templates
    BOOL ParseChildObjects(IDirectXFileData* xfile_data,       
                           DWORD depth, 
                           
void** data,         
                           BOOL force_ref 
= FALSE);
    
  
public:
    
// Function to start parsing an .X file
    BOOL Parse(const char* filename, void** data = NULL);

    
// Functions to help retrieve template information    
    const GUID* GetObjectGUID(IDirectXFileData* xfile_data);
    
char* GetObjectName(IDirectXFileData* xfile_data);
    
void* GetObjectData(IDirectXFileData* xfile_data, DWORD* size);    
};

#include 
"XParser.h"

#define ReleaseCOM(x) { if(x) { (x)->Release(); (x) = NULL; } }

BOOL cXParser::Parse(
const char* filename, void** data)
{
  
if(filename == NULL)
    
return FALSE;

  IDirectXFile
*    xfile;

  
if(FAILED(DirectXFileCreate(&xfile)))
    
return FALSE;
  
  
if(FAILED(xfile->RegisterTemplates((LPVOID) D3DRM_XTEMPLATES, D3DRM_XTEMPLATE_BYTES))) 
  {
    xfile
->Release();
    
return FALSE;
  }

  IDirectXFileEnumObject
* xfile_enum;
  
  
if(FAILED(xfile->CreateEnumObject((LPVOID) filename, DXFILELOAD_FROMFILE, &xfile_enum))) 
  {
    xfile
->Release();
    
return FALSE;
  }
    
  
if(BeginParse(data)) 
  {    
    IDirectXFileData
* xfile_data;

    
// Loop through all top-level objects, breaking on errors.
    while(SUCCEEDED(xfile_enum->GetNextDataObject(&xfile_data))) 
    {
      BOOL parse_result 
= ParseObject(xfile_data, NULL, 0, data, FALSE);
      ReleaseCOM(xfile_data);

      
if(parse_result == FALSE)
        
break;
    }

    EndParse(data);
  }   
  
  ReleaseCOM(xfile_enum);
  ReleaseCOM(xfile);

  
return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

BOOL cXParser::ParseChildObjects(IDirectXFileData
* xfile_data,       
                                 DWORD depth, 
                                 
void** data,         
                                 BOOL force_ref)
{       
  IDirectXFileObject
* child_xfile_obj;

  
// Scan for embedded templates
  while(SUCCEEDED(xfile_data->GetNextObject(&child_xfile_obj))) 
  {
    IDirectXFileDataReference
*     xfile_data_ref;
    IDirectXFileData
*             child_xfile_data;

    BOOL parse_result 
= TRUE;
    
    
if(SUCCEEDED(child_xfile_obj->QueryInterface(IID_IDirectXFileDataReference, (void**)&xfile_data_ref))) 
    {
      
// Process embedded references
      
      
if(SUCCEEDED(xfile_data_ref->Resolve(&child_xfile_data)))
      {
        parse_result 
= ParseObject(child_xfile_data, xfile_data, depth+1, data, TRUE);              
        ReleaseCOM(child_xfile_data);
      }

      ReleaseCOM(xfile_data_ref);
    } 
    
else if(SUCCEEDED(child_xfile_obj->QueryInterface(IID_IDirectXFileData, (void**)&child_xfile_data))) 
    {
      
// Process non-referenced embedded templates

      parse_result 
= ParseObject(child_xfile_data, xfile_data, depth+1, data, force_ref); 
      ReleaseCOM(child_xfile_data);
    }

    ReleaseCOM(child_xfile_obj);
    
    
if(parse_result == FALSE)    // parsing failure
      return FALSE;
  }

  
return TRUE;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////

const GUID* cXParser::GetObjectGUID(IDirectXFileData* xfile_data)
{   
  
if(xfile_data == NULL)
    
return NULL;

  
const GUID* type;
  
  
if(FAILED(xfile_data->GetType(&type)))
    
return NULL;

  
return type;
}

char* cXParser::GetObjectName(IDirectXFileData* xfile_data)
{  
  
if(xfile_data == NULL)
    
return NULL;

  DWORD  size 
= 0;  

  
if(FAILED(xfile_data->GetName(NULL, &size)))
    
return NULL;

  
char* name = NULL;
  
  
if(size) 
  {
    name 
= new char[size];    
    xfile_data
->GetName(name, &size);
  }

  
return name;
}

void* cXParser::GetObjectData(IDirectXFileData* xfile_data, DWORD* size)
{   
  
if(xfile_data == NULL)
    
return NULL;

  
void* object_data;
  DWORD object_size;
  
  xfile_data
->GetData(NULL, &object_size, (PVOID*&object_data);
  
  
if(size != NULL)    // save size if needed
    *size = object_size;

  
return object_data;
}

 

You can see the addition of the BeginParse, EndParse, GetObjectGUID, GetObjectName, and GetObjectData functions in cXParser. You've already seen the code for the three Get functions−it's the virtual BeginParse and EndParse functions that are unknown.

In their current form, both BeginParse and EndParse return TRUE values, which signify a successful function call. It's your job to override these two functions in a derived class so that you can perform any operations prior to and following a file parse. For instance, you might want to initialize any data or provide a data pointer inside your BeginParse function and clean up any used resources inside the EndParse function.

Both the BeginParse and EndParse functions are called directly from the Parse function−you merely need to override them and write the code for them.

As for the three Get functions, you use those by passing a valid IDirectXFileData object; in return, you'll get the name in a newly−allocated data buffer, a pointer to the template's GUID, or a pointer to the object's data buffer and data size value. For example, here's some code that calls the three functions to get and access an object's data:

// pData = pre−loaded data object
char *Name = pParser−>GetObjectName(pData);
const GUID *Type = pParser−>GetObjectGUID(pData);

DWORD Size;
char *Ptr = (char*)pParser−>GetObjectData(pData, &Size);

// Do something with data and free up resource when done
delete [] Name;

As I was saying, using your brand−new cXParser class is going to be really simple.  Advanced readers might want to take it upon themselves to modify the parser class to fit their individual needs. I personally find the class very useful in its current form.

As a quick test of your new cXParser class, let's see just how to derive and use it. Suppose you want to parse .X files for all Mesh data objects and display each one's name in a message box. Here's the code for the parser that will do just that:

class cParser : public cXParser
{
public:
cParser() { Parse("test.x"); }
	BOOL ParseObject(IDirectXFileData *pDataObj, 
IDirectXFileData *pParentDataObj,
DWORD Depth,
void **Data, BOOL Reference)
{
if(*GetObjectGUID(pDataObj) == TID_D3DRMMesh) {
char *Name = GetObjectName(pDataObj);
MessageBox(NULL, Name, "Mesh template", MB_OK);
delete [] Name;
}
		return ParseChildObjects(pDataObj,Depth,Data,Reference);
}
};
cParser Parser; // Instancing this will run the parser

Now tell me that wasn't easy! Enough basics, let's get into using .X files for something useful, such as 3D mesh data.


posted on 2008-04-17 19:54 lovedday 閱讀(482) 評論(0)  編輯 收藏 引用


只有注冊用戶登錄后才能發表評論。
網站導航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


公告

導航

統計

常用鏈接

隨筆分類(178)

3D游戲編程相關鏈接

搜索

最新評論

青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            男人插女人欧美| 久久精选视频| 亚洲精品一级| 欧美精品xxxxbbbb| 一区二区三区产品免费精品久久75| 欧美激情a∨在线视频播放| 久久久一区二区三区| 亚洲精品1234| 亚洲免费高清视频| 国产精品视频最多的网站| 欧美亚洲免费高清在线观看| 午夜精品久久久久久99热软件 | 久久一综合视频| 久久久久久有精品国产| 亚洲黄色性网站| 一本大道久久精品懂色aⅴ| 国产精品99免费看 | 国产日韩欧美不卡| 久久免费视频在线观看| 免费影视亚洲| 亚洲免费一在线| 久久国产精品高清| 夜夜嗨av一区二区三区中文字幕 | 欧美成va人片在线观看| 一区二区三区免费在线观看| 亚洲视频www| 在线观看日韩专区| 一本色道**综合亚洲精品蜜桃冫| 国产女人精品视频| 亚洲高清网站| 国产日本欧美一区二区三区| 欧美激情中文不卡| 国产精品自拍一区| 亚洲国产欧美一区二区三区久久 | 夜夜嗨av一区二区三区免费区| 亚洲性xxxx| 亚洲国产日韩欧美| 午夜精品视频一区| 一区二区三区**美女毛片| 午夜精品久久久| 一本久久综合| 免费成人av资源网| 久久久噜噜噜久久人人看| 欧美日韩综合视频网址| 欧美成人一品| 国产日韩欧美一区二区三区在线观看 | 毛片基地黄久久久久久天堂| 亚洲欧美日韩精品久久亚洲区| 免费成人av在线看| 久久综合九色综合久99| 国产精品私人影院| 一区二区三区日韩欧美精品| 亚洲日本中文字幕免费在线不卡| 亚洲欧美日韩一区在线| 亚洲一区在线看| 欧美日韩a区| 亚洲国产精品久久久| 影音先锋欧美精品| 欧美在线亚洲综合一区| 欧美中文字幕| 国产模特精品视频久久久久| 一区二区三区四区国产| 日韩系列在线| 欧美精品激情| 亚洲人成网站色ww在线| 亚洲欧洲一区二区天堂久久 | 亚洲国产一区二区三区高清| 亚洲国产精品专区久久| 欧美 日韩 国产 一区| 欧美国产日韩在线观看| 亚洲国产另类久久精品| 久久婷婷久久一区二区三区| 欧美**字幕| 最新国产成人在线观看| 欧美www视频在线观看| 亚洲二区在线| 亚洲性视频h| 国产精品亚洲网站| 午夜一区不卡| 欧美96在线丨欧| 日韩天天综合| 欧美视频在线观看免费网址| 亚洲一区二区精品在线| 久久精品国产第一区二区三区最新章节| 国产精品欧美经典| 欧美一区在线看| 欧美国产日韩xxxxx| 一区二区高清在线| 国产欧美三级| 另类综合日韩欧美亚洲| 亚洲韩国青草视频| 亚洲欧美日韩天堂一区二区| 国产日韩欧美视频| 免费成人av| 亚洲一区二区三区在线| 久久久久久久久久久一区| 亚洲二区精品| 国产精品久久久一本精品| 欧美伊人久久大香线蕉综合69| 欧美成人激情视频| 亚洲社区在线观看| 国产一区二区精品久久99| 欧美好骚综合网| 亚洲午夜精品| 欧美国产免费| 午夜免费日韩视频| 91久久极品少妇xxxxⅹ软件| 国产精品国产馆在线真实露脸| 欧美在线观看视频| 亚洲人成网站色ww在线| 久久视频国产精品免费视频在线 | 国产麻豆精品在线观看| 久久在线视频在线| 亚洲欧美日韩在线高清直播| 亚洲国产精品va在线观看黑人| 欧美一区=区| 宅男噜噜噜66一区二区 | 欧美日韩一区成人| 欧美一区二区网站| 亚洲精品久久久久久一区二区| 久久国产精品亚洲77777| 一区二区三区精品在线| 玉米视频成人免费看| 欧美性猛交99久久久久99按摩| 久久视频在线免费观看| 午夜日韩福利| 亚洲一区二区三区四区中文 | 美女日韩在线中文字幕| 亚洲欧美精品一区| 宅男噜噜噜66一区二区66| 亚洲丰满在线| 韩国女主播一区二区三区| 国产精品久久一区主播| 欧美美女操人视频| 欧美成人四级电影| 麻豆精品视频在线观看| 久久久av水蜜桃| 欧美一区二区三区在线观看| 亚洲天堂网在线观看| 日韩一级黄色大片| 亚洲精品一区在线观看| 最新国产乱人伦偷精品免费网站| 欧美+亚洲+精品+三区| 久久最新视频| 男人的天堂亚洲在线| 美国十次成人| 欧美激情欧美狂野欧美精品| 噜噜噜噜噜久久久久久91| 开心色5月久久精品| 免费一级欧美片在线观看| 美日韩精品免费观看视频| 嫩草成人www欧美| 欧美成人中文| 最新国产成人在线观看| 99re6这里只有精品| 一区二区免费在线播放| 亚洲视频一区二区| 亚洲主播在线| 午夜在线播放视频欧美| 久久国产精彩视频| 久久中文精品| 欧美精品久久久久久久| 欧美三日本三级少妇三2023| 国产精品美女久久久| 国产人成一区二区三区影院| 精品粉嫩aⅴ一区二区三区四区| 亚洲第一网站| 亚洲一级免费视频| 久久精品亚洲一区| 欧美成人久久| 妖精成人www高清在线观看| 亚洲免费在线播放| 久久久久久夜精品精品免费| 欧美—级在线免费片| 国产精品久久久久久户外露出| 国内精品久久久久影院优| 亚洲精品欧美日韩专区| 亚洲欧美日韩爽爽影院| 久久久中精品2020中文| 亚洲国产综合在线看不卡| 亚洲免费在线电影| 欧美/亚洲一区| 国产精品无码专区在线观看| 亚洲第一在线| 亚洲欧美日韩天堂| 欧美激情亚洲激情| 亚洲欧美亚洲| 欧美日韩国产免费| 国内精品嫩模av私拍在线观看| 亚洲免费成人| 久久综合色天天久久综合图片| 亚洲九九精品| 裸体一区二区三区| 国产日韩欧美在线观看| 日韩一级免费| 免费欧美在线| 午夜精品久久久久久久99樱桃| 欧美国产欧美亚洲国产日韩mv天天看完整 | 久久综合一区二区| 亚洲一区二区在线|