0

I have a bunch of sprites that share the same texture atlas, like this

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"Atlas.plist"];
CCSprite *sprite1 = [CCSprite spriteWithSpriteFrameName:@"Star1.png"];
CCSprite *sprite2 = [CCSprite spriteWithSpriteFrameName:@"Star2.png"];
[self addChild:sprite1];
[self addChild:sprite2];

And a bunch of bitmap font labels that use the same FNT font, like this

CCLabelBMFont *label1 = [CCLabelBMFont labelWithString:@"label1" fntFile:@"font.fnt"];
CCLabelBMFont * label2 = [CCLabelBMFont labelWithString:@"label2” fntFile:@"font.fnt"];
[self addChild: label1];
[self addChild: label2];

All of them are in the same layer

I wonder if packing the font.png file (used by font.fnt) into the texture atlas, say with a tool like texture packer, would cause cocos2d to use the same texture atlas to draw both labels and sprites

rraallvv
  • 2,555
  • 5
  • 24
  • 58
  • interesting thought, but why haven't you just tried this out? – LearnCocos2D Aug 05 '14 at 19:45
  • @LearnCocos2D I tried it but it didn't work out of the box, maybe someone has a workaround or a patch. If I find a solution I will post it here. – rraallvv Aug 05 '14 at 20:36
  • check the cclabelbmfont init code, see where it gets the texture from – LearnCocos2D Aug 05 '14 at 22:54
  • @LearnCocos2D It retrieves the texture from the texture cache passing the image filename, `[[CCTextureCache sharedTextureCache] addImage:_configuration.atlasName]` where `_configuration.atlasName` is the file specified by the .fnt file, I think I can workaround this by merging the two image files Atlas.png and font.png into one big image by packing font.png into the Atlas.png in the right location and specifying that file in the .fnt file – rraallvv Aug 06 '14 at 02:19

1 Answers1

0

So, this is how I did:

I packed the font.png file into the Atlas.pvr file using texture packer

Then with Xcode I looked for the frame origin of the font.png image specified in Atlas.plist

enter image description here

Then using the following bash script I added the frame origin to the coordinates origin of each glyph in the .fnt file

#!/bin/bash

infile='testfont.fnt'
framex=258
framey=62

output=''
while ifs=$'\n' read -r line || [[ -n "$line" ]] ; do
if [[ $line =~ ^(.*x\ ?=\ ?)([0-9]*)(.*y\ ?=\ ?)([0-9]*)(.*)$ ]] ; then
    output=$output${BASH_REMATCH[1]}$((${BASH_REMATCH[2]}+framex))${BASH_REMATCH[3]}$((${BASH_REMATCH[4]}+framey))${BASH_REMATCH[5]}$'\n'
  else
    output=$output$line$'\n'
  fi
done < "$infile"

echo "$output"

Then in the output .fnt file I changed the image filename from testfont.png to Atlas.pvr

rraallvv
  • 2,555
  • 5
  • 24
  • 58