1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-20 20:23:03 +02:00
vcmi/client/battle/CCreatureAnimation.cpp

435 lines
12 KiB
C++
Raw Normal View History

#include "StdInc.h"
2008-06-07 20:16:52 +03:00
#include "CCreatureAnimation.h"
#include "../../lib/CConfigHandler.h"
#include "../../lib/CCreatureHandler.h"
#include "../../lib/vcmi_endian.h"
#include "../gui/SDL_Extensions.h"
#include "../gui/SDL_Pixels.h"
#include "../../lib/filesystem/CResourceLoader.h"
#include "../../lib/filesystem/CBinaryReader.h"
#include "../../lib/filesystem/CMemoryStream.h"
/*
* CCreatureAnimation.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
*
*/
static const SDL_Color creatureBlueBorder = { 0, 255, 255, 255 };
static const SDL_Color creatureGoldBorder = { 255, 255, 0, 255 };
static const SDL_Color creatureNoBorder = { 0, 0, 0, 0 };
SDL_Color AnimationControls::getBlueBorder()
{
return creatureBlueBorder;
}
SDL_Color AnimationControls::getGoldBorder()
{
return creatureGoldBorder;
}
SDL_Color AnimationControls::getNoBorder()
{
return creatureNoBorder;
}
CCreatureAnimation * AnimationControls::getAnimation(const CCreature * creature)
{
auto func = boost::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
return new CCreatureAnimation(creature->animDefName, func);
}
float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t group)
{
// possible new fields for creature format
//Shoot Animation Time
//Cast Animation Time
//Defence and/or Death Animation Time
// a lot of arbitrary multipliers, mostly to make animation speed closer to H3
CCreatureAnim::EAnimType type = CCreatureAnim::EAnimType(group);
const float baseSpeed = 10;
const float speedMult = settings["battle"]["animationSpeed"].Float() * 20;
const float speed = baseSpeed * speedMult;
switch (type)
{
case CCreatureAnim::MOVING:
return speed / creature->animation.walkAnimationTime / anim->framesInGroup(type);
case CCreatureAnim::MOUSEON:
case CCreatureAnim::HOLDING:
return baseSpeed;
case CCreatureAnim::ATTACK_UP:
case CCreatureAnim::ATTACK_FRONT:
case CCreatureAnim::ATTACK_DOWN:
case CCreatureAnim::SHOOT_UP:
case CCreatureAnim::SHOOT_FRONT:
case CCreatureAnim::SHOOT_DOWN:
case CCreatureAnim::CAST_UP:
case CCreatureAnim::CAST_FRONT:
case CCreatureAnim::CAST_DOWN:
return speed * 2 / creature->animation.attackAnimationTime / anim->framesInGroup(type);
case CCreatureAnim::TURN_L:
case CCreatureAnim::TURN_R:
return speed;
case CCreatureAnim::MOVE_START:
case CCreatureAnim::MOVE_END:
return speed / 5;
case CCreatureAnim::HITTED:
case CCreatureAnim::DEFENCE:
case CCreatureAnim::DEATH:
case CCreatureAnim::DEAD:
return speed / 5;
default:
assert(0);
return 1;
}
}
float AnimationControls::getProjectileSpeed()
{
return settings["battle"]["animationSpeed"].Float() * 100;
}
float AnimationControls::getMovementDuration(const CCreature * creature)
{
return settings["battle"]["animationSpeed"].Float() * 4 / creature->animation.walkAnimationTime;
}
float AnimationControls::getFlightDistance(const CCreature * creature)
{
return creature->animation.flightAnimationDistance * 200;
}
CCreatureAnim::EAnimType CCreatureAnimation::getType() const
2008-06-07 20:16:52 +03:00
{
return type;
}
void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
2008-06-07 20:16:52 +03:00
{
assert(type >= 0);
assert(framesInGroup(type) != 0);
2008-06-07 20:16:52 +03:00
this->type = type;
currentFrame = 0;
once = false;
play();
2008-06-07 20:16:52 +03:00
}
CCreatureAnimation::CCreatureAnimation(std::string name, TSpeedController controller)
: defName(name),
speed(0.1),
currentFrame(0),
elapsedTime(0),
type(CCreatureAnim::HOLDING),
border(CSDL_Ext::makeColor(0, 0, 0, 0)),
speedController(controller),
once(false)
2008-06-07 20:16:52 +03:00
{
// separate block to avoid accidental use of "data" after it was moved into "pixelData"
{
auto data = CResourceHandler::get()->loadData(
ResourceID(std::string("SPRITES/") + name, EResType::ANIMATION));
pixelData = std::move(data.first);
pixelDataSize = data.second;
}
CBinaryReader reader(new CMemoryStream(pixelData.get(), pixelDataSize));
2008-06-07 20:16:52 +03:00
reader.readInt32(); // def type, unused
fullWidth = reader.readInt32();
fullHeight = reader.readInt32();
int totalBlocks = reader.readInt32();
2008-06-07 20:16:52 +03:00
for (auto & elem : palette)
2008-06-07 20:16:52 +03:00
{
elem.r = reader.readUInt8();
elem.g = reader.readUInt8();
elem.b = reader.readUInt8();
elem.unused = 0;
2008-06-07 20:16:52 +03:00
}
for (int i=0; i<totalBlocks; i++)
2008-06-07 20:16:52 +03:00
{
int groupID = reader.readInt32();
int totalInBlock = reader.readInt32();
reader.skip(4 + 4 + 13 * totalInBlock); // some unused data
for (int j=0; j<totalInBlock; j++)
dataOffsets[groupID].push_back(reader.readUInt32());
2008-06-07 20:16:52 +03:00
}
// if necessary, add one frame into vcmi-only group DEAD
if (dataOffsets.count(CCreatureAnim::DEAD) == 0)
dataOffsets[CCreatureAnim::DEAD].push_back(dataOffsets[CCreatureAnim::DEATH].back());
play();
2008-06-07 20:16:52 +03:00
}
void CCreatureAnimation::endAnimation()
2008-06-07 20:16:52 +03:00
{
once = false;
auto copy = onAnimationReset;
onAnimationReset.clear();
copy();
2008-06-07 20:16:52 +03:00
}
bool CCreatureAnimation::incrementFrame(float timePassed)
2008-06-07 20:16:52 +03:00
{
elapsedTime += timePassed;
currentFrame += timePassed * speed;
if (currentFrame >= float(framesInGroup(type)))
2008-06-07 20:16:52 +03:00
{
// just in case of extremely low fps
while (currentFrame >= float(framesInGroup(type)))
currentFrame -= framesInGroup(type);
if (once)
setType(CCreatureAnim::HOLDING);
endAnimation();
return true;
2008-06-07 20:16:52 +03:00
}
return false;
}
void CCreatureAnimation::setBorderColor(SDL_Color palette)
{
border = palette;
}
int CCreatureAnimation::getWidth() const
{
return fullWidth;
}
int CCreatureAnimation::getHeight() const
{
return fullHeight;
}
float CCreatureAnimation::getCurrentFrame() const
2009-03-05 19:57:26 +02:00
{
return currentFrame;
2009-03-05 19:57:26 +02:00
}
void CCreatureAnimation::playOnce( CCreatureAnim::EAnimType type )
{
setType(type);
once = true;
}
inline int getBorderStrength(float time)
{
float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
return borderStrength * 155 + 100; // scale to 0-255
}
static SDL_Color genShadow(ui8 alpha)
{
return CSDL_Ext::makeColor(0, 0, 0, alpha);
}
static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
{
return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.unused * alpha / 256));
}
static ui8 mixChannels(ui8 c1, ui8 c2, ui8 a1, ui8 a2)
{
return c1*a1 / 256 + c2*a2*(255 - a1) / 256 / 256;
}
static SDL_Color addColors(const SDL_Color & base, const SDL_Color & over)
{
return CSDL_Ext::makeColor(
mixChannels(over.r, base.r, over.unused, base.unused),
mixChannels(over.g, base.g, over.unused, base.unused),
mixChannels(over.b, base.b, over.unused, base.unused),
ui8(over.unused + base.unused * (255 - over.unused) / 256)
);
}
std::array<SDL_Color, 8> CCreatureAnimation::genSpecialPalette()
{
std::array<SDL_Color, 8> ret;
ret[0] = genShadow(0);
ret[1] = genShadow(64);
ret[2] = genShadow(128);
ret[3] = genShadow(128);
ret[4] = genShadow(128);
ret[5] = genBorderColor(getBorderStrength(elapsedTime), border);
ret[6] = addColors(genShadow(128), genBorderColor(getBorderStrength(elapsedTime), border));
ret[7] = addColors(genShadow(64), genBorderColor(getBorderStrength(elapsedTime), border));
return ret;
}
2010-08-12 08:22:48 +03:00
template<int bpp>
void CCreatureAnimation::nextFrameT(SDL_Surface * dest, int x, int y, bool rotate, SDL_Rect * destRect /*= nullptr*/)
{
assert(dataOffsets.count(type) && dataOffsets.at(type).size() > size_t(currentFrame));
ui32 offset = dataOffsets.at(type).at(floor(currentFrame));
CBinaryReader reader(new CMemoryStream(pixelData.get(), pixelDataSize));
reader.getStream()->seek(offset);
2010-04-04 11:47:00 +03:00
reader.readUInt32(); // unused, size of pixel data for this frame
const ui32 defType2 = reader.readUInt32();
const ui32 fullWidth = reader.readUInt32();
/*const ui32 fullHeight =*/ reader.readUInt32();
const ui32 spriteWidth = reader.readUInt32();
const ui32 spriteHeight = reader.readUInt32();
const int leftMargin = reader.readInt32();
const int topMargin = reader.readInt32();
2010-04-04 11:47:00 +03:00
const int rightMargin = fullWidth - spriteWidth - leftMargin;
//const int bottomMargin = fullHeight - spriteHeight - topMargin;
2010-04-04 11:47:00 +03:00
const size_t baseOffset = reader.getStream()->tell();
2010-04-04 11:47:00 +03:00
assert(defType2 == 1);
2010-04-04 11:47:00 +03:00
auto specialPalette = genSpecialPalette();
2010-04-04 11:47:00 +03:00
for (ui32 i=0; i<spriteHeight; i++)
{
//NOTE: if this loop will be optimized to skip empty lines - recheck this read access
ui8 * lineData = pixelData.get() + baseOffset + reader.readUInt32();
size_t destX = x;
if (rotate)
destX += rightMargin + spriteWidth - 1;
else
destX += leftMargin;
2010-04-04 11:47:00 +03:00
size_t destY = y + topMargin + i;
size_t currentOffset = 0;
size_t totalRowLength = 0;
while (totalRowLength < spriteWidth)
{
ui8 type = lineData[currentOffset++];
ui32 length = lineData[currentOffset++] + 1;
if (type==0xFF)//Raw data
2008-06-07 20:16:52 +03:00
{
for (size_t j=0; j<length; j++)
putPixelAt<bpp>(dest, destX + (rotate?(-j):(j)), destY, lineData[currentOffset + j], specialPalette, destRect);
currentOffset += length;
2008-06-07 20:16:52 +03:00
}
else// RLE
{
if (type != 0) // transparency row, handle it here for speed
{
for (size_t j=0; j<length; j++)
putPixelAt<bpp>(dest, destX + (rotate?(-j):(j)), destY, type, specialPalette, destRect);
}
}
destX += rotate ? (-length) : (length);
totalRowLength += length;
2008-06-07 20:16:52 +03:00
}
}
}
void CCreatureAnimation::nextFrame(SDL_Surface *dest, int x, int y, bool attacker, SDL_Rect * destRect)
2010-08-12 08:22:48 +03:00
{
switch(dest->format->BytesPerPixel)
{
case 2: return nextFrameT<2>(dest, x, y, !attacker, destRect);
case 3: return nextFrameT<3>(dest, x, y, !attacker, destRect);
case 4: return nextFrameT<4>(dest, x, y, !attacker, destRect);
2010-08-12 08:22:48 +03:00
default:
logGlobal->errorStream() << (int)dest->format->BitsPerPixel << " bpp is not supported!!!";
2010-08-12 08:22:48 +03:00
}
}
int CCreatureAnimation::framesInGroup(CCreatureAnim::EAnimType group) const
2008-06-07 20:16:52 +03:00
{
if(dataOffsets.count(group) == 0)
return 0;
return dataOffsets.at(group).size();
2008-06-07 20:16:52 +03:00
}
ui8 * CCreatureAnimation::getPixelAddr(SDL_Surface * dest, int X, int Y) const
2008-06-07 20:16:52 +03:00
{
return (ui8*)dest->pixels + X * dest->format->BytesPerPixel + Y * dest->pitch;
2008-06-07 20:16:52 +03:00
}
2010-08-12 08:22:48 +03:00
template<int bpp>
inline void CCreatureAnimation::putPixelAt(SDL_Surface * dest, int X, int Y, size_t index, const std::array<SDL_Color, 8> & special, SDL_Rect * destRect) const
{
if (destRect == nullptr)
putPixel<bpp>(getPixelAddr(dest, X, Y), palette[index], index, special);
else
{
if ( X > destRect->x && X < destRect->w + destRect->x &&
Y > destRect->y && Y < destRect->h + destRect->y )
putPixel<bpp>(getPixelAddr(dest, X, Y), palette[index], index, special);
}
}
2010-08-12 08:22:48 +03:00
template<int bpp>
inline void CCreatureAnimation::putPixel(ui8 * dest, const SDL_Color & color, size_t index, const std::array<SDL_Color, 8> & special) const
{
if (index < 8)
{
const SDL_Color & pal = special[index];
ColorPutter<bpp, 0>::PutColor(dest, pal.r, pal.g, pal.b, pal.unused);
}
else
{
ColorPutter<bpp, 0>::PutColor(dest, color.r, color.g, color.b);
2008-06-07 20:16:52 +03:00
}
}
bool CCreatureAnimation::isDead() const
{
return getType() == CCreatureAnim::DEAD
|| getType() == CCreatureAnim::DEATH;
}
bool CCreatureAnimation::isIdle() const
{
return getType() == CCreatureAnim::HOLDING
|| getType() == CCreatureAnim::MOUSEON;
}
void CCreatureAnimation::pause()
{
speed = 0;
}
void CCreatureAnimation::play()
{
speed = speedController(this, type);
}