tibia-client/src/otclient/core/spritemanager.cpp

137 lines
4.0 KiB
C++
Raw Normal View History

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
{
try {
2011-08-20 22:30:41 +02:00
g_resources.loadFile(file, m_fin);
m_signature = Fw::getU32(m_fin);
m_spritesCount = Fw::getU16(m_fin);
m_sprites.resize(m_spritesCount);
return true;
} catch(Exception& e) {
logError("Failed to load sprites from '", file, "': ", e.what());
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);
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
Fw::getU8(m_fin);
Fw::getU8(m_fin);
Fw::getU8(m_fin);
2011-08-15 23:02:52 +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);
2011-11-30 20:26:42 +01:00
if(writePos + transparentPixels*4 + coloredPixels*3 >= 4096)
return g_graphics.getEmptyTexture();
2011-08-15 23:02:52 +02:00
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] = 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-12-08 18:28:29 +01:00
TexturePtr SpriteManager::getSpriteTexture(int id)
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-12-08 18:28:29 +01:00
TexturePtr& sprite = m_sprites[id-1];
if(!sprite)
sprite = loadSpriteTexture(id);
2011-08-15 23:02:52 +02:00
//TODO: release unused sprites textures after X seconds
// to avoid massive texture allocations
2011-12-08 18:28:29 +01:00
return sprite;
2011-08-19 14:26:26 +02:00
}