Skip to content

Commit 4ffe24f

Browse files
author
minggo
committed
Merge branch 'iss374' of https://github.com/natural-law/cocos2d-x into natural-law-iss374
2 parents f1363af + 1af4aa1 commit 4ffe24f

File tree

6 files changed

+197
-144
lines changed

6 files changed

+197
-144
lines changed

HelloWorld/android/jni/helloworld/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thi
3030
}
3131
else
3232
{
33-
cocos2d::CCTexture2D::reloadAllTextures();
33+
cocos2d::CCTextureCache::reloadAllTextures();
3434
cocos2d::CCDirector::sharedDirector()->setGLDefaultValues();
3535
}
3636
}

cocos2dx/include/CCTexture2D.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,11 +212,6 @@ class CC_DLL CCTexture2D : public CCObject
212212
*/
213213
static CCTexture2DPixelFormat defaultAlphaPixelFormat();
214214

215-
/** Reload all textures
216-
It's only useful when the value of CC_ENABLE_CACHE_TEXTTURE_DATA is 1
217-
*/
218-
static void reloadAllTextures();
219-
220215
private:
221216
bool initPremultipliedATextureWithImage(CCImage * image, unsigned int pixelsWide, unsigned int pixelsHigh);
222217

cocos2dx/include/CCTextureCache.h

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ THE SOFTWARE.
3030
#include "CCObject.h"
3131
#include "CCMutableDictionary.h"
3232

33+
#if CC_ENABLE_CACHE_TEXTTURE_DATA
34+
#include "CCImage.h"
35+
#include <list>
36+
#endif
37+
3338
namespace cocos2d {
3439
class CCTexture2D;
3540
class CCAsyncObject;
@@ -146,7 +151,48 @@ class CC_DLL CCTextureCache : public CCObject
146151
*/
147152
CCTexture2D* addPVRTCImage(const char* fileimage);
148153
#endif
154+
155+
/** Reload all textures
156+
It's only useful when the value of CC_ENABLE_CACHE_TEXTTURE_DATA is 1
157+
*/
158+
static void reloadAllTextures();
159+
};
160+
161+
#if CC_ENABLE_CACHE_TEXTTURE_DATA
162+
163+
class VolatileTexture
164+
{
165+
public:
166+
VolatileTexture(CCTexture2D *t);
167+
~VolatileTexture();
168+
169+
static void addImageTexture(CCTexture2D *tt, const char* imageFileName, CCImage::EImageFormat format);
170+
static void addStringTexture(CCTexture2D *tt, const char* text, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize);
171+
172+
static void removeTexture(CCTexture2D *t);
173+
static void reloadAllTextures();
174+
175+
public:
176+
static std::list<VolatileTexture*> textures;
177+
static bool isReloading;
178+
179+
protected:
180+
CCTexture2D *texture;
181+
182+
bool m_bIsString;
183+
184+
std::string m_strFileName;
185+
CCImage::EImageFormat m_FmtImage;
186+
187+
CCSize m_size;
188+
CCTextAlignment m_alignment;
189+
std::string m_strFontName;
190+
std::string m_strText;
191+
float m_fFontSize;
149192
};
193+
194+
#endif
195+
150196
}//namespace cocos2d
151197

152198
#endif //__CCTEXTURE_CACHE_H__

cocos2dx/textures/CCTexture2D.cpp

Lines changed: 6 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ THE SOFTWARE.
3434

3535
#include "ccConfig.h"
3636
#include "ccMacros.h"
37-
#include "CCTexture2D.h"
3837
#include "CCConfiguration.h"
3938
#include "platform/platform.h"
4039
#include "CCImage.h"
@@ -47,7 +46,7 @@ THE SOFTWARE.
4746
#endif
4847

4948
#if CC_ENABLE_CACHE_TEXTTURE_DATA
50-
#include <list>
49+
#include "CCTextureCache.h"
5150
#endif
5251

5352
namespace cocos2d {
@@ -58,122 +57,6 @@ namespace cocos2d {
5857

5958
//CLASS IMPLEMENTATIONS:
6059

61-
#if CC_ENABLE_CACHE_TEXTTURE_DATA
62-
class VolatileTexture
63-
{
64-
protected:
65-
CCTexture2D *texture;
66-
unsigned char *data;
67-
CCTexture2DPixelFormat pixelFormat;
68-
unsigned int pixelsWide;
69-
unsigned int pixelsHigh;
70-
CCSize contentSize;
71-
72-
public:
73-
74-
static std::list<VolatileTexture*> textures;
75-
static bool isReloading;
76-
77-
VolatileTexture(CCTexture2D *t) : texture(t), data(0)
78-
{
79-
textures.push_back(this);
80-
}
81-
82-
~VolatileTexture()
83-
{
84-
if (data)
85-
delete [] data;
86-
textures.remove(this);
87-
}
88-
89-
static void addTextureWithData(CCTexture2D *tt,
90-
const void *d,
91-
CCTexture2DPixelFormat f,
92-
unsigned int w,
93-
unsigned int h,
94-
CCSize s)
95-
{
96-
if (isReloading)
97-
return;
98-
99-
VolatileTexture *vt = 0;
100-
std::list<VolatileTexture *>::iterator i = textures.begin();
101-
while( i != textures.end() )
102-
{
103-
VolatileTexture *v = *i++;
104-
if (v->texture == tt) {
105-
vt = v;
106-
break;
107-
}
108-
}
109-
110-
if (!vt)
111-
vt = new VolatileTexture(tt);
112-
113-
vt->pixelFormat = f;
114-
vt->pixelsWide = w;
115-
vt->pixelsHigh = h;
116-
vt->contentSize = s;
117-
118-
//CCLOGINFO("added volatile %d", textures.size());
119-
120-
if (vt->data) {
121-
delete [] vt->data;
122-
vt->data = 0;
123-
}
124-
125-
switch(f) {
126-
case kCCTexture2DPixelFormat_RGBA8888:
127-
case kCCTexture2DPixelFormat_RGBA4444:
128-
case kCCTexture2DPixelFormat_RGB5A1:
129-
case kCCTexture2DPixelFormat_RGB565:
130-
case kCCTexture2DPixelFormat_A8:
131-
vt->data = new unsigned char[w * h * 4];
132-
memcpy(vt->data, d, w * h * 4);
133-
break;
134-
case kCCTexture2DPixelFormat_RGB888:
135-
vt->data = new unsigned char[w * h * 3];
136-
memcpy(vt->data, d, w * h * 3);
137-
break;
138-
}
139-
}
140-
141-
static void removeTexture(CCTexture2D *t) {
142-
143-
std::list<VolatileTexture *>::iterator i = textures.begin();
144-
while( i != textures.end() )
145-
{
146-
VolatileTexture *vt = *i++;
147-
if (vt->texture == t) {
148-
delete vt;
149-
break;
150-
}
151-
}
152-
}
153-
154-
static void reloadAllTextures()
155-
{
156-
isReloading = true;
157-
158-
CCLOG("reload all texture");
159-
std::list<VolatileTexture *>::iterator i = textures.begin();
160-
161-
while( i != textures.end() )
162-
{
163-
VolatileTexture *vt = *i++;
164-
if (vt->data) {
165-
vt->texture->initWithData((const void *)vt->data, vt->pixelFormat, vt->pixelsWide, vt->pixelsHigh, vt->contentSize);
166-
}
167-
}
168-
169-
isReloading = false;
170-
}
171-
};
172-
173-
std::list<VolatileTexture*> VolatileTexture::textures;
174-
bool VolatileTexture::isReloading = false;
175-
#endif // CC_ENABLE_CACHE_TEXTTURE_DATA
176-
17760
// If the image has alpha, you can create RGBA8 (32-bit) or RGBA4 (16-bit) or RGB5A1 (16-bit)
17861
// Default is: RGBA8888 (32-bit textures)
17962
static CCTexture2DPixelFormat g_defaultAlphaPixelFormat = kCCTexture2DPixelFormat_Default;
@@ -273,12 +156,6 @@ bool CCTexture2D::getHasPremultipliedAlpha()
273156

274157
bool CCTexture2D::initWithData(const void *data, CCTexture2DPixelFormat pixelFormat, unsigned int pixelsWide, unsigned int pixelsHigh, CCSize contentSize)
275158
{
276-
277-
#if CC_ENABLE_CACHE_TEXTTURE_DATA
278-
// cache the texture data
279-
VolatileTexture::addTextureWithData(this, data, pixelFormat, pixelsWide, pixelsHigh, contentSize);
280-
#endif
281-
282159
glGenTextures(1, &m_uName);
283160
glBindTexture(GL_TEXTURE_2D, m_uName);
284161

@@ -559,6 +436,11 @@ bool CCTexture2D::initWithString(const char *text, const char *fontName, float f
559436
}
560437
bool CCTexture2D::initWithString(const char *text, CCSize dimensions, CCTextAlignment alignment, const char *fontName, float fontSize)
561438
{
439+
#if CC_ENABLE_CACHE_TEXTTURE_DATA
440+
// cache the texture data
441+
VolatileTexture::addStringTexture(this, text, dimensions, alignment, fontName, fontSize);
442+
#endif
443+
562444
CCImage image;
563445
CCImage::ETextAlign eAlign = (CCTextAlignmentCenter == alignment) ? CCImage::kAlignCenter
564446
: (CCTextAlignmentLeft == alignment) ? CCImage::kAlignLeft : CCImage::kAlignRight;
@@ -742,11 +624,4 @@ CCTexture2DPixelFormat CCTexture2D::defaultAlphaPixelFormat()
742624
return g_defaultAlphaPixelFormat;
743625
}
744626

745-
void CCTexture2D::reloadAllTextures()
746-
{
747-
#if CC_ENABLE_CACHE_TEXTTURE_DATA
748-
VolatileTexture::reloadAllTextures();
749-
#endif
750-
}
751-
752627
}//namespace cocos2d

0 commit comments

Comments
 (0)