Skip to content

Commit ee51c21

Browse files
author
Thayer J Andrews
committed
Merge remote-tracking branch 'upstream/develop' into develop
2 parents 016c90d + 6caf6b3 commit ee51c21

File tree

8 files changed

+197
-37
lines changed

8 files changed

+197
-37
lines changed

cocos2d-tests-ios.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0E28FE1A197FCE4500F78989 /* CCCacheTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 0E28FE19197FCE4500F78989 /* CCCacheTest.m */; };
1011
75556A04185636F100ED1B0F /* XCTest.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 75556A03185636F100ED1B0F /* XCTest.framework */; };
1112
75556A05185636F100ED1B0F /* Foundation.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7E2605717E7D278007067F0 /* Foundation.framework */; };
1213
75556A06185636F100ED1B0F /* UIKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = B7E2605517E7D278007067F0 /* UIKit.framework */; };
@@ -107,6 +108,7 @@
107108
/* End PBXContainerItemProxy section */
108109

109110
/* Begin PBXFileReference section */
111+
0E28FE19197FCE4500F78989 /* CCCacheTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CCCacheTest.m; path = "cocos2d-ui-tests/tests/CCCacheTest.m"; sourceTree = SOURCE_ROOT; };
110112
755569E31856361100ED1B0F /* CCFileUtilTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCFileUtilTests.m; sourceTree = "<group>"; };
111113
755569E41856361100ED1B0F /* CCNodeTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCNodeTests.m; sourceTree = "<group>"; };
112114
755569E51856361100ED1B0F /* CCPhysicsTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CCPhysicsTests.m; sourceTree = "<group>"; };
@@ -354,6 +356,7 @@
354356
B71B087F17EA5B500082EBC0 /* Tests */ = {
355357
isa = PBXGroup;
356358
children = (
359+
0E28FE19197FCE4500F78989 /* CCCacheTest.m */,
357360
D2B4894B1917EE6700C3443A /* CCEffectsTest.m */,
358361
D3D6CF5E18BD5F0500A51531 /* CCRendererTest.m */,
359362
D3870C5F18B440150033D885 /* SpritePerformanceTest.m */,
@@ -656,6 +659,7 @@
656659
B7EE697918186D5200B983FE /* CCTextFieldTest.m in Sources */,
657660
D29698121926DF7500BBB097 /* CCEffectPongTest.m in Sources */,
658661
B7E2620017E7D321007067F0 /* AppDelegate.m in Sources */,
662+
0E28FE1A197FCE4500F78989 /* CCCacheTest.m in Sources */,
659663
B77060AB1832E3310043CC67 /* CCTextureCacheTest.m in Sources */,
660664
A664A4EF18A3D9B8006184B8 /* PositioningTest.m in Sources */,
661665
B7E2621C17E7D370007067F0 /* main.m in Sources */,

cocos2d-ui-tests/tests/CCCacheTest.m

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#import "TestBase.h"
2+
#import "CCCache.h"
3+
4+
// -----------------------------------------------------------------
5+
// Simple demo cache, holding an allocated array
6+
// -----------------------------------------------------------------
7+
8+
@interface DemoCache : CCCache @end
9+
10+
@implementation DemoCache
11+
12+
- (id)createSharedDataForKey:(id<NSCopying>)key
13+
{
14+
unsigned char* rawData = malloc(255);
15+
CCLOG(@"Data created for key: %@", key);
16+
return([NSValue valueWithPointer:rawData]);
17+
}
18+
19+
- (id)createPublicObjectForSharedData:(id)data
20+
{
21+
return([NSValue valueWithNonretainedObject:data]);
22+
}
23+
24+
- (void)disposeOfSharedData:(id)data
25+
{
26+
free([((NSValue *)data) pointerValue]);
27+
}
28+
29+
- (void)logCache
30+
{
31+
for (unsigned char letter = 'A'; letter < 'X'; letter ++)
32+
{
33+
NSString *key = [NSString stringWithFormat:@"object%c", letter];
34+
if ([self keyExists:key])
35+
{
36+
NSValue *value = [self rawObjectForKey:key];
37+
if (value) CCLOG(@"%@", key); else CCLOG(@"%@ (unused)", key);
38+
}
39+
}
40+
}
41+
42+
@end
43+
44+
// -----------------------------------------------------------------
45+
// Cache test
46+
// -----------------------------------------------------------------
47+
48+
@interface CCCacheTest : TestBase @end
49+
50+
// -----------------------------------------------------------------
51+
52+
@implementation CCCacheTest
53+
{
54+
DemoCache *_cache;
55+
int _testStep;
56+
NSValue *_A;
57+
NSValue *_B;
58+
}
59+
60+
- (NSArray*) testConstructors
61+
{
62+
return [NSArray arrayWithObjects:
63+
@"cacheTest",
64+
nil];
65+
}
66+
67+
- (void)cacheTest
68+
{
69+
self.subTitle = @"Cache Test";
70+
71+
CCButton *button = [CCButton buttonWithTitle:@"[ Step test]" fontName:@"Arial" fontSize:18];
72+
button.positionType = CCPositionTypeNormalized;
73+
button.position = ccp(0.5, 0.5);
74+
[button setTarget:self selector:@selector(stepClicked:)];
75+
[self.contentNode addChild:button];
76+
77+
_testStep = 0;
78+
79+
_cache = [DemoCache cache];
80+
81+
[_cache preload:@"objectA"];
82+
[_cache preload:@"objectA"]; // same object as above
83+
[_cache preload:@"objectB"];
84+
[_cache preload:@"objectC"];
85+
}
86+
87+
- (void)stepClicked:(id)sender
88+
{
89+
CCLOG(@"---------------------------------------------------");
90+
switch (_testStep) {
91+
case 0:
92+
CCLOG(@"-- All cached objects should be unused");
93+
break;
94+
95+
case 1:
96+
CCLOG(@"-- objectA in use");
97+
_A = [_cache objectForKey:@"objectA"];
98+
break;
99+
100+
case 2:
101+
CCLOG(@"-- objectB in use");
102+
_B = [_cache objectForKey:@"objectB"];
103+
break;
104+
105+
case 3:
106+
CCLOG(@"-- Flush, and thus remove objectC");
107+
[_cache flush];
108+
break;
109+
110+
case 4:
111+
CCLOG(@"-- objectA not in use anymore");
112+
_A = nil;
113+
break;
114+
115+
case 5:
116+
CCLOG(@"-- Flush, and thus only leaving objectB");
117+
[_cache flush];
118+
break;
119+
120+
case 6:
121+
CCLOG(@"-- New objectA should be created");
122+
_A = [_cache objectForKey:@"objectA"];
123+
break;
124+
125+
case 7:
126+
CCLOG(@"-- No new objectB should be created");
127+
[_cache flush];
128+
_B = [_cache objectForKey:@"objectB"];
129+
break;
130+
131+
default:
132+
CCLOG(@"End of test!");
133+
_testStep = -1;
134+
break;
135+
}
136+
[_cache logCache];
137+
_testStep ++;
138+
}
139+
140+
141+
// -----------------------------------------------------------------
142+
143+
@end

cocos2d/CCCache.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
- (instancetype)init;
4646

4747
- (void)preload:(id<NSCopying>)key;
48+
- (BOOL)keyExists:(id<NSCopying>)key;
4849

4950
- (id)rawObjectForKey:(id<NSCopying>)key;
5051
- (id)objectForKey:(id<NSCopying>)key;

cocos2d/CCCache.m

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,28 +84,28 @@ - (CCCacheEntry *)entryForKey:(id<NSCopying>)key
8484
CCCacheEntry *entry = [_cacheList objectForKey:key];
8585

8686
if (entry == nil)
87-
{
88-
// Create the cached entry with the shared data.
87+
{
88+
// Create the cached entry with the shared data.
8989
entry = [[CCCacheEntry alloc] init];
90-
entry.sharedData = [self createSharedDataForKey:key];
91-
90+
entry.sharedData = [self createSharedDataForKey:key];
91+
9292
[_cacheList setObject:entry forKey:key];
9393
}
94-
95-
return entry;
94+
95+
return entry;
9696
}
9797

9898
- (id)objectForKey:(id<NSCopying>)key
9999
{
100100
CCCacheEntry *entry = [self entryForKey:key];
101101

102-
id object = entry.publicObject;
103-
if (object == nil)
104-
{
102+
id object = entry.publicObject;
103+
if (object == nil)
104+
{
105105
// Create the public object from the shared data.
106-
object = entry.publicObject = [self createPublicObjectForSharedData:entry.sharedData];
107-
}
108-
106+
object = entry.publicObject = [self createPublicObjectForSharedData:entry.sharedData];
107+
}
108+
109109
return object;
110110
}
111111

@@ -115,6 +115,11 @@ - (void)makeAlias:(id<NSCopying>)alias forKey:(id<NSCopying>)key
115115
[_cacheList setObject:entry forKey:alias];
116116
}
117117

118+
- (BOOL)keyExists:(id<NSCopying>)key
119+
{
120+
return([_cacheList objectForKey:key] != nil);
121+
}
122+
118123
//------------------------------------------------------------------------------
119124

120125
- (void)flush

cocos2d/CCNode.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -393,28 +393,6 @@ A common user pattern in building a Cocos2d game is to subclass CCNode, add it t
393393
/** The z order of the node relative to its "siblings": children of the same parent. */
394394
@property(nonatomic,assign) NSInteger zOrder;
395395

396-
397-
/// -----------------------------------------------------------------------
398-
/// @name Hit tests
399-
/// -----------------------------------------------------------------------
400-
401-
/**
402-
* Check if a touch is inside the node.
403-
* To allow for custom detection, override this method.
404-
*
405-
* @param pos World position.
406-
*
407-
* @return Returns true, if the position is inside the node.
408-
*/
409-
- (BOOL)hitTestWithWorldPos:(CGPoint)pos;
410-
411-
/**
412-
* Expands ( or contracts ) the hit area of the node.
413-
* The expansion is calculated as a margin around the sprite, in points.
414-
*/
415-
@property (nonatomic, assign) float hitAreaExpansion;
416-
417-
418396
/// -----------------------------------------------------------------------
419397
/// @name Scene Management
420398
/// -----------------------------------------------------------------------

cocos2d/CCNode.m

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ - (CGPoint)convertToWindowSpace:(CGPoint)nodePoint
16181618
- (BOOL)hitTestWithWorldPos:(CGPoint)pos
16191619
{
16201620
pos = [self convertToNodeSpace:pos];
1621-
CGPoint offset = ccp(-_hitAreaExpansion, -_hitAreaExpansion);
1621+
CGPoint offset = ccp(-self.hitAreaExpansion, -self.hitAreaExpansion);
16221622
CGSize size = CGSizeMake(self.contentSizeInPoints.width - offset.x, self.contentSizeInPoints.height - offset.y);
16231623
if ((pos.y < offset.y) || (pos.y > size.height) || (pos.x < offset.x) || (pos.x > size.width)) return(NO);
16241624

@@ -1627,7 +1627,6 @@ - (BOOL)hitTestWithWorldPos:(CGPoint)pos
16271627

16281628
// -----------------------------------------------------------------
16291629

1630-
16311630
#pragma mark - CCColor methods
16321631

16331632
@synthesize cascadeColorEnabled=_cascadeColorEnabled;

cocos2d/CCResponder.h

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
@interface CCResponder : NSObject
4242

43+
/// -----------------------------------------------------------------------
44+
/// @name Basic responder control
45+
/// -----------------------------------------------------------------------
4346

4447
/** Enables user interaction on a node. */
4548
@property ( nonatomic, assign, getter = isUserInteractionEnabled ) BOOL userInteractionEnabled;
@@ -61,6 +64,12 @@
6164
*/
6265
@property (nonatomic, assign, getter = isExclusiveTouch) BOOL exclusiveTouch;
6366

67+
/**
68+
* Expands ( or contracts ) the hit area of the node.
69+
* The expansion is calculated as a margin around the sprite, in points.
70+
*/
71+
@property (nonatomic, assign) float hitAreaExpansion;
72+
6473
/// -----------------------------------------------------------------------
6574
/// @name Initializing a CCResponder Object
6675
/// -----------------------------------------------------------------------
@@ -72,8 +81,21 @@
7281
*/
7382
- (id)init;
7483

75-
#if ( TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR )
84+
/// -----------------------------------------------------------------------
85+
/// @name Hit tests
86+
/// -----------------------------------------------------------------------
7687

88+
/**
89+
* Check if a touch is inside the node.
90+
* To allow for custom detection, override this method.
91+
*
92+
* @param pos World position.
93+
*
94+
* @return Returns true, if the position is inside the node.
95+
*/
96+
- (BOOL)hitTestWithWorldPos:(CGPoint)pos;
97+
98+
#if ( TARGET_OS_IPHONE || TARGET_IPHONE_SIMULATOR )
7799

78100
#pragma mark -
79101
#pragma mark Touch Handling Methods

cocos2d/CCResponder.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ - (void)setUserInteractionEnabled:(BOOL)userInteractionEnabled
6464
[[[CCDirector sharedDirector] responderManager] markAsDirty];
6565
}
6666

67+
// -----------------------------------------------------------------
68+
// override for touch and mouse functionality
69+
70+
- (BOOL)hitTestWithWorldPos:(CGPoint)pos
71+
{
72+
return(NO);
73+
}
74+
6775
// -----------------------------------------------------------------
6876
#pragma mark - iOS
6977
// -----------------------------------------------------------------

0 commit comments

Comments
 (0)