Hey folks,
I have spent a few hours knocking up a quick method for loading assets (images, fonts, etc.) using the PhysicsFS library.
Feel free to rip it apart as much as you like, though please take a moment just to let me know what it's being used in as well as any changes that have been made to make it more efficient.
Thanks
Mikey
PLEASE NOTE: This is to be used in conjunction with the PhysFS library, and all initialisation, adding of paths etc. has to be done using the standard PhysFS methods.
PhysFSLoader.hpp
// ----------------------------------------------------------------------------------------------------
// Title: SFML PhysFS Asset Loader
// Description: Load Assets (Images, Fonts, etc.) using the PhysFS library.
// Revision: 1 (17 July 2011)
// Author(s): Michael R. King (mrking2910@gmail.com)
// Dependencies: SFML (http://www.sfml-dev.org/) (Designed for version 2.0)
// PhysFS (http://icculus.org/physfs/) (Designed for version 2.0.2)
// Notes: Will only load the assets.
// ----------------------------------------------------------------------------------------------------
// - Include Protection -
#ifndef _H_SFML_PHYSFS_LOADER_
#define _H_SFML_PHYSFS_LOADER_
// - Include SFML -
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
// - Include PhysFS -
#include <physfs.h>
// - Include in SFML Namespace -
namespace sf
{
// - Error Codes -
enum PFSL_Error
{
PFSL_Error_None,
PFSL_Error_PhysFSNotReady,
PFSL_Error_InvalidFileHandle,
PFSL_Error_FileNotFound,
PFSL_Error_LoadFailed,
PFSL_Error_FileTooBig,
PFLS_Error_NotEnoughRead
};
// - Loader Class Declaration -
class PhysFSLoader
{
protected:
PFSL_Error m_lastError;
PHYSFS_sint64 m_maxFileSize;
char* m_fileBuffer;
PHYSFS_sint64 m_fileSize;
protected:
bool doLoad(sf::String Filename);
public:
PFSL_Error GetLastError();
void SetMaxFileSize(PHYSFS_sint64 Size);
bool Load(sf::Font* Object, sf::String Filename);
bool Load(sf::Image* Object, sf::String Filename);
bool Load(sf::SoundBuffer* Object, sf::String Filename);
public:
PhysFSLoader();
~PhysFSLoader();
};
}
#endif //_H_SFML_PHYSFS_LOADER_
PhysFSLoader.cpp
#include <PhysFSLoader.hpp>
namespace sf
{
// - Shared loading routine -
bool PhysFSLoader::doLoad(sf::String Filename)
{
// - Check if PhysFS is initialised -
if(PHYSFS_isInit() == 0)
{
m_lastError = PFSL_Error_PhysFSNotReady;
return false;
}
// - Check if file exists -
if(! PHYSFS_exists(Filename.ToAnsiString().c_str()))
{
m_lastError = PFSL_Error_FileNotFound;
return false;
}
// - Attempt to open file -
PHYSFS_file* fileObj = PHYSFS_openRead(Filename.ToAnsiString().c_str());
if(fileObj == NULL)
{
m_lastError = PFSL_Error_InvalidFileHandle;
return false;
}
// - Check file size -
PHYSFS_sint64 fSize = PHYSFS_fileLength(fileObj);
if(fSize > m_maxFileSize)
{
m_lastError = PFSL_Error_FileTooBig;
return false;
}
// - Read data -
m_fileBuffer = new char [fSize];
PHYSFS_sint64 totalRead = PHYSFS_read(fileObj, m_fileBuffer, 1, fSize);
if(totalRead != fSize)
{
m_lastError = PFLS_Error_NotEnoughRead;
delete m_fileBuffer;
m_fileBuffer = NULL;
return false;
}
return true;
}
// - Return Last Error -
PFSL_Error PhysFSLoader::GetLastError()
{
return m_lastError;
}
// - Set Filesize Limit -
void PhysFSLoader::SetMaxFileSize(PHYSFS_sint64 Size)
{
m_maxFileSize = Size;
}
// - Load Font -
bool PhysFSLoader::Load(sf::Font* Object, sf::String Filename)
{
m_lastError = PFSL_Error_None;
if(doLoad(Filename))
{
if(Object->LoadFromMemory(m_fileBuffer, m_fileSize))
{
delete m_fileBuffer;
m_fileBuffer = NULL;
return true;
}
delete m_fileBuffer;
m_fileBuffer = NULL;
m_lastError = PFSL_Error_LoadFailed;
}
return false;
}
// - Load Image -
bool PhysFSLoader::Load(sf::Image* Object, sf::String Filename)
{
m_lastError = PFSL_Error_None;
if(doLoad(Filename))
{
if(Object->LoadFromMemory(m_fileBuffer, m_fileSize))
{
delete m_fileBuffer;
m_fileBuffer = NULL;
return true;
}
delete m_fileBuffer;
m_fileBuffer = NULL;
m_lastError = PFSL_Error_LoadFailed;
}
return false;
}
// - Load Sound Buffer -
bool PhysFSLoader::Load(sf::SoundBuffer* Object, sf::String Filename)
{
m_lastError = PFSL_Error_None;
if(doLoad(Filename))
{
if(Object->LoadFromMemory(m_fileBuffer, m_fileSize))
{
delete m_fileBuffer;
m_fileBuffer = NULL;
return true;
}
delete m_fileBuffer;
m_fileBuffer = NULL;
m_lastError = PFSL_Error_LoadFailed;
}
return false;
}
// - Constructor -
PhysFSLoader::PhysFSLoader()
{
m_fileBuffer = NULL;
m_lastError = PFSL_Error_None;
m_maxFileSize = 4194304; // 4 MB Limit
}
// - Deconstructor -
PhysFSLoader::~PhysFSLoader()
{
if(m_fileBuffer)
{
delete m_fileBuffer;
m_fileBuffer = NULL;
}
}
}
Usage
#include <physfs.h>
#include <physFSLoader.hpp>
#include <SFML\Graphics.hpp>
void main()
{
PHYSFS_init(NULL);
PHYSFS_addToSearchPath("test.zip", 1);
sf::PhysFSLoader Loader;
sf::Image MyImage;
Loader.Load(&MyImage, "test.jpg");
}