修复无效的动作倒转

This commit is contained in:
Nomango 2018-05-14 00:36:01 +08:00
parent 2528e41a58
commit d125e5cf5b
14 changed files with 158 additions and 51 deletions

View File

@ -46,12 +46,6 @@ void e2d::Action::setName(const String& name)
_name = name; _name = name;
} }
e2d::Action * e2d::Action::reverse() const
{
ASSERT(false, "Action cannot be reversed!");
return nullptr;
}
e2d::Node * e2d::Action::getTarget() e2d::Node * e2d::Action::getTarget()
{ {
return _target; return _target;

View File

@ -64,8 +64,8 @@ void e2d::Animate::_update()
target->open(frames[_frameIndex]); target->open(frames[_frameIndex]);
} }
_frameIndex++;
_last += _animation->getInterval(); _last += _animation->getInterval();
_frameIndex++;
if (_frameIndex == frames.size()) if (_frameIndex == frames.size())
{ {
@ -99,11 +99,8 @@ e2d::Animate * e2d::Animate::clone() const
{ {
return new (std::nothrow) Animate(_animation); return new (std::nothrow) Animate(_animation);
} }
else
{
return nullptr; return nullptr;
} }
}
e2d::Animate * e2d::Animate::reverse() const e2d::Animate * e2d::Animate::reverse() const
{ {

View File

@ -10,6 +10,11 @@ e2d::CallFunc * e2d::CallFunc::clone() const
return new (std::nothrow) CallFunc(_func); return new (std::nothrow) CallFunc(_func);
} }
e2d::CallFunc * e2d::CallFunc::reverse() const
{
return new (std::nothrow) CallFunc(_func);
}
void e2d::CallFunc::_init() void e2d::CallFunc::_init()
{ {
} }

View File

@ -11,6 +11,11 @@ e2d::Delay * e2d::Delay::clone() const
return new (std::nothrow) Delay(_delay); return new (std::nothrow) Delay(_delay);
} }
e2d::Delay * e2d::Delay::reverse() const
{
return new (std::nothrow) Delay(_delay);
}
void e2d::Delay::reset() void e2d::Delay::reset()
{ {
Action::reset(); Action::reset();

View File

@ -31,6 +31,11 @@ e2d::Loop * e2d::Loop::clone() const
} }
} }
e2d::Loop * e2d::Loop::reverse() const
{
return new (std::nothrow) Loop(_action);
}
void e2d::Loop::_init() void e2d::Loop::_init()
{ {
Action::_init(); Action::_init();

View File

@ -111,13 +111,17 @@ e2d::Sequence * e2d::Sequence::clone() const
e2d::Sequence * e2d::Sequence::reverse() const e2d::Sequence * e2d::Sequence::reverse() const
{ {
auto sequence = new (std::nothrow) Sequence(); auto sequence = new (std::nothrow) Sequence();
for (const auto& action : _actions) if (!_actions.empty())
{ {
if (action) std::vector<Action*> newActions(_actions.size());
for (auto iter = _actions.crbegin(), iterCrend = _actions.crend(); iter != iterCrend; ++iter)
{ {
sequence->add(action->reverse()); if (*iter)
{
newActions.push_back(*iter);
} }
} }
sequence->_actions.reserve(_actions.size()); sequence->add(newActions);
}
return sequence; return sequence;
} }

View File

@ -109,13 +109,17 @@ e2d::Spawn * e2d::Spawn::clone() const
e2d::Spawn * e2d::Spawn::reverse() const e2d::Spawn * e2d::Spawn::reverse() const
{ {
auto spawn = new (std::nothrow) Spawn(); auto spawn = new (std::nothrow) Spawn();
for (const auto& action : _actions) if (!_actions.empty())
{ {
if (action) std::vector<Action*> newActions(_actions.size());
for (auto iter = _actions.crbegin(), iterCrend = _actions.crend(); iter != iterCrend; ++iter)
{ {
spawn->add(action->reverse()); if (*iter)
{
newActions.push_back(*iter);
} }
} }
spawn->_actions.reserve(_actions.size()); spawn->add(newActions);
}
return spawn; return spawn;
} }

View File

@ -223,8 +223,7 @@ void e2d::Renderer::__render()
if (FAILED(hr)) if (FAILED(hr))
{ {
// 渲染时产生了未知的错误,退出游戏 Window::error(L"Device loss recovery failed. Exiting game.");
ASSERT(false, L"Renderer error: %#X!", hr);
Game::quit(); Game::quit();
} }
} }

View File

@ -68,10 +68,10 @@ const std::vector<e2d::Image*>& e2d::Animation::getFrames() const
e2d::Animation * e2d::Animation::clone() const e2d::Animation * e2d::Animation::clone() const
{ {
auto a = new Animation(_interval); auto animation = new (std::nothrow) Animation(_interval);
for (auto frame : _frames) for (auto frame : _frames)
{ {
a->add(frame); animation->add(frame);
} }
return a; return animation;
} }

View File

@ -1,8 +1,10 @@
#include "..\e2dnode.h" #include "..\e2dcommon.h"
#include "..\e2dbase.h"
#include <map> #include <map>
static std::map<size_t, ID2D1Bitmap*> s_mBitmapsFromFile; static std::map<size_t, ID2D1Bitmap*> s_mBitmapsFromFile;
static std::map<int, ID2D1Bitmap*> s_mBitmapsFromResource; static std::map<int, ID2D1Bitmap*> s_mBitmapsFromResource;
static std::set<ID2D1Bitmap*> s_vBitmaps;
e2d::Image::Image() e2d::Image::Image()
@ -26,6 +28,12 @@ e2d::Image::Image(int resNameId, const String& resType)
this->open(resNameId, resType); this->open(resNameId, resType);
} }
e2d::Image::Image(ID2D1Bitmap * bitmap)
: _bitmap(nullptr)
{
this->open(bitmap);
}
e2d::Image::Image(const String& filePath, double cropX, double cropY, double cropWidth, double cropHeight) e2d::Image::Image(const String& filePath, double cropX, double cropY, double cropWidth, double cropHeight)
: _bitmap(nullptr) : _bitmap(nullptr)
{ {
@ -46,7 +54,7 @@ e2d::Image::~Image()
bool e2d::Image::open(const String& filePath) bool e2d::Image::open(const String& filePath)
{ {
WARN_IF(filePath.isEmpty(), "Image cannot load bitmap from NULL file name."); WARN_IF(filePath.isEmpty(), "Image open failed! Invalid file name.");
if (filePath.isEmpty()) if (filePath.isEmpty())
return false; return false;
@ -57,10 +65,7 @@ bool e2d::Image::open(const String& filePath)
return false; return false;
} }
_bitmap = s_mBitmapsFromFile.at(filePath.getHashCode()); this->_setBitmap(s_mBitmapsFromFile.at(filePath.getHashCode()));
_cropX = _cropY = 0;
_cropWidth = _bitmap->GetSize().width;
_cropHeight = _bitmap->GetSize().height;
return true; return true;
} }
@ -72,13 +77,24 @@ bool e2d::Image::open(int resNameId, const String& resType)
return false; return false;
} }
_bitmap = s_mBitmapsFromResource.at(resNameId); this->_setBitmap(s_mBitmapsFromResource.at(resNameId));
_cropX = _cropY = 0;
_cropWidth = _bitmap->GetSize().width;
_cropHeight = _bitmap->GetSize().height;
return true; return true;
} }
bool e2d::Image::open(ID2D1Bitmap * bitmap)
{
if (bitmap)
{
if (s_vBitmaps.find(bitmap) != s_vBitmaps.end())
{
s_vBitmaps.insert(bitmap);
}
this->_setBitmap(bitmap);
return true;
}
return false;
}
void e2d::Image::crop(double x, double y, double width, double height) void e2d::Image::crop(double x, double y, double width, double height)
{ {
if (_bitmap) if (_bitmap)
@ -156,9 +172,9 @@ e2d::Point e2d::Image::getCropPos() const
return Point(_cropX, _cropY); return Point(_cropX, _cropY);
} }
bool e2d::Image::preload(const String& fileName) bool e2d::Image::preload(const String& filePath)
{ {
if (s_mBitmapsFromFile.find(fileName.getHashCode()) != s_mBitmapsFromFile.end()) if (s_mBitmapsFromFile.find(filePath.getHashCode()) != s_mBitmapsFromFile.end())
{ {
return true; return true;
} }
@ -173,7 +189,7 @@ bool e2d::Image::preload(const String& fileName)
// 创建解码器 // 创建解码器
hr = Renderer::getIWICImagingFactory()->CreateDecoderFromFilename( hr = Renderer::getIWICImagingFactory()->CreateDecoderFromFilename(
fileName, filePath,
NULL, NULL,
GENERIC_READ, GENERIC_READ,
WICDecodeMetadataCacheOnLoad, WICDecodeMetadataCacheOnLoad,
@ -221,7 +237,7 @@ bool e2d::Image::preload(const String& fileName)
// 保存图片指针和图片的 Hash 名 // 保存图片指针和图片的 Hash 名
s_mBitmapsFromFile.insert( s_mBitmapsFromFile.insert(
std::map<size_t, ID2D1Bitmap*>::value_type( std::map<size_t, ID2D1Bitmap*>::value_type(
fileName.getHashCode(), filePath.getHashCode(),
pBitmap) pBitmap)
); );
} }
@ -375,6 +391,23 @@ void e2d::Image::clearCache()
SafeReleaseInterface(bitmap.second); SafeReleaseInterface(bitmap.second);
} }
s_mBitmapsFromResource.clear(); s_mBitmapsFromResource.clear();
for (auto bitmap : s_vBitmaps)
{
SafeReleaseInterface(bitmap);
}
s_vBitmaps.clear();
}
void e2d::Image::_setBitmap(ID2D1Bitmap * bitmap)
{
if (bitmap)
{
_bitmap = bitmap;
_cropX = _cropY = 0;
_cropWidth = _bitmap->GetSize().width;
_cropHeight = _bitmap->GetSize().height;
}
} }
ID2D1Bitmap * e2d::Image::getBitmap() ID2D1Bitmap * e2d::Image::getBitmap()

View File

@ -27,7 +27,7 @@ void e2d::ObjectManager::__update()
} }
else else
{ {
iter++; // 移动迭代器 iter++;
} }
} }
} }

View File

@ -49,7 +49,7 @@ public:
virtual Action * clone() const = 0; virtual Action * clone() const = 0;
// 获取动作的倒转 // 获取动作的倒转
virtual Action * reverse() const; virtual Action * reverse() const = 0;
// 重置动作 // 重置动作
virtual void reset(); virtual void reset();
@ -161,6 +161,13 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual MoveTo * clone() const override; virtual MoveTo * clone() const override;
// 获取该动作的倒转
virtual MoveTo * reverse() const override
{
ASSERT(false, "reverse() not supported in MoveTo");
return nullptr;
}
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -221,6 +228,13 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual JumpTo * clone() const override; virtual JumpTo * clone() const override;
// 获取该动作的倒转
virtual JumpTo * reverse() const override
{
ASSERT(false, "reverse() not supported in JumpTo");
return nullptr;
}
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -290,6 +304,13 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual ScaleTo * clone() const override; virtual ScaleTo * clone() const override;
// 获取该动作的倒转
virtual ScaleTo * reverse() const override
{
ASSERT(false, "reverse() not supported in ScaleTo");
return nullptr;
}
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -344,6 +365,13 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual OpacityTo * clone() const override; virtual OpacityTo * clone() const override;
// 获取该动作的倒转
virtual OpacityTo * reverse() const override
{
ASSERT(false, "reverse() not supported in OpacityTo");
return nullptr;
}
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -427,6 +455,13 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual RotateTo * clone() const override; virtual RotateTo * clone() const override;
// 获取该动作的倒转
virtual RotateTo * reverse() const override
{
ASSERT(false, "reverse() not supported in RotateTo");
return nullptr;
}
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;
@ -449,6 +484,9 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual Delay * clone() const override; virtual Delay * clone() const override;
// 获取该动作的倒转
virtual Delay * reverse() const override;
// 重置动作 // 重置动作
virtual void reset() override; virtual void reset() override;
@ -484,6 +522,9 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual Loop * clone() const override; virtual Loop * clone() const override;
// 获取该动作的倒转
virtual Loop * reverse() const override;
// 重置动作 // 重置动作
virtual void reset() override; virtual void reset() override;
@ -520,6 +561,9 @@ public:
// 获取该动作的拷贝对象 // 获取该动作的拷贝对象
virtual CallFunc * clone() const override; virtual CallFunc * clone() const override;
// 获取该动作的倒转
virtual CallFunc * reverse() const override;
protected: protected:
// 初始化动作 // 初始化动作
virtual void _init() override; virtual void _init() override;

View File

@ -619,6 +619,11 @@ public:
const String& resType /* 图片资源类型 */ const String& resType /* 图片资源类型 */
); );
// 加载位图
Image(
ID2D1Bitmap * bitmap /* 位图资源 */
);
// 加载图片文件并裁剪 // 加载图片文件并裁剪
Image( Image(
const String& filePath, /* 图片文件路径 */ const String& filePath, /* 图片文件路径 */
@ -642,7 +647,7 @@ public:
// 加载图片文件 // 加载图片文件
bool open( bool open(
const String& filePath const String& filePath /* 图片文件路径 */
); );
// 加载图片资源 // 加载图片资源
@ -651,6 +656,11 @@ public:
const String& resType /* 图片资源类型 */ const String& resType /* 图片资源类型 */
); );
// 加载位图
bool open(
ID2D1Bitmap * bitmap /* 位图资源 */
);
// 将图片裁剪为矩形 // 将图片裁剪为矩形
void crop( void crop(
double cropX, /* 裁剪位置 X 坐标 */ double cropX, /* 裁剪位置 X 坐标 */
@ -703,6 +713,12 @@ public:
// 清空缓存 // 清空缓存
static void clearCache(); static void clearCache();
protected:
// 设置 Bitmap
void _setBitmap(
ID2D1Bitmap * bitmap
);
protected: protected:
double _cropX; double _cropX;
double _cropY; double _cropY;
@ -720,7 +736,12 @@ public:
// 创建帧动画 // 创建帧动画
Animation(); Animation();
// 创建特定间隔的帧动画 // 创建帧动画
Animation(
const std::vector<Image*>& frames /* 关键帧数组 */
);
// 创建特定帧间隔的帧动画
Animation( Animation(
double interval /* 帧间隔(秒) */ double interval /* 帧间隔(秒) */
); );
@ -731,11 +752,6 @@ public:
const std::vector<Image*>& frames /* 关键帧数组 */ const std::vector<Image*>& frames /* 关键帧数组 */
); );
// 创建帧动画
Animation(
const std::vector<Image*>& frames /* 关键帧列表 */
);
virtual ~Animation(); virtual ~Animation();
// 添加关键帧 // 添加关键帧

View File

@ -551,6 +551,7 @@ protected:
}; };
// 文本
class Text : class Text :
public Node public Node
{ {