Multi-thread loading principle and usage of Cocos2D-x and CCTextureCache

Source: Internet
Author: User

When I read the materials for study, I saw a texture asynchronous loading, so I thought it was quite useful to share the specific principles and usage, later, you can modify and transplant the files based on the browser features. Void CCTextureCache: addImageAsync (const char * path, CCObject * target, SEL_CallFuncO selector) {CCAssert (path! = NULL, "TextureCache: fileimage MUST not be NULL"); CCTexture2D * texture = NULL; // optimized std: string pathKey = path; CCFileUtils: ccRemoveHDSuffixFromFile (pathKey ); pathKey = CCFileUtils: fullPathFromRelativePath (pathKey. c_str (); texture = m_pTextures-& gt; objectForKey (pathKey); // check whether the texture has been loaded Based on the pathkey. If yes, do not reload std :: string fullpath = pathKey; if (texture! = NULL) {if (target & amp; selector) {(target-& gt; * selector) (texture) ;}return ;} if (target) {target-& gt; retain () ;}// lazy initstatic bool firstRun = true; if (firstRun) {s_pAsyncStructQueue = new queue & lt; asyncstruct & gt ;(); s_pImageQueue = new queue & lt; imageinfo & gt; (); pthread_mutex_init (& amp; s_asyncStructQueueMutex, NULL); sem_init (& amp; s_sem, 0, 0 ); pthread_mutex_init (& amp; s_ImageInfoMutex, NULL); pthread_create (& amp; s_loadingThread, NULL, loadImage, NULL); // create a thread for loading images in the background // create a scheduling queue, CCScheduler: sharedschedtor ()-& amp; gt; scheduleSelector (schedule_selector (CCTextureCache: addImageAsyncCallBack), this, 0, false ); need_quit = false; firstRun = false;} // generate AsyncStruct * data = new AsyncStruct (); data-& amp; gt; filename = fullpath. c_str (); data-& amp; gt; target = target; dat A-& amp; gt; selector = selector; // Add to queue asynchronous structure pthread_mutex_lock (& amp; s_asyncStructQueueMutex); s_pAsyncStructQueue-& amp; gt; push (data ); // put the image to be loaded into the queue pthread_mutex_unlock (& amp; s_asyncStructQueueMutex); sem_post (& amp; s_sem);} the process can be seen from the code analysis above: 1. create a thread for loading in the background. 2. 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. 4. 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 ()-& amp; gt; 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.

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.