revert windows and line ending sh*t

This commit is contained in:
Eduardo Bart 2012-04-28 22:07:47 -03:00
parent df0147cf64
commit 008fa85635
4 changed files with 529 additions and 529 deletions

View File

@ -1,223 +1,223 @@
/* /*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient> * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "resourcemanager.h" #include "resourcemanager.h"
#include "filestream.h" #include "filestream.h"
#include <framework/application.h> #include <framework/application.h>
#include <framework/luascript/luainterface.h> #include <framework/luascript/luainterface.h>
#include <physfs.h> #include <physfs.h>
ResourceManager g_resources; ResourceManager g_resources;
void ResourceManager::init(const char *argv0) void ResourceManager::init(const char *argv0)
{ {
PHYSFS_init(argv0); PHYSFS_init(argv0);
} }
void ResourceManager::terminate() void ResourceManager::terminate()
{ {
PHYSFS_deinit(); PHYSFS_deinit();
} }
bool ResourceManager::setupWriteDir(const std::string& appWriteDirName) bool ResourceManager::setupWriteDir(const std::string& appWriteDirName)
{ {
std::string userDir = PHYSFS_getUserDir(); std::string userDir = PHYSFS_getUserDir();
std::string dirName = Fw::mkstr(".", appWriteDirName); std::string dirName = Fw::mkstr(".", appWriteDirName);
std::string writeDir = userDir + dirName; std::string writeDir = userDir + dirName;
if(!PHYSFS_setWriteDir(writeDir.c_str())) { if(!PHYSFS_setWriteDir(writeDir.c_str())) {
if(!PHYSFS_setWriteDir(userDir.c_str())) if(!PHYSFS_setWriteDir(userDir.c_str()))
return false; return false;
if(!PHYSFS_mkdir(dirName.c_str())) { if(!PHYSFS_mkdir(dirName.c_str())) {
PHYSFS_setWriteDir(NULL); PHYSFS_setWriteDir(NULL);
return false; return false;
} }
if(!PHYSFS_setWriteDir(writeDir.c_str())) if(!PHYSFS_setWriteDir(writeDir.c_str()))
return false; return false;
} }
addToSearchPath(writeDir); addToSearchPath(writeDir);
return true; return true;
} }
bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront) bool ResourceManager::addToSearchPath(const std::string& path, bool insertInFront)
{ {
if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1)) if(!PHYSFS_addToSearchPath(path.c_str(), insertInFront ? 0 : 1))
return false; return false;
return true; return true;
} }
void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append) void ResourceManager::searchAndAddPackages(const std::string& packagesDir, const std::string& packageExt, bool append)
{ {
auto files = listDirectoryFiles(resolvePath(packagesDir)); auto files = listDirectoryFiles(resolvePath(packagesDir));
for(const std::string& file : files) { for(const std::string& file : files) {
if(boost::ends_with(file, packageExt)) if(boost::ends_with(file, packageExt))
addToSearchPath(packagesDir + "/" + file, !append); addToSearchPath(packagesDir + "/" + file, !append);
} }
} }
bool ResourceManager::fileExists(const std::string& fileName) bool ResourceManager::fileExists(const std::string& fileName)
{ {
return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str())); return (PHYSFS_exists(resolvePath(fileName).c_str()) && !PHYSFS_isDirectory(resolvePath(fileName).c_str()));
} }
bool ResourceManager::directoryExists(const std::string& directoryName) bool ResourceManager::directoryExists(const std::string& directoryName)
{ {
return (PHYSFS_isDirectory(resolvePath(directoryName).c_str())); return (PHYSFS_isDirectory(resolvePath(directoryName).c_str()));
} }
void ResourceManager::loadFile(const std::string& fileName, std::iostream& out) void ResourceManager::loadFile(const std::string& fileName, std::iostream& out)
{ {
std::string fullPath = resolvePath(fileName); std::string fullPath = resolvePath(fileName);
out.clear(std::ios::goodbit); out.clear(std::ios::goodbit);
PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_file* file = PHYSFS_openRead(fullPath.c_str());
if(!file) { if(!file) {
out.clear(std::ios::failbit); out.clear(std::ios::failbit);
Fw::throwException("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError()); Fw::throwException("failed to load file '", fullPath.c_str(), "': ", PHYSFS_getLastError());
} else { } else {
int fileSize = PHYSFS_fileLength(file); int fileSize = PHYSFS_fileLength(file);
if(fileSize > 0) { if(fileSize > 0) {
std::vector<char> buffer(fileSize); std::vector<char> buffer(fileSize);
PHYSFS_read(file, (void*)&buffer[0], 1, fileSize); PHYSFS_read(file, (void*)&buffer[0], 1, fileSize);
out.write(&buffer[0], fileSize); out.write(&buffer[0], fileSize);
} else } else
out.clear(std::ios::eofbit); out.clear(std::ios::eofbit);
PHYSFS_close(file); PHYSFS_close(file);
out.seekg(0, std::ios::beg); out.seekg(0, std::ios::beg);
} }
} }
std::string ResourceManager::loadFile(const std::string& fileName) std::string ResourceManager::loadFile(const std::string& fileName)
{ {
std::stringstream fin; std::stringstream fin;
loadFile(fileName, fin); loadFile(fileName, fin);
return fin.str(); return fin.str();
} }
bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size) bool ResourceManager::saveFile(const std::string& fileName, const uchar* data, uint size)
{ {
PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_file* file = PHYSFS_openWrite(fileName.c_str());
if(!file) if(!file)
{ {
logError(PHYSFS_getLastError()); logError(PHYSFS_getLastError());
return false; return false;
} }
PHYSFS_write(file, (void*)data, size, 1); PHYSFS_write(file, (void*)data, size, 1);
PHYSFS_close(file); PHYSFS_close(file);
return true; return true;
} }
bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in) bool ResourceManager::saveFile(const std::string& fileName, std::iostream& in)
{ {
std::streampos oldPos = in.tellg(); std::streampos oldPos = in.tellg();
in.seekg(0, std::ios::end); in.seekg(0, std::ios::end);
std::streampos size = in.tellg(); std::streampos size = in.tellg();
in.seekg(0, std::ios::beg); in.seekg(0, std::ios::beg);
std::vector<char> buffer(size); std::vector<char> buffer(size);
in.read(&buffer[0], size); in.read(&buffer[0], size);
bool ret = saveFile(fileName, (const uchar*)&buffer[0], size); bool ret = saveFile(fileName, (const uchar*)&buffer[0], size);
in.seekg(oldPos, std::ios::beg); in.seekg(oldPos, std::ios::beg);
return ret; return ret;
} }
bool ResourceManager::saveFile(const std::string& fileName, const std::string& data) bool ResourceManager::saveFile(const std::string& fileName, const std::string& data)
{ {
return saveFile(fileName, (const uchar*)data.c_str(), data.size()); return saveFile(fileName, (const uchar*)data.c_str(), data.size());
} }
FileStreamPtr ResourceManager::openFile(const std::string& fileName) FileStreamPtr ResourceManager::openFile(const std::string& fileName)
{ {
std::string fullPath = resolvePath(fileName); std::string fullPath = resolvePath(fileName);
PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str()); PHYSFS_File* file = PHYSFS_openRead(fullPath.c_str());
if(!file) { if(!file) {
logTraceError("unable to open file '", fullPath, "': ", PHYSFS_getLastError()); logTraceError("unable to open file '", fullPath, "': ", PHYSFS_getLastError());
return nullptr; return nullptr;
} }
return FileStreamPtr(new FileStream(fullPath, file)); return FileStreamPtr(new FileStream(fullPath, file));
} }
FileStreamPtr ResourceManager::appendFile(const std::string& fileName) FileStreamPtr ResourceManager::appendFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str()); PHYSFS_File* file = PHYSFS_openAppend(fileName.c_str());
if(!file) { if(!file) {
logTraceError("failed to append file '", fileName, "': ", PHYSFS_getLastError()); logTraceError("failed to append file '", fileName, "': ", PHYSFS_getLastError());
return nullptr; return nullptr;
} }
return FileStreamPtr(new FileStream(fileName, file)); return FileStreamPtr(new FileStream(fileName, file));
} }
FileStreamPtr ResourceManager::createFile(const std::string& fileName) FileStreamPtr ResourceManager::createFile(const std::string& fileName)
{ {
PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str()); PHYSFS_File* file = PHYSFS_openWrite(fileName.c_str());
if(!file) { if(!file) {
logTraceError("failed to create file '", fileName, "': ", PHYSFS_getLastError()); logTraceError("failed to create file '", fileName, "': ", PHYSFS_getLastError());
return nullptr; return nullptr;
} }
return FileStreamPtr(new FileStream(fileName, file)); return FileStreamPtr(new FileStream(fileName, file));
} }
bool ResourceManager::deleteFile(const std::string& fileName) bool ResourceManager::deleteFile(const std::string& fileName)
{ {
return PHYSFS_delete(resolvePath(fileName).c_str()) != 0; return PHYSFS_delete(resolvePath(fileName).c_str()) != 0;
} }
bool ResourceManager::makeDir(const std::string directory) bool ResourceManager::makeDir(const std::string directory)
{ {
return PHYSFS_mkdir(directory.c_str()); return PHYSFS_mkdir(directory.c_str());
} }
std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath) std::list<std::string> ResourceManager::listDirectoryFiles(const std::string& directoryPath)
{ {
std::list<std::string> files; std::list<std::string> files;
auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str()); auto rc = PHYSFS_enumerateFiles(resolvePath(directoryPath).c_str());
for(int i = 0; rc[i] != NULL; i++) for(int i = 0; rc[i] != NULL; i++)
files.push_back(rc[i]); files.push_back(rc[i]);
PHYSFS_freeList(rc); PHYSFS_freeList(rc);
return files; return files;
} }
std::string ResourceManager::resolvePath(const std::string& path) std::string ResourceManager::resolvePath(const std::string& path)
{ {
std::string fullPath; std::string fullPath;
if(boost::starts_with(path, "/")) if(boost::starts_with(path, "/"))
fullPath = path; fullPath = path;
else { else {
std::string scriptPath = "/" + g_lua.getCurrentSourcePath(); std::string scriptPath = "/" + g_lua.getCurrentSourcePath();
if(!scriptPath.empty()) if(!scriptPath.empty())
fullPath += scriptPath + "/"; fullPath += scriptPath + "/";
fullPath += path; fullPath += path;
} }
if(!(boost::starts_with(fullPath, "/"))) if(!(boost::starts_with(fullPath, "/")))
logTraceWarning("the following file path is not fully resolved: ", path); logTraceWarning("the following file path is not fully resolved: ", path);
boost::replace_all(fullPath, "//", "/"); boost::replace_all(fullPath, "//", "/");
return fullPath; return fullPath;
} }
std::string ResourceManager::getBaseDir() std::string ResourceManager::getBaseDir()
{ {
return PHYSFS_getBaseDir(); return PHYSFS_getBaseDir();
} }

View File

@ -1,238 +1,238 @@
/* /*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient> * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#include "spritemanager.h" #include "spritemanager.h"
#include <framework/core/resourcemanager.h> #include <framework/core/resourcemanager.h>
#include <framework/core/eventdispatcher.h> #include <framework/core/eventdispatcher.h>
#include <framework/core/filestream.h> #include <framework/core/filestream.h>
#include <framework/graphics/graphics.h> #include <framework/graphics/graphics.h>
#include <framework/thirdparty/apngloader.h> #include <framework/thirdparty/apngloader.h>
#include <physfs.h> #include <physfs.h>
SpriteManager g_sprites; SpriteManager g_sprites;
SpriteManager::SpriteManager() SpriteManager::SpriteManager()
{ {
m_spritesCount = 0; m_spritesCount = 0;
m_signature = 0; m_signature = 0;
} }
bool SpriteManager::load(const std::string& file) bool SpriteManager::load(const std::string& file)
{ {
try { try {
m_spritesFile = g_resources.openFile(file); m_spritesFile = g_resources.openFile(file);
if(!m_spritesFile) if(!m_spritesFile)
return false; return false;
// cache file buffer to avoid lags from hard drive // cache file buffer to avoid lags from hard drive
m_spritesFile->cache(); m_spritesFile->cache();
m_signature = m_spritesFile->getU32(); m_signature = m_spritesFile->getU32();
m_spritesCount = m_spritesFile->getU16(); m_spritesCount = m_spritesFile->getU16();
m_sprites.resize(m_spritesCount); m_sprites.resize(m_spritesCount);
m_loaded = true; m_loaded = true;
return true; return true;
} catch(Exception& e) { } catch(Exception& e) {
logError("Failed to load sprites from '", file, "': ", e.what()); logError("Failed to load sprites from '", file, "': ", e.what());
return false; return false;
} }
} }
void SpriteManager::unload() void SpriteManager::unload()
{ {
m_sprites.clear(); m_sprites.clear();
m_spritesCount = 0; m_spritesCount = 0;
m_signature = 0; m_signature = 0;
} }
void SpriteManager::preloadSprites() void SpriteManager::preloadSprites()
{ {
// preload every 50 sprites, periodically // preload every 50 sprites, periodically
const int burst = 50; const int burst = 50;
const int interval = 10; const int interval = 10;
auto preload = [this](int start) { auto preload = [this](int start) {
for(int i=start;i<start+burst && i<=m_spritesCount;++i) { for(int i=start;i<start+burst && i<=m_spritesCount;++i) {
loadSpriteTexture(i); loadSpriteTexture(i);
} }
}; };
for(int i=1;i<=m_spritesCount;i+=burst) for(int i=1;i<=m_spritesCount;i+=burst)
g_eventDispatcher.scheduleEvent(std::bind(preload, i), (i/burst) * interval); g_eventDispatcher.scheduleEvent(std::bind(preload, i), (i/burst) * interval);
} }
TexturePtr SpriteManager::loadSpriteTexture(int id) TexturePtr SpriteManager::loadSpriteTexture(int id)
{ {
m_spritesFile->seek(((id-1) * 4) + 6); m_spritesFile->seek(((id-1) * 4) + 6);
uint32 spriteAddress = m_spritesFile->getU32(); uint32 spriteAddress = m_spritesFile->getU32();
// no sprite? return an empty texture // no sprite? return an empty texture
if(spriteAddress == 0) if(spriteAddress == 0)
return g_graphics.getEmptyTexture(); return g_graphics.getEmptyTexture();
m_spritesFile->seek(spriteAddress); m_spritesFile->seek(spriteAddress);
// skip color key // skip color key
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
uint16 pixelDataSize = m_spritesFile->getU16(); uint16 pixelDataSize = m_spritesFile->getU16();
static std::vector<uint8> pixels(SPRITE_SIZE); static std::vector<uint8> pixels(SPRITE_SIZE);
int writePos = 0; int writePos = 0;
int read = 0; int read = 0;
// decompress pixels // decompress pixels
while(read < pixelDataSize) { while(read < pixelDataSize) {
uint16 transparentPixels = m_spritesFile->getU16(); uint16 transparentPixels = m_spritesFile->getU16();
uint16 coloredPixels = m_spritesFile->getU16(); uint16 coloredPixels = m_spritesFile->getU16();
if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE) if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
return g_graphics.getEmptyTexture(); return g_graphics.getEmptyTexture();
for(int i = 0; i < transparentPixels; i++) { for(int i = 0; i < transparentPixels; i++) {
pixels[writePos + 0] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 3] = 0x00;
writePos += 4; writePos += 4;
} }
for(int i = 0; i < coloredPixels; i++) { for(int i = 0; i < coloredPixels; i++) {
pixels[writePos + 0] = m_spritesFile->getU8(); pixels[writePos + 0] = m_spritesFile->getU8();
pixels[writePos + 1] = m_spritesFile->getU8(); pixels[writePos + 1] = m_spritesFile->getU8();
pixels[writePos + 2] = m_spritesFile->getU8(); pixels[writePos + 2] = m_spritesFile->getU8();
pixels[writePos + 3] = 0xFF; pixels[writePos + 3] = 0xFF;
writePos += 4; writePos += 4;
} }
read += 4 + (3 * coloredPixels); read += 4 + (3 * coloredPixels);
} }
// fill remaining pixels with alpha // fill remaining pixels with alpha
while(writePos < SPRITE_SIZE) { while(writePos < SPRITE_SIZE) {
pixels[writePos + 0] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 3] = 0x00;
writePos += 4; writePos += 4;
} }
TexturePtr spriteTex(new Texture(32, 32, 4, &pixels[0])); TexturePtr spriteTex(new Texture(32, 32, 4, &pixels[0]));
spriteTex->setSmooth(true); spriteTex->setSmooth(true);
if(g_graphics.canUseMipmaps()) if(g_graphics.canUseMipmaps())
spriteTex->generateSoftwareMipmaps(pixels); spriteTex->generateSoftwareMipmaps(pixels);
return spriteTex; return spriteTex;
} }
TexturePtr& SpriteManager::getSpriteTexture(int id) TexturePtr& SpriteManager::getSpriteTexture(int id)
{ {
if(id == 0) if(id == 0)
return g_graphics.getEmptyTexture(); return g_graphics.getEmptyTexture();
assert(id > 0 && id <= m_spritesCount); assert(id > 0 && id <= m_spritesCount);
// load sprites on demand // load sprites on demand
TexturePtr& sprite = m_sprites[id-1]; TexturePtr& sprite = m_sprites[id-1];
if(!sprite) if(!sprite)
sprite = loadSpriteTexture(id); sprite = loadSpriteTexture(id);
//TODO: release unused sprites textures after X seconds //TODO: release unused sprites textures after X seconds
// to avoid massive texture allocations // to avoid massive texture allocations
return sprite; return sprite;
} }
bool SpriteManager::exportSprites() bool SpriteManager::exportSprites()
{ {
g_resources.makeDir("sprites"); g_resources.makeDir("sprites");
std::stringstream ss; std::stringstream ss;
for(volatile int i = 1; i <= m_spritesCount; i++) { for(volatile int i = 1; i <= m_spritesCount; i++) {
m_spritesFile->seek(((i-1) * 4) + 6); m_spritesFile->seek(((i-1) * 4) + 6);
uint32 spriteAddress = m_spritesFile->getU32(); uint32 spriteAddress = m_spritesFile->getU32();
if(spriteAddress == 0) if(spriteAddress == 0)
continue; continue;
m_spritesFile->seek(spriteAddress); m_spritesFile->seek(spriteAddress);
// skip color key // skip color key
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
m_spritesFile->getU8(); m_spritesFile->getU8();
uint16 pixelDataSize = m_spritesFile->getU16(); uint16 pixelDataSize = m_spritesFile->getU16();
uchar pixels[SPRITE_SIZE]; uchar pixels[SPRITE_SIZE];
int writePos = 0; int writePos = 0;
int read = 0; int read = 0;
// decompress pixels // decompress pixels
while(read < pixelDataSize) { while(read < pixelDataSize) {
uint16 transparentPixels = m_spritesFile->getU16(); uint16 transparentPixels = m_spritesFile->getU16();
uint16 coloredPixels = m_spritesFile->getU16(); uint16 coloredPixels = m_spritesFile->getU16();
if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE) if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
return false; // skip the whole process or just ignore this sprite? return false; // skip the whole process or just ignore this sprite?
for(int i = 0; i < transparentPixels; i++) { for(int i = 0; i < transparentPixels; i++) {
pixels[writePos + 0] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 3] = 0x00;
writePos += 4; writePos += 4;
} }
for(int i = 0; i < coloredPixels; i++) { for(int i = 0; i < coloredPixels; i++) {
pixels[writePos + 0] = m_spritesFile->getU8(); pixels[writePos + 0] = m_spritesFile->getU8();
pixels[writePos + 1] = m_spritesFile->getU8(); pixels[writePos + 1] = m_spritesFile->getU8();
pixels[writePos + 2] = m_spritesFile->getU8(); pixels[writePos + 2] = m_spritesFile->getU8();
pixels[writePos + 3] = 0xFF; pixels[writePos + 3] = 0xFF;
writePos += 4; writePos += 4;
} }
read += 4 + (3 * coloredPixels); read += 4 + (3 * coloredPixels);
} }
// fill remaining pixels with alpha // fill remaining pixels with alpha
while(writePos < SPRITE_SIZE) { while(writePos < SPRITE_SIZE) {
pixels[writePos + 0] = 0x00; pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00; pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00; pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00; pixels[writePos + 3] = 0x00;
writePos += 4; writePos += 4;
} }
// We should get the OTClient and export to that folder... // We should get the OTClient and export to that folder...
std::string fileName = Fw::formatString("sprites/%d.png", i); std::string fileName = Fw::formatString("sprites/%d.png", i);
ss.str(""); ss.str("");
save_png(ss, SPRITE_WIDTH, SPRITE_HEIGHT, SPRITE_CHANNELS, pixels); save_png(ss, SPRITE_WIDTH, SPRITE_HEIGHT, SPRITE_CHANNELS, pixels);
g_resources.saveFile(fileName, ss); g_resources.saveFile(fileName, ss);
} }
return true; return true;
} }

View File

@ -1,67 +1,67 @@
/* /*
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient> * Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE. * THE SOFTWARE.
*/ */
#ifndef SPRITEMANAGER_H #ifndef SPRITEMANAGER_H
#define SPRITEMANAGER_H #define SPRITEMANAGER_H
#include <framework/core/declarations.h> #include <framework/core/declarations.h>
#include <framework/graphics/texture.h> #include <framework/graphics/texture.h>
#include <png.h> #include <png.h>
class SpriteManager class SpriteManager
{ {
enum { enum {
SPRITE_WIDTH=32, SPRITE_WIDTH=32,
SPRITE_HEIGHT=32, SPRITE_HEIGHT=32,
SPRITE_CHANNELS=4, SPRITE_CHANNELS=4,
SPRITE_SIZE=SPRITE_WIDTH*SPRITE_HEIGHT*SPRITE_CHANNELS SPRITE_SIZE=SPRITE_WIDTH*SPRITE_HEIGHT*SPRITE_CHANNELS
}; };
public: public:
SpriteManager(); SpriteManager();
bool load(const std::string& file); bool load(const std::string& file);
void unload(); void unload();
void preloadSprites(); void preloadSprites();
uint32 getSignature() { return m_signature; } uint32 getSignature() { return m_signature; }
int getSpritesCount() { return m_spritesCount; } int getSpritesCount() { return m_spritesCount; }
bool exportSprites(); bool exportSprites();
TexturePtr& getSpriteTexture(int id); TexturePtr& getSpriteTexture(int id);
bool isLoaded() { return m_loaded; } bool isLoaded() { return m_loaded; }
private: private:
TexturePtr loadSpriteTexture(int id); TexturePtr loadSpriteTexture(int id);
Boolean<false> m_loaded; Boolean<false> m_loaded;
uint32 m_signature; uint32 m_signature;
int m_spritesCount; int m_spritesCount;
std::vector<TexturePtr> m_sprites; std::vector<TexturePtr> m_sprites;
TexturePtr m_transparentSprite; TexturePtr m_transparentSprite;
FileStreamPtr m_spritesFile; FileStreamPtr m_spritesFile;
}; };
extern SpriteManager g_sprites; extern SpriteManager g_sprites;
#endif #endif

View File

@ -57,7 +57,7 @@ void OTClient::registerLuaFunctions()
g_lua.bindClassStaticFunction("g_sprites", "load", std::bind(&SpriteManager::load, &g_sprites, std::placeholders::_1)); g_lua.bindClassStaticFunction("g_sprites", "load", std::bind(&SpriteManager::load, &g_sprites, std::placeholders::_1));
g_lua.bindClassStaticFunction("g_sprites", "isLoaded", std::bind(&SpriteManager::isLoaded, &g_sprites)); g_lua.bindClassStaticFunction("g_sprites", "isLoaded", std::bind(&SpriteManager::isLoaded, &g_sprites));
g_lua.bindClassStaticFunction("g_sprites", "getSignature", std::bind(&SpriteManager::getSignature, &g_sprites)); g_lua.bindClassStaticFunction("g_sprites", "getSignature", std::bind(&SpriteManager::getSignature, &g_sprites));
g_lua.bindClassStaticFunction("g_sprites", "preloadSprites", std::bind(&SpriteManager::preloadSprites, &g_sprites)); g_lua.bindClassStaticFunction("g_sprites", "preloadSprites", std::bind(&SpriteManager::preloadSprites, &g_sprites));
g_lua.bindClassStaticFunction("g_sprites", "export", std::bind(&SpriteManager::exportSprites, &g_sprites)); g_lua.bindClassStaticFunction("g_sprites", "export", std::bind(&SpriteManager::exportSprites, &g_sprites));
g_lua.registerStaticClass("g_map"); g_lua.registerStaticClass("g_map");