Cocos2d-x source code analysis (1) -- map module (1)

Source: Internet
Author: User

Cocos loads the tmx file generated by tiled to generate a game map. This article mainly analyzes the source code of the cocos load map module.

 


The map loading module consists of the preceding classes.

The external portal is the CCTMXTiledMap class. When using this class, the programmer can parse the tmx file to generate a map without having to know its underlying classes.

First, we will analyze how the CCTMXTiledMap class calls other classes for parsing. The declaration of this class is as follows:

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; };


By viewing the function implementation, we find that the function

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


Call a function

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


And create a CCTMXTiledMap object to return to the user.

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;}


By looking at the source code, we found that there are two important functions: formatmxmapinfo: formatWithTMXFile (tmxFile) and buildWithMapInfo (mapInfo). The name should be aware that formatWithTMXFile is structured by parsing XML files, buildWithMapInfo converts structured data into CCNode, which is displayed on the game interface.

 

Next, let's check how CCTMXMapInfo organizes data and parses XML files. First, let's check the declaration of 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;};

By viewing the Declaration, we found that CCTMXMapInfo mainly includes the following data:

  1. An array of TMXLayerInfo used to store map information.
  2. An array of TMXTilesetInfo to store block information.
  3. ObjectGroups Array

In addition, CCTMXMapInfo has several important functions:

 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);


 

These three functions are the agent functions for xml parsing in libxml2, indicating the start of the tag, the end of the tag, and the content of the tag.

 

The tags for parsing xml are in these functions.

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.