Skip to content

Commit d23e95d

Browse files
committed
Merge branch 'v3.2' into develop
Conflicts: cocos2d-tests-ios.xcodeproj/project.pbxproj cocos2d/CCRenderer.m
2 parents 43e2785 + 4ba9c90 commit d23e95d

File tree

5 files changed

+89
-7
lines changed

5 files changed

+89
-7
lines changed

UnitTests/CCRendererTests.m

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,22 @@ -(void)testBlendModeDefaults
8282
}
8383
}
8484

85+
-(void)testRenderStateCacheFlush
86+
{
87+
__weak CCRenderState *renderState = nil;
88+
89+
@autoreleasepool {
90+
CCBlendMode *mode = [CCBlendMode alphaMode];
91+
CCShader *shader = [CCShader positionColorShader];
92+
CCTexture *texture = [CCTexture textureWithFile:@"Images/grossini_dance_01.png"];
93+
94+
renderState = [CCRenderState renderStateWithBlendMode:mode shader:shader mainTexture:texture];
95+
XCTAssertNotNil(renderState, @"Render state was not created.");
96+
}
97+
98+
[CCRENDERSTATE_CACHE flush];
99+
100+
XCTAssertNil(renderState, @"Render state was not released.");
101+
}
102+
85103
@end

UnitTests/CCTextureTests.m

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#import <XCTest/XCTest.h>
2+
#import "cocos2d.h"
3+
4+
#import "CCTextureCache.h"
5+
6+
@interface CCTextureTests : XCTestCase
7+
@end
8+
9+
@implementation CCTextureTests
10+
11+
-(void)testTextureCache
12+
{
13+
__weak CCTexture *textures[4];
14+
__weak CCRenderState *renderStates[3];
15+
16+
@autoreleasepool {
17+
// Load some cached textures
18+
for(int i=0; i<4; i++){
19+
NSString *name = [NSString stringWithFormat:@"Images/grossini_dance_0%d.png", i + 1];
20+
textures[i] = [CCTexture textureWithFile:name];
21+
}
22+
23+
// Make sure the textures were loaded.
24+
for(int i=0; i<4; i++){
25+
XCTAssertNotNil(textures[i], @"Texture %d not loaded.", i);
26+
}
27+
28+
// Create render states for the textures.
29+
CCBlendMode *blend = [CCBlendMode premultipliedAlphaMode];
30+
CCShader *shader = [CCShader positionColorShader];
31+
32+
// A cached render state..
33+
renderStates[0] = [CCRenderState renderStateWithBlendMode:blend shader:shader mainTexture:textures[0]];
34+
// An uncached, immutable render state.
35+
NSDictionary *uniforms1 = @{@"SomeTexture": textures[1]};
36+
renderStates[1] = [CCRenderState renderStateWithBlendMode:blend shader:shader shaderUniforms:uniforms1 copyUniforms:YES];
37+
// An uncached, mutable render state.
38+
NSMutableDictionary *uniforms2 = [NSMutableDictionary dictionaryWithObject:textures[2] forKey:@"SomeTexture"];
39+
renderStates[2] = [CCRenderState renderStateWithBlendMode:blend shader:shader shaderUniforms:uniforms2 copyUniforms:NO];
40+
// Leave textures[3] unused.
41+
42+
// Make sure the render states were loaded
43+
for(int i=0; i<3; i++){
44+
XCTAssertNotNil(renderStates[i], @"Render state %d not loaded.", i);
45+
}
46+
}
47+
48+
// Flush the texture cache
49+
[[CCTextureCache sharedTextureCache] removeUnusedTextures];
50+
51+
// Make sure the textures were unloaded.
52+
for(int i=0; i<4; i++){
53+
XCTAssertNil(textures[i], @"Texture %d still loaded.", i);
54+
}
55+
}
56+
57+
@end

cocos2d-tests-ios.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@
7171
D28A2E1F1954F7E900ADC03D /* OpenGLES.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D28A2E1E1954F7E900ADC03D /* OpenGLES.framework */; };
7272
D2B4894C1917EE6700C3443A /* CCEffectsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D2B4894B1917EE6700C3443A /* CCEffectsTest.m */; };
7373
D2D59897198747DE00BDAF25 /* Music in Resources */ = {isa = PBXBuildFile; fileRef = D2D59896198747DE00BDAF25 /* Music */; };
74+
D32FDE8619B645CA0078CC16 /* CCTextureTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D32FDE8519B645CA0078CC16 /* CCTextureTests.m */; };
7475
D3395F1A187F83E600F22C74 /* CCMemoryTests.m in Sources */ = {isa = PBXBuildFile; fileRef = D3395F19187F83E600F22C74 /* CCMemoryTests.m */; settings = {COMPILER_FLAGS = "-fno-objc-arc"; }; };
7576
D340E10E185660BE006E605C /* CCPhysicsTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D340E10C185660BE006E605C /* CCPhysicsTest.m */; };
7677
D340E10F185660BE006E605C /* TilemapTest.m in Sources */ = {isa = PBXBuildFile; fileRef = D340E10D185660BE006E605C /* TilemapTest.m */; };
@@ -273,6 +274,7 @@
273274
D2D0E6951950B16300E9103F /* libGLESv3.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libGLESv3.so; path = "../../../Library/Application Support/Developer/Shared/Xcode/Platforms/Android.platform/Developer/SDKs/ApportableSDK1.0.sdk/usr/lib/armeabi/libGLESv3.so"; sourceTree = "<group>"; };
274275
D2D0E6991950B16800E9103F /* libEGL.so */ = {isa = PBXFileReference; lastKnownFileType = "compiled.mach-o.dylib"; name = libEGL.so; path = "../../../Library/Application Support/Developer/Shared/Xcode/Platforms/Android.platform/Developer/SDKs/ApportableSDK1.0.sdk/usr/lib/armeabi/libEGL.so"; sourceTree = "<group>"; };
275276
D2D59896198747DE00BDAF25 /* Music */ = {isa = PBXFileReference; lastKnownFileType = folder; name = Music; path = Resources/Music; sourceTree = SOURCE_ROOT; };
277+
D32FDE8519B645CA0078CC16 /* CCTextureTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCTextureTests.m; sourceTree = "<group>"; };
276278
D3395F19187F83E600F22C74 /* CCMemoryTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCMemoryTests.m; sourceTree = "<group>"; };
277279
D340E10C185660BE006E605C /* CCPhysicsTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCPhysicsTest.m; path = "cocos2d-ui-tests/tests/CCPhysicsTest.m"; sourceTree = SOURCE_ROOT; };
278280
D340E10D185660BE006E605C /* TilemapTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TilemapTest.m; path = "cocos2d-ui-tests/tests/TilemapTest.m"; sourceTree = SOURCE_ROOT; };
@@ -399,6 +401,7 @@
399401
D3395F19187F83E600F22C74 /* CCMemoryTests.m */,
400402
D34CD3C418ABF4AA00D8E537 /* CCRendererTests.m */,
401403
92324E2918EB635500D78D3F /* CCReaderTest.m */,
404+
D32FDE8519B645CA0078CC16 /* CCTextureTests.m */,
402405
755569EC1856361100ED1B0F /* Supporting Files */,
403406
);
404407
path = UnitTests;
@@ -922,6 +925,7 @@
922925
buildActionMask = 2147483647;
923926
files = (
924927
92324E2A18EB635500D78D3F /* CCReaderTest.m in Sources */,
928+
D32FDE8619B645CA0078CC16 /* CCTextureTests.m in Sources */,
925929
75556A161856370A00ED1B0F /* CCFileUtilTests.m in Sources */,
926930
92FE241118F5F06F00647961 /* CCAnimationTest.m in Sources */,
927931
75556A171856370A00ED1B0F /* CCNodeTests.m in Sources */,

cocos2d-ui/CCBReader/CCAnimationManager.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ - (void)runActionsForNode:(CCNode*)node sequenceProperty:(CCBSequenceProperty*)s
360360
if(numKeyframes<1) return;
361361

362362
// Action Sequence Builder
363-
NSMutableArray* actions = [NSMutableArray array];
363+
NSMutableArray* actions = [NSMutableArray array];
364364
int endFrame = startFrame+1;
365365

366366
if(endFrame==numKeyframes || endFrame<0)

cocos2d/CCRenderer.m

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,18 @@ +(void)initialize
273273
-(instancetype)initWithBlendMode:(CCBlendMode *)blendMode shader:(CCShader *)shader shaderUniforms:(NSDictionary *)shaderUniforms
274274
{
275275
// Allocate a new instance of the correct class instead of self. (This method was already deprecated).
276-
return [[CCRenderStateClass alloc] initWithBlendMode:blendMode shader:shader shaderUniforms:shaderUniforms copyUniforms:NO];
276+
return [[CCRenderStateClass alloc] initWithBlendMode:blendMode shader:shader mainTexture:nil shaderUniforms:shaderUniforms copyUniforms:NO];
277277
}
278278

279-
-(instancetype)initWithBlendMode:(CCBlendMode *)blendMode shader:(CCShader *)shader shaderUniforms:(NSDictionary *)shaderUniforms copyUniforms:(BOOL)copyUniforms
279+
-(instancetype)initWithBlendMode:(CCBlendMode *)blendMode shader:(CCShader *)shader mainTexture:(CCTexture *)mainTexture shaderUniforms:(NSDictionary *)shaderUniforms copyUniforms:(BOOL)copyUniforms
280280
{
281281
if((self = [super init])){
282282
_blendMode = blendMode;
283283
_shader = shader;
284284
_shaderUniforms = (copyUniforms ? [shaderUniforms copy] : shaderUniforms);
285285

286+
_mainTexture = mainTexture;
287+
286288
// The renderstate as a whole is immutable if the uniforms are copied.
287289
_immutable = copyUniforms;
288290
}
@@ -297,23 +299,24 @@ +(instancetype)renderStateWithBlendMode:(CCBlendMode *)blendMode shader:(CCShade
297299
mainTexture = [CCTexture none];
298300
}
299301

300-
CCRenderState *renderState = [[CCRenderStateClass alloc] initWithBlendMode:blendMode shader:shader shaderUniforms:@{CCShaderUniformMainTexture: mainTexture} copyUniforms:YES];
301-
renderState->_mainTexture = mainTexture;
302+
// The uniforms are immutable, but don't mark them to be copied.
303+
// This forces the cache to create a unique public object for the shared/key values.
304+
CCRenderState *renderState = [[self alloc] initWithBlendMode:blendMode shader:shader mainTexture:mainTexture shaderUniforms:@{CCShaderUniformMainTexture: mainTexture} copyUniforms:NO];
302305

303306
return [CCRENDERSTATE_CACHE objectForKey:renderState];
304307
}
305308

306309
+(instancetype)renderStateWithBlendMode:(CCBlendMode *)blendMode shader:(CCShader *)shader shaderUniforms:(NSDictionary *)shaderUniforms copyUniforms:(BOOL)copyUniforms
307310
{
308-
return [[CCRenderStateClass alloc] initWithBlendMode:blendMode shader:shader shaderUniforms:shaderUniforms copyUniforms:copyUniforms];
311+
return [[CCRenderStateClass alloc] initWithBlendMode:blendMode shader:shader mainTexture:nil shaderUniforms:shaderUniforms copyUniforms:copyUniforms];
309312
}
310313

311314
-(id)copyWithZone:(NSZone *)zone
312315
{
313316
if(_immutable){
314317
return self;
315318
} else {
316-
return [[CCRenderStateClass allocWithZone:zone] initWithBlendMode:_blendMode shader:_shader shaderUniforms:_shaderUniforms copyUniforms:YES];
319+
return [[CCRenderStateClass allocWithZone:zone] initWithBlendMode:_blendMode shader:_shader mainTexture:_mainTexture shaderUniforms:_shaderUniforms copyUniforms:YES];
317320
}
318321
}
319322

0 commit comments

Comments
 (0)