Using TrueType Fonts in CocosSharp

I'm looking to use a free TrueType font that I downloaded from FontSquirrel in my game (Android & Windows platforms) but I'm having a little difficulty understanding the proper way to get it loaded into CocosSharp.

According to the article here (https://github.com/mono/CocosSharp/wiki/Labels), it indicates that CCLabel can use TTF font if included with the application. Also, I've looked at the tests project for CocosSharp, specifically the SystemFontTest class and content folders and tried some tests of my own with CCLabel and my font, but I keep getting the "stock" font for the label, which seems to indicate the font didn't load and I'm doing something wrong. I'm currently testing on Android using the Xamarin Android Player.

My questions are the following:

  • What's the correct way to include a TrueType font with the CocosSharp application (per the Labels article above)?
  • Do I need to have a spritefont that refers to the TrueType font as well? The Labels article seemed to indicate spritefonts (using CCLabelTtf) and TTF (using CCLabel) were separate.
  • Do I need to load the TrueType font specially in my code or is it enough to include it in the fonts directory of my Content folder and CocosSharp does the rest? I didn't see any special loading code in the SystemFontTest class, so I'm assuming CocosSharp will do the heavy lifting.

Any help would be appreciated. :) Thanks.

Posts

  • Take a look at this link.

  • RickElliottRickElliott USUniversity ✭✭

    @CharlieKilian - thanks for the link. I had actually already read through that post, but it didn't really answer my questions and actually was one of the reasons I created my post here. That talked about creating spritefont XNB resources, which I knew about. But the Labels post I listed above seems to indicate you could use a TrueType font in your project without making a spritefont by using a CCLabel object, with the advantage being on-the-fly scaling of the font as opposed to a fixed font size with a spritefont (several for any different sizes you want) & CCLabelTtf. I'm curious about the mechanics behind adding each to a project as it doesn't seem clear to me what the distinction is. :)

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai
    edited February 2015

    Rick

    Take a look at the SystemFontTest.cs.

    Add the .ttf to the Content directory and make sure you mark it as Content

    I would go with CCLabelBMFont or spritefont if you are targeting WP8 as the WP8 does not support .ttf. They are truthfully faster in my opinion even though the CCLabel and .ttf is easier and also has the drawback of no support for WP8.

  • RickElliottRickElliott USUniversity ✭✭

    @kjpou1 - Thanks for the reply. I tried your suggestion and couldn't get it to work as you specified (marked as Content). However, I was able to get it to work with it placed in my Content directory marked as an AndroidAsset (I'm working on my Android project at the moment), so I'm happy with it at the moment. I'll keep your suggestion on the Bitmap/spritefont in mind if I target WP8.

    I have another question after getting this to work. I wanted to put the font in my Content/fonts directory of the project but couldn't get it to work unless I prefixed the fonts directory to the name as shown below:

    var label = new CCLabel("Welcome to My Game!", "fonts/CrimsonText-Bold.ttf", 72);
    

    I tried adding the fonts directory as a content search path in my AppDelegate but that didn't seem to help:

    application.ContentSearchPaths.Add("fonts");
    

    Any thoughts? Thanks for the help, I've at least got a working font now. :smiley:

  • kjpou1kjpou1 LUMember, Xamarin Team Xamurai
    edited February 2015

    Hey Rick

    Glad you got it working. Android is different and of course needs to be marked as AndroidAsset. All the other platforms use Content so went with that.

    There is already an issue created for what you are describing and we will be addressing it in the near future. Just for a little background, it was easier to keep .ttf files in the Content directory with full path specified because of the way each platform loads them. We are hoping to be able to get around this next time.

  • RickElliottRickElliott USUniversity ✭✭

    Okay, thanks for the help. I appreciate it.

Sign In or Register to comment.