2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2011-02-20 11:24:53 +02:00
|
|
|
#include <SDL_image.h>
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2012-08-01 15:02:54 +03:00
|
|
|
#include "../lib/Filesystem/CResourceLoader.h"
|
2012-08-06 10:34:37 +03:00
|
|
|
#include "../lib/Filesystem/ISimpleResourceLoader.h"
|
2011-07-21 21:29:22 +03:00
|
|
|
#include "../lib/JsonNode.h"
|
2011-10-08 04:23:46 +03:00
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
#include "CBitmapHandler.h"
|
2011-02-06 19:26:27 +02:00
|
|
|
#include "Graphics.h"
|
2010-10-18 18:08:59 +03:00
|
|
|
#include "CAnimation.h"
|
2011-12-22 16:05:19 +03:00
|
|
|
#include "UIFramework/SDL_Extensions.h"
|
2012-05-17 13:44:48 +03:00
|
|
|
#include "UIFramework/SDL_Pixels.h"
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* CAnimation.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2011-07-21 21:29:22 +03:00
|
|
|
typedef std::map <size_t, std::vector <JsonNode> > source_map;
|
2011-02-06 19:26:27 +02:00
|
|
|
typedef std::map<size_t, IImage* > image_map;
|
|
|
|
typedef std::map<size_t, image_map > group_map;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
class SDLImageLoader
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
SDLImage * image;
|
|
|
|
ui8 * lineStart;
|
|
|
|
ui8 * position;
|
|
|
|
public:
|
|
|
|
//load size raw pixels from data
|
|
|
|
inline void Load(size_t size, const ui8 * data);
|
|
|
|
//set size pixels to color
|
|
|
|
inline void Load(size_t size, ui8 color=0);
|
|
|
|
inline void EndLine();
|
|
|
|
//init image with these sizes and palette
|
2011-12-22 16:05:19 +03:00
|
|
|
inline void init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal);
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
SDLImageLoader(SDLImage * Img);
|
|
|
|
~SDLImageLoader();
|
|
|
|
};
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
class CompImageLoader
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
CompImage * image;
|
|
|
|
ui8 *position;
|
|
|
|
ui8 *entry;
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 currentLine;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
inline ui8 typeOf(ui8 color);
|
|
|
|
inline void NewEntry(ui8 color, size_t size);
|
|
|
|
inline void NewEntry(const ui8 * &data, size_t size);
|
|
|
|
|
|
|
|
public:
|
|
|
|
//load size raw pixels from data
|
|
|
|
inline void Load(size_t size, const ui8 * data);
|
|
|
|
//set size pixels to color
|
|
|
|
inline void Load(size_t size, ui8 color=0);
|
|
|
|
inline void EndLine();
|
|
|
|
//init image with these sizes and palette
|
2011-12-22 16:05:19 +03:00
|
|
|
inline void init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal);
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
CompImageLoader(CompImage * Img);
|
|
|
|
~CompImageLoader();
|
|
|
|
};
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2012-12-14 22:47:38 +03:00
|
|
|
// Extremely simple file cache. TODO: smarter, more general solution
|
|
|
|
class CFileCache
|
|
|
|
{
|
|
|
|
static const int cacheSize = 50; //Max number of cached files
|
|
|
|
struct FileData
|
|
|
|
{
|
|
|
|
ResourceID name;
|
|
|
|
size_t size;
|
|
|
|
ui8 * data;
|
|
|
|
|
|
|
|
ui8 * getCopy()
|
|
|
|
{
|
|
|
|
ui8 * ret = new ui8[size];
|
|
|
|
std::copy(data, data + size, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
2012-12-15 16:40:22 +03:00
|
|
|
FileData():
|
|
|
|
size(0),
|
|
|
|
data(nullptr)
|
|
|
|
{}
|
2012-12-14 22:47:38 +03:00
|
|
|
~FileData()
|
|
|
|
{
|
2012-12-15 16:40:22 +03:00
|
|
|
delete [] data;
|
2012-12-14 22:47:38 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
std::list<FileData> cache;
|
|
|
|
public:
|
|
|
|
ui8 * getCachedFile(ResourceID && rid)
|
|
|
|
{
|
|
|
|
BOOST_FOREACH(auto & file, cache)
|
|
|
|
{
|
|
|
|
if (file.name == rid)
|
|
|
|
return file.getCopy();
|
|
|
|
}
|
|
|
|
// Still here? Cache miss
|
|
|
|
if (cache.size() > cacheSize)
|
|
|
|
cache.pop_front();
|
|
|
|
cache.push_back(FileData());
|
|
|
|
|
|
|
|
auto data = CResourceHandler::get()->loadData(rid);
|
|
|
|
cache.back().name = ResourceID(rid);
|
|
|
|
cache.back().size = data.second;
|
|
|
|
cache.back().data = data.first.release();
|
|
|
|
|
|
|
|
return cache.back().getCopy();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static CFileCache animationCache;
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
/*************************************************************************
|
|
|
|
* DefFile, class used for def loading *
|
|
|
|
*************************************************************************/
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
CDefFile::CDefFile(std::string Name):
|
|
|
|
data(NULL),
|
|
|
|
palette(NULL)
|
|
|
|
{
|
|
|
|
//First 8 colors in def palette used for transparency
|
|
|
|
static SDL_Color H3Palette[8] =
|
|
|
|
{
|
|
|
|
{ 0, 0, 0, 0},// 100% - transparency
|
2013-02-05 22:56:28 +03:00
|
|
|
{ 0, 0, 0, 64},// 75% - shadow border,
|
2011-02-06 19:26:27 +02:00
|
|
|
{ 0, 0, 0, 128},// TODO: find exact value
|
|
|
|
{ 0, 0, 0, 128},// TODO: for transparency
|
|
|
|
{ 0, 0, 0, 128},// 50% - shadow body
|
|
|
|
{ 0, 0, 0, 0},// 100% - selection highlight
|
|
|
|
{ 0, 0, 0, 128},// 50% - shadow body below selection
|
2013-02-05 22:56:28 +03:00
|
|
|
{ 0, 0, 0, 64} // 75% - shadow border below selection
|
2011-02-06 19:26:27 +02:00
|
|
|
};
|
2012-12-14 22:47:38 +03:00
|
|
|
data = animationCache.getCachedFile(ResourceID(std::string("SPRITES/") + Name, EResType::ANIMATION));
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
palette = new SDL_Color[256];
|
|
|
|
int it = 0;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 type = read_le_u32(data + it);
|
2011-02-06 19:26:27 +02:00
|
|
|
it+=4;
|
2011-10-08 04:23:46 +03:00
|
|
|
//int width = read_le_u32(data + it); it+=4;//not used
|
|
|
|
//int height = read_le_u32(data + it); it+=4;
|
2011-02-06 19:26:27 +02:00
|
|
|
it+=8;
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 totalBlocks = read_le_u32(data + it);
|
2011-02-06 19:26:27 +02:00
|
|
|
it+=4;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i= 0; i<256; i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
palette[i].r = data[it++];
|
|
|
|
palette[i].g = data[it++];
|
|
|
|
palette[i].b = data[it++];
|
|
|
|
palette[i].unused = 255;
|
|
|
|
}
|
2012-12-14 22:47:38 +03:00
|
|
|
if (type == 71 || type == 64)//Buttons/buildings don't have shadows\semi-transparency
|
|
|
|
memset(palette, 0, sizeof(SDL_Color)*2);
|
2011-07-27 12:35:27 +03:00
|
|
|
else
|
|
|
|
memcpy(palette, H3Palette, sizeof(SDL_Color)*8);//initialize shadow\selection colors
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i=0; i<totalBlocks; i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2011-10-08 04:23:46 +03:00
|
|
|
size_t blockID = read_le_u32(data + it);
|
2011-02-06 19:26:27 +02:00
|
|
|
it+=4;
|
2011-10-08 04:23:46 +03:00
|
|
|
size_t totalEntries = read_le_u32(data + it);
|
2011-02-06 19:26:27 +02:00
|
|
|
it+=12;
|
|
|
|
//8 unknown bytes - skipping
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//13 bytes for name of every frame in this block - not used, skipping
|
|
|
|
it+= 13 * totalEntries;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 j=0; j<totalEntries; j++)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2011-10-08 04:23:46 +03:00
|
|
|
size_t currOffset = read_le_u32(data + it);
|
2011-02-06 19:26:27 +02:00
|
|
|
offset[blockID].push_back(currOffset);
|
|
|
|
it += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
template<class ImageLoader>
|
|
|
|
void CDefFile::loadFrame(size_t frame, size_t group, ImageLoader &loader) const
|
|
|
|
{
|
|
|
|
std::map<size_t, std::vector <size_t> >::const_iterator it;
|
|
|
|
it = offset.find(group);
|
|
|
|
assert (it != offset.end());
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
const ui8 * FDef = data+it->second[frame];
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
const SSpriteDef sd = * reinterpret_cast<const SSpriteDef *>(FDef);
|
|
|
|
SSpriteDef sprite;
|
|
|
|
|
2013-02-05 22:56:28 +03:00
|
|
|
sprite.format = read_le_u32(&sd.format);
|
|
|
|
sprite.fullWidth = read_le_u32(&sd.fullWidth);
|
|
|
|
sprite.fullHeight = read_le_u32(&sd.fullHeight);
|
|
|
|
sprite.width = read_le_u32(&sd.width);
|
|
|
|
sprite.height = read_le_u32(&sd.height);
|
|
|
|
sprite.leftMargin = read_le_u32(&sd.leftMargin);
|
|
|
|
sprite.topMargin = read_le_u32(&sd.topMargin);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 currentOffset = sizeof(SSpriteDef);
|
|
|
|
ui32 BaseOffset = sizeof(SSpriteDef);
|
2010-11-18 15:34:21 +02:00
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
loader.init(Point(sprite.width, sprite.height),
|
|
|
|
Point(sprite.leftMargin, sprite.topMargin),
|
|
|
|
Point(sprite.fullWidth, sprite.fullHeight), palette);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
switch (sprite.format)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
//pixel data is not compressed, copy data to surface
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i=0; i<sprite.height; i++)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(sprite.width, FDef[currentOffset]);
|
|
|
|
currentOffset += sprite.width;
|
|
|
|
loader.EndLine();
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
break;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
case 1:
|
|
|
|
{
|
2010-11-18 15:34:21 +02:00
|
|
|
//for each line we have offset of pixel data
|
2011-02-09 14:33:58 +02:00
|
|
|
const ui32 * RWEntriesLoc = reinterpret_cast<const ui32 *>(FDef+currentOffset);
|
2011-02-06 19:26:27 +02:00
|
|
|
currentOffset += sizeof(ui32) * sprite.height;
|
2010-11-18 15:34:21 +02:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i=0; i<sprite.height; i++)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
//get position of the line
|
2011-10-08 04:23:46 +03:00
|
|
|
currentOffset=BaseOffset + read_le_u32(RWEntriesLoc + i);
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 TotalRowLength = 0;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
while (TotalRowLength<sprite.width)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 type=FDef[currentOffset++];
|
|
|
|
ui32 length=FDef[currentOffset++] + 1;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
if (type==0xFF)//Raw data
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(length, FDef + currentOffset);
|
|
|
|
currentOffset+=length;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
else// RLE
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(length, type);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
TotalRowLength += length;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.EndLine();
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
break;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
case 2:
|
|
|
|
{
|
2011-10-08 04:23:46 +03:00
|
|
|
currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i=0; i<sprite.height; i++)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 TotalRowLength=0;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
while (TotalRowLength<sprite.width)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 SegmentType=FDef[currentOffset++];
|
|
|
|
ui8 code = SegmentType / 32;
|
|
|
|
ui8 length = (SegmentType & 31) + 1;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
if (code==7)//Raw data
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(length, FDef[currentOffset]);
|
|
|
|
currentOffset += length;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
else//RLE
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(length, code);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
TotalRowLength+=length;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.EndLine();
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
break;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
case 3:
|
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
for (ui32 i=0; i<sprite.height; i++)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-10-08 04:23:46 +03:00
|
|
|
currentOffset = BaseOffset + read_le_u16(FDef + BaseOffset+i*2*(sprite.width/32));
|
2011-12-14 00:23:17 +03:00
|
|
|
ui32 TotalRowLength=0;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
while (TotalRowLength<sprite.width)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 segment = FDef[currentOffset++];
|
|
|
|
ui8 code = segment / 32;
|
|
|
|
ui8 length = (segment & 31) + 1;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
if (code==7)//Raw data
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(length, FDef + currentOffset);
|
|
|
|
currentOffset += length;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
else//RLE
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.Load(length, code);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
TotalRowLength += length;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
loader.EndLine();
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
break;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
default:
|
2012-02-14 21:04:45 +03:00
|
|
|
tlog0<<"Error: unsupported format of def file: "<<sprite.format<<"\n";
|
2010-10-18 18:08:59 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
CDefFile::~CDefFile()
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
delete[] data;
|
|
|
|
delete[] palette;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
const std::map<size_t, size_t > CDefFile::getEntries() const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
std::map<size_t, size_t > ret;
|
|
|
|
|
|
|
|
for (std::map<size_t, std::vector <size_t> >::const_iterator mapIt = offset.begin(); mapIt!=offset.end(); ++mapIt)
|
|
|
|
ret[mapIt->first] = mapIt->second.size();
|
|
|
|
return ret;
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Classes for image loaders - helpers for loading from def files *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
SDLImageLoader::SDLImageLoader(SDLImage * Img):
|
|
|
|
image(Img),
|
|
|
|
lineStart(NULL),
|
|
|
|
position(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void SDLImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
//Init image
|
|
|
|
image->surf = SDL_CreateRGBSurface(SDL_SWSURFACE, SpriteSize.x, SpriteSize.y, 8, 0, 0, 0, 0);
|
|
|
|
image->margins = Margins;
|
|
|
|
image->fullSize = FullSize;
|
|
|
|
|
|
|
|
//Prepare surface
|
|
|
|
SDL_SetColors(image->surf, pal, 0, 256);
|
|
|
|
SDL_LockSurface(image->surf);
|
|
|
|
|
|
|
|
lineStart = position = (ui8*)image->surf->pixels;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void SDLImageLoader::Load(size_t size, const ui8 * data)
|
|
|
|
{
|
|
|
|
if (size)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
memcpy((void *)position, data, size);
|
|
|
|
position += size;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
inline void SDLImageLoader::Load(size_t size, ui8 color)
|
|
|
|
{
|
|
|
|
if (size)
|
|
|
|
{
|
|
|
|
memset((void *)position, color, size);
|
|
|
|
position += size;
|
|
|
|
}
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
inline void SDLImageLoader::EndLine()
|
|
|
|
{
|
|
|
|
lineStart += image->surf->pitch;
|
|
|
|
position = lineStart;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
SDLImageLoader::~SDLImageLoader()
|
|
|
|
{
|
|
|
|
SDL_UnlockSurface(image->surf);
|
|
|
|
SDL_SetColorKey(image->surf, SDL_SRCCOLORKEY, 0);
|
|
|
|
//TODO: RLE if compressed and bpp>1
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
CompImageLoader::CompImageLoader(CompImage * Img):
|
|
|
|
image(Img),
|
|
|
|
position(NULL),
|
|
|
|
entry(NULL),
|
|
|
|
currentLine(0)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void CompImageLoader::init(Point SpriteSize, Point Margins, Point FullSize, SDL_Color *pal)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2011-12-22 16:05:19 +03:00
|
|
|
image->sprite = Rect(Margins, SpriteSize);
|
2011-02-06 19:26:27 +02:00
|
|
|
image->fullSize = FullSize;
|
|
|
|
if (SpriteSize.x && SpriteSize.y)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
image->palette = new SDL_Color[256];
|
|
|
|
memcpy((void*)image->palette, (void*)pal, 256*sizeof(SDL_Color));
|
|
|
|
//Allocate enought space for worst possible case, c-style malloc used due to resizing after load
|
|
|
|
image->surf = (ui8*)malloc(SpriteSize.x*SpriteSize.y*3);
|
2011-12-14 00:23:17 +03:00
|
|
|
image->line = new ui32[SpriteSize.y+1];
|
2011-02-06 19:26:27 +02:00
|
|
|
image->line[0] = 0;
|
|
|
|
position = image->surf;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
inline void CompImageLoader::NewEntry(ui8 color, size_t size)
|
|
|
|
{
|
|
|
|
assert(color != 0xff);
|
|
|
|
assert(size && size<256);
|
|
|
|
entry = position;
|
|
|
|
entry[0] = color;
|
|
|
|
entry[1] = size;
|
|
|
|
position +=2;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
inline void CompImageLoader::NewEntry(const ui8 * &data, size_t size)
|
|
|
|
{
|
|
|
|
assert(size && size<256);
|
|
|
|
entry = position;
|
|
|
|
entry[0] = 0xff;
|
|
|
|
entry[1] = size;
|
|
|
|
position +=2;
|
|
|
|
memcpy(position, data, size);
|
|
|
|
position+=size;
|
|
|
|
data+=size;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
inline ui8 CompImageLoader::typeOf(ui8 color)
|
|
|
|
{
|
|
|
|
if (color == 0)
|
|
|
|
return 0;
|
|
|
|
if (image->palette[color].unused != 255)
|
|
|
|
return 1;
|
|
|
|
return 2;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
inline void CompImageLoader::Load(size_t size, const ui8 * data)
|
|
|
|
{
|
|
|
|
while (size)
|
|
|
|
{
|
|
|
|
//Try to compress data
|
|
|
|
while(true)
|
|
|
|
{
|
|
|
|
ui8 color = data[0];
|
|
|
|
if (color != 0xff)
|
|
|
|
{
|
|
|
|
size_t runLength = 1;
|
|
|
|
while (runLength < size && color == data[runLength])
|
|
|
|
runLength++;
|
2010-11-15 17:15:00 +02:00
|
|
|
|
2012-12-14 22:47:38 +03:00
|
|
|
if (runLength > 1 && runLength < 255)//Row of one color found - use RLE
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
Load(runLength, color);
|
|
|
|
data += runLength;
|
|
|
|
size -= runLength;
|
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
//Select length for new raw entry
|
|
|
|
size_t runLength = 1;
|
|
|
|
ui8 color = data[0];
|
|
|
|
ui8 type = typeOf(color);
|
|
|
|
ui8 color2;
|
|
|
|
ui8 type2;
|
|
|
|
|
|
|
|
if (size > 1)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
do
|
|
|
|
{
|
|
|
|
color2 = data[runLength];
|
|
|
|
type2 = typeOf(color2);
|
|
|
|
runLength++;
|
|
|
|
}
|
|
|
|
//While we have data of this type and different colors
|
|
|
|
while ((runLength < size) && (type == type2) && ( (color2 != 0xff) || (color2 != color)));
|
|
|
|
}
|
|
|
|
size -= runLength;
|
|
|
|
|
|
|
|
//add data to last entry
|
|
|
|
if (entry && entry[0] == 0xff && type == typeOf(entry[2]))
|
|
|
|
{
|
|
|
|
size_t toCopy = std::min<size_t>(runLength, 255 - entry[1]);
|
|
|
|
runLength -= toCopy;
|
|
|
|
entry[1] += toCopy;
|
|
|
|
memcpy(position, data, toCopy);
|
|
|
|
data+=toCopy;
|
|
|
|
position+=toCopy;
|
|
|
|
}
|
|
|
|
//Create new entries
|
|
|
|
while (runLength > 255)
|
|
|
|
{
|
|
|
|
NewEntry(data, 255);
|
|
|
|
runLength -= 255;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
if (runLength)
|
|
|
|
NewEntry(data, runLength);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
inline void CompImageLoader::Load(size_t size, ui8 color)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (!size)
|
|
|
|
return;
|
|
|
|
if (color==0xff)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
ui8* tmpbuf = new ui8[size];
|
|
|
|
memset((void*)tmpbuf, color, size);
|
|
|
|
Load(size, tmpbuf);
|
|
|
|
delete [] tmpbuf;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
//Current entry is RLE with same color as new block
|
|
|
|
if (entry && entry[0] == color)
|
|
|
|
{
|
|
|
|
size_t toCopy = std::min<size_t>(size, 255 - entry[1]);
|
|
|
|
size -= toCopy;
|
|
|
|
entry[1] += toCopy;
|
|
|
|
}
|
|
|
|
//Create new entries
|
|
|
|
while (size > 255)
|
|
|
|
{
|
|
|
|
NewEntry(color, 255);
|
|
|
|
size -= 255;
|
|
|
|
}
|
|
|
|
if (size)
|
|
|
|
NewEntry(color, size);
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
void CompImageLoader::EndLine()
|
|
|
|
{
|
|
|
|
currentLine++;
|
|
|
|
image->line[currentLine] = position - image->surf;
|
|
|
|
entry = NULL;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
CompImageLoader::~CompImageLoader()
|
|
|
|
{
|
|
|
|
if (!image->surf)
|
|
|
|
return;
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
ui8* newPtr = (ui8*)realloc((void*)image->surf, position - image->surf);
|
|
|
|
if (newPtr)
|
|
|
|
image->surf = newPtr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* Classes for images, support loading from file and drawing on surface *
|
|
|
|
*************************************************************************/
|
|
|
|
|
|
|
|
IImage::IImage():
|
|
|
|
refCount(1)
|
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IImage::decreaseRef()
|
|
|
|
{
|
|
|
|
refCount--;
|
|
|
|
return refCount <= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IImage::increaseRef()
|
|
|
|
{
|
|
|
|
refCount++;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLImage::SDLImage(CDefFile *data, size_t frame, size_t group, bool compressed):
|
|
|
|
surf(NULL)
|
|
|
|
{
|
|
|
|
SDLImageLoader loader(this);
|
|
|
|
data->loadFrame(frame, group, loader);
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLImage::SDLImage(SDL_Surface * from, bool extraRef):
|
|
|
|
margins(0,0)
|
|
|
|
{
|
|
|
|
surf = from;
|
|
|
|
if (extraRef)
|
|
|
|
surf->refcount++;
|
|
|
|
fullSize.x = surf->w;
|
|
|
|
fullSize.y = surf->h;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLImage::SDLImage(std::string filename, bool compressed):
|
|
|
|
margins(0,0)
|
|
|
|
{
|
2011-07-18 18:21:16 +03:00
|
|
|
surf = BitmapHandler::loadBitmap(filename);
|
2011-05-28 13:05:25 +03:00
|
|
|
|
|
|
|
if (surf == NULL)
|
|
|
|
{
|
|
|
|
tlog1 << "Error: failed to load image "<<filename<<"\n";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
fullSize.x = surf->w;
|
|
|
|
fullSize.y = surf->h;
|
|
|
|
}
|
2011-07-21 21:29:22 +03:00
|
|
|
if (compressed)
|
|
|
|
{
|
|
|
|
SDL_Surface *temp = surf;
|
|
|
|
// add RLE flag
|
|
|
|
if (surf->format->palette)
|
|
|
|
{
|
|
|
|
const SDL_Color &c = temp->format->palette->colors[0];
|
|
|
|
SDL_SetColorKey(temp, (SDL_SRCCOLORKEY | SDL_RLEACCEL),
|
|
|
|
SDL_MapRGB(temp -> format, c.r, c.g, c.b));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
SDL_SetColorKey(temp, SDL_RLEACCEL, 0);
|
|
|
|
|
|
|
|
// convert surface to enable RLE
|
|
|
|
surf = SDL_ConvertSurface(temp, temp->format, temp->flags);
|
|
|
|
SDL_FreeSurface(temp);
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void SDLImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 rotation) const
|
2011-03-15 19:25:51 +02:00
|
|
|
{
|
|
|
|
if (!surf)
|
2011-02-20 11:24:53 +02:00
|
|
|
return;
|
2011-12-22 16:05:19 +03:00
|
|
|
Rect sourceRect(margins.x, margins.y, surf->w, surf->h);
|
2011-02-06 19:26:27 +02:00
|
|
|
//TODO: rotation and scaling
|
|
|
|
if (src)
|
|
|
|
{
|
|
|
|
sourceRect = sourceRect & *src;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-12-22 16:05:19 +03:00
|
|
|
Rect destRect(posX, posY, surf->w, surf->h);
|
2011-02-06 19:26:27 +02:00
|
|
|
destRect += sourceRect.topLeft();
|
|
|
|
sourceRect -= margins;
|
|
|
|
CSDL_Ext::blitSurface(surf, &sourceRect, where, &destRect);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
void SDLImage::playerColored(PlayerColor player)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
graphics->blueToPlayersAdv(surf, player);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
int SDLImage::width() const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
return fullSize.x;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
int SDLImage::height() const
|
|
|
|
{
|
|
|
|
return fullSize.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDLImage::~SDLImage()
|
|
|
|
{
|
|
|
|
SDL_FreeSurface(surf);
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
CompImage::CompImage(const CDefFile *data, size_t frame, size_t group):
|
2010-10-18 18:08:59 +03:00
|
|
|
surf(NULL),
|
2011-02-06 19:26:27 +02:00
|
|
|
line(NULL),
|
|
|
|
palette(NULL)
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
CompImageLoader loader(this);
|
|
|
|
data->loadFrame(frame, group, loader);
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
CompImage::CompImage(SDL_Surface * surf)
|
|
|
|
{
|
|
|
|
//TODO
|
|
|
|
assert(0);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void CompImage::draw(SDL_Surface *where, int posX, int posY, Rect *src, ui8 alpha) const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-03-22 15:19:07 +02:00
|
|
|
int rotation = 0; //TODO
|
2011-02-20 11:24:53 +02:00
|
|
|
//rotation & 2 = horizontal rotation
|
|
|
|
//rotation & 4 = vertical rotation
|
2011-02-06 19:26:27 +02:00
|
|
|
if (!surf)
|
|
|
|
return;
|
2011-12-22 16:05:19 +03:00
|
|
|
Rect sourceRect(sprite);
|
2011-02-06 19:26:27 +02:00
|
|
|
//TODO: rotation and scaling
|
|
|
|
if (src)
|
|
|
|
sourceRect = sourceRect & *src;
|
|
|
|
//Limit source rect to sizes of surface
|
2011-12-22 16:05:19 +03:00
|
|
|
sourceRect = sourceRect & Rect(0, 0, where->w, where->h);
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
//Starting point on SDL surface
|
2011-12-22 16:05:19 +03:00
|
|
|
Point dest(posX+sourceRect.x, posY+sourceRect.y);
|
2011-02-20 11:24:53 +02:00
|
|
|
if (rotation & 2)
|
|
|
|
dest.y += sourceRect.h;
|
|
|
|
if (rotation & 4)
|
|
|
|
dest.x += sourceRect.w;
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
sourceRect -= sprite.topLeft();
|
|
|
|
|
|
|
|
for (int currY = 0; currY <sourceRect.h; currY++)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
ui8* data = surf + line[currY+sourceRect.y];
|
|
|
|
ui8 type = *(data++);
|
|
|
|
ui8 size = *(data++);
|
|
|
|
int currX = sourceRect.x;
|
|
|
|
|
|
|
|
//Skip blocks until starting position reached
|
|
|
|
while ( currX > size )
|
|
|
|
{
|
|
|
|
currX -= size;
|
|
|
|
if (type == 0xff)
|
|
|
|
data += size;
|
|
|
|
type = *(data++);
|
|
|
|
size = *(data++);
|
|
|
|
}
|
|
|
|
//This block will be shown partially - calculate size\position
|
|
|
|
size -= currX;
|
|
|
|
if (type == 0xff)
|
|
|
|
data += currX;
|
|
|
|
|
|
|
|
currX = 0;
|
|
|
|
ui8 bpp = where->format->BytesPerPixel;
|
|
|
|
|
|
|
|
//Calculate position for blitting: pixels + Y + X
|
|
|
|
ui8* blitPos = (ui8*) where->pixels;
|
2011-02-20 11:24:53 +02:00
|
|
|
if (rotation & 4)
|
|
|
|
blitPos += (dest.y - currY) * where->pitch;
|
|
|
|
else
|
|
|
|
blitPos += (dest.y + currY) * where->pitch;
|
2011-02-06 19:26:27 +02:00
|
|
|
blitPos += dest.x * bpp;
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
//Blit blocks that must be fully visible
|
2011-02-06 19:26:27 +02:00
|
|
|
while (currX + size < sourceRect.w)
|
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
//blit block, pointers will be modified if needed
|
2011-03-22 15:19:07 +02:00
|
|
|
BlitBlockWithBpp(bpp, type, size, data, blitPos, alpha, rotation & 2);
|
2011-02-06 19:26:27 +02:00
|
|
|
|
|
|
|
currX += size;
|
|
|
|
type = *(data++);
|
|
|
|
size = *(data++);
|
|
|
|
}
|
|
|
|
//Blit last, semi-visible block
|
|
|
|
size = sourceRect.w - currX;
|
2011-03-22 15:19:07 +02:00
|
|
|
BlitBlockWithBpp(bpp, type, size, data, blitPos, alpha, rotation & 2);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
#define CASEBPP(x,y) case x: BlitBlock<x,y>(type, size, data, dest, alpha); break
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
//FIXME: better way to get blitter
|
2011-03-22 15:19:07 +02:00
|
|
|
void CompImage::BlitBlockWithBpp(ui8 bpp, ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha, bool rotated) const
|
2011-02-20 11:24:53 +02:00
|
|
|
{
|
|
|
|
assert(bpp>1 && bpp<5);
|
|
|
|
|
|
|
|
if (rotated)
|
|
|
|
switch (bpp)
|
|
|
|
{
|
|
|
|
CASEBPP(2,1);
|
|
|
|
CASEBPP(3,1);
|
|
|
|
CASEBPP(4,1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
switch (bpp)
|
|
|
|
{
|
|
|
|
CASEBPP(2,1);
|
|
|
|
CASEBPP(3,1);
|
|
|
|
CASEBPP(4,1);
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
2011-02-20 11:24:53 +02:00
|
|
|
#undef CASEBPP
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//Blit one block from RLE-d surface
|
2011-02-20 11:24:53 +02:00
|
|
|
template<int bpp, int dir>
|
2011-03-22 15:19:07 +02:00
|
|
|
void CompImage::BlitBlock(ui8 type, ui8 size, ui8 *&data, ui8 *&dest, ui8 alpha) const
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
//Raw data
|
|
|
|
if (type == 0xff)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
ui8 color = *data;
|
2011-03-22 15:19:07 +02:00
|
|
|
if (alpha != 255)//Per-surface alpha is set
|
|
|
|
{
|
|
|
|
for (size_t i=0; i<size; i++)
|
|
|
|
{
|
|
|
|
SDL_Color col = palette[*(data++)];
|
2012-05-28 22:29:32 +03:00
|
|
|
col.unused = (ui32)col.unused*alpha/255;
|
2011-03-22 15:19:07 +02:00
|
|
|
ColorPutter<bpp, 1>::PutColorAlpha(dest, col);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
if (palette[color].unused == 255)
|
|
|
|
{
|
|
|
|
//Put row of RGB data
|
2011-03-22 15:19:07 +02:00
|
|
|
for (size_t i=0; i<size; i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
ColorPutter<bpp, 1>::PutColor(dest, palette[*(data++)]);
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
else
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
//Put row of RGBA data
|
2011-03-22 15:19:07 +02:00
|
|
|
for (size_t i=0; i<size; i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
ColorPutter<bpp, 1>::PutColorAlpha(dest, palette[*(data++)]);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
//RLE-d sequence
|
|
|
|
else
|
|
|
|
{
|
2011-03-22 15:19:07 +02:00
|
|
|
if (alpha != 255 && palette[type].unused !=0)//Per-surface alpha is set
|
|
|
|
{
|
|
|
|
SDL_Color col = palette[type];
|
|
|
|
col.unused = (int)col.unused*(255-alpha)/255;
|
|
|
|
for (size_t i=0; i<size; i++)
|
|
|
|
ColorPutter<bpp, 1>::PutColorAlpha(dest, col);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
switch (palette[type].unused)
|
|
|
|
{
|
|
|
|
case 0:
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
//Skip row
|
|
|
|
dest += size*bpp;
|
|
|
|
break;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
case 255:
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
//Put RGB row
|
2011-02-20 11:24:53 +02:00
|
|
|
ColorPutter<bpp, 1>::PutColorRow(dest, palette[type], size);
|
2011-02-06 19:26:27 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
{
|
|
|
|
//Put RGBA row
|
2011-03-22 15:19:07 +02:00
|
|
|
for (size_t i=0; i<size; i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
ColorPutter<bpp, 1>::PutColorAlpha(dest, palette[type]);
|
|
|
|
break;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
void CompImage::playerColored(PlayerColor player)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
SDL_Color *pal = NULL;
|
2013-03-03 20:06:03 +03:00
|
|
|
if(player < PlayerColor::PLAYER_LIMIT)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2013-03-03 20:06:03 +03:00
|
|
|
pal = graphics->playerColorPalette + 32*player.getNum();
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2013-03-03 20:06:03 +03:00
|
|
|
else if(player == PlayerColor::NEUTRAL)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
pal = graphics->neutralColorPalette;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
assert(0);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
for(int i=0; i<32; ++i)
|
|
|
|
{
|
|
|
|
palette[224+i].r = pal[i].r;
|
|
|
|
palette[224+i].g = pal[i].g;
|
|
|
|
palette[224+i].b = pal[i].b;
|
|
|
|
palette[224+i].unused = pal[i].unused;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
int CompImage::width() const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
return fullSize.x;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
int CompImage::height() const
|
|
|
|
{
|
|
|
|
return fullSize.y;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
CompImage::~CompImage()
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
free(surf);
|
|
|
|
delete [] line;
|
|
|
|
delete [] palette;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*************************************************************************
|
|
|
|
* CAnimation for animations handling, can load part of file if needed *
|
|
|
|
*************************************************************************/
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
IImage * CAnimation::getFromExtraDef(std::string filename)
|
|
|
|
{
|
|
|
|
size_t pos = filename.find(':');
|
|
|
|
if (pos == -1)
|
|
|
|
return NULL;
|
|
|
|
CAnimation anim(filename.substr(0, pos));
|
|
|
|
pos++;
|
|
|
|
size_t frame = atoi(filename.c_str()+pos);
|
|
|
|
size_t group = 0;
|
|
|
|
pos = filename.find(':', pos);
|
|
|
|
if (pos != -1)
|
|
|
|
{
|
|
|
|
group = frame;
|
|
|
|
frame = atoi(filename.c_str()+pos);
|
|
|
|
}
|
|
|
|
anim.load(frame ,group);
|
|
|
|
IImage * ret = anim.images[group][frame];
|
|
|
|
anim.images.clear();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
bool CAnimation::loadFrame(CDefFile * file, size_t frame, size_t group)
|
|
|
|
{
|
|
|
|
if (size(group) <= frame)
|
|
|
|
{
|
|
|
|
printError(frame, group, "LoadFrame");
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
IImage *image = getImage(frame, group, false);
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
image->increaseRef();
|
|
|
|
return true;
|
|
|
|
}
|
2010-10-18 18:08:59 +03:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
//try to get image from def
|
2011-07-21 21:29:22 +03:00
|
|
|
if (source[group][frame].getType() == JsonNode::DATA_NULL)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
|
|
|
if (compressed)
|
|
|
|
images[group][frame] = new CompImage(file, frame, group);
|
2010-11-15 17:15:00 +02:00
|
|
|
else
|
2011-02-06 19:26:27 +02:00
|
|
|
images[group][frame] = new SDLImage(file, frame, group);
|
|
|
|
}
|
|
|
|
else //load from separate file
|
|
|
|
{
|
2011-07-21 21:29:22 +03:00
|
|
|
std::string filename = source[group][frame].Struct().find("file")->second.String();
|
|
|
|
|
|
|
|
IImage * img = getFromExtraDef(filename);
|
|
|
|
if (!img)
|
|
|
|
img = new SDLImage(filename, compressed);
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
images[group][frame] = img;
|
2011-02-06 19:26:27 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CAnimation::unloadFrame(size_t frame, size_t group)
|
|
|
|
{
|
|
|
|
IImage *image = getImage(frame, group, false);
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
//decrease ref count for image and delete if needed
|
|
|
|
if (image->decreaseRef())
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
delete image;
|
|
|
|
images[group].erase(frame);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
if (images[group].empty())
|
|
|
|
images.erase(group);
|
|
|
|
return true;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
return false;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
void CAnimation::init(CDefFile * file)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-07-21 21:29:22 +03:00
|
|
|
if (file)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
const std::map<size_t, size_t> defEntries = file->getEntries();
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
for (std::map<size_t, size_t>::const_iterator mapIt = defEntries.begin(); mapIt!=defEntries.end(); ++mapIt)
|
|
|
|
source[mapIt->first].resize(mapIt->second);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2012-08-06 10:34:37 +03:00
|
|
|
auto & configList = CResourceHandler::get()->getResourcesWithName(
|
|
|
|
ResourceID(std::string("SPRITES/") + name, EResType::TEXT));
|
|
|
|
|
|
|
|
BOOST_FOREACH(auto & entry, configList)
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2012-08-06 10:34:37 +03:00
|
|
|
auto stream = entry.getLoader()->load(entry.getResourceName());
|
|
|
|
std::unique_ptr<ui8[]> textData(new ui8[stream->getSize()]);
|
|
|
|
stream->read(textData.get(), stream->getSize());
|
|
|
|
|
|
|
|
const JsonNode config((char*)textData.get(), stream->getSize());
|
2011-07-22 19:22:22 +03:00
|
|
|
|
|
|
|
std::string basepath;
|
2011-09-23 18:58:18 +03:00
|
|
|
basepath = config["basepath"].String();
|
2011-07-22 19:22:22 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &group, config["sequences"].Vector())
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
size_t groupID = group["group"].Float();//TODO: string-to-value conversion("moving" -> MOVING)
|
|
|
|
source[groupID].clear();
|
|
|
|
|
|
|
|
BOOST_FOREACH(const JsonNode &frame, group["frames"].Vector())
|
|
|
|
{
|
2012-09-27 16:23:33 +03:00
|
|
|
source[groupID].push_back(JsonNode());
|
|
|
|
std::string filename = frame.String();
|
2011-09-23 18:58:18 +03:00
|
|
|
source[groupID].back()["file"].String() = basepath + filename;
|
2011-07-22 19:22:22 +03:00
|
|
|
}
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
2011-07-22 19:22:22 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
BOOST_FOREACH(const JsonNode &node, config["images"].Vector())
|
2011-07-21 21:29:22 +03:00
|
|
|
{
|
2011-09-23 18:58:18 +03:00
|
|
|
size_t group = node["group"].Float();
|
|
|
|
size_t frame = node["frame"].Float();
|
2011-07-22 19:22:22 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
if (source[group].size() <= frame)
|
|
|
|
source[group].resize(frame+1);
|
2011-07-21 21:29:22 +03:00
|
|
|
|
2011-09-23 18:58:18 +03:00
|
|
|
source[group][frame] = node;
|
|
|
|
std::string filename = node["file"].String();
|
|
|
|
source[group][frame]["file"].String() = basepath + filename;
|
2011-07-21 21:29:22 +03:00
|
|
|
}
|
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CDefFile * CAnimation::getFile() const
|
|
|
|
{
|
2012-08-01 15:02:54 +03:00
|
|
|
ResourceID identifier(std::string("SPRITES/") + name, EResType::ANIMATION);
|
|
|
|
|
|
|
|
if (CResourceHandler::get()->existsResource(identifier))
|
2011-02-06 19:26:27 +02:00
|
|
|
return new CDefFile(name);
|
|
|
|
return NULL;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::printError(size_t frame, size_t group, std::string type) const
|
|
|
|
{
|
|
|
|
tlog0 << type <<" error: Request for frame not present in CAnimation!\n"
|
|
|
|
<<"\tFile name: "<<name<<" Group: "<<group<<" Frame: "<<frame<<"\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
CAnimation::CAnimation(std::string Name, bool Compressed):
|
|
|
|
name(Name),
|
2011-02-06 19:26:27 +02:00
|
|
|
compressed(Compressed)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
size_t dotPos = name.find_last_of('.');
|
|
|
|
if ( dotPos!=-1 )
|
2010-10-18 18:08:59 +03:00
|
|
|
name.erase(dotPos);
|
2011-02-06 19:26:27 +02:00
|
|
|
std::transform(name.begin(), name.end(), name.begin(), toupper);
|
2010-10-18 18:08:59 +03:00
|
|
|
CDefFile * file = getFile();
|
|
|
|
init(file);
|
|
|
|
delete file;
|
2011-03-22 15:19:07 +02:00
|
|
|
loadedAnims.insert(this);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CAnimation::CAnimation():
|
|
|
|
name(""),
|
2011-02-06 19:26:27 +02:00
|
|
|
compressed(false)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
init(NULL);
|
2011-03-22 15:19:07 +02:00
|
|
|
loadedAnims.insert(this);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CAnimation::~CAnimation()
|
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
if (!images.empty())
|
|
|
|
{
|
|
|
|
tlog2<<"Warning: not all frames were unloaded from "<<name<<"\n";
|
|
|
|
for (group_map::iterator group = images.begin(); group != images.end(); ++group )
|
|
|
|
for (image_map::iterator image = group->second.begin(); image != group->second.end(); ++image )
|
|
|
|
delete image->second;
|
|
|
|
}
|
2011-03-22 15:19:07 +02:00
|
|
|
loadedAnims.erase(this);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
void CAnimation::setCustom(std::string filename, size_t frame, size_t group)
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (source[group].size() <= frame)
|
|
|
|
source[group].resize(frame+1);
|
2011-07-22 19:22:22 +03:00
|
|
|
source[group][frame]["file"].String() = filename;
|
2011-02-20 11:24:53 +02:00
|
|
|
//FIXME: update image if already loaded
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
IImage * CAnimation::getImage(size_t frame, size_t group, bool verbose) const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
group_map::const_iterator groupIter = images.find(group);
|
|
|
|
if (groupIter != images.end())
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
image_map::const_iterator imageIter = groupIter->second.find(frame);
|
|
|
|
if (imageIter != groupIter->second.end())
|
|
|
|
return imageIter->second;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
if (verbose)
|
|
|
|
printError(frame, group, "GetImage");
|
2010-10-18 18:08:59 +03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::load()
|
|
|
|
{
|
|
|
|
CDefFile * file = getFile();
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
for (source_map::iterator group = source.begin(); group != source.end(); ++group )
|
|
|
|
for (size_t image=0; image < group->second.size(); image++)
|
|
|
|
loadFrame(file, image, group->first);
|
2010-10-18 18:08:59 +03:00
|
|
|
|
|
|
|
delete file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::unload()
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
for (source_map::iterator group = source.begin(); group != source.end(); ++group )
|
|
|
|
for (size_t image=0; image < group->second.size(); image++)
|
|
|
|
unloadFrame(image, group->first);
|
|
|
|
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::loadGroup(size_t group)
|
|
|
|
{
|
|
|
|
CDefFile * file = getFile();
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
if (vstd::contains(source, group))
|
|
|
|
for (size_t image=0; image < source[group].size(); image++)
|
|
|
|
loadFrame(file, image, group);
|
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
delete file;
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::unloadGroup(size_t group)
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (vstd::contains(source, group))
|
|
|
|
for (size_t image=0; image < source[group].size(); image++)
|
|
|
|
unloadFrame(image, group);
|
2010-10-18 18:08:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::load(size_t frame, size_t group)
|
|
|
|
{
|
|
|
|
CDefFile * file = getFile();
|
|
|
|
loadFrame(file, frame, group);
|
|
|
|
delete file;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimation::unload(size_t frame, size_t group)
|
|
|
|
{
|
|
|
|
unloadFrame(frame, group);
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
size_t CAnimation::size(size_t group) const
|
2010-10-18 18:08:59 +03:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
source_map::const_iterator iter = source.find(group);
|
|
|
|
if (iter != source.end())
|
|
|
|
return iter->second.size();
|
2010-10-18 18:08:59 +03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-03-22 15:19:07 +02:00
|
|
|
std::set<CAnimation*> CAnimation::loadedAnims;
|
|
|
|
|
2011-03-27 20:24:30 +03:00
|
|
|
void CAnimation::getAnimInfo()
|
2011-03-22 15:19:07 +02:00
|
|
|
{
|
|
|
|
tlog1<<"Animation stats: Loaded "<<loadedAnims.size()<<" total\n";
|
|
|
|
for (std::set<CAnimation*>::iterator it = loadedAnims.begin(); it != loadedAnims.end(); it++)
|
|
|
|
{
|
|
|
|
CAnimation * anim = *it;
|
|
|
|
tlog1<<"Name: "<<anim->name<<" Groups: "<<anim->images.size();
|
|
|
|
if (!anim->images.empty())
|
|
|
|
tlog1<<", "<<anim->images.begin()->second.size()<<" image loaded in group "<< anim->images.begin()->first;
|
|
|
|
tlog1<<"\n";
|
2011-03-27 20:24:30 +03:00
|
|
|
}
|
|
|
|
}
|
2011-03-22 15:19:07 +02:00
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
CAnimImage::CAnimImage(std::string name, size_t Frame, size_t Group, int x, int y, ui8 Flags):
|
2010-11-15 17:15:00 +02:00
|
|
|
frame(Frame),
|
2011-02-20 11:24:53 +02:00
|
|
|
group(Group),
|
|
|
|
player(-1),
|
|
|
|
flags(Flags)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
anim = new CAnimation(name);
|
|
|
|
init();
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
CAnimImage::CAnimImage(CAnimation *Anim, size_t Frame, size_t Group, int x, int y, ui8 Flags):
|
2011-02-20 11:24:53 +02:00
|
|
|
anim(Anim),
|
|
|
|
frame(Frame),
|
|
|
|
group(Group),
|
|
|
|
player(-1),
|
|
|
|
flags(Flags)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
pos.x += x;
|
|
|
|
pos.y += y;
|
|
|
|
init();
|
|
|
|
}
|
2010-11-15 17:15:00 +02:00
|
|
|
|
2011-05-28 13:05:25 +03:00
|
|
|
size_t CAnimImage::size()
|
|
|
|
{
|
|
|
|
return anim->size(group);
|
|
|
|
}
|
|
|
|
|
2011-02-20 11:24:53 +02:00
|
|
|
void CAnimImage::init()
|
|
|
|
{
|
|
|
|
anim->load(frame, group);
|
|
|
|
if (flags & CShowableAnim::BASE)
|
|
|
|
anim->load(0,group);
|
|
|
|
|
|
|
|
IImage *img = anim->getImage(frame, group);
|
2011-04-07 20:54:08 +03:00
|
|
|
if (img)
|
|
|
|
{
|
|
|
|
pos.w = img->width();
|
|
|
|
pos.h = img->height();
|
|
|
|
}
|
2011-02-20 11:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CAnimImage::~CAnimImage()
|
|
|
|
{
|
|
|
|
anim->unload(frame, group);
|
|
|
|
if (flags & CShowableAnim::BASE)
|
|
|
|
anim->unload(0,group);
|
|
|
|
delete anim;
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void CAnimImage::showAll(SDL_Surface * to)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2012-01-19 17:33:22 +03:00
|
|
|
IImage *img;
|
|
|
|
|
|
|
|
if ( flags & CShowableAnim::BASE && frame != 0)
|
|
|
|
if ((img = anim->getImage(0, group)))
|
|
|
|
img->draw(to, pos.x, pos.y);
|
|
|
|
|
|
|
|
if ((img = anim->getImage(frame, group)))
|
2011-04-07 20:54:08 +03:00
|
|
|
img->draw(to, pos.x, pos.y);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CAnimImage::setFrame(size_t Frame, size_t Group)
|
|
|
|
{
|
|
|
|
if (frame == Frame && group==Group)
|
|
|
|
return;
|
2011-02-20 11:24:53 +02:00
|
|
|
if (anim->size(Group) > Frame)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
anim->load(Frame, Group);
|
|
|
|
anim->unload(frame, group);
|
2010-11-15 17:15:00 +02:00
|
|
|
frame = Frame;
|
|
|
|
group = Group;
|
2011-04-07 20:54:08 +03:00
|
|
|
IImage *img = anim->getImage(frame, group);
|
|
|
|
if (img)
|
|
|
|
{
|
|
|
|
if (flags & CShowableAnim::PLAYER_COLORED)
|
|
|
|
img->playerColored(player);
|
|
|
|
pos.w = img->width();
|
|
|
|
pos.h = img->height();
|
|
|
|
}
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
2012-12-02 15:21:44 +03:00
|
|
|
else
|
|
|
|
tlog1 << "Error: accessing unavailable frame " << Group << ":" << Frame << " in CAnimation!\n";
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2013-03-03 20:06:03 +03:00
|
|
|
void CAnimImage::playerColored(PlayerColor currPlayer)
|
2011-02-20 11:24:53 +02:00
|
|
|
{
|
|
|
|
player = currPlayer;
|
|
|
|
flags |= CShowableAnim::PLAYER_COLORED;
|
|
|
|
anim->getImage(frame, group)->playerColored(player);
|
|
|
|
if (flags & CShowableAnim::BASE)
|
|
|
|
anim->getImage(0, group)->playerColored(player);
|
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
CShowableAnim::CShowableAnim(int x, int y, std::string name, ui8 Flags, ui32 Delay, size_t Group):
|
2011-02-06 19:26:27 +02:00
|
|
|
anim(name, Flags & USE_RLE),
|
2010-11-15 17:15:00 +02:00
|
|
|
group(Group),
|
|
|
|
frame(0),
|
|
|
|
first(0),
|
|
|
|
frameDelay(Delay),
|
|
|
|
value(0),
|
2011-02-06 19:26:27 +02:00
|
|
|
flags(Flags),
|
2010-11-15 17:15:00 +02:00
|
|
|
xOffset(0),
|
2011-03-22 15:19:07 +02:00
|
|
|
yOffset(0),
|
|
|
|
alpha(255)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
|
|
|
anim.loadGroup(group);
|
2011-02-06 19:26:27 +02:00
|
|
|
last = anim.size(group);
|
|
|
|
|
|
|
|
pos.w = anim.getImage(0, group)->width();
|
|
|
|
pos.h = anim.getImage(0, group)->height();
|
|
|
|
pos.x+= x;
|
|
|
|
pos.y+= y;
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CShowableAnim::~CShowableAnim()
|
|
|
|
{
|
2011-02-20 11:24:53 +02:00
|
|
|
anim.unloadGroup(group);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-12-14 00:23:17 +03:00
|
|
|
void CShowableAnim::setAlpha(ui32 alphaValue)
|
2011-03-22 15:19:07 +02:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
alpha = std::min<ui32>(alphaValue, 255);
|
2011-03-22 15:19:07 +02:00
|
|
|
}
|
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
bool CShowableAnim::set(size_t Group, size_t from, size_t to)
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
size_t max = anim.size(Group);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
2011-11-01 15:58:01 +03:00
|
|
|
if (to < max)
|
2010-11-15 17:15:00 +02:00
|
|
|
max = to;
|
|
|
|
|
|
|
|
if (max < from || max == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
anim.load(Group);
|
|
|
|
anim.unload(group);
|
|
|
|
group = Group;
|
|
|
|
frame = first = from;
|
|
|
|
last = max;
|
|
|
|
value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CShowableAnim::set(size_t Group)
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (anim.size(Group)== 0)
|
2010-11-15 17:15:00 +02:00
|
|
|
return false;
|
|
|
|
if (group != Group)
|
|
|
|
{
|
|
|
|
anim.loadGroup(Group);
|
|
|
|
anim.unloadGroup(group);
|
|
|
|
first = 0;
|
|
|
|
group = Group;
|
2011-02-06 19:26:27 +02:00
|
|
|
last = anim.size(Group);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
frame = value = 0;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CShowableAnim::reset()
|
|
|
|
{
|
|
|
|
value = 0;
|
|
|
|
frame = first;
|
2012-06-13 16:04:06 +03:00
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
if (callback)
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
void CShowableAnim::clipRect(int posX, int posY, int width, int height)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
xOffset = posX;
|
|
|
|
yOffset = posY;
|
|
|
|
pos.w = width;
|
|
|
|
pos.h = height;
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void CShowableAnim::show(SDL_Surface * to)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if ( flags & BASE && frame != first)
|
|
|
|
blitImage(first, group, to);
|
|
|
|
blitImage(frame, group, to);
|
|
|
|
|
2012-06-13 16:04:06 +03:00
|
|
|
if ((flags & PLAY_ONCE) && frame + 1 == last)
|
|
|
|
return;
|
|
|
|
|
2010-11-15 17:15:00 +02:00
|
|
|
if ( ++value == frameDelay )
|
|
|
|
{
|
|
|
|
value = 0;
|
2011-11-01 15:58:01 +03:00
|
|
|
if ( ++frame >= last)
|
2010-11-15 17:15:00 +02:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
void CShowableAnim::showAll(SDL_Surface * to)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if ( flags & BASE && frame != first)
|
|
|
|
blitImage(first, group, to);
|
|
|
|
blitImage(frame, group, to);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
void CShowableAnim::blitImage(size_t frame, size_t group, SDL_Surface *to)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
assert(to);
|
2011-12-22 16:05:19 +03:00
|
|
|
Rect src( xOffset, yOffset, pos.w, pos.h);
|
2011-02-06 19:26:27 +02:00
|
|
|
IImage * img = anim.getImage(frame, group);
|
2011-04-07 20:54:08 +03:00
|
|
|
if (img)
|
|
|
|
img->draw(to, pos.x-xOffset, pos.y-yOffset, &src, alpha);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
void CShowableAnim::rotate(bool on, bool vertical)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-12-14 00:23:17 +03:00
|
|
|
ui8 flag = vertical? VERTICAL_FLIP:HORIZONTAL_FLIP;
|
2010-11-15 17:15:00 +02:00
|
|
|
if (on)
|
2011-02-06 19:26:27 +02:00
|
|
|
flags |= flag;
|
2010-11-15 17:15:00 +02:00
|
|
|
else
|
2011-02-06 19:26:27 +02:00
|
|
|
flags &= ~flag;
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
2011-12-22 16:05:19 +03:00
|
|
|
CCreatureAnim::CCreatureAnim(int x, int y, std::string name, Rect picPos, ui8 flags, EAnimType type):
|
2012-12-19 20:24:53 +03:00
|
|
|
CShowableAnim(x,y,name,flags,4,type)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
xOffset = picPos.x;
|
|
|
|
yOffset = picPos.y;
|
|
|
|
if (picPos.w)
|
|
|
|
pos.w = picPos.w;
|
|
|
|
if (picPos.h)
|
|
|
|
pos.h = picPos.h;
|
2010-11-15 17:15:00 +02:00
|
|
|
};
|
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
void CCreatureAnim::loopPreview(bool warMachine)
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
|
|
|
std::vector<EAnimType> available;
|
2012-06-27 23:44:01 +03:00
|
|
|
|
|
|
|
static const EAnimType creaPreviewList[] = {HOLDING, HITTED, DEFENCE, ATTACK_FRONT, CAST_FRONT};
|
|
|
|
static const EAnimType machPreviewList[] = {HOLDING, MOVING, SHOOT_UP, SHOOT_FRONT, SHOOT_DOWN};
|
|
|
|
auto & previewList = warMachine ? machPreviewList : creaPreviewList;
|
2011-02-06 19:26:27 +02:00
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
for (size_t i=0; i<ARRAY_COUNT(previewList); i++)
|
2011-02-06 19:26:27 +02:00
|
|
|
if (anim.size(previewList[i]))
|
|
|
|
available.push_back(previewList[i]);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
|
|
|
size_t rnd = rand()%(available.size()*2);
|
|
|
|
|
|
|
|
if (rnd >= available.size())
|
|
|
|
{
|
2012-06-27 23:44:01 +03:00
|
|
|
EAnimType type;
|
2011-02-06 19:26:27 +02:00
|
|
|
if ( anim.size(MOVING) == 0 )//no moving animation present
|
2012-06-27 23:44:01 +03:00
|
|
|
type = HOLDING;
|
2010-11-15 17:15:00 +02:00
|
|
|
else
|
2012-06-27 23:44:01 +03:00
|
|
|
type = MOVING;
|
|
|
|
|
|
|
|
//display this anim for ~1 second (time is random, but it looks good)
|
|
|
|
for (size_t i=0; i< 12/anim.size(type) + 1; i++)
|
|
|
|
addLast(type);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
addLast(available[rnd]);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnim::addLast(EAnimType newType)
|
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
if (type != MOVING && newType == MOVING)//starting moving - play init sequence
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
queue.push( MOVE_START );
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
else if (type == MOVING && newType != MOVING )//previous anim was moving - finish it
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
2011-02-06 19:26:27 +02:00
|
|
|
queue.push( MOVE_END );
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
2011-02-06 19:26:27 +02:00
|
|
|
if (newType == TURN_L || newType == TURN_R)
|
2010-11-15 17:15:00 +02:00
|
|
|
queue.push(newType);
|
|
|
|
|
|
|
|
queue.push(newType);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnim::reset()
|
|
|
|
{
|
|
|
|
//if we are in the middle of rotation - set flag
|
2011-02-06 19:26:27 +02:00
|
|
|
if (type == TURN_L && !queue.empty() && queue.front() == TURN_L)
|
|
|
|
rotate(true);
|
|
|
|
if (type == TURN_R && !queue.empty() && queue.front() == TURN_R)
|
|
|
|
rotate(false);
|
2010-11-15 17:15:00 +02:00
|
|
|
|
2011-02-06 19:26:27 +02:00
|
|
|
while (!queue.empty())
|
2010-11-15 17:15:00 +02:00
|
|
|
{
|
|
|
|
EAnimType at = queue.front();
|
|
|
|
queue.pop();
|
|
|
|
if (set(at))
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (callback)
|
|
|
|
callback();
|
|
|
|
while (!queue.empty())
|
|
|
|
{
|
|
|
|
EAnimType at = queue.front();
|
|
|
|
queue.pop();
|
|
|
|
if (set(at))
|
|
|
|
return;
|
|
|
|
}
|
2011-03-22 15:19:07 +02:00
|
|
|
set(HOLDING);
|
2011-02-06 19:26:27 +02:00
|
|
|
}
|
|
|
|
|
2012-06-27 23:44:01 +03:00
|
|
|
void CCreatureAnim::startPreview(bool warMachine)
|
2011-02-06 19:26:27 +02:00
|
|
|
{
|
2012-06-27 23:44:01 +03:00
|
|
|
callback = boost::bind(&CCreatureAnim::loopPreview, this, warMachine);
|
2010-11-15 17:15:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnim::clearAndSet(EAnimType type)
|
|
|
|
{
|
|
|
|
while (!queue.empty())
|
|
|
|
queue.pop();
|
|
|
|
set(type);
|
|
|
|
}
|