2011-08-28 15:17:58 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2010-2011 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.
|
|
|
|
*/
|
|
|
|
|
2011-08-15 23:02:52 +02:00
|
|
|
#include "spritemanager.h"
|
|
|
|
#include <framework/core/resourcemanager.h>
|
|
|
|
#include <framework/graphics/graphics.h>
|
|
|
|
|
|
|
|
SpriteManager g_sprites;
|
|
|
|
|
|
|
|
SpriteManager::SpriteManager()
|
|
|
|
{
|
|
|
|
m_spritesCount = 0;
|
|
|
|
m_signature = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-20 22:30:41 +02:00
|
|
|
bool SpriteManager::load(const std::string& file)
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
2011-08-19 20:53:23 +02:00
|
|
|
try {
|
2011-08-20 22:30:41 +02:00
|
|
|
g_resources.loadFile(file, m_fin);
|
2011-08-28 18:02:26 +02:00
|
|
|
m_signature = Fw::getU32(m_fin);
|
|
|
|
m_spritesCount = Fw::getU16(m_fin);
|
2011-08-19 20:53:23 +02:00
|
|
|
m_sprites.resize(m_spritesCount);
|
|
|
|
return true;
|
|
|
|
} catch(std::exception& e) {
|
2011-08-20 22:30:41 +02:00
|
|
|
logError("faile to load sprites from '", file, "': ", e.what());
|
2011-08-19 20:53:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
2011-08-15 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void SpriteManager::unload()
|
|
|
|
{
|
|
|
|
m_sprites.clear();
|
|
|
|
m_spritesCount = 0;
|
|
|
|
m_signature = 0;
|
|
|
|
}
|
|
|
|
|
2011-08-19 16:18:24 +02:00
|
|
|
TexturePtr SpriteManager::loadSpriteTexture(int id)
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
|
|
|
m_fin.seekg(((id-1) * 4) + 6, std::ios_base::beg);
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
uint32 spriteAddress = Fw::getU32(m_fin);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
// no sprite? return an empty texture
|
|
|
|
if(spriteAddress == 0)
|
|
|
|
return g_graphics.getEmptyTexture();
|
|
|
|
|
|
|
|
m_fin.seekg(spriteAddress, std::ios_base::beg);
|
|
|
|
assert(m_fin.good());
|
|
|
|
|
|
|
|
// skip color key
|
2011-08-28 18:02:26 +02:00
|
|
|
Fw::getU8(m_fin);
|
|
|
|
Fw::getU8(m_fin);
|
|
|
|
Fw::getU8(m_fin);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
uint16 pixelDataSize = Fw::getU16(m_fin);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
uchar pixels[4096];
|
|
|
|
int writePos = 0;
|
|
|
|
int read = 0;
|
|
|
|
|
|
|
|
// decompress pixels
|
|
|
|
while(read < pixelDataSize) {
|
|
|
|
uint16 transparentPixels, coloredPixels;
|
|
|
|
m_fin.read((char*)&transparentPixels, 2);
|
|
|
|
m_fin.read((char*)&coloredPixels, 2);
|
|
|
|
|
|
|
|
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++) {
|
2011-08-28 18:02:26 +02:00
|
|
|
pixels[writePos + 0] = Fw::getU8(m_fin);
|
|
|
|
pixels[writePos + 1] = Fw::getU8(m_fin);
|
|
|
|
pixels[writePos + 2] = Fw::getU8(m_fin);
|
2011-08-15 23:02:52 +02:00
|
|
|
pixels[writePos + 3] = 0xFF;
|
|
|
|
|
|
|
|
writePos += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
read += 4 + (3 * coloredPixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill remaining pixels with alpha
|
|
|
|
while(writePos < 4096) {
|
|
|
|
pixels[writePos + 0] = 0x00;
|
|
|
|
pixels[writePos + 1] = 0x00;
|
|
|
|
pixels[writePos + 2] = 0x00;
|
|
|
|
pixels[writePos + 3] = 0x00;
|
|
|
|
writePos += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TexturePtr(new Texture(32, 32, 4, pixels));
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
TexturePtr SpriteManager::loadSpriteMask(TexturePtr spriteTex, Otc::SpriteMask mask)
|
2011-08-19 16:23:02 +02:00
|
|
|
{
|
|
|
|
auto pixels = spriteTex->getPixels();
|
|
|
|
|
2011-08-28 19:49:15 +02:00
|
|
|
static uint32 maskColors[4] = { Fw::red, Fw::green, Fw::blue, Fw::yellow };
|
|
|
|
uint32 maskColor = maskColors[mask];
|
|
|
|
uint32 whiteColor = Fw::white;
|
|
|
|
uint32 alphaColor = Fw::alpha;
|
2011-08-19 16:23:02 +02:00
|
|
|
|
|
|
|
// convert pixels
|
|
|
|
// masked color -> white color
|
|
|
|
// any other color -> alpha color
|
|
|
|
for(int i=0;i<4096;i+=4) {
|
2011-08-28 19:49:15 +02:00
|
|
|
uint32& currentColor = *(uint32*)&pixels[i];
|
2011-08-19 16:23:02 +02:00
|
|
|
if(currentColor == maskColor)
|
|
|
|
currentColor = whiteColor;
|
|
|
|
else
|
|
|
|
currentColor = alphaColor;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TexturePtr(new Texture(32, 32, 4, &pixels[0]));
|
|
|
|
}
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
TexturePtr SpriteManager::getSpriteTexture(int id, Otc::SpriteMask mask)
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
|
|
|
if(id == 0)
|
|
|
|
return g_graphics.getEmptyTexture();
|
|
|
|
|
2011-08-16 15:25:21 +02:00
|
|
|
assert(id > 0 && id <= m_spritesCount);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
// load sprites on demand
|
2011-08-19 16:18:24 +02:00
|
|
|
Sprite& sprite = m_sprites[id-1];
|
|
|
|
if(!sprite.texture)
|
|
|
|
sprite.texture = loadSpriteTexture(id);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
//TODO: release unused sprites textures after X seconds
|
|
|
|
// to avoid massive texture allocations
|
|
|
|
|
2011-08-28 18:02:26 +02:00
|
|
|
if(mask != Otc::SpriteNoMask) {
|
2011-08-19 16:23:02 +02:00
|
|
|
if(!sprite.masks[mask])
|
|
|
|
sprite.masks[mask] = loadSpriteMask(sprite.texture, mask);
|
2011-08-19 16:18:24 +02:00
|
|
|
return sprite.masks[mask];
|
|
|
|
} else
|
|
|
|
return sprite.texture;
|
2011-08-19 14:26:26 +02:00
|
|
|
}
|