"Game engine" in-depth analysis of "textures" in Cocos2d-x 2.0 (iv)

Source: Internet
Author: User
transferred from: http://www.dapps.net/dev/gamedev/game-dev-engine-cocos2d-x-2-0-grain-4.html " game engine" in-depth analysis of "textures" in Cocos2d-x 2.0 (iv)

Category: Uncategorized, Game development Tags: cocos2d-x, game engine, textures

Three Cctexturecache:

Open CCTextureCache.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef __cctexture_cache_h__
#define __cctexture_cache_h__
Derived from Ccobject
#include "Cocoa/ccobject.h"
Need to use a dictionary
#include "Cocoa/ccdictionary.h"
#include "Textures/cctexture2d.h"
#include <string>

Here we use one of the ccimage and STL containers list
#if Cc_enable_cache_texture_data
#include "platform/ccimage.h"
#include <list>
#endif

COCOS2D namespaces
Ns_cc_begin
Use the thread lock
Class CCLock;
Use Ccimage to process pictures
Class Ccimage;

Texture Manager
Class Cc_dll Cctexturecache:public Ccobject
{
Protected:
The Dictionary object pointer.
Ccdictionary * m_ptextures;
Thread critical section. Used to lock dictionary access, seemingly not available. It's blocked here ~
pthread_mutex_t *m_pdictlock;


Private:
Sets the callback function for multi-line loads when loading pictures.
void Addimageasynccallback (float dt);

Public:
constructor function
Cctexturecache ();
Destructors
Virtual ~cctexturecache ();
Gets the current class description
const char * description (void);
Get a snapshot of the current dictionary (copy)
Ccdictionary * Snapshottextures ();

Returns an instance pointer to a unique texture manager
Static Cctexturecache * Sharedtexturecache ();

Destroying an instance pointer for a unique texture manager
static void Purgesharedtexturecache ();

Load a picture to generate the texture, the file name as the dictionary query corresponding to the keyword. Returns the generated texture pointer, supporting formats such as Png,bmp,tiff,jpeg,pvr,gif.
CCTEXTURE2D * AddImage (const char * fileimage);

This function can support multi-threaded loading of pictures, the call will create a thread to asynchronously load, after the successful loading by the main thread call the set callback function, of course, the created texture will be passed as a parameter. PNG and JPG support
void Addimageasync (const char *path, Ccobject *target, Sel_callfunco selector);


Load a picture to generate the texture, specifying the parameter key (which is actually required to be the relative path string of the picture) as the dictionary query corresponding to the keyword.
Cctexture2d * Adduiimage (ccimage *image, const char *key);

Find the corresponding texture from the dictionary by querying the keyword (which actually requires a relative path string for the picture).
CCTEXTURE2D * Textureforkey (const char * key);

Empty the dictionary and release all textures.
void Removealltextures ();

Clears textures that are not used externally. How to know not to use it. Because the reference counters are used in cocos2d-x to manage the usage of various resources, textures are no exception. When the resource class is constructed, the texture's counter value is 0, but when created by Cctexturecache, a 1 action is added to the texture's resource counter to inform the texture that you are now occupied by me. If the texture is used externally, it should call its resource counter again to do add 1 operations, quit using the minus 1 action to notify its "I don't need You now." So it is only necessary to traverse the texture below the counter value of 1, which is not released by the externally used texture.
void Removeunusedtextures ();

Remove a texture
void Removetexture (CCTEXTURE2D * texture);

The dictionary query keyword finds the corresponding texture and removes it.
void Removetextureforkey (const char *texturekeyname);

Prints out the currently managed texture information, including the memory now used by the texture and the total texture memory.
void Dumpcachedtextureinfo ();

#ifdef CC_SUPPORT_PVRTC
If you turn on a PVR-enabled compression format, here is a function for loading the PVR compressed file to generate textures.
Parameter 1:PVR Compressed file name
Parameter 2: Compression quality parameters, can only be set to 2 or 4, 4:2 high quality, but low compression ratio. 2 is the opposite.
Parameter 3: Whether there is an alpha channel. This will be based on whether there is an alpha channel to generate the corresponding default texture format.
Parameter 4: The picture must be a power square of 2, so just fill in the width, which is the image size.
CCTEXTURE2D * Addpvrtcimage (const char * fileimage, int BPP, bool hasalpha, int width);
#endif//CC_SUPPORT_PVRTC

Load normal PVR picture files to generate textures.
CCTEXTURE2D * Addpvrimage (const char * filename);

If the Cc_enable_cache_texture_data macro is defined as available (that is, a value of 1), calling this function will preload all the textures into memory to generate the corresponding texture.
static void Reloadalltextures ();
} ;

If Cc_enable_cache_texture_data is defined, a new class is defined here
#if Cc_enable_cache_texture_data

The newly defined class name is volatiletexture, which means changeable textures. This represents the manager of textures generated by a variety of data sources.
Class Volatiletexture
{
This declares an enumeration that represents several types of data sources that correspond to changeable textures.
typedef enum {
Kinvalid = 0,//invalid status of no data loaded
Kimagefile,//Picture file
Kimagedata,//image data in memory
Kstring,//String
KImage,//Picture object (Ccimage)
}cccachedimagetype;

Public:
Structure
Volatiletexture (cctexture2d *t);
Destruction
~volatiletexture ();
Static function: Creates a changeable texture and puts its pointer into the container by creating a texture and related information from the picture file.
static void Addimagetexture (Cctexture2d *tt, const char * imagefilename, Ccimage:: Eimageformat format);
Static function: Creates a changeable texture and puts its pointer into the container by generating a texture and related information from a string.
static void Addstringtexture (Cctexture2d *tt, const char * text, const ccsize & dimensions, cctextalignment Alig Nment,
Ccverticaltextalignment valignment, const char *fontname, float fontSize);
Textures and related information generated by the image data generate a changeable texture and place its pointer into the container.
static void Adddatatexture (Cctexture2d *tt, void * data, Cctexture2dpixelformat PixelFormat, const Ccsize & Conte Ntsize);
Creates a changeable texture and puts its pointer into the container, using the texture and related information generated by the picture object.
static void Addccimage (Cctexture2d *tt, Ccimage *image);
Removes the corresponding changeable texture from the container with the texture pointer parameter
static void Removetexture (Cctexture2d *t);
Re-load all textures
static void Reloadalltextures ();

Public:
Static variable texture pointer container, used to store all the changeable texture object pointers.
Static std:: List <volatiletexture * > textures;
are all re-loading in progress
static bool isreloading;

Private:
Find the corresponding changeable texture object pointer from the container by the texture pointer parameter
Static Volatiletexture * Findvolotiletexture (cctexture2d *tt);

Protected:
Texture pointer corresponding to the current changeable texture
Cctexture2d *texture;
The corresponding Picture object
Ccimage *uiimage;
Data Source Type
Cccachedimagetype M_ecashedimagetype;
Texture data pointer
void *m_ptexturedata;
The actual size of the texture
Ccsize m_texturesize;
Pixel format for Textures
Cctexture2dpixelformat M_pixelformat;
The corresponding picture name
std:: string m_strfilename;
The pixel format of the picture
Ccimage:: Eimageformat m_fmtimage;
The size of the picture
Ccsize m_size;
How to align horizontal text
Cctextalignment m_alignment;
How vertical text is aligned
Ccverticaltextalignment m_valignment;
Font Name
std:: string m_strfontname;
Text
std:: string m_strtext;
Font size
float m_ffontsize;
} ;

#endif


Ns_cc_end

#endif//__cctexture_cache_h__

Then the CPP file:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21st
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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.