Cocos2d-x源碼解析(1)——地圖模塊(1)

cocos通過載入tiled 生成的tmx文件來生成遊戲地圖,本文主要分析cocos載入地圖模塊的源碼。

 


如圖所示,地圖載入模塊由以上幾個類組成。

對外的入口是類CCTMXTiledMap,在使用該類時,程序員不需要知道其底層的其他類便可解析tmx文件生成地圖。

那麼,我們首先分析類CCTMXTiledMap是如何調用其他類進行解析,該類的聲明如下:

class CC_DLL CCTMXTiledMap :public CCNode
{
    /** the map's size property measured intiles */
    CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tMapSize, MapSize);
    /** the tiles's size property measured inpixels */
    CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tTileSize, TileSize);
    /** map orientation */
    CC_SYNTHESIZE(int, m_nMapOrientation,MapOrientation);
    /** object groups */
    CC_PROPERTY(CCArray*, m_pObjectGroups,ObjectGroups);
    /** properties */
    CC_PROPERTY(CCDictionary*, m_pProperties,Properties);
public:
    /**
     * @js ctor
     */
    CCTMXTiledMap();
    /**
     * @js NA
     * @lua NA
     */
    virtual ~CCTMXTiledMap();
 
    /** creates a TMX Tiled Map with a TMXfile.*/
    static CCTMXTiledMap* create(const char*tmxFile);
 
    /** initializes a TMX Tiled Map with a TMXformatted XML string and a path to TMX resources */
    static CCTMXTiledMap* createWithXML(constchar* tmxString, const char* resourcePath);
 
    /** initializes a TMX Tiled Map with a TMXfile */
    bool initWithTMXFile(const char *tmxFile);
 
    /** initializes a TMX Tiled Map with a TMXformatted XML string and a path to TMX resources */
    bool initWithXML(const char* tmxString,const char* resourcePath);
 
    /** return the TMXLayer for the specificlayer
     * @js getLayer
     */
    CCTMXLayer* layerNamed(const char*layerName);
 
    /** return the TMXObjectGroup for thespecific group
     * @js getObjectGroup
     */
    CCTMXObjectGroup* objectGroupNamed(constchar *groupName);
 
    /** return the value for the specificproperty name
     * @js getProperty
     */
    CCString *propertyNamed(const char*propertyName);
 
    /** return properties dictionary for tileGID */
    CCDictionary* propertiesForGID(int GID);
 
private:
   CCTMXLayer *parseLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
    CCTMXTilesetInfo *tilesetForLayer(CCTMXLayerInfo *layerInfo, CCTMXMapInfo *mapInfo);
    void buildWithMapInfo(CCTMXMapInfo*mapInfo);
protected:
    //! tile properties
    CCDictionary* m_pTileProperties;
 
};


通過查看函數實現,我們發現函數

/** creates a TMX Tiled Map with a TMX file.*/
    static CCTMXTiledMap* create(const char*tmxFile);


通過調用函數

/** initializes a TMX Tiled Mapwith a TMX file */
    bool initWithTMXFile(const char *tmxFile);


來實現文件的解析,並創建一個CCTMXTiledMap對象返回給使用者。

boolCCTMXTiledMap::initWithTMXFile(const char *tmxFile)
{
    CCAssert(tmxFile != NULL &&strlen(tmxFile)>0, "TMXTiledMap: tmx file should not bi NULL");
   
    setContentSize(CCSizeZero);
 
    CCTMXMapInfo *mapInfo =CCTMXMapInfo::formatWithTMXFile(tmxFile);
 
    if (! mapInfo)
    {
        return false;
    }
    CCAssert(mapInfo->getTilesets()->count() != 0, "TMXTiledMap: Map not found.Please check the filename.");
    buildWithMapInfo(mapInfo);
 
    return true;
}


通過看源碼,我們發現有2個重要的函數 CCTMXMapInfo::formatWithTMXFile(tmxFile)和buildWithMapInfo(mapInfo),看名字應該能知道formatWithTMXFile是解析XML文件將其結構化,buildWithMapInfo是將結構化的數據變成CCNode從而顯示在遊戲界面上。

 

接下來,我們查看CCTMXMapInfo是如何組織數據以及如何解析XML文件的,首先我們還是查看CCTMXMapInfo的聲明

 

class CC_DLL CCTMXMapInfo :public CCObject, public CCSAXDelegator
{   
public:   
    /// map orientation
    CC_SYNTHESIZE(int,    m_nOrientation, Orientation);
    /// map width & height
    CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tMapSize, MapSize);
    /// tiles width & height
    CC_SYNTHESIZE_PASS_BY_REF(CCSize,m_tTileSize, TileSize);
    /// Layers
    CC_PROPERTY(CCArray*, m_pLayers, Layers);
    /// tilesets
    CC_PROPERTY(CCArray*, m_pTilesets,Tilesets);
    /// ObjectGroups
    CC_PROPERTY(CCArray*, m_pObjectGroups,ObjectGroups);
    /// parent element
    CC_SYNTHESIZE(int, m_nParentElement,ParentElement);
    /// parent GID
    CC_SYNTHESIZE(unsigned int, m_uParentGID,ParentGID);
    /// layer attribs
    CC_SYNTHESIZE(int, m_nLayerAttribs,LayerAttribs);
    /// is storing characters?
    CC_SYNTHESIZE(bool, m_bStoringCharacters,StoringCharacters);
    /// properties
    CC_PROPERTY(CCDictionary*, m_pProperties,Properties);
public:
    /**
     * @js ctor
     * @lua NA
     */
    CCTMXMapInfo();
    /**
     * @js NA
     * @lua NA
     */
    virtual ~CCTMXMapInfo();
    /** creates a TMX Format with a tmx file */
    static CCTMXMapInfo *formatWithTMXFile(const char *tmxFile);
    /** creates a TMX Format with an XML stringand a TMX resource path */
    static CCTMXMapInfo * formatWithXML(constchar* tmxString, const char* resourcePath);
    /** initializes a TMX format with a  tmx file
     * @lua NA
     */
    bool initWithTMXFile(const char *tmxFile);
    /** initializes a TMX format with an XMLstring and a TMX resource path
     * @lua NA
     */
    bool initWithXML(const char* tmxString,const char* resourcePath);
    /** initializes parsing of an XML file,either a tmx (Map) file or tsx (Tileset) file */
    bool parseXMLFile(const char *xmlFilename);
    /* initializes parsing of an XML string,either a tmx (Map) string or tsx (Tileset) string */
    bool parseXMLString(const char *xmlString);
 
    CCDictionary* getTileProperties();
    void setTileProperties(CCDictionary*tileProperties);
 
    /** implement pure virtual methods ofCCSAXDelegator
     * @js NA
     */
    void startElement(void *ctx, const char*name, const char **atts);
    /**
     * @js NA
     */
    void endElement(void *ctx, const char*name);
    /**
     * @js NA
     */
    void textHandler(void *ctx, const char *ch,int len);
   
    inline const char* getCurrentString(){return m_sCurrentString.c_str(); }
    inline void setCurrentString(const char*currentString){ m_sCurrentString = currentString; }
    inline const char* getTMXFileName(){ returnm_sTMXFileName.c_str(); }
    inline void setTMXFileName(const char*fileName){ m_sTMXFileName = fileName; }
private:
    void internalInit(const char* tmxFileName,const char* resourcePath);
protected:
    //! tmx filename
    std::string m_sTMXFileName;
    // tmx resource path
    std::string m_sResources;
    //! current string
    std::string m_sCurrentString;
    //! tile properties
    CCDictionary* m_pTileProperties;
    unsigned int m_uCurrentFirstGID;
};

通過查看聲明 我們發現CCTMXMapInfo主要包括以下幾個數據:

  1. TMXLayerInfo的數組,用來存放地圖信息
  2. TMXTilesetInfo的數組,用來存放塊信息
  3. ObjectGroups的數組

另外,CCTMXMapInfo中有幾個重要的函數:

 bool initWithXML(const char* tmxString, constchar* resourcePath);
    /** initializes parsing of an XML file,either a tmx (Map) file or tsx (Tileset) file */
    bool parseXMLFile(const char *xmlFilename);
    /* initializes parsing of an XML string, eithera tmx (Map) string or tsx (Tileset) string */
    bool parseXMLString(const char *xmlString);


 

這3個函數是libxml2中所需要解析xml的代理函數,分別表示標籤的開始,標籤的結束,標籤的內容

 

解析xml的標籤就在這幾個函數中。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章