2011-12-14 00:23:17 +03:00
|
|
|
#include "StdInc.h"
|
2008-06-07 20:16:52 +03:00
|
|
|
#include "CCreatureAnimation.h"
|
2011-12-14 00:23:17 +03:00
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../../lib/vcmi_endian.h"
|
2013-07-06 19:10:20 +03:00
|
|
|
#include "../../lib/CConfigHandler.h"
|
|
|
|
#include "../../lib/CCreatureHandler.h"
|
2013-07-28 17:49:50 +03:00
|
|
|
#include "../../lib/filesystem/Filesystem.h"
|
2013-07-06 19:10:20 +03:00
|
|
|
#include "../../lib/filesystem/CBinaryReader.h"
|
|
|
|
#include "../../lib/filesystem/CMemoryStream.h"
|
|
|
|
|
2014-07-13 20:53:37 +03:00
|
|
|
#include "../gui/SDL_Pixels.h"
|
|
|
|
|
2009-04-15 17:03:31 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
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)
|
|
|
|
{
|
2014-08-04 21:33:59 +03:00
|
|
|
auto func = std::bind(&AnimationControls::getCreatureAnimationSpeed, creature, _1, _2);
|
2013-07-06 19:10:20 +03:00
|
|
|
return new CCreatureAnimation(creature->animDefName, func);
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationControls::getCreatureAnimationSpeed(const CCreature * creature, const CCreatureAnimation * anim, size_t group)
|
|
|
|
{
|
2013-07-17 01:59:39 +03:00
|
|
|
CCreatureAnim::EAnimType type = CCreatureAnim::EAnimType(group);
|
|
|
|
|
|
|
|
assert(creature->animation.walkAnimationTime != 0);
|
|
|
|
assert(creature->animation.attackAnimationTime != 0);
|
|
|
|
assert(anim->framesInGroup(type) != 0);
|
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
// possible new fields for creature format:
|
|
|
|
//split "Attack time" into "Shoot Time" and "Cast Time"
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
// a lot of arbitrary multipliers, mostly to make animation speed closer to H3
|
2013-07-17 01:59:39 +03:00
|
|
|
const float baseSpeed = 0.1;
|
|
|
|
const float speedMult = settings["battle"]["animationSpeed"].Float();
|
|
|
|
const float speed = baseSpeed / speedMult;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case CCreatureAnim::MOVING:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed * 2 * creature->animation.walkAnimationTime / anim->framesInGroup(type);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::MOUSEON:
|
|
|
|
return baseSpeed;
|
2013-11-06 16:42:58 +03:00
|
|
|
case CCreatureAnim::HOLDING:
|
|
|
|
return baseSpeed * creature->animation.idleAnimationTime / anim->framesInGroup(type);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::SHOOT_UP:
|
|
|
|
case CCreatureAnim::SHOOT_FRONT:
|
|
|
|
case CCreatureAnim::SHOOT_DOWN:
|
|
|
|
case CCreatureAnim::CAST_UP:
|
|
|
|
case CCreatureAnim::CAST_FRONT:
|
|
|
|
case CCreatureAnim::CAST_DOWN:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed * 4 * creature->animation.attackAnimationTime / anim->framesInGroup(type);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
// as strange as it looks like "attackAnimationTime" does not affects melee attacks
|
2013-07-17 01:59:39 +03:00
|
|
|
// necessary because length of these animations must be same for all creatures for synchronization
|
2013-07-16 21:12:47 +03:00
|
|
|
case CCreatureAnim::ATTACK_UP:
|
|
|
|
case CCreatureAnim::ATTACK_FRONT:
|
|
|
|
case CCreatureAnim::ATTACK_DOWN:
|
2013-07-17 01:59:39 +03:00
|
|
|
case CCreatureAnim::HITTED:
|
2013-07-16 21:12:47 +03:00
|
|
|
case CCreatureAnim::DEFENCE:
|
|
|
|
case CCreatureAnim::DEATH:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed * 3 / anim->framesInGroup(type);
|
2013-07-16 21:12:47 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
case CCreatureAnim::TURN_L:
|
|
|
|
case CCreatureAnim::TURN_R:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed / 3;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::MOVE_START:
|
|
|
|
case CCreatureAnim::MOVE_END:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed / 3;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
case CCreatureAnim::DEAD:
|
2013-07-17 01:59:39 +03:00
|
|
|
return speed;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
float AnimationControls::getProjectileSpeed()
|
|
|
|
{
|
|
|
|
return settings["battle"]["animationSpeed"].Float() * 100;
|
|
|
|
}
|
|
|
|
|
2013-07-16 21:12:47 +03:00
|
|
|
float AnimationControls::getSpellEffectSpeed()
|
|
|
|
{
|
|
|
|
return settings["battle"]["animationSpeed"].Float() * 60;
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
CCreatureAnim::EAnimType CCreatureAnimation::getType() const
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
|
|
|
return type;
|
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
void CCreatureAnimation::setType(CCreatureAnim::EAnimType type)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
assert(type >= 0);
|
|
|
|
assert(framesInGroup(type) != 0);
|
|
|
|
|
2008-06-07 20:16:52 +03:00
|
|
|
this->type = type;
|
2013-07-06 19:10:20 +03:00
|
|
|
currentFrame = 0;
|
|
|
|
once = false;
|
|
|
|
|
|
|
|
play();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
CCreatureAnimation::CCreatureAnimation(std::string name, TSpeedController controller)
|
|
|
|
: defName(name),
|
|
|
|
speed(0.1),
|
|
|
|
currentFrame(0),
|
|
|
|
elapsedTime(0),
|
2013-07-07 11:27:27 +03:00
|
|
|
type(CCreatureAnim::HOLDING),
|
|
|
|
border(CSDL_Ext::makeColor(0, 0, 0, 0)),
|
2013-07-06 19:10:20 +03:00
|
|
|
speedController(controller),
|
|
|
|
once(false)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
// separate block to avoid accidental use of "data" after it was moved into "pixelData"
|
|
|
|
{
|
2013-07-28 17:49:50 +03:00
|
|
|
ResourceID resID(std::string("SPRITES/") + name, EResType::ANIMATION);
|
|
|
|
|
|
|
|
auto data = CResourceHandler::get()->load(resID)->readAll();
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
pixelData = std::move(data.first);
|
|
|
|
pixelDataSize = data.second;
|
|
|
|
}
|
|
|
|
|
2016-08-30 04:38:56 +02:00
|
|
|
CMemoryStream stm(pixelData.get(), pixelDataSize);
|
|
|
|
|
|
|
|
CBinaryReader reader(&stm);
|
2008-06-07 20:16:52 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
reader.readInt32(); // def type, unused
|
2010-10-31 00:53:41 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
fullWidth = reader.readInt32();
|
|
|
|
fullHeight = reader.readInt32();
|
|
|
|
|
|
|
|
int totalBlocks = reader.readInt32();
|
2008-06-07 20:16:52 +03:00
|
|
|
|
2013-06-29 16:05:48 +03:00
|
|
|
for (auto & elem : palette)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
elem.r = reader.readUInt8();
|
|
|
|
elem.g = reader.readUInt8();
|
|
|
|
elem.b = reader.readUInt8();
|
2015-09-02 17:49:29 +02:00
|
|
|
elem.a = SDL_ALPHA_OPAQUE;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
for (int i=0; i<totalBlocks; i++)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +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
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +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
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void CCreatureAnimation::endAnimation()
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
once = false;
|
|
|
|
auto copy = onAnimationReset;
|
|
|
|
onAnimationReset.clear();
|
|
|
|
copy();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2010-02-12 17:04:01 +02:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
bool CCreatureAnimation::incrementFrame(float timePassed)
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
elapsedTime += timePassed;
|
|
|
|
currentFrame += timePassed * speed;
|
|
|
|
if (currentFrame >= float(framesInGroup(type)))
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-16 21:12:47 +03:00
|
|
|
// just in case of extremely low fps (or insanely high speed)
|
2013-07-06 19:10:20 +03:00
|
|
|
while (currentFrame >= float(framesInGroup(type)))
|
|
|
|
currentFrame -= framesInGroup(type);
|
|
|
|
|
|
|
|
if (once)
|
|
|
|
setType(CCreatureAnim::HOLDING);
|
|
|
|
|
|
|
|
endAnimation();
|
|
|
|
return true;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2013-07-06 19:10:20 +03:00
|
|
|
return false;
|
2008-08-07 03:38:44 +03:00
|
|
|
}
|
2008-09-24 19:25:14 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void CCreatureAnimation::setBorderColor(SDL_Color palette)
|
2008-09-24 19:25:14 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
border = palette;
|
2008-09-24 19:25:14 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
int CCreatureAnimation::getWidth() const
|
2011-05-26 17:47:45 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
return fullWidth;
|
2011-05-26 17:47:45 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
int CCreatureAnimation::getHeight() const
|
2009-10-13 05:01:29 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
return fullHeight;
|
2009-10-13 05:01:29 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
float CCreatureAnimation::getCurrentFrame() const
|
2009-03-05 19:57:26 +02:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
return currentFrame;
|
2009-03-05 19:57:26 +02:00
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
void CCreatureAnimation::playOnce( CCreatureAnim::EAnimType type )
|
2009-02-21 17:43:28 +02:00
|
|
|
{
|
|
|
|
setType(type);
|
|
|
|
once = true;
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
inline int getBorderStrength(float time)
|
|
|
|
{
|
2013-07-07 11:27:27 +03:00
|
|
|
float borderStrength = fabs(vstd::round(time) - time) * 2; // generate value in range 0-1
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
return borderStrength * 155 + 100; // scale to 0-255
|
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_Color genShadow(ui8 alpha)
|
|
|
|
{
|
2013-07-07 11:27:27 +03:00
|
|
|
return CSDL_Ext::makeColor(0, 0, 0, alpha);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static SDL_Color genBorderColor(ui8 alpha, const SDL_Color & base)
|
|
|
|
{
|
2014-05-21 19:04:34 +03:00
|
|
|
return CSDL_Ext::makeColor(base.r, base.g, base.b, ui8(base.a * alpha / 256));
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2014-05-21 19:04:34 +03:00
|
|
|
return CSDL_Ext::makeColor(
|
|
|
|
mixChannels(over.r, base.r, over.a, base.a),
|
|
|
|
mixChannels(over.g, base.g, over.a, base.a),
|
|
|
|
mixChannels(over.b, base.b, over.a, base.a),
|
|
|
|
ui8(over.a + base.a * (255 - over.a) / 256)
|
|
|
|
);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2008-08-07 03:38:44 +03:00
|
|
|
|
2010-08-12 08:22:48 +03:00
|
|
|
template<int bpp>
|
2013-07-07 22:44:08 +03:00
|
|
|
void CCreatureAnimation::nextFrameT(SDL_Surface * dest, bool rotate)
|
2013-07-06 19:10:20 +03:00
|
|
|
{
|
|
|
|
assert(dataOffsets.count(type) && dataOffsets.at(type).size() > size_t(currentFrame));
|
2011-10-13 07:33:19 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
ui32 offset = dataOffsets.at(type).at(floor(currentFrame));
|
2011-10-13 07:33:19 +03:00
|
|
|
|
2016-08-30 04:38:56 +02:00
|
|
|
CMemoryStream stm(pixelData.get(), pixelDataSize);
|
|
|
|
|
|
|
|
CBinaryReader reader(&stm);
|
2011-10-13 07:33:19 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
reader.getStream()->seek(offset);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2013-07-06 19:10:20 +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
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
const int rightMargin = fullWidth - spriteWidth - leftMargin;
|
|
|
|
//const int bottomMargin = fullHeight - spriteHeight - topMargin;
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
const size_t baseOffset = reader.getStream()->tell();
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
assert(defType2 == 1);
|
2013-11-09 16:49:36 +03:00
|
|
|
UNUSED(defType2);
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
auto specialPalette = genSpecialPalette();
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2013-07-06 19:10:20 +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();
|
|
|
|
|
2013-07-07 22:44:08 +03:00
|
|
|
size_t destX = pos.x;
|
2013-07-06 19:10:20 +03:00
|
|
|
if (rotate)
|
|
|
|
destX += rightMargin + spriteWidth - 1;
|
|
|
|
else
|
|
|
|
destX += leftMargin;
|
2010-04-04 11:47:00 +03:00
|
|
|
|
2013-07-07 22:44:08 +03:00
|
|
|
size_t destY = pos.y + topMargin + i;
|
2013-07-06 19:10:20 +03:00
|
|
|
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
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
for (size_t j=0; j<length; j++)
|
2013-07-07 22:44:08 +03:00
|
|
|
putPixelAt<bpp>(dest, destX + (rotate?(-j):(j)), destY, lineData[currentOffset + j], specialPalette);
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
currentOffset += length;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
2013-07-06 19:10:20 +03:00
|
|
|
else// RLE
|
|
|
|
{
|
|
|
|
if (type != 0) // transparency row, handle it here for speed
|
|
|
|
{
|
|
|
|
for (size_t j=0; j<length; j++)
|
2013-07-07 22:44:08 +03:00
|
|
|
putPixelAt<bpp>(dest, destX + (rotate?(-j):(j)), destY, type, specialPalette);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
destX += rotate ? (-length) : (length);
|
|
|
|
totalRowLength += length;
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 22:44:08 +03:00
|
|
|
void CCreatureAnimation::nextFrame(SDL_Surface *dest, bool attacker)
|
2010-08-12 08:22:48 +03:00
|
|
|
{
|
2013-07-07 22:44:08 +03:00
|
|
|
// Note: please notice that attacker value is inversed when passed further.
|
|
|
|
// This is intended behavior because "attacker" actually does not needs rotation
|
2010-08-12 08:22:48 +03:00
|
|
|
switch(dest->format->BytesPerPixel)
|
|
|
|
{
|
2013-07-07 22:44:08 +03:00
|
|
|
case 2: return nextFrameT<2>(dest, !attacker);
|
|
|
|
case 3: return nextFrameT<3>(dest, !attacker);
|
|
|
|
case 4: return nextFrameT<4>(dest, !attacker);
|
2010-08-12 08:22:48 +03:00
|
|
|
default:
|
2016-03-12 03:41:27 +02:00
|
|
|
logGlobal->errorStream() << (int)dest->format->BitsPerPixel << " bpp is not supported!!!";
|
2010-08-12 08:22:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-02-09 15:56:30 +02:00
|
|
|
int CCreatureAnimation::framesInGroup(CCreatureAnim::EAnimType group) const
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +03:00
|
|
|
if(dataOffsets.count(group) == 0)
|
2009-02-20 15:06:20 +02:00
|
|
|
return 0;
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
return dataOffsets.at(group).size();
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
ui8 * CCreatureAnimation::getPixelAddr(SDL_Surface * dest, int X, int Y) const
|
2008-06-07 20:16:52 +03:00
|
|
|
{
|
2013-07-06 19:10:20 +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>
|
2013-07-07 22:44:08 +03:00
|
|
|
inline void CCreatureAnimation::putPixelAt(SDL_Surface * dest, int X, int Y, size_t index, const std::array<SDL_Color, 8> & special) const
|
2013-07-06 19:10:20 +03:00
|
|
|
{
|
2013-09-02 01:55:57 +03:00
|
|
|
if ( X < pos.x + pos.w && Y < pos.y + pos.h && X >= 0 && Y >= 0)
|
2013-08-01 01:24:44 +03:00
|
|
|
putPixel<bpp>(getPixelAddr(dest, X, Y), palette[index], index, special);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
2010-08-12 08:22:48 +03:00
|
|
|
|
2013-07-06 19:10:20 +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];
|
2014-05-21 19:04:34 +03:00
|
|
|
ColorPutter<bpp, 0>::PutColor(dest, pal.r, pal.g, pal.b, pal.a);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ColorPutter<bpp, 0>::PutColor(dest, color.r, color.g, color.b);
|
2008-06-07 20:16:52 +03:00
|
|
|
}
|
|
|
|
}
|
2013-07-06 19:10:20 +03:00
|
|
|
|
|
|
|
bool CCreatureAnimation::isDead() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::DEAD
|
|
|
|
|| getType() == CCreatureAnim::DEATH;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCreatureAnimation::isIdle() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::HOLDING
|
|
|
|
|| getType() == CCreatureAnim::MOUSEON;
|
|
|
|
}
|
|
|
|
|
2013-07-21 13:10:38 +03:00
|
|
|
bool CCreatureAnimation::isMoving() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::MOVE_START
|
|
|
|
|| getType() == CCreatureAnim::MOVING
|
|
|
|
|| getType() == CCreatureAnim::MOVE_END;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CCreatureAnimation::isShooting() const
|
|
|
|
{
|
|
|
|
return getType() == CCreatureAnim::SHOOT_UP
|
|
|
|
|| getType() == CCreatureAnim::SHOOT_FRONT
|
|
|
|
|| getType() == CCreatureAnim::SHOOT_DOWN;
|
|
|
|
}
|
|
|
|
|
2013-07-06 19:10:20 +03:00
|
|
|
void CCreatureAnimation::pause()
|
|
|
|
{
|
|
|
|
speed = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCreatureAnimation::play()
|
|
|
|
{
|
2013-11-06 16:42:58 +03:00
|
|
|
speed = 0;
|
|
|
|
if (speedController(this, type) != 0)
|
|
|
|
speed = 1 / speedController(this, type);
|
2013-07-06 19:10:20 +03:00
|
|
|
}
|