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

Author Topic: Trouble loading font ttfs from stream.  (Read 4569 times)

0 Members and 1 Guest are viewing this topic.

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Trouble loading font ttfs from stream.
« on: February 14, 2012, 02:45:00 pm »
Hey, all my images and data files load from a stream fine. apart from fonts, though they are being done in the same way.


I'm using the physfs class from the wiki.

this loads my font

Code: [Select]
const sf::Font * imageManager::getFont( const sf::String& filename )
{
if(filename.GetSize() < 1)
{
return &sf::Font::GetDefaultFont();
}

// Check, whether the image already exists
std::map<sf::String, sf::Font>::iterator it = fontResources.find(filename);
if(it != fontResources.end())
{
return &it->second;
}

CGameEngine &Game = CGameEngine::getInstance();

sf::String fontpath("fonts/");
fontpath += filename;
std::string s(fontpath);

if(PHYSFS_exists(fontpath.ToAnsiString().c_str()))
{
sf::Font font;
sf::physfs fontstream(fontpath.ToAnsiString().c_str());

// Search project's main directory
if( font.LoadFromStream(fontstream) )
{
fontResources[filename] = font;
//std::cout << "DEBUG_MESSAGE: " << filename << " loading image.\n";
return &fontResources[filename];
}
}

Game.consolePrint("Failed loading font \"" + fontpath + "\"");
return &sf::Font::GetDefaultFont();
}



this fails
Code: [Select]

if( font.LoadFromStream(fontstream) )


fontstream returns TRUE and not NULL.

if(PHYSFS_exists(fontpath.ToAnsiString().c_str()))
returns true, it exists.


The images, textures is exactly the same, BUT it loads
Code: [Select]

if(texture.LoadFromStream(imagestream) )
{




This is what loads the file:

Code: [Select]

sf::physfs::physfs(const char *filename){
    file = PHYSFS_openRead(filename);
    error = false;
    if (file == NULL){
        sf::Err() << PHYSFS_getLastError() << std::endl;
        error = true;
    }
}


PHYSFS_openRead, file is valid.

Any suggestions?

I tried loading the font from file, works fine.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble loading font ttfs from stream.
« Reply #1 on: February 14, 2012, 03:01:06 pm »
Does font.LoadFromStream return false? What does "fontstream returns TRUE and not NULL" mean?

Can I see the complete implementation of the sf::physfs class?

One thing that you will need to fix, after loading works, is that the font source (in this case the stream -- or just its contents, I don't remember) has to remain alive as long as the font is used for drawing. This is because I cannot preload all the font's glyphs directly in the LoadFromXxx function, it is done on the fly.
Laurent Gomila - SFML developer

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Trouble loading font ttfs from stream.
« Reply #2 on: February 14, 2012, 03:24:01 pm »
Quote from: "Laurent"
Does font.LoadFromStream return false?
.

it returns false.

Quote from: "Laurent"
What does "fontstream returns TRUE and not NULL" mean?
.

I mean, the file has a handle and the error section of the class is false,  So I mean it reports it as loading "correctly"

here is an image to illustrate what I mean;
http://i41.tinypic.com/qn787d.png

Quote from: "Laurent"

Can I see the complete implementation of the sf::physfs class?
.

I am using the following code:
https://github.com/SFML/SFML/wiki/SourceSfmlPhysfs

Its header is simply
Code: [Select]

namespace sf{

    class physfs: public sf::InputStream{
        private:
            PHYSFS_File *file;
        public:
            mutable bool error;
        public:
            physfs(const char *);
            ~physfs();
            sf::Int64 Read(char *, sf::Int64);
            sf::Int64 Seek(sf::Int64);
            sf::Int64 Tell();
            sf::Int64 GetSize();
    };

}


Quote from: "Laurent"

One thing that you will need to fix, after loading works, is that the font source (in this case the stream -- or just its contents, I don't remember) has to remain alive as long as the font is used for drawing. This is because I cannot preload all the font's glyphs directly in the LoadFromXxx function, it is done on the fly

.

I see, thank you

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Trouble loading font ttfs from stream.
« Reply #3 on: February 14, 2012, 03:43:22 pm »
Your implementation of Seek is incorrect, you return a status (see the doc of PHYSFS_seek) but sf::InputStream::Seek must return the new position.

You should tell the class writer (if it's not you) so that he can correct its class and reupload it on the wiki. Too bad he didn't paste this tiny code directly on the wiki page, so that anyone can edit it.

And please resize your screenshot, it's too wide.
Laurent Gomila - SFML developer

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Trouble loading font ttfs from stream.
« Reply #4 on: February 14, 2012, 04:12:04 pm »
yeah I'm sorry about the screenshot, it turned up a lot bigger than I thought, I assumed the link would be a thumbnail.

Code: [Select]

int PHYSFS_seek ( PHYSFS_File * handle,
PHYSFS_uint64 pos
)


I returned the same position and it seems to work well. thank you, I will let the author know..

amazingly spotted.

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Trouble loading font ttfs from stream.
« Reply #5 on: February 15, 2012, 02:32:39 am »
You should check the version used on the wiki example as well as the version you downloaded. If you're using the latest source version, there seem to be several more or less significant changes as well as deprecated functions.

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Trouble loading font ttfs from stream.
« Reply #6 on: February 15, 2012, 01:49:58 pm »
Quote from: "Mario"
You should check the version used on the wiki example as well as the version you downloaded. If you're using the latest source version, there seem to be several more or less significant changes as well as deprecated functions.


May I ask Which?

Everything seems good, I should probably go over and make sure its good, rather than just assuming, thank you

I finally worked out who the Author was, and will notify them of this thread

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Trouble loading font ttfs from stream.
« Reply #7 on: February 15, 2012, 05:07:30 pm »
Not without rechecking the headers, but most are marked as "deprecated" or simply missing.

.teri

  • Newbie
  • *
  • Posts: 28
    • View Profile
Trouble loading font ttfs from stream.
« Reply #8 on: February 15, 2012, 07:02:28 pm »
Try the fixed version https://legacy.sfmluploads.org/file/89

Link to module fixed at the wiki.

:)

Elgan

  • Jr. Member
  • **
  • Posts: 77
    • AOL Instant Messenger - Flat+1,+17+st+Cl
    • View Profile
Trouble loading font ttfs from stream.
« Reply #9 on: February 15, 2012, 07:28:14 pm »
thanx v.much for your work!