Added support for exporting the sprites as PNG files.

This commit is contained in:
Cayan 2012-04-28 20:31:18 -03:00 committed by Eduardo Bart
parent cc965bb77b
commit 6ed0e099db
5 changed files with 149 additions and 11 deletions

View File

@ -52,8 +52,8 @@ void exitSignalHandler(int sig)
break; break;
} }
} }
Application::Application(const std::string& appName) Application::Application(const std::string& appName)
{ {
g_app = this; g_app = this;
m_appName = appName; m_appName = appName;

View File

@ -116,8 +116,11 @@ std::string ResourceManager::loadFile(const std::string& fileName)
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)
return false; {
logError(PHYSFS_getLastError());
return false;
}
PHYSFS_write(file, (void*)data, size, 1); PHYSFS_write(file, (void*)data, size, 1);
PHYSFS_close(file); PHYSFS_close(file);

View File

@ -24,7 +24,8 @@
#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 <physfs.h>
SpriteManager g_sprites; SpriteManager g_sprites;
@ -96,7 +97,7 @@ TexturePtr SpriteManager::loadSpriteTexture(int id)
uint16 pixelDataSize = m_spritesFile->getU16(); uint16 pixelDataSize = m_spritesFile->getU16();
static std::vector<uint8> pixels(4096); static std::vector<uint8> pixels(SPRITE_SIZE);
int writePos = 0; int writePos = 0;
int read = 0; int read = 0;
@ -105,7 +106,7 @@ TexturePtr SpriteManager::loadSpriteTexture(int id)
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 >= 4096) 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++) {
@ -129,7 +130,7 @@ TexturePtr SpriteManager::loadSpriteTexture(int id)
} }
// fill remaining pixels with alpha // fill remaining pixels with alpha
while(writePos < 4096) { 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;
@ -161,4 +162,121 @@ TexturePtr& SpriteManager::getSpriteTexture(int 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()
{
for(volatile int i = 1; i <= m_spritesCount; i++) {
m_spritesFile->seek(((i-1) * 4) + 6);
uint32 spriteAddress = m_spritesFile->getU32();
if(spriteAddress == 0)
continue;
m_spritesFile->seek(spriteAddress);
// skip color key
m_spritesFile->getU8();
m_spritesFile->getU8();
m_spritesFile->getU8();
uint16 pixelDataSize = m_spritesFile->getU16();
uchar pixels[SPRITE_SIZE];
int writePos = 0;
int read = 0;
// decompress pixels
while(read < pixelDataSize) {
uint16 transparentPixels = m_spritesFile->getU16();
uint16 coloredPixels = m_spritesFile->getU16();
if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
return false; // skip the whole process or just ignore this sprite?
for(int i = 0; i < transparentPixels; i++) {
pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00;
writePos += 4;
}
for(int i = 0; i < coloredPixels; i++) {
pixels[writePos + 0] = m_spritesFile->getU8();
pixels[writePos + 1] = m_spritesFile->getU8();
pixels[writePos + 2] = m_spritesFile->getU8();
pixels[writePos + 3] = 0xFF;
writePos += 4;
}
read += 4 + (3 * coloredPixels);
}
// fill remaining pixels with alpha
while(writePos < SPRITE_SIZE) {
pixels[writePos + 0] = 0x00;
pixels[writePos + 1] = 0x00;
pixels[writePos + 2] = 0x00;
pixels[writePos + 3] = 0x00;
writePos += 4;
}
// We should get the OTClient and export to that folder...
std::string fileName = Fw::formatString("%s/otclient/sprites/%d.png", PHYSFS_getUserDir(), i);
FILE *pFile = fopen(fileName.c_str(), "wb");
if(!pFile)
return false;
png_structp pPng = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!pPng) {
fclose(pFile);
return false;
}
png_infop pPngInfo = png_create_info_struct(pPng);
if(!pPngInfo) {
fclose(pFile);
png_destroy_write_struct(&pPng, NULL);
return false;
}
if(setjmp(png_jmpbuf(pPng))) {
fclose(pFile);
png_destroy_write_struct(&pPng, &pPngInfo);
return false;
}
png_init_io(pPng, pFile);
png_set_compression_level(pPng, PNG_COMPRESSION);
int bitDepthPerChannel = SPRITE_CHANNELS/4*8;
int colorType = PNG_COLOR_TYPE_RGB_ALPHA;
png_set_IHDR(pPng, pPngInfo, SPRITE_WIDTH, SPRITE_HEIGHT, bitDepthPerChannel, colorType,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
png_write_info(pPng, pPngInfo);
if(bitDepthPerChannel == 16) // Reverse endian order (PNG is Big Endian)
png_set_swap(pPng);
int bytesPerRow = SPRITE_WIDTH*SPRITE_CHANNELS;
uint8* pImgData = pixels;
for(int row=0; row < SPRITE_HEIGHT; row++) { // Write non-interlaced buffer
png_write_row(pPng, pImgData);
pImgData += bytesPerRow;
}
png_write_end(pPng, NULL);
png_destroy_write_struct(&pPng, &pPngInfo);
fclose(pFile);
}
return true;
} }

View File

@ -24,7 +24,22 @@
#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>
#define SPRITE_WIDTH 32
#define SPRITE_HEIGHT 32
#define SPRITE_CHANNELS 4
#define SPRITE_SIZE SPRITE_WIDTH*SPRITE_HEIGHT*SPRITE_CHANNELS
/*
Compression levels:
None = 0
Fastest = 1
Best = 9
*/
#define PNG_COMPRESSION 6
class SpriteManager class SpriteManager
{ {
@ -36,7 +51,9 @@ public:
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();
TexturePtr& getSpriteTexture(int id); TexturePtr& getSpriteTexture(int id);
bool isLoaded() { return m_loaded; } bool isLoaded() { return m_loaded; }

View File

@ -44,7 +44,6 @@
#include <otclient/ui/uimap.h> #include <otclient/ui/uimap.h>
#include <otclient/core/outfit.h> #include <otclient/core/outfit.h>
void OTClient::registerLuaFunctions() void OTClient::registerLuaFunctions()
{ {
Application::registerLuaFunctions(); Application::registerLuaFunctions();
@ -58,7 +57,8 @@ 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.registerStaticClass("g_map"); g_lua.registerStaticClass("g_map");
g_lua.bindClassStaticFunction("g_map", "isLookPossible", std::bind(&Map::isLookPossible, &g_map, std::placeholders::_1)); g_lua.bindClassStaticFunction("g_map", "isLookPossible", std::bind(&Map::isLookPossible, &g_map, std::placeholders::_1));