Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: How about getting the Font in xCode 5?  (Read 4268 times)

0 Members and 1 Guest are viewing this topic.

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
How about getting the Font in xCode 5?
« on: June 01, 2014, 05:22:27 am »
I'm not sure if i understand how to get a font from my folder using SFML in xCode 5 for Mac OS X.
I know that i must include
std::string resourcePath(void);
but how exactly must i get a font?
i use the font.loadFromFile(/sansation.tff);
but that wont work! How else can i get the font to load
    //Import the Font and create a Score Text for the user
    std::string resourcePath(void);
    font.loadFromFile("RetroPong/Resource/sansation.ttf");
    playerTextScore.setFont(font);
    opponentTextScore.setFont(font);
    playerTextScore.setCharacterSize(24);
    opponentTextScore.setCharacterSize(24);
    playerTextScore.setColor(sf::Color::Black);
    opponentTextScore.setColor(sf::Color::White);
    opponentTextScore.setPosition(sf::Vector2f(450 ,0));
    playerTextScore.setPosition(sf::Vector2f(150, 0));
    playerTextScore.setString("Player Score");
    opponentTextScore.setString("Opponent Score");
 

Also the font is placed here
RetroPong/BoxFont.tff (the only thing that is include here is RetroPong.xcodeproj and the RetroPong Folder)
and in RetroPong/RetroPong/BoxFont.tff (Main.cpp is also include in this directory, as well as everything else)

Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: How about getting the Font in xCode 5?
« Reply #1 on: June 01, 2014, 05:47:55 am »
If I'm reading this corrently: if your .ttf is in the same directory as your main.cpp you wouldn't need to specify folders e.g.
font.loadFromFile("sansation.ttf");

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: How about getting the Font in xCode 5?
« Reply #2 on: June 01, 2014, 05:59:39 am »
Yeah i've done that but for some reason it just doesnt show up! What is the need for the resourcePath()?
I have visual Studio but since i like using my Mac OS X laptop, i want to learn how to do this in xcode 5


Syntactic Fructose

  • Jr. Member
  • **
  • Posts: 80
  • Overflowing stacks and eating snacks
    • View Profile
Re: How about getting the Font in xCode 5?
« Reply #3 on: June 01, 2014, 07:25:18 am »
From what I can gather, resourcepath was supposed to be your file path that would be loaded into your font. I also noticed you're mentioning two different files, BoxFont.ttf and sansation.ttf . Make sure you load the correct file name first off, secondly you could check if the font loaded correctly:

if (!font.loadFromFile("BoxFont.ttf"))
{
    // error...
}

and then from there add the font to a sf::text and write

sf::Text text;
text.setFont(font);
text.setString("Hello world");
text.setCharacterSize(24); // in pixels
text.setColor(sf::Color::Red);
text.setStyle(sf::Text::Bold | sf::Text::Underlined);

...

// inside the main loop, between window.clear() and window.display()
window.draw(text);

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: How about getting the Font in xCode 5?
« Reply #4 on: June 01, 2014, 09:53:44 am »
Remember, when it comes to Mac, you need to think differently. Syntactic Fructose, what you said is probably right on Linux/Windows but on Mac your binary is not in the same directory as your resources and you can't rely on the default working directory either.

So, everything is described in the tutorial for OS X, as always.

Quote from: tutorial
The usefulness of this [resourcePath()] function, as illustrated in the provided example, is to get in a convenient way access to the Resources folder of your application bundle.

You also get an example when creating your project. Here it is:

(click to show/hide)

and the relevant passage:

font.loadFromFile(resourcePath() + "sansation.ttf")

SFML / OS X developer

AndreeU17

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
Re: How about getting the Font in xCode 5?
« Reply #5 on: June 01, 2014, 07:01:34 pm »
Thank you so much! I knew how to do it in Windows since i've used SFML in Windows but moving over to Mac made it a bit different and the tutorial guide on the main site seem a bit confusing.
But i did what you said
font.loadFromFile(resourcePath() + "sansation.ttf");
and it worked great the way i wanted it ! Thanks so much!~

Hiura

  • SFML Team
  • Hero Member
  • *****
  • Posts: 4321
    • View Profile
    • Email
Re: How about getting the Font in xCode 5?
« Reply #6 on: June 01, 2014, 07:35:01 pm »
Quote
the tutorial guide on the main site seem a bit confusing.
Can you elaborate? I'll be happy to change a few unclear sentences if it makes it better.
SFML / OS X developer