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