"Go" "Topsy cocos2d-x 23" multithreading and synchronization 03-picture asynchronous loading

Source: Internet
Author: User
Tags addchild

original works, reproduced please indicate : http://blog.csdn.net/jackystudio/article/details/15334159


Cocos2d-x and Android,windows are the same, if the main thread to deal with some time-consuming operation, then the main thread will appear blocking phenomenon, performance in the interface is stuck, not responding and so on. To avoid this, we need to open up working threads in the background to process the data, and then use messaging or other forms to notify the main thread of UI changes. The most common situation is the loading before the game enters.


1. Asynchronous loading of images

In the first introduction of multithreading and synchronization to the use of the Pthread library, it is said that because Ccautoreleasepool is not thread-safe, it is not possible to introduce COCOS2D-X-related APIs in worker threads (not all APIs are not available). But Cocos2d-x obviously took this issue into account, so it helped us encapsulate an API, avoiding the embarrassment of manually introducing the Pthread library.

[CPP]View Plaincopy
    1. void Cctexturecache::addimageasync (const char *path, ccobject *target, Sel_callfunco selector)

Where path is the location of the picture, selector is the callback function when loading is complete. Handy, if you need to load a lot of pictures, do callback processing for each, then update the UI in update.

asynchronous loading of 2.plist

However, due to memory reasons, most of the pieces will be synthesized and packaged and brought into the plist. How to do the asynchronous loading of the picture at this time? At this time, we need to further explore the source code of Addimageasync.


2.1. What is the time-consuming?

The first thing to understand is what the time-consuming action is, and the asynchronous load only makes sense if the time-consuming work is really caught and thrown onto the worker thread. We know that the image in memory is in the form of texture, and the loading of the picture, in layman's terms, is the generation of textures, which is the reason for time-consuming. What did the Cctexurecache AddImage (synchronous loading) and ADDIMAGEAYSNC (asynchronous loading) do separately?

(1) AddImage

It can be seen that AddImage uses synchronization to generate textures, which is a time-consuming load operation in the main thread.

[CPP]View Plaincopy
  1. ... cocos2d-x maintains a global texture that determines whether a texture already exists
  2. if (! Texture)
  3. {
  4. Do
  5. {
  6. //... Determine image format
  7. Pimage = new Ccimage ();
  8. cc_break_if (NULL = = pimage);
  9. bool BRet = Pimage->initwithimagefile (Fullpath.c_str (), Eimageformat);
  10. Cc_break_if (!bret);
  11. Texture = new cctexture2d (); //Open texture space
  12. if (Texture &&
  13. Texture->initwithimage (pimage)) //Initialize texture with ccimage
  14. {
  15. #if Cc_enable_cache_texture_data
  16. //cache The texture file name
  17. Volatiletexture::addimagetexture (texture, fullpath.c_str (), Eimageformat);
  18. #endif
  19. M_ptextures->setobject (texture, pathkey.c_str ());
  20. Texture->release ();
  21. }
  22. Else
  23. {
  24. Cclog ("cocos2d:couldn ' t create texture for file:%s in Cctexturecache", path);
  25. }
  26. } while (0);
  27. }
  28. //... Releasing resources, returning textures


(2) Addimageasync

The Addimageasync is to load the picture in the worker thread and then transform the texture through the scheduler.

[CPP]View Plaincopy
  1. Create a worker thread to load a picture in the background
  2. Pthread_create (&s_loadingthread, NULL, loadimage, NULL);
  3. Create a schedule queue that is used to transform textures based on loaded pictures
  4. Ccdirector::shareddirector ()->getscheduler ()->scheduleselector (Schedule_selector (CCTextureCache::  Addimageasynccallback), this , 0, false);
  5. void Cctexturecache::addimageasynccallback (float DT)
  6. {
  7. //...  
  8. Cctexture2d *texture = new Cctexture2d (); //Open texture space
  9. #if 0//todo: (cc_target_platform = = Cc_platform_ios)
  10. Texture->initwithimage (Pimage, Kccresolutioniphone);
  11. #else
  12. Texture->initwithimage (Pimage); //Use Ccimage to initialize textures
  13. #endif
  14. #if Cc_enable_cache_texture_data
  15. Volatiletexture::addimagetexture (texture, filename, pimageinfo->imagetype);
  16. #endif
  17. //... will join Autorelease, make the call of the loaded callback function, release the related resources
  18. }


the principle of 2.2.plist loading

The previous use of plist is this:

[CPP]View Plaincopy
    1. void Ccspriteframecache::addspriteframeswithfile (const char *pszplist)//incoming plist files can be

In its implementation, it was discovered that:

[CPP]View Plaincopy
    1. Cctexture2d *ptexture = Cctexturecache::sharedtexturecache ()->addimage (Texturepath.c_str ());

It turns out that addspriteframeswithfile will look for textures first, otherwise it will look for a picture with the same name in the. plist directory, and then call the synchronized AddImage function to generate the texture. This is why only the plist is loaded, and the texture exists.


2.3. Asynchronous load Plist

Know this, then let Addspriteframeswithfile call asynchronous Addimageasync function can, of course, do not need to change the source, because Ccspriteframecache also provides the following plist loading mode:

[CPP]View Plaincopy
    1. void Ccspriteframecache::addspriteframeswithfile (const char *pszplist, cctexture2d *pobtexture)

So we can manually generate the texture asynchronously, and in the callback function and. plist into the addspriteframeswithfile, done! Do you remember the first selector? The resulting texture is passed into this callback function as a parameter, perfect!


2.4. Note

One thing to note is that when using a small image in any plist, the engine does not generate a texture for each small image, but instead uses the texture of the large image, specifying the coordinates and the length and width of the small picture.


3. Example

Where Ui_text.png is the big picture, Raffle_b_friend.png and raffle_b_diamond.png are two small pictures.

[CPP]View Plaincopy
  1. BOOL Ctestlayer::init ()
  2. {
  3. bool bret=false;
  4. Do
  5. {
  6. CC_BREAK_IF (! Cclayer::init ());
  7. create a texture in//addimage or Addimageasync
  8. Cctexturecache::sharedtexturecache ()->addimageasync ("Ui_text.png",this,callfunco_selector (  Ctestlayer::showsprite));
  9. Bret=true;
  10. } while (0);
  11. return bRet;
  12. }
  13. void Ctestlayer::showsprite (ccobject* obj)
  14. {
  15. cctexture2d* texture_ui_text= (cctexture2d*) obj; //The incoming obj is an asynchronously generated texture
  16. Ccspriteframecache::sharedspriteframecache ()->addspriteframeswithfile ("Ui_text.plist", texture_ui_text); Adding Ccspriteframecache through textures and. plist Files
  17. ccsprite* raffle_b_friend=ccsprite::createwithspriteframename ("raffle_b_friend.png"); Use the small picture.
  18. Raffle_b_friend->setposition (CCP (100,100));
  19. This->addchild (raffle_b_friend);
  20. //error, can only get texture of ui_text.png
  21. //cctexture2d* Raffle_b_diamond_texture=cctexturecache::sharedtexturecache ()->textureforkey ("Raffle_b_  Diamond.png ");
  22. //Correct, you can use this to get the sprite frame first, then get the ui_text texture from the sprite frame, and the size, to build the Ccsprite
  23. ccspriteframe* Raffle_b_diamond_spriteframe=ccspriteframecache::sharedspriteframecache ()->spriteFrameByName (  "Raffle_b_diamond.png");
  24. cctexture2d* texture=raffle_b_diamond_spriteframe->gettexture ();
  25. Ccrect Rect=raffle_b_diamond_spriteframe->getrect ();
  26. ccsprite* raffle_b_diamond=ccsprite::createwithtexture (Texture,rect); //If the texture needs to be rotated, setrotation it
  27. Raffle_b_diamond->setrotation (false);
  28. Raffle_b_diamond->setposition (CCP (300,100));
  29. This->addchild (Raffle_b_diamond);
  30. }


4. Effectsuse asynchronous load plist to handle:

5. Source code Download

http://download.csdn.net/detail/jackyvincefu/6533293

"Go" "Topsy cocos2d-x 23" multithreading and synchronization 03-picture asynchronous loading

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.