diff --git a/Easy2D/Base/EApp.cpp b/Easy2D/Base/EApp.cpp index 9a1e560f..d77c0308 100644 --- a/Easy2D/Base/EApp.cpp +++ b/Easy2D/Base/EApp.cpp @@ -254,10 +254,11 @@ void e2d::EApp::_mainLoop() // 判断间隔时间是否足够 if (nInterval >= nAnimationInterval) { + // 记录当前时间 + tLast = GetNow(); + if (!m_bPaused) { - // 记录当前时间 - tLast = GetNow(); // 游戏控制流程 _onControl(); // 刷新游戏画面 diff --git a/Easy2D/Node/ENode.cpp b/Easy2D/Node/ENode.cpp index e5f6adf1..588a4f27 100644 --- a/Easy2D/Node/ENode.cpp +++ b/Easy2D/Node/ENode.cpp @@ -164,13 +164,13 @@ void e2d::ENode::_updateTransformToReal() { // 计算锚点坐标 D2D1_POINT_2F anchorPos = D2D1::Point2F( - m_Size.width * m_fAnchorX, - m_Size.height * m_fAnchorY + getRealWidth() * m_fAnchorX, + getRealHeight() * m_fAnchorY ); // 计算左上角坐标 D2D1_POINT_2F upperLeftCorner = D2D1::Point2F( - m_Pos.x - m_Size.width * m_fAnchorX, - m_Pos.y - m_Size.height * m_fAnchorY + m_Pos.x - getRealWidth() * m_fAnchorX, + m_Pos.y - getRealHeight() * m_fAnchorY ); // 二维矩形变换 m_Matri = D2D1::Matrix3x2F::Scale( diff --git a/Easy2D/Node/ESprite.cpp b/Easy2D/Node/ESprite.cpp index f32be238..7836f249 100644 --- a/Easy2D/Node/ESprite.cpp +++ b/Easy2D/Node/ESprite.cpp @@ -28,6 +28,11 @@ static ID2D1Bitmap * GetBitmapFromResource(const e2d::EString & resourceName, co 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); } +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) : ESprite() { 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) { 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; - SafeReleaseInterface(&pBitmap); - pBitmap = GetBitmapFromFile(m_sFileName); + SafeReleaseInterface(&m_pBitmap); + 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) @@ -76,23 +143,41 @@ void e2d::ESprite::setImage(const EString & resourceName, const EString & resour m_sResourceName = resourceName; m_sResourceType = resourceType; - SafeReleaseInterface(&pBitmap); - pBitmap = GetBitmapFromResource(resourceName, resourceType); + SafeReleaseInterface(&m_pBitmap); + 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() { - if (pBitmap) + if (m_pBitmap) { // Draw bitmap GetRenderTarget()->DrawBitmap( - pBitmap, - D2D1::RectF(0, 0, getRealWidth(), getRealHeight()), - m_fDisplayOpacity + m_pBitmap, + D2D1::RectF(0, 0, m_fSourceWidth, m_fSourceHeight), + m_fDisplayOpacity, + D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, + D2D1::RectF(m_fSourcePosX, m_fSourcePosY, m_fSourceWidth, m_fSourceHeight) ); } } diff --git a/Easy2D/ecommon.h b/Easy2D/ecommon.h index 3a9e14f8..947a093d 100644 --- a/Easy2D/ecommon.h +++ b/Easy2D/ecommon.h @@ -26,6 +26,16 @@ struct EPoint 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 y; }; @@ -46,6 +56,16 @@ struct ESize 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 height; }; diff --git a/Easy2D/emacros.h b/Easy2D/emacros.h index 8bc0845f..460d32bc 100644 --- a/Easy2D/emacros.h +++ b/Easy2D/emacros.h @@ -52,5 +52,8 @@ #endif +#define DEPRECATED_ATTRIBUTE __declspec(deprecated) + + template inline void SafeDelete(T** p) { if (*p) { delete *p; *p = nullptr; } } \ No newline at end of file diff --git a/Easy2D/enodes.h b/Easy2D/enodes.h index 0ea52c96..0a0f15ac 100644 --- a/Easy2D/enodes.h +++ b/Easy2D/enodes.h @@ -368,23 +368,96 @@ public: const EString & imageFileName ); + // 从文件图片创建精灵并裁剪 + ESprite( + const EString & imageFileName, + float x, + float y, + float width, + float height + ); + // 从资源图片创建精灵 ESprite( const EString & resourceName, 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( const EString & fileName ); + // 从文件加载图片并裁剪 + void setImage( + const EString & fileName, + float x, + float y, + float width, + float height + ); + // 从资源加载图片 void setImage( const EString & resourceName, 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( const EString & fileName @@ -404,10 +477,14 @@ protected: virtual void _onRender() override; protected: + float m_fSourcePosX; + float m_fSourcePosY; + float m_fSourceWidth; + float m_fSourceHeight; EString m_sFileName; EString m_sResourceName; EString m_sResourceType; - ID2D1Bitmap * pBitmap; + ID2D1Bitmap * m_pBitmap; }; } \ No newline at end of file