Restore support for animated png files

* Rework resource manager
* Add missing files
* Improve some graphics classes
This commit is contained in:
Eduardo Bart
2013-01-08 19:31:41 -02:00
parent fdcad184f9
commit 0120b7554c
47 changed files with 807 additions and 161 deletions

View File

@@ -109,10 +109,10 @@ void Application::deinit()
g_modules.unloadModules();
g_modules.clear();
// release remaining lua object references
// release reamaning lua object references
g_lua.collectGarbage();
// poll remaining events
// poll reamaning events
poll();
// disable dispatcher events

View File

@@ -45,7 +45,7 @@ void EventDispatcher::poll()
int loops = 0;
for(int count = 0, max = m_scheduledEventList.size(); count < max && !m_scheduledEventList.empty(); ++count) {
ScheduledEventPtr scheduledEvent = m_scheduledEventList.top();
if(scheduledEvent->remainingTicks() > 0)
if(scheduledEvent->reamaningTicks() > 0)
break;
m_scheduledEventList.pop();
scheduledEvent->execute();

View File

@@ -35,6 +35,17 @@ FileStream::FileStream(const std::string& name, PHYSFS_File *fileHandle, bool wr
{
}
FileStream::FileStream(const std::string& name, const std::string& buffer) :
m_name(name),
m_fileHandle(nullptr),
m_pos(0),
m_writeable(false),
m_caching(true)
{
m_data.resize(buffer.length());
memcpy(&m_data[0], &buffer[0], buffer.length());
}
FileStream::~FileStream()
{
#ifndef NDEBUG
@@ -338,3 +349,4 @@ void FileStream::throwError(const std::string& message, bool physfsError)
completeMessage += std::string(": ") + PHYSFS_getLastError();
stdext::throw_exception(completeMessage);
}

View File

@@ -36,6 +36,7 @@ class FileStream : public LuaObject
{
public:
FileStream(const std::string& name, PHYSFS_File *fileHandle, bool writeable);
FileStream(const std::string& name, const std::string& buffer);
~FileStream();
void cache();

View File

@@ -76,7 +76,7 @@ void GraphicalApplication::terminate()
// destroy particles
g_particles.terminate();
// destroy any remaining widget
// destroy any reamaning widget
g_ui.terminate();
Application::terminate();

View File

@@ -48,10 +48,10 @@ bool Module::load()
for(const std::string& depName : m_dependencies) {
ModulePtr dep = g_modules.getModule(depName);
if(!dep)
stdext::throw_exception(stdext::format("dependency '%s' was not found", m_name, depName));
stdext::throw_exception(stdext::format("dependency '%s' was not found", depName));
if(!dep->isLoaded() && !dep->load())
stdext::throw_exception(stdext::format("dependency '%s' has failed to load", m_name, depName));
stdext::throw_exception(stdext::format("dependency '%s' has failed to load", depName));
}
if(m_sandboxed)
@@ -199,8 +199,8 @@ void Module::discover(const OTMLNodePtr& moduleNode)
}
if(OTMLNodePtr node = moduleNode->get("@onLoad"))
m_onLoadFunc = std::make_tuple(node->value(), "@" + node->source() + "[" + node->tag() + "]");
m_onLoadFunc = std::make_tuple(node->value(), "@" + node->source() + ":[" + node->tag() + "]");
if(OTMLNodePtr node = moduleNode->get("@onUnload"))
m_onUnloadFunc = std::make_tuple(node->value(), "@" + node->source() + "[" + node->tag() + "]");
m_onUnloadFunc = std::make_tuple(node->value(), "@" + node->source() + ":[" + node->tag() + "]");
}

View File

@@ -42,14 +42,12 @@ void ResourceManager::terminate()
PHYSFS_deinit();
}
void ResourceManager::discoverWorkDir(const std::string& appName, const std::string& existentFile)
bool ResourceManager::discoverWorkDir(const std::string& existentFile)
{
// search for modules directory
std::string possiblePaths[] = { g_resources.getCurrentDir(),
g_resources.getBaseDir() + "../",
g_resources.getBaseDir() + "../share/" + appName + "/",
g_resources.getBaseDir() + appName + "/" };
g_resources.getBaseDir(),
g_resources.getBaseDir() + "../" };
bool found = false;
for(const std::string& dir : possiblePaths) {
if(!PHYSFS_addToSearchPath(dir.c_str(), 0))
@@ -64,8 +62,7 @@ void ResourceManager::discoverWorkDir(const std::string& appName, const std::str
PHYSFS_removeFromSearchPath(dir.c_str());
}
if(!found)
g_logger.fatal(stdext::format("Unable to find %s, the application cannot be initialized.", existentFile));
return found;
}
bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName)
@@ -78,7 +75,7 @@ bool ResourceManager::setupUserWriteDir(const std::string& appWriteDirName)
dirName = appWriteDirName;
#endif
std::string writeDir = userDir + dirName;
if(!PHYSFS_setWriteDir(writeDir.c_str())) {
if(!PHYSFS_setWriteDir(userDir.c_str()) || !PHYSFS_mkdir(dirName.c_str())) {
g_logger.error(stdext::format("Unable to create write directory '%s': %s", writeDir, PHYSFS_getLastError()));
@@ -165,35 +162,35 @@ bool ResourceManager::directoryExists(const std::string& directoryName)
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
}
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
void ResourceManager::readFileStream(const std::string& fileName, std::iostream& out)
{
out.clear(std::ios::goodbit);
std::string fullPath = resolvePath(fileName);
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
if(!file) {
out.clear(std::ios::failbit);
stdext::throw_exception(stdext::format("unable to load file '%s': %s", fullPath.c_str(), PHYSFS_getLastError()));
} else {
int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) {
std::vector<char> buffer(fileSize);
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
out.write(&buffer[0], fileSize);
} else
out.clear(std::ios::eofbit);
PHYSFS_close(file);
out.seekg(0, std::ios::beg);
std::string buffer = readFileContents(fileName);
if(buffer.length() == 0) {
out.clear(std::ios::eofbit);
return;
}
out.clear(std::ios::goodbit);
out.write(&buffer[0], buffer.length());
out.seekg(0, std::ios::beg);
}
std::string ResourceManager::loadFile(const std::string& fileName)
std::string ResourceManager::readFileContents(const std::string& fileName)
{
std::stringstream fin;
loadFile(fileName, fin);
return fin.str();
std::string fullPath = resolvePath(fileName);
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file)
stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
int fileSize = PHYSFS_fileLength(file);
std::string buffer(fileSize, 0);
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
PHYSFS_close(file);
return buffer;
}
bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size)
bool ResourceManager::writeFileBuffer(const std::string& fileName, const uchar* data, uint size)
{
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
if(!file) {
@@ -206,7 +203,7 @@ bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, u
return true;
}
bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in)
bool ResourceManager::writeFileStream(const std::string& fileName, std::iostream& in)
{
std::streampos oldPos = in.tellg();
in.seekg(0, std::ios::end);
@@ -214,19 +211,20 @@ bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in)
in.seekg(0, std::ios::beg);
std::vector<char> buffer(size);
in.read(&buffer[0], size);
bool ret = saveFile(fileName, (const uchar*)&buffer[0], size);
bool ret = writeFileBuffer(fileName, (const uchar*)&buffer[0], size);
in.seekg(oldPos, std::ios::beg);
return ret;
}
bool ResourceManager::saveFile(const std::string& fileName, const std::string& data)
bool ResourceManager::writeFileContents(const std::string& fileName, const std::string& data)
{
return saveFile(fileName, (const uchar*)data.c_str(), data.size());
return writeFileBuffer(fileName, (const uchar*)data.c_str(), data.size());
}
FileStreamPtr ResourceManager::openFile(const std::string& fileName)
{
std::string fullPath = resolvePath(fileName);
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file)
stdext::throw_exception(stdext::format("unable to open file '%s': %s", fullPath, PHYSFS_getLastError()));
@@ -299,16 +297,12 @@ std::string ResourceManager::getRealDir(const std::string& path)
std::string ResourceManager::getCurrentDir()
{
char buffer[2048];
PHYSFS_utf8FromLatin1((boost::filesystem::current_path().generic_string() + "/").c_str(), buffer, 2048);
return buffer;
return boost::filesystem::current_path().generic_string<std::string>() + "/";
}
std::string ResourceManager::getBaseDir()
{
char buffer[2048];
PHYSFS_utf8FromLatin1(PHYSFS_getBaseDir(), buffer, 2048);
return buffer;
return PHYSFS_getBaseDir();
}
std::string ResourceManager::guessFileType(const std::string& filename, const std::string& type)
@@ -316,4 +310,4 @@ std::string ResourceManager::guessFileType(const std::string& filename, const st
if(g_resources.fileExists(filename))
return filename;
return filename + "." + type;
}
}

View File

@@ -34,7 +34,7 @@ public:
// @dontbind
void terminate();
void discoverWorkDir(const std::string& appName, const std::string& existentFile);
bool discoverWorkDir(const std::string& existentFile);
bool setupUserWriteDir(const std::string& appWriteDirName);
bool setWriteDir(const std::string& writeDir, bool create = false);
@@ -46,13 +46,13 @@ public:
bool directoryExists(const std::string& directoryName);
// @dontbind
void loadFile(const std::string& fileName, std::iostream& out);
std::string loadFile(const std::string& fileName);
void readFileStream(const std::string& fileName, std::iostream& out);
std::string readFileContents(const std::string& fileName);
// @dontbind
bool saveFile(const std::string& fileName, const uchar* data, uint size);
bool saveFile(const std::string& fileName, const std::string& data);
bool writeFileBuffer(const std::string& fileName, const uchar* data, uint size);
bool writeFileContents(const std::string& fileName, const std::string& data);
// @dontbind
bool saveFile(const std::string& fileName, std::iostream& in);
bool writeFileStream(const std::string& fileName, std::iostream& in);
FileStreamPtr openFile(const std::string& fileName);
FileStreamPtr appendFile(const std::string& fileName);

View File

@@ -35,7 +35,7 @@ public:
bool nextCycle();
int ticks() { return m_ticks; }
int remainingTicks() { return m_ticks - g_clock.millis(); }
int reamaningTicks() { return m_ticks - g_clock.millis(); }
int delay() { return m_delay; }
int cyclesExecuted() { return m_cyclesExecuted; }
int maxCycles() { return m_maxCycles; }