SFML community forums

Bindings - other languages => D => Topic started by: malkierian on December 06, 2013, 07:58:11 am

Title: Font Issues
Post by: malkierian on December 06, 2013, 07:58:11 am
So I'm trying to get a Text going in a class of mine, and in order to do that, I need a Font.  However, when I try this, and use font.loadfromFile(), I get 6 errors related to it:
Code: [Select]
unexpected ( in declarator
basic type expected, not "fonts/OratorStd.ttf"
found '"fonts/OratorStd.ttf"' when expecting ')'
no identifier for declarator font.loadFromFile(int)
semicolon expected following function declaration
declaration expected, not ')'
And when I hover over the line, I see "Parser Error: <Identifier> expected, <Literal> found!".  The issue I can see is that, for some reason, Font isn't showing as having any of the methods necessary to utilizing the class.  Whenever I type a period after my font object, NOTHING shows up.

Here's my code:
module sound;

import dsfml.graphics;
import scene2d.actor;

class Sound: Actor
{
        Sprite playSprite;
        Sprite stopSprite;
        Sprite folderSprite;

        auto font = new Font();
        font.loadFromFile("fonts/OratorStd.ttf");
        Text label = new Text("Uninitialized", font);
        this(Texture *play, Texture *stop, Texture *folder, int index)
        {
                int x, y;
                x = (index % 10) * 96 + (index % 10) * 16;
                y = (index % 9) * 16 + (index % 10) * 64;

                playSprite = new Sprite(play);
                playSprite.position(Vector2f(x, y + 32));
                stopSprite = new Sprite(play);
                stopSprite.position(Vector2f(x + 32, y + 32));
                folderSprite = new Sprite(play);
                folderSprite.position(Vector2f(x + 64, y + 32));
        }

        ~this()
        {
                ~playSprite();
                ~stopSprite();
                ~folderSprite();
        }

        override draw(Drawable surface)
        {
                surface.draw(label);
                surface.draw(playSprite);
                surface.draw(stopSprite);
                surface.draw(folderSprite);
        }
}

EDIT: Well, that was weird.  I moved the font loading into my main class and passed it as a parameter to the SoundActor constructor (the new name for the class), and everything seems to be working fine, though I still don't get the lookup window when calling a member...
Title: Re: Font Issues
Post by: Jebbs on December 06, 2013, 06:47:59 pm
I don't know if this is something Java allowed, but you're getting this error because you are trying to call an object's method inside the class definition, which isn't correct. Move the font loading and the text construction that loads the font into the class' initializer instead and it'll work. Like so(how I would write it):

module sound;

import dsfml.graphics;
import scene2d.actor;

class Sound: Actor
{
    Sprite playSprite;
    Sprite stopSprite;
    Sprite folderSprite;

    //Defined here
    Font font;
    Text label;

    this(Texture play, Texture stop, Texture folder, int index)
    {
        int x, y;
        x = (index % 10) * 96 + (index % 10) * 16;
        y = (index % 9) * 16 + (index % 10) * 64;

        //And loaded here
        font = new Font();
        font.loadFromFile("fonts/OratorStd.ttf");
        label = new Text("Uninitialized", font);

        playSprite = new Sprite(play);
        playSprite.position(Vector2f(x, y + 32));
        stopSprite = new Sprite(play);
        stopSprite.position(Vector2f(x + 32, y + 32));
        folderSprite = new Sprite(play);
        folderSprite.position(Vector2f(x + 64, y + 32));
    }


    override draw(Drawable surface)
    {
        surface.draw(label);
        surface.draw(playSprite);
        surface.draw(stopSprite);
        surface.draw(folderSprite);
    }
}



I also removed the destructor because you almost never need to worry explicitly handling that yourself in D. The only time I used them was for cleaning up objects that weren't handled the GC.(e.g., the pointers to the objects created in the C++ code)

Also, there isn't any need to use pointers to pass classes. Classes are always passed by reference.

Hope that helps!