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

Author Topic: PhysFS and SFML, seeking advice  (Read 2599 times)

0 Members and 1 Guest are viewing this topic.

Buckermann

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • http://isotactics.blogspot.com/
PhysFS and SFML, seeking advice
« on: April 23, 2009, 11:42:39 am »
Greetings,
I'm playing around with a simple isometric graphics engine, and since it uses lots of small files, I decided to use PhysFS for the file handling. Thankfully SFML provides LoadFromMemory() functions, so I figured a way out to actually use PhysFS (only for images for now, but the principle should be the same for sound/music I hope).
Since I'm not a very experienced coder, I have the nagging feeling that I did something wrong, even when it seems to work like it should. So it would be nice if you could have a look at this simple function and tell me if something is wrong with it.

The image loading function:
Code: [Select]

int PHYS_LoadImage(char* Filename, sf::Image* ptrImage)
{
    PHYSFS_File* LoadFile;
    if(!(LoadFile = PHYSFS_openRead(Filename)))
    {
        const char* error = PHYSFS_getLastError();
        std::cout<<error<<std::endl;
        Log(error);
        return 1;
    }
    int FileSize = PHYSFS_fileLength(LoadFile);
    //std::cout<<"Filesize in byte for "<<Filename<<" is "<<FileSize<<std::endl;
    char *FileBuffer = new char [FileSize];
    PHYSFS_setBuffer(LoadFile, 1024);
    PHYSFS_read(LoadFile, FileBuffer, FileSize, 1);
    ptrImage->LoadFromMemory(FileBuffer, FileSize);
    delete[] FileBuffer;
    return 0;
}


And here is how I call it:
Code: [Select]


    //physfs testing
    PHYSFS_init(NULL);
    if(!PHYSFS_mount("Data/IsoTiles/Chars.zip", "Chars", 1))
    {
        std::cout << "Zip File NOT found!"<<std::endl;
        Log("Zip File NOT found!");
    }
    sf::Image CharImageSource[9];
    if(!PHYS_LoadImage("Chars/char_m001_001.png", &CharImageSource[0]));
        Log("PhysFS File load to Sfml Image failed");



And if somebody is interested in my project: IsoTactics Blog.

Iori Branford

  • Newbie
  • *
  • Posts: 1
    • View Profile
PhysFS and SFML, seeking advice
« Reply #1 on: April 28, 2009, 01:27:15 am »
You should PHYSFS_close(LoadFile) if LoadFile is no longer needed.

You can probably do without PHYSFS_setBuffer() as well.

 

anything