Cocos2d-x added texture auto recovery mechanism

Source: Internet
Author: User

1. It is not a complete module, so it does not provide completeCode, Only provides ideas and core code. The idea is simple. Maintain a life cycle and the last rendering time for each texture, update the rendering time for each rendering, and traverse the current texture set every certain time (15 seconds) in cctexturecache, if a texture is not rendered after the specified time in the lifecycle, the texture is released. The getname function in cctexture2d is a good way to update the rendering time and release the texture reload entry. Because cctexture2d needs to provide a reload image mechanism, it is best to provide an initwithfile interface and do not forget to record some special OpenGL labels, when you reload the texture, you need to set these flags for the texture, such as the call to gltexparameteri.

2. The MMO solution is not very advanced. Many game engines provide similar or even more advanced features, and it is not necessary for small games (because it is just a little resource in total, fully loaded into the memory does not consume much)

3. The advantage is that a non-rendered texture is automatically released based on the texture used, and the texture is loaded again when needed. This saves memory and saves the trouble of maintaining the texture lifecycle.

Note: the release of ccsprite will not be released to the texture data, and it still exists in the memory. Manually call cctexturecache: removeunusedtextures at the right time to release unused textures. Or call cctexturecache: removetextureforkey to release the specified texture. For an MMO, this is likely to become a cumbersome operation, or even evolve into a trap. (For example, a specified texture is released in a scenario, but it is used in another place, for example, ccspriteframecache ).

Through an automatic "Garbage Collection", externalProgramMembers can ignore the texture lifecycle, and some interfaces will not cause too many problems if they forget to release the texture, because when the texture has been released, the remaining ccsprite does not occupy the memory, and ccspriteframe does.

4. The side effect is that the image loading may cause a transient lag. This requires logical control in the code. For example, some images that wish to stay in the memory are set to no garbage collection.

5. Because of the side effects mentioned in 4, the image packaging process is not as large as possible. Instead, it tends to look at relatively small images, such as 128*128 or 256*256. This ensures the loading speed. You can also use the image to load the image to avoid meaningless duplicate loading operations.

The code is very simple:

[CPP] View plaincopyprint?
  1. VoidCctexturecache: processtexturecache ()
  2. {
  3. If(Timeget ()-m_lastchecktime <2000 ){
  4. Return;
  5. }
  6. M_lastchecktime = timeget ();
  7. Uint32 currenttime = timeget ();
  8. Ccdictelement * pelement = NULL;
  9. Ccdict_foreach (m_ptextures, pelement)
  10. {
  11. Cctexture2d * ptexture = (cctexture2d *) pelement-> GetObject ();
  12. If(Ptexture & ptexture-> isautogc () & currenttime-ptexture-> getlastupdatetime ()> ptexture-> getlivetime ())
  13. {
  14. Ptexture-> releasetexture ();
  15. }
  16. }
  17. }
Void cctexturecache: processtexturecache () {If (timeget ()-m_lastchecktime <2000) {return;} m_lastchecktime = timeget (); uint32 currenttime = timeget (); ccdictelement * pelement = NULL; values (m_ptextures, pelement) {cctexture2d * ptexture = (cctexture2d *) pelement-> GetObject (); If (ptexture & ptexture-> isautogc () & currenttime-ptexture-> getlastupdatetime ()> ptexture-> getlivetime () {ptexture-> releasetexture ();}}}
[CPP] View plaincopyprint?
    1. gluint cctexture2d: getname ()
    2. {
    3. # If cc_enable_texture_auto_gc
    4. If (m_uname =-1) {
    5. reloadtexture ();
    6. }
    7. m_lastupdatetime = timeget ();
    8. # endif
    9. return m_uname;
    10. }

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.