Skip to content

Commit 13ea509

Browse files
committed
Fix for infinite loop and performance of BMFonts
1 parent 012ce6f commit 13ea509

File tree

2 files changed

+30
-52
lines changed

2 files changed

+30
-52
lines changed

cocos2d-ui-tests/tests/CCBMFontTest.m

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
@",.?!;:'\"",
1616
@"()[]{}<>\\|/\n",
1717
@"@#$%^&*+-=_",
18+
@" ab c de\n fg hi j k",
1819
};
1920
static const int TEST_STRING_COUNT = sizeof(TEST_STRINGS)/sizeof(*TEST_STRINGS);
2021

cocos2d/CCLabelBMFont.m

Lines changed: 29 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -424,39 +424,6 @@ -(void) parseKerningEntry:(NSString*) line
424424
#pragma mark -
425425
#pragma mark CCLabelBMFont
426426

427-
static NSString* stringFromUInt32(UInt32 i)
428-
{
429-
static NSString* strings[] = { @"0", @"1", @"2", @"3", @"4", @"5", @"6", @"7", @"8", @"9",
430-
@"10", @"11", @"12", @"13", @"14", @"15", @"16", @"17", @"18", @"19",
431-
@"20", @"21", @"22", @"23", @"24", @"25", @"26", @"27", @"28", @"29",
432-
@"30", @"31", @"32", @"33", @"34", @"35", @"36", @"37", @"38", @"39",
433-
@"40", @"41", @"42", @"43", @"44", @"45", @"46", @"47", @"48", @"49",
434-
@"50", @"51", @"52", @"53", @"54", @"55", @"56", @"57", @"58", @"59",
435-
@"60", @"61", @"62", @"63", @"64", @"65", @"66", @"67", @"68", @"69",
436-
@"70", @"71", @"72", @"73", @"74", @"75", @"76", @"77", @"78", @"79",
437-
@"80", @"81", @"82", @"83", @"84", @"85", @"86", @"87", @"88", @"89",
438-
@"90", @"91", @"92", @"93", @"94", @"95", @"96", @"97", @"98", @"99",
439-
@"100", @"101", @"102", @"103", @"104", @"105", @"106", @"107", @"108", @"109",
440-
@"110", @"111", @"112", @"113", @"114", @"115", @"116", @"117", @"118", @"119",
441-
@"120", @"121", @"122", @"123", @"124", @"125", @"126", @"127", @"128", @"129",
442-
@"130", @"131", @"132", @"133", @"134", @"135", @"136", @"137", @"138", @"139",
443-
@"140", @"141", @"142", @"143", @"144", @"145", @"146", @"147", @"148", @"149",
444-
@"150", @"151", @"152", @"153", @"154", @"155", @"156", @"157", @"158", @"159",
445-
@"160", @"161", @"162", @"163", @"164", @"165", @"166", @"167", @"168", @"169",
446-
@"170", @"171", @"172", @"173", @"174", @"175", @"176", @"177", @"178", @"179",
447-
@"180", @"181", @"182", @"183", @"184", @"185", @"186", @"187", @"188", @"189",
448-
@"190", @"191", @"192", @"193", @"194", @"195", @"196", @"197", @"198", @"199",
449-
@"200", @"201", @"202", @"203", @"204", @"205", @"206", @"207", @"208", @"209",
450-
@"210", @"211", @"212", @"213", @"214", @"215", @"216", @"217", @"218", @"219",
451-
@"220", @"221", @"222", @"223", @"224", @"225", @"226", @"227", @"228", @"229",
452-
@"230", @"231", @"232", @"233", @"234", @"235", @"236", @"237", @"238", @"239",
453-
@"240", @"241", @"242", @"243", @"244", @"245", @"246", @"247", @"248", @"249",
454-
@"250", @"251", @"252", @"253", @"254", @"255" };
455-
456-
457-
return ((i & 0xFFFFFF00) ? @(i).stringValue : strings[i]);
458-
}
459-
460427
@interface CCLabelBMFont ()
461428

462429
-(int) kerningAmountForFirst:(unichar)first second:(unichar)second;
@@ -468,7 +435,11 @@ -(void) setString:(NSString*) newString updateLabel:(BOOL)update;
468435
#pragma mark -
469436
#pragma mark CCLabelBMFont
470437

471-
@implementation CCLabelBMFont
438+
@implementation CCLabelBMFont {
439+
// Replacement for the old CCNode.tag property which was
440+
// used heavily in the original code.
441+
NSMutableArray *_childForTag;
442+
}
472443

473444
@synthesize alignment = _alignment;
474445

@@ -559,13 +530,31 @@ -(id) initWithString:(NSString*)theString fntFile:(NSString*)fntFile width:(floa
559530

560531
_reusedChar = [[CCSprite alloc] initWithTexture:_textureAtlas.texture rect:CGRectMake(0, 0, 0, 0) rotated:NO];
561532
[_reusedChar setBatchNode:self];
533+
_childForTag = [NSMutableArray array];
562534

563535
[self setString:theString updateLabel:YES];
564536
}
565537

566538
return self;
567539
}
568540

541+
-(CCSprite *)childForTag:(NSUInteger)tag
542+
{
543+
if(tag < _childForTag.count){
544+
id child = _childForTag[tag];
545+
return (child == [NSNull null] ? nil : child);
546+
} else {
547+
return nil;
548+
}
549+
}
550+
551+
-(void)setTag:(NSUInteger)tag forChild:(CCSprite *)child
552+
{
553+
// Insert NSNull to fill holes if necessary.
554+
while(_childForTag.count < tag) [_childForTag addObject:[NSNull null]];
555+
[_childForTag addObject:child];
556+
}
557+
569558

570559
#pragma mark LabelBMFont - Alignment
571560

@@ -585,9 +574,7 @@ - (void)updateLabel
585574
for (int j = 0; j < [_children count]; j++) {
586575
CCSprite *characterSprite;
587576
int justSkipped = 0;
588-
int idx = j+skip+justSkipped;
589-
NSString* idxStr = stringFromUInt32(idx);
590-
while(!(characterSprite = (CCSprite *)[self getChildByName:idxStr recursively:NO]))
577+
while(!(characterSprite = [self childForTag:j+skip+justSkipped]))
591578
justSkipped++;
592579
skip += justSkipped;
593580

@@ -682,13 +669,7 @@ - (void)updateLabel
682669
continue;
683670

684671
//Find position of last character on the line
685-
CCSprite *lastChar;
686-
for(CCSprite* child in [self children]) {
687-
if([child atlasIndex]==index) {
688-
lastChar = child;
689-
break;
690-
}
691-
}
672+
CCSprite *lastChar = [self childForTag:index];
692673

693674
lineWidth = lastChar.position.x + lastChar.contentSize.width/2;
694675

@@ -711,8 +692,7 @@ - (void)updateLabel
711692
index = i + j + lineNumber;
712693
if (index < 0)
713694
continue;
714-
NSString* indexStr1 = stringFromUInt32((UInt32)index);
715-
CCSprite *characterSprite = (CCSprite *)[self getChildByName:indexStr1 recursively:NO];
695+
CCSprite *characterSprite = [self childForTag:index];
716696
characterSprite.position = ccpAdd(characterSprite.position, ccp(shift, 0));
717697
}
718698
}
@@ -807,11 +787,8 @@ -(void) createFontChars
807787
rect.origin.x += _imageOffset.x;
808788
rect.origin.y += _imageOffset.y;
809789

810-
CCSprite *fontChar;
811-
812790
BOOL hasSprite = YES;
813-
NSString* iStr = stringFromUInt32((UInt32)i);
814-
fontChar = (CCSprite*) [self getChildByName:iStr recursively:NO];
791+
CCSprite *fontChar = [self childForTag:i];
815792
if( fontChar )
816793
{
817794
// Reusing previous Sprite
@@ -830,8 +807,8 @@ -(void) createFontChars
830807
hasSprite = NO;
831808
} else {
832809
fontChar = [[CCSprite alloc] initWithTexture:_textureAtlas.texture rect:rect];
833-
NSString* iStr1 = stringFromUInt32((UInt32)i);
834-
[self addChild:fontChar z:i name:iStr1];
810+
[self addChild:fontChar z:i];
811+
[self setTag:i forChild:fontChar];
835812
}
836813

837814
// Apply label properties

0 commit comments

Comments
 (0)