2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2012-01-02 17:58:37 +01:00
|
|
|
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
|
2011-08-28 15:17:58 +02:00
|
|
|
*
|
|
|
|
* 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>
|
2012-02-02 15:07:02 +01:00
|
|
|
#include <framework/core/eventdispatcher.h>
|
2012-04-02 17:51:03 +02:00
|
|
|
#include <framework/core/filestream.h>
|
2012-04-29 01:31:18 +02:00
|
|
|
#include <framework/graphics/graphics.h>
|
|
|
|
#include <physfs.h>
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
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 {
|
2012-04-02 17:51:03 +02:00
|
|
|
m_spritesFile = g_resources.openFile(file);
|
|
|
|
if(!m_spritesFile)
|
|
|
|
return false;
|
|
|
|
|
2012-04-24 23:05:46 +02:00
|
|
|
// cache file buffer to avoid lags from hard drive
|
|
|
|
m_spritesFile->cache();
|
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
m_signature = m_spritesFile->getU32();
|
|
|
|
m_spritesCount = m_spritesFile->getU16();
|
2011-08-19 20:53:23 +02:00
|
|
|
m_sprites.resize(m_spritesCount);
|
2012-01-05 02:28:29 +01:00
|
|
|
m_loaded = true;
|
2011-08-19 20:53:23 +02:00
|
|
|
return true;
|
2011-12-01 23:25:32 +01:00
|
|
|
} catch(Exception& e) {
|
|
|
|
logError("Failed 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;
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:07:02 +01:00
|
|
|
void SpriteManager::preloadSprites()
|
|
|
|
{
|
2012-02-20 03:27:08 +01:00
|
|
|
// preload every 50 sprites, periodically
|
2012-02-02 15:07:02 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
for(int i=1;i<=m_spritesCount;i+=burst)
|
2012-03-14 19:45:15 +01:00
|
|
|
g_eventDispatcher.scheduleEvent(std::bind(preload, i), (i/burst) * interval);
|
2012-02-02 15:07:02 +01:00
|
|
|
}
|
|
|
|
|
2011-08-19 16:18:24 +02:00
|
|
|
TexturePtr SpriteManager::loadSpriteTexture(int id)
|
2011-08-15 23:02:52 +02:00
|
|
|
{
|
2012-04-02 17:51:03 +02:00
|
|
|
m_spritesFile->seek(((id-1) * 4) + 6);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
uint32 spriteAddress = m_spritesFile->getU32();
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
// no sprite? return an empty texture
|
|
|
|
if(spriteAddress == 0)
|
|
|
|
return g_graphics.getEmptyTexture();
|
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
m_spritesFile->seek(spriteAddress);
|
2011-08-15 23:02:52 +02:00
|
|
|
|
|
|
|
// skip color key
|
2012-04-02 17:51:03 +02:00
|
|
|
m_spritesFile->getU8();
|
|
|
|
m_spritesFile->getU8();
|
|
|
|
m_spritesFile->getU8();
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2012-04-02 17:51:03 +02:00
|
|
|
uint16 pixelDataSize = m_spritesFile->getU16();
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2012-04-29 01:31:18 +02:00
|
|
|
static std::vector<uint8> pixels(SPRITE_SIZE);
|
2011-08-15 23:02:52 +02:00
|
|
|
int writePos = 0;
|
|
|
|
int read = 0;
|
|
|
|
|
|
|
|
// decompress pixels
|
|
|
|
while(read < pixelDataSize) {
|
2012-04-02 17:51:03 +02:00
|
|
|
uint16 transparentPixels = m_spritesFile->getU16();
|
|
|
|
uint16 coloredPixels = m_spritesFile->getU16();
|
2011-08-15 23:02:52 +02:00
|
|
|
|
2012-04-29 01:31:18 +02:00
|
|
|
if(writePos + transparentPixels*4 + coloredPixels*3 >= SPRITE_SIZE)
|
2011-11-30 20:26:42 +01:00
|
|
|
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++) {
|
2012-04-02 17:51:03 +02:00
|
|
|
pixels[writePos + 0] = m_spritesFile->getU8();
|
|
|
|
pixels[writePos + 1] = m_spritesFile->getU8();
|
|
|
|
pixels[writePos + 2] = m_spritesFile->getU8();
|
2011-08-15 23:02:52 +02:00
|
|
|
pixels[writePos + 3] = 0xFF;
|
|
|
|
|
|
|
|
writePos += 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
read += 4 + (3 * coloredPixels);
|
|
|
|
}
|
|
|
|
|
|
|
|
// fill remaining pixels with alpha
|
2012-04-29 01:31:18 +02:00
|
|
|
while(writePos < SPRITE_SIZE) {
|
2011-08-15 23:02:52 +02:00
|
|
|
pixels[writePos + 0] = 0x00;
|
|
|
|
pixels[writePos + 1] = 0x00;
|
|
|
|
pixels[writePos + 2] = 0x00;
|
|
|
|
pixels[writePos + 3] = 0x00;
|
|
|
|
writePos += 4;
|
|
|
|
}
|
|
|
|
|
2012-02-02 15:07:02 +01:00
|
|
|
TexturePtr spriteTex(new Texture(32, 32, 4, &pixels[0]));
|
2012-01-30 01:00:12 +01:00
|
|
|
spriteTex->setSmooth(true);
|
2012-03-21 13:41:43 +01:00
|
|
|
|
2012-04-19 01:03:43 +02:00
|
|
|
if(g_graphics.canUseMipmaps())
|
2012-03-21 13:41:43 +01:00
|
|
|
spriteTex->generateSoftwareMipmaps(pixels);
|
|
|
|
|
2012-01-30 01:00:12 +01:00
|
|
|
return spriteTex;
|
2011-08-15 23:02:52 +02:00
|
|
|
}
|
|
|
|
|
2012-02-20 03:27:08 +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;
|
2012-04-29 01:31:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2011-08-19 14:26:26 +02:00
|
|
|
}
|