SFML community forums

Bindings - other languages => Java => Topic started by: white_nitro0 on February 26, 2014, 05:25:21 pm

Title: [Resolved] Font won't render if it is loaded in a different class
Post by: white_nitro0 on February 26, 2014, 05:25:21 pm
Hi,

For the sake of keeping my code tidy, I've implemented a FontLibrary class:

public class FontLibrary {

    public static Font arial = new Font();

    public static Font getFont(String font){
        if(font.equals("arial")){
             if(arial == null){
                 try{
                     arial.loadFromFile(Paths.get("resources" + File.separatorChar + "fonts" + File.separatorChar + "arial.ttf"));
                 } catch(IOException e){
                     e.printStackTrace();
                 }
             }
            return arial;

        }
        else return null;
    }

I've validated that non-null values are being returned, but when I try to use the FontLibrary to get fonts in a different class, the font is not rendered:

t = new Text("Hello world!", FontLibrary.getFont("arial"), 32);
t.setPosition(0,0);
t.setColor(Color.GREEN);

window.draw(t);

If I do the font loading in the same method as creating the Text object t, it displays as expected.

Can anyone offer any suggestions or point out my mistake?

Thanks in advance!
Title: Re: Font won't render if it is loaded in a different class
Post by: pdinklag on February 26, 2014, 06:22:53 pm
Your font is never loaded due to your code. Your static initializer states:
public static Font arial = new Font();

arial is therefore not null. However, you only load the font if arial is null:
if(arial == null){ /* loading code */ }

Alas, the loading code is never executed. I believe what you want to do is to initialize the arial variable with null:
public static Font arial = null;

Don't forget to construct it before you load it, then.
Title: Re: Font won't render if it is loaded in a different class
Post by: white_nitro0 on February 26, 2014, 06:27:40 pm
Just came to the exact same conclusion myself, thanks for your help :)