The ccimage is drawn through cctexture2d (OpenGL ES) to a certain surface through texture.
(The methods mentioned in this article are partially adjusted in cocos2d2.0. Please check the source code when applying them)
1. First, let's take a look at several classes related to the genie:
(1) cctexture2d
It can be seen as a texture, it is an important parameter of cocos2d-x rendering graphics, used for texture, because the cocos2d-x uses OpenGL ES to draw 2D graphics, its size is 2 n. It is generally obtained through the following methods:
Cctexture2d * cache = cctexturecache: sharedtexturecache ()-> addimage ("hero.png ");
(2) ccsprite
This is the genie class. It is a subclass of ccnode and can be directly added to cclayer or ccscene. It encapsulates cctexture2d (texture) and can initialize the genie object in the following ways.
Static ccsprite * spritewithtexture (cctexture2dPtexture); // cctexture2d indicates the image contained by the Genie. The range is the entire image.
Static ccspriteSpritewithtexture (cctexture2dPtexture, const ccrect & rect); // ccrect indicates the specified range of the image, that is, from the specified rectangular area of the image
Static ccspriteSpritewithspriteframe (ccspriteframePspriteframe); // ccspriteframe indicates a certain frame of the genie, which encapsulates cctexture2d and ccrect, and ccrect indicates the specified range of the image, that is, cropping from the specified rectangular area of the image
Static ccspriteSpritewithspriteframename (const charPszspriteframename );
Static ccspriteSpritewithfile (const charPszfilename );
Static ccspriteSpritewithfile (const charPszfilename, const ccrect & rect );
Static ccspriteSpritewithbatchnode (ccspritebatchnodeBatchnode, const ccrect & rect );
The following are two common methods to initialize the genie:
CcspriteSprite = ccsprite: spritewithfile ("hero.png ");
/** Or **/
Cctexture2d * cache = cctexturecache: sharedtexturecache ()-> addimage ("hero.png ");
Ccsprite * sprite = ccsprite: spritewithtexture (cache );
(3) cctexturecache
It is equivalent to a cctexture2d container, which is a memory pool used to cache cctexture2d objects. It has a dictionary ccmutabledictionary m_ptextures, whose key is the image name and whose value is cctexture2d. When you call its addimage function to add an image, the system first checks whether the image exists in the memory based on the image name. The following is the source code of addimage:
[CPP]
Cctexture2d * cctexturecache: addimage (const char * path)
{
Cctexture2d * texture = NULL;
STD: String pathkey = path;
Ccfileutils: ccremovehdsuffixfromfile (pathkey );
Pathkey = ccfileutils: fullpathfromrelativepath (pathkey. c_str ());
Texture = m_ptextures-> objectforkey (pathkey );
STD: String fullpath = pathkey; // (ccfileutils: fullpathfromrelativepath (PATH ));
If (! Texture)
{
/**....*/
}
Return texture;
}
If you need to load multiple images at a time, you can first load the images to cctexturecache, so that the image usage speed will be very fast.
(4) ccspritebatchnode
It is a batch rendering Sprite, mainly used to improve the rendering efficiency of the Genie. The more genie you need to draw, the more obvious the effect. Because the cocos2d-x uses OpenGL ES to draw images, OpenGL ES draws each sprite to execute the Open-draw-close process. While ccspritebatchnode puts multiple genie on one texture, and draws the texture directly and does not need to draw child nodes separately. In this way, OpenGL ES becomes: open-draw ()... -Draw ()-close (), which saves multiple open-close times. Ccspritebatchnode encapsulates a cctextureatlas (texture gallery, which encapsulates a cctexture2d) and a ccarray (child node used to store ccspritebatchnode: A single genie ). Note: because only one open-close operation is performed during painting, all subnodes of the ccspritebatchnode object must use the same texture (same image) as the ccspritebatchnode object ):
When addchild is used, the system checks whether the texture name of the subnode is the same as that of ccspritebatchnode. If it is different, an error occurs. Source Code:
[CPP] child (ccnode * child, int zorder, int tag)
{
/**...*/
// Check ccsprite is using the same texture ID
Ccassert (psprite-> gettexture ()-> getname () = m_pobtextureatlas-> gettexture ()-> getname (),"");
/**...*/
}
The following describes how to use ccspritebatchnode.CodeExample:
[CPP]
Ccspritebatchnode * batchnode1 = ccspritebatchnode: batchnodewithfile ("images/grossiniDanceAtlas.png ", 50 );
Addchild (batchnode1, 0, ktagspritebatchnode );
Ccspritebatchnode * batchnode = (ccspritebatchnode *) getchildbytag (ktagspritebatchnode );
Int idx = ccrandom_0_1 () * 1400/100;
Int x = (idx % 5) * 85;
Int y = (idx/5) * 121;
Ccsprite * sprite = ccsprite: spritewithtexture (batchnode-> gettexture (), ccrectmake (X, Y, 85,121 ));
Batchnode-> addchild (sprite );
Sprite-> setposition (CCP (P. X, p. y ));
(5) ccspriteframecache
It is the memory pool used to manage ccspriteframe. It is similar to the cctexturecache function, but unlike cctexturecache, if there is no frame to be searched in the memory pool, it will prompt that it cannot be found, instead of loading images locally. It encapsulates a dictionary: ccdictionary * m_pspriteframes, and the key is the frame name. Ccspriteframecache is generally used to process plist files (this file specifies the location and size of each independent genie in this large image). This file corresponds to a large image containing multiple genie, plist files can be created using texturepacker.
The following is a sample code for using ccspriteframecache:
[CPP]
Ccspriteframecache * cache = ccspriteframecache: sharedspriteframecache ();
Cache-> addspriteframeswithfile ("animations/grossini. plist", "animations/grossini.png ");
MPsprite1 = ccsprite: spritewithspriteframename ("grossiniDance01. PNG ");
MPsprite1-> setposition (CCP (S. width/2-80, S. Height/2 ));
Addspriteframeswithfile ("animations/grossini. plist "," animations/grossini.png ") can be changed to addspriteframeswithfile (" animations/grossini. plist ");
- Ccspritebatchnode and ccspriteframecache
Make sure that ccspriteframecache and ccspritebatchnode load the same texture map.
[CPP]
Ccspriteframecache: sharedspriteframecache ()-> addspriteframeswithfile ("animations/ghosts. plist", "animations/ghosts.png ");
Ccspritebatchnode * aparent = ccspritebatchnode: batchnodewithfile ("animations/ghosts.png ");
Addchild (aparent, 0, ktagsprite1 );
Ccsprite * pfather = ccsprite: spritewithspriteframename ("father.gif ");
Pfather-> setposition (CCP (S. width/2, S. Height/2 ));
Aparent-> addchild (pfather, 0, ktagsprite2 );
//////////////////////////////////////// //////////////////////////////
Asynchronous loading of voidcctexturecache
Voidcctexturecache: addimageasync (constchar * path, ccobject * target, sel_callfunco selector)
{
Ccassert (path! = NULL, "texturecache: fileimage must not be null ");
Cctexture2d * texture = NULL;
STD: String pathkey = path;
Ccfileutils: ccremovehdsuffixfromfile (pathkey );
Pathkey = ccfileutils: fullpathfromrelativepath (pathkey. c_str ());
Texture = m_ptextures-> objectforkey (pathkey); // check whether the texture has been loaded Based on the pathkey. If yes, do not load the texture repeatedly.
STD: String fullpath = pathkey;
If (texture! = NULL)
{
If (target & selector)
{
(Target-> * selector) (texture );
}
Return;
}
If (target)
{
Target-> retain ();
}
// Lazy init
Static bool firstrun = true;
If (firstrun)
{
S_pasyncstructqueue = new queue & lt; asyncstruct *> ();
S_pimagequeue = new queue & lt; imageinfo *> ();
Pthread_mutex_init (& amp; s_asyncstructqueuemutex, null );
SEMInit (& SSEM, 0, 0 );
PthreadMutexInit (& s_imageinfomutex, null );
PthreadCreate (& SLoadingthread, null, LoadImage, null); // creates a thread for loading images in the background.
// Create a scheduling queue for texture Conversion Based on Loaded Images
Ccschedtor: sharedscheduler ()-> scheduleselector (schedule_selector (cctexturecache: addimageasynccallback), this, 0, false );
Need_quit = false;
Firstrun = false;
}
// Generate async struct
Asyncstruct * Data = new asyncstruct ();
Data-> filename = fullpath. c_str ();
Data-> Target = target;
Data-> selector = selector;
// Add async struct into queue
PthreadMutexLock (& s_asyncstructqueuemutex );
S_pasyncstructqueue-> push (data); // put the image to be loaded into the queue
PthreadMutexUnlock (& s_asyncstructqueuemutex );
SEMPost (& SSEM );
}
The above code analysis shows the process:
1. Create a thread for background Loading
Put the image to be loaded into the queue
Callback Function setting is used to convert the loaded image into a texture. It is called by cctimer: update.
After processing the texture conversion, the addimageasynccallback function also calls the sel_callfunco selector passed in by addimageasync to implement the processing after the user loads the image texture.
Example:
Cctexturecache: sharedtexturecache ()-> addimageasync ("images/blocks.png", this, callfunco_selector (texturecachetest: loadingcallback ));
The loadingcallback function allows users to process results after asynchronous loading, such as initializing a Sprite and pasting the texture.
Or create an animation.
//////////////////////////////////////// //////////////////////////////
Redbaby
In-depth analysis of Cocos2d-x 2.0 in the "texture" article on cctexture2d and cctexturepvr, cctexturecache API is described, we recommend you read
Http://blog.csdn.net/honghaier/article/details/8068895