COCOS2DX Project--Action game memory Optimization--spine structure Analysis 1

Source: Internet
Author: User

Spine Data Organization

Spatlas: This is the structure that was extracted from the. Atlas file, which contains the textures

struct Spatlas {    spatlaspage* pages;    Spatlasregion* regions;     void* rendererobject;     int ref ;};

Don't take care of it, look at its data organization, what is Spatlaspage,spatlasregion,rendererobject? Look down.

struct spatlaspage {    const spatlas* Atlas;     Const Char* name;    Spatlasformat format;    Spatlasfilter Minfilter, Magfilter;    Spatlaswrap Uwrap, vwrap;     void* rendererobject;     int width, height;    Spatlaspage* next;};


Spatalspage look at its members, can be very simple to see, wow, this is the texture, give you a look at the truth, haha

void Const Char* path) {    cctexture2d* texture = Cctexturecache::sharedtexturecache (),AddImage ( path);    Texture, retain ();    Self->rendererobject = texture;    Self->width = texture->getpixelswide ();    Self->height = texture->Getpixelshigh ();}


See no, the truth here, there is the width of the texture, high. The wonderful thing is that he still has (spatlaspage* next), it is obvious that this is a linked list structure Ah, if you have an action file with multiple textures, then this pointer will come in handy. Other temporarily ignored, see above (_spatlaspage_createtexture) found no, here the texture to hold. So the texture will not release oh.

structspatlasregion {Const Char*name; intx, y, width, height; floatu, V, U2, V2; intOffsetX, OffsetY; intOriginalWidth, OriginalHeight; intindex; int/*BOOL*/rotate; int/*BOOL*/Flip; int*splits; int*pads; Spatlaspage*page; Spatlasregion*next;};

A look at the goods, is the texture of the elements contained, a piece, a piece of descriptive information.

See this texture, each of its pieces should correspond to one (spatlasregion) it.

This is the information contained in the. Atlas File Ah! This is how the picture is divided into how many parts. What is the coordinates of each part.

What's that renderobject thing? I don't know, keep looking.

Spatlas* Spatlas_create (Const Char* Begin,intLengthConst Char* Dir,void*rendererobject) {Spatlas*Self ; intcount; Const Char* End = begin +length; intDirlength =strlen (dir); intNeedsslash = dirlength >0&& Dir[dirlength-1] !='/'&& Dir[dirlength-1] !='\\'; Spatlaspage*page =0; Spatlaspage*lastpage =0; Spatlasregion*lastregion =0;    STR str; STR tuple[4]; Self=NEW (Spatlas); Self->rendererobject =Rendererobject; ................  

See no, highlight in the last sentence. When you create this object, you need to pass in a thing void* rendererobject, and then this thing is given to itself.
spatlas* self ;

.......
Self = NEW (spatlas);
Self->rendererobject = rendererobject;

And then on the outside my call is

Atlas = Spatlas_createfromfile (atlasfile, 0); I actually passed a 0, haha, no use, temporarily to here.

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

. The JSON file is the organization information of the above spatlasregion tree structure, which is the skeleton information, and each action information, each region in each key frame data. Ha ha.

First look at the code that was created

Skeletonrenderer::skeletonrenderer (Const Char* Skeletondatafile,Const Char* Atlasfile,floatScale )    {Initialize (); if(1) {ctimemgr::getinst ()->time_begin ("Spatlas_createfromfile"); Atlas= Spatlas_createfromfile (Atlasfile,0); Ccassert (Atlas,"Error reading Atlas file."); Ctimemgr::getinst ()-Time_end (); Ctimemgr::getinst ()->time_begin ("spskeletonjson_create"); Spskeletonjson* JSON =Spskeletonjson_create (Atlas); Ctimemgr::getinst ()-Time_end (); JSON->scale =Scale ; Ctimemgr::getinst ()->time_begin ("Spskeletonjson_readskeletondatafile"); Spskeletondata* Skeletondata =Spskeletonjson_readskeletondatafile (JSON, skeletondatafile); Ctimemgr::getinst ()-Time_end (); Ccassert (Skeletondata, JSON->error? Json->error:"Error reading skeleton data file.");        Spskeletonjson_dispose (JSON); Setskeletondata (Skeletondata,true); }    Else{Atlas= Spatlas_createfromfile (Atlasfile,0); Ccassert (Atlas,"Error reading Atlas file."); Spskeletonjson* JSON =Spskeletonjson_create (Atlas); JSON->scale =Scale ; Spskeletondata* Skeletondata =Spskeletonjson_readskeletondatafile (JSON, skeletondatafile); Ccassert (Skeletondata, JSON->error? Json->error:"Error reading skeleton data file.");        Spskeletonjson_dispose (JSON); Setskeletondata (Skeletondata,true); }    }

if (1) the content, I changed, else inside the content is original. The purpose of my change is to use 1 times to load multiple times, and of course there is a reference count for the destruction of other places. Let's be honest about what else is.

Atlas = Spatlas_createfromfile (Atlasfile,0); Let's call it a moment. Create a texture atlas
 spskeletonjson * JSON = Spskeletonjson_create (Atlas); Based on the Texture Atlas, create a JSON object that is like a funnel, texture parsing json file prepared, After the use of the destruction.
       json->scale = scale;
       spskeletondata* skeletondata = Spskeletonjson_ Readskeletondatafile (JSON, skeletondatafile); parse skeleton file
       & Nbsp;spskeletonjson_dispose (JSON); Funnel-destructor.
       setskeletondata (skeletondata, true< Span style= "color: #000000;" >); (The above code in the constructor of the Skeletonrenderer class, so, see clearly no, parse out the spskeletondata was skeletonrenderer hold it)

spskeletondata* Spskeletonjson_readskeletondatafile (spskeletonjson* self,Const Char*path) {    intlength; Spskeletondata*Skeletondata; Const Char* JSON = _sputil_readfile (path, &length); if(!JSON) {_spskeletonjson_seterror (self,0,"Unable to read skeleton file:", path); return 0; } skeletondata=Spskeletonjson_readskeletondata (self, JSON);    Free (JSON); returnSkeletondata;}

What's here, the highlight in this sentence Skeletondata = spskeletonjson_readskeletondata (self, JSON);
Anyway, we know that the JSON file is resolved in this area!

Come on. We just need to know that the JSON file is for the data that contains the skeleton organization and animation motion. The atlas file is ok if it is related to textures. What we want to do is let it not hold the texture, want to use the time to load, do not want to use the time, the texture is removed on it!

If the load texture needs 40ms, the texture occupies memory 512*512*4 = 1M, if we change it according to this target. In fact, it took 40ms to swap 1 m of memory.

Speed and memory need to be balanced. Haha, we are going to start minimally invasive surgery.

COCOS2DX Project--Action game memory Optimization--spine structure Analysis 1

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.