新增Sprite裁剪函数,修复了最小化窗口时cpu占用高的bug

This commit is contained in:
Nomango 2017-10-18 01:15:25 +08:00
parent 0689e1b0e9
commit ecc1cbffce
6 changed files with 205 additions and 19 deletions

View File

@ -253,11 +253,12 @@ void e2d::EApp::_mainLoop()
nInterval = GetInterval(tLast); nInterval = GetInterval(tLast);
// 判断间隔时间是否足够 // 判断间隔时间是否足够
if (nInterval >= nAnimationInterval) if (nInterval >= nAnimationInterval)
{
if (!m_bPaused)
{ {
// 记录当前时间 // 记录当前时间
tLast = GetNow(); tLast = GetNow();
if (!m_bPaused)
{
// 游戏控制流程 // 游戏控制流程
_onControl(); _onControl();
// 刷新游戏画面 // 刷新游戏画面

View File

@ -164,13 +164,13 @@ void e2d::ENode::_updateTransformToReal()
{ {
// 计算锚点坐标 // 计算锚点坐标
D2D1_POINT_2F anchorPos = D2D1::Point2F( D2D1_POINT_2F anchorPos = D2D1::Point2F(
m_Size.width * m_fAnchorX, getRealWidth() * m_fAnchorX,
m_Size.height * m_fAnchorY getRealHeight() * m_fAnchorY
); );
// 计算左上角坐标 // 计算左上角坐标
D2D1_POINT_2F upperLeftCorner = D2D1::Point2F( D2D1_POINT_2F upperLeftCorner = D2D1::Point2F(
m_Pos.x - m_Size.width * m_fAnchorX, m_Pos.x - getRealWidth() * m_fAnchorX,
m_Pos.y - m_Size.height * m_fAnchorY m_Pos.y - getRealHeight() * m_fAnchorY
); );
// 二维矩形变换 // 二维矩形变换
m_Matri = D2D1::Matrix3x2F::Scale( m_Matri = D2D1::Matrix3x2F::Scale(

View File

@ -28,6 +28,11 @@ static ID2D1Bitmap * GetBitmapFromResource(const e2d::EString & resourceName, co
e2d::ESprite::ESprite() e2d::ESprite::ESprite()
: m_pBitmap(nullptr)
, m_fSourcePosX(0)
, m_fSourcePosY(0)
, m_fSourceWidth(0)
, m_fSourceHeight(0)
{ {
} }
@ -37,12 +42,66 @@ e2d::ESprite::ESprite(const EString & imageFileName)
setImage(imageFileName); setImage(imageFileName);
} }
e2d::ESprite::ESprite(const EString & imageFileName, float x, float y, float width, float height)
{
setImage(imageFileName);
clipImage(x, y, width, height);
}
e2d::ESprite::ESprite(const EString & resourceName, const EString & resourceType) e2d::ESprite::ESprite(const EString & resourceName, const EString & resourceType)
: ESprite() : ESprite()
{ {
setImage(resourceName, resourceType); setImage(resourceName, resourceType);
} }
e2d::ESprite::ESprite(const EString & resourceName, const EString & resourceType, float x, float y, float width, float height)
{
setImage(resourceName, resourceType);
clipImage(x, y, width, height);
}
float e2d::ESprite::getWidth() const
{
return m_fSourceWidth * m_fScaleX;
}
float e2d::ESprite::getHeight() const
{
return m_fSourceHeight * m_fScaleY;
}
e2d::ESize e2d::ESprite::getSize() const
{
return ESize(getWidth(), getHeight());
}
float e2d::ESprite::getRealWidth() const
{
return m_fSourceWidth;
}
float e2d::ESprite::getRealHeight() const
{
return m_fSourceHeight;
}
e2d::ESize e2d::ESprite::getRealSize() const
{
return ESize(m_fSourceWidth, m_fSourceHeight);
}
void e2d::ESprite::setWidth(float)
{
}
void e2d::ESprite::setHeight(float)
{
}
void e2d::ESprite::setSize(float, float)
{
}
void e2d::ESprite::setImage(const EString & fileName) void e2d::ESprite::setImage(const EString & fileName)
{ {
WARN_IF(fileName.empty(), "ESprite cannot load bitmap from NULL file name."); WARN_IF(fileName.empty(), "ESprite cannot load bitmap from NULL file name.");
@ -52,12 +111,20 @@ void e2d::ESprite::setImage(const EString & fileName)
m_sFileName = fileName; m_sFileName = fileName;
SafeReleaseInterface(&pBitmap); SafeReleaseInterface(&m_pBitmap);
pBitmap = GetBitmapFromFile(m_sFileName); m_pBitmap = GetBitmapFromFile(m_sFileName);
ASSERT(pBitmap, "ESprite create device resources failed!"); ASSERT(m_pBitmap, "ESprite create device resources failed!");
this->setSize(pBitmap->GetSize().width, pBitmap->GetSize().height); m_fSourcePosX = m_fSourcePosY = 0;
m_fSourceWidth = m_pBitmap->GetSize().width;
m_fSourceHeight = m_pBitmap->GetSize().height;
}
void e2d::ESprite::setImage(const EString & fileName, float x, float y, float width, float height)
{
setImage(fileName);
clipImage(x, y, width, height);
} }
void e2d::ESprite::setImage(const EString & resourceName, const EString & resourceType) void e2d::ESprite::setImage(const EString & resourceName, const EString & resourceType)
@ -76,23 +143,41 @@ void e2d::ESprite::setImage(const EString & resourceName, const EString & resour
m_sResourceName = resourceName; m_sResourceName = resourceName;
m_sResourceType = resourceType; m_sResourceType = resourceType;
SafeReleaseInterface(&pBitmap); SafeReleaseInterface(&m_pBitmap);
pBitmap = GetBitmapFromResource(resourceName, resourceType); m_pBitmap = GetBitmapFromResource(resourceName, resourceType);
ASSERT(pBitmap, "ESprite create device resources failed!"); ASSERT(m_pBitmap, "ESprite create device resources failed!");
this->setSize(pBitmap->GetSize().width, pBitmap->GetSize().height); m_fSourcePosX = m_fSourcePosY = 0;
m_fSourceWidth = m_pBitmap->GetSize().width;
m_fSourceHeight = m_pBitmap->GetSize().height;
}
void e2d::ESprite::setImage(const EString & resourceName, const EString & resourceType, float x, float y, float width, float height)
{
setImage(resourceName, resourceType);
clipImage(x, y, width, height);
}
void e2d::ESprite::clipImage(float x, float y, float width, float height)
{
m_fSourcePosX = max(x, 0);
m_fSourcePosY = max(y, 0);
m_fSourceWidth = min(max(width, 0), m_pBitmap->GetSize().width - m_fSourcePosX);
m_fSourceHeight = min(max(height, 0), m_pBitmap->GetSize().width - m_fSourcePosX);
} }
void e2d::ESprite::_onRender() void e2d::ESprite::_onRender()
{ {
if (pBitmap) if (m_pBitmap)
{ {
// Draw bitmap // Draw bitmap
GetRenderTarget()->DrawBitmap( GetRenderTarget()->DrawBitmap(
pBitmap, m_pBitmap,
D2D1::RectF(0, 0, getRealWidth(), getRealHeight()), D2D1::RectF(0, 0, m_fSourceWidth, m_fSourceHeight),
m_fDisplayOpacity m_fDisplayOpacity,
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF(m_fSourcePosX, m_fSourcePosY, m_fSourceWidth, m_fSourceHeight)
); );
} }
} }

View File

@ -26,6 +26,16 @@ struct EPoint
this->y = y; this->y = y;
} }
EPoint operator + (EPoint const & p)
{
return EPoint(x + p.x, y + p.y);
}
EPoint operator - (EPoint const & p)
{
return EPoint(x - p.x, y - p.y);
}
float x; float x;
float y; float y;
}; };
@ -46,6 +56,16 @@ struct ESize
this->height = height; this->height = height;
} }
ESize operator + (ESize const & size)
{
return ESize(width + size.width, height + size.height);
}
ESize operator - (ESize const & size)
{
return ESize(width - size.width, height - size.height);
}
float width; float width;
float height; float height;
}; };

View File

@ -52,5 +52,8 @@
#endif #endif
#define DEPRECATED_ATTRIBUTE __declspec(deprecated)
template<typename T> template<typename T>
inline void SafeDelete(T** p) { if (*p) { delete *p; *p = nullptr; } } inline void SafeDelete(T** p) { if (*p) { delete *p; *p = nullptr; } }

View File

@ -368,23 +368,96 @@ public:
const EString & imageFileName const EString & imageFileName
); );
// 从文件图片创建精灵并裁剪
ESprite(
const EString & imageFileName,
float x,
float y,
float width,
float height
);
// 从资源图片创建精灵 // 从资源图片创建精灵
ESprite( ESprite(
const EString & resourceName, const EString & resourceName,
const EString & resourceType const EString & resourceType
); );
// 从资源图片创建精灵并裁剪
ESprite(
const EString & resourceName,
const EString & resourceType,
float x,
float y,
float width,
float height
);
// 获取精灵宽度
virtual float getWidth() const override;
// 获取精灵高度
virtual float getHeight() const override;
// 获取精灵大小
virtual ESize getSize() const override;
// 获取精灵宽度(不考虑缩放)
virtual float getRealWidth() const override;
// 获取精灵高度(不考虑缩放)
virtual float getRealHeight() const override;
// 获取精灵大小(不考虑缩放)
virtual ESize getRealSize() const override;
// 设置精灵宽度是失效的
virtual void setWidth(float) override;
// 设置精灵高度是失效的
virtual void setHeight(float) override;
// 设置精灵大小是失效的
virtual void setSize(float, float) override;
// 从文件加载图片 // 从文件加载图片
void setImage( void setImage(
const EString & fileName const EString & fileName
); );
// 从文件加载图片并裁剪
void setImage(
const EString & fileName,
float x,
float y,
float width,
float height
);
// 从资源加载图片 // 从资源加载图片
void setImage( void setImage(
const EString & resourceName, const EString & resourceName,
const EString & resourceType const EString & resourceType
); );
// 从资源加载图片并裁剪
void setImage(
const EString & resourceName,
const EString & resourceType,
float x,
float y,
float width,
float height
);
// 裁剪原图片
void clipImage(
float x,
float y,
float width,
float height
);
// 预加载资源 // 预加载资源
static bool preloadImage( static bool preloadImage(
const EString & fileName const EString & fileName
@ -404,10 +477,14 @@ protected:
virtual void _onRender() override; virtual void _onRender() override;
protected: protected:
float m_fSourcePosX;
float m_fSourcePosY;
float m_fSourceWidth;
float m_fSourceHeight;
EString m_sFileName; EString m_sFileName;
EString m_sResourceName; EString m_sResourceName;
EString m_sResourceType; EString m_sResourceType;
ID2D1Bitmap * pBitmap; ID2D1Bitmap * m_pBitmap;
}; };
} }