diff --git a/core/Base/Renderer.cpp b/core/Base/Renderer.cpp index 3db2669c..622cd1c3 100644 --- a/core/Base/Renderer.cpp +++ b/core/Base/Renderer.cpp @@ -1,5 +1,6 @@ #include "..\ebase.h" #include "..\emanager.h" +#include "..\enode.h" static ID2D1Factory * s_pDirect2dFactory = nullptr; static ID2D1HwndRenderTarget * s_pRenderTarget = nullptr; @@ -8,6 +9,13 @@ static IWICImagingFactory * s_pIWICFactory = nullptr; static IDWriteFactory * s_pDWriteFactory = nullptr; static D2D1_COLOR_F s_nClearColor = D2D1::ColorF(D2D1::ColorF::Black); +static bool s_bShowFps = false; +static int s_nRenderTimes = 0; +static double s_fLastRenderTime = 0; +static e2d::String s_sFpsText = L""; +static IDWriteTextFormat * s_pTextFormat = nullptr; + + bool e2d::Renderer::__createDeviceIndependentResources() { @@ -43,6 +51,21 @@ bool e2d::Renderer::__createDeviceIndependentResources() ASSERT(SUCCEEDED(hr), "Create IDWriteFactory Failed!"); } + if (SUCCEEDED(hr)) + { + // 创建文本格式化对象 + hr = s_pDWriteFactory->CreateTextFormat( + L"", + NULL, + DWRITE_FONT_WEIGHT_NORMAL, + DWRITE_FONT_STYLE_NORMAL, + DWRITE_FONT_STRETCH_NORMAL, + 18, + L"", + &s_pTextFormat + ); + } + return SUCCEEDED(hr); } @@ -119,6 +142,27 @@ void e2d::Renderer::__render() // 渲染场景 SceneManager::__render(); + // 渲染 FPS + if (s_bShowFps) + { + s_nRenderTimes++; + + double fDelay = Time::getTotalTime() - s_fLastRenderTime; + if (fDelay >= 0.3) + { + s_sFpsText = String::format(L"FPS: %.1lf", (1 / fDelay) * s_nRenderTimes); + s_fLastRenderTime = Time::getTotalTime(); + s_nRenderTimes = 0; + } + + D2D1_SIZE_F renderTargetSize = s_pRenderTarget->GetSize(); + D2D1_RECT_F rect = D2D1::RectF(0, 0, renderTargetSize.width, renderTargetSize.height); + + s_pSolidBrush->SetColor(D2D1::ColorF(D2D1::ColorF::White)); + s_pRenderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); + s_pRenderTarget->DrawTextW(s_sFpsText, (UINT32)s_sFpsText.getLength(), s_pTextFormat, rect, s_pSolidBrush); + } + // 终止渲染 hr = s_pRenderTarget->EndDraw(); @@ -144,6 +188,11 @@ void e2d::Renderer::setBackgroundColor(UINT32 color) s_nClearColor = D2D1::ColorF(color); } +void e2d::Renderer::showFps(bool show) +{ + s_bShowFps = show; +} + ID2D1Factory * e2d::Renderer::getID2D1Factory() { return s_pDirect2dFactory; diff --git a/core/Base/Time.cpp b/core/Base/Time.cpp index 7a01fccb..f5e381dd 100644 --- a/core/Base/Time.cpp +++ b/core/Base/Time.cpp @@ -39,7 +39,7 @@ static milliseconds s_tExceptedInvertal; bool e2d::Time::__init() { s_tStart = s_tLastUpdate = s_tFixedUpdate = s_tNow = steady_clock::now(); - s_tExceptedInvertal = milliseconds(17); + s_tExceptedInvertal = milliseconds(15); return true; } @@ -107,7 +107,7 @@ bool e2d::Time::__init() ::QueryPerformanceFrequency(&s_tFreq); // 获取时钟频率 ::QueryPerformanceCounter(&s_tNow); // 刷新当前时间 s_tStart = s_tLastUpdate = s_tFixedUpdate = s_tNow; - s_tExceptedInvertal = 17LL * s_tFreq.QuadPart / 1000LL; + s_tExceptedInvertal = 15LL * s_tFreq.QuadPart / 1000LL; return true; } diff --git a/core/Common/String.cpp b/core/Common/String.cpp index 9374d87e..cace3304 100644 --- a/core/Common/String.cpp +++ b/core/Common/String.cpp @@ -90,7 +90,7 @@ e2d::String e2d::String::parse(double value) return std::move(tmp); } -e2d::String & e2d::String::format(const char * format, ...) +e2d::String e2d::String::format(const char * format, ...) { std::string tmp; @@ -108,11 +108,11 @@ e2d::String & e2d::String::format(const char * format, ...) va_end(marker); - m_str = static_cast(_bstr_t(tmp.c_str())); - return (*this); + String str = tmp.c_str(); + return std::move(str); } -e2d::String & e2d::String::format(const wchar_t * format, ...) +e2d::String e2d::String::format(const wchar_t * format, ...) { std::wstring tmp; @@ -130,8 +130,8 @@ e2d::String & e2d::String::format(const wchar_t * format, ...) va_end(marker); - m_str = tmp.c_str(); - return (*this); + String str = tmp.c_str(); + return std::move(str); } e2d::String & e2d::String::operator=(const String &str) diff --git a/core/Node/Text.cpp b/core/Node/Text.cpp index c376fb2b..9ee02a17 100644 --- a/core/Node/Text.cpp +++ b/core/Node/Text.cpp @@ -245,7 +245,7 @@ void e2d::Text::_createFormat() m_Font.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL, DWRITE_FONT_STRETCH_NORMAL, static_cast(m_Font.size), - L"zh-cn", + L"", &m_pDWriteTextFormat ); diff --git a/core/Transition/Transition.cpp b/core/Transition/Transition.cpp index c23a1f8d..a7e6ca15 100644 --- a/core/Transition/Transition.cpp +++ b/core/Transition/Transition.cpp @@ -1,5 +1,6 @@ #include "..\ebase.h" #include "..\etransition.h" +#include "..\enode.h" e2d::Transition::Transition(double duration) : m_bEnd(false) @@ -49,10 +50,8 @@ void e2d::Transition::_init(Scene * prev, Scene * next) if (m_pPrevScene) m_pPrevScene->retain(); if (m_pNextScene) m_pNextScene->retain(); - auto size = Window::getSize(); - m_sPrevLayerParam = m_sNextLayerParam = D2D1::LayerParameters( - D2D1::RectF(0, 0, float(size.width), float(size.height)) - ); + m_WindowSize = Window::getSize(); + m_sPrevLayerParam = m_sNextLayerParam = D2D1::LayerParameters(); } void e2d::Transition::_update() @@ -83,27 +82,45 @@ void e2d::Transition::_update() void e2d::Transition::_render() { auto pRT = Renderer::getRenderTarget(); - + Size windowSize = Window::getSize(); if (m_pPrevScene) { + Point rootPos = m_pPrevScene->getRoot()->getPos(); + auto clipRect = D2D1::RectF( + float(max(rootPos.x, 0)), + float(max(rootPos.y, 0)), + float(min(rootPos.x + windowSize.width, windowSize.width)), + float(min(rootPos.y + windowSize.height, windowSize.height)) + ); pRT->SetTransform(D2D1::Matrix3x2F::Identity()); + pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE); pRT->PushLayer(m_sPrevLayerParam, m_pPrevLayer); // 渲染场景 m_pPrevScene->_render(); pRT->PopLayer(); + pRT->PopAxisAlignedClip(); } if (m_pNextScene) { + Point rootPos = m_pNextScene->getRoot()->getPos(); + auto clipRect = D2D1::RectF( + float(max(rootPos.x, 0)), + float(max(rootPos.y, 0)), + float(min(rootPos.x + windowSize.width, windowSize.width)), + float(min(rootPos.y + windowSize.height, windowSize.height)) + ); pRT->SetTransform(D2D1::Matrix3x2F::Identity()); + pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE); pRT->PushLayer(m_sNextLayerParam, m_pNextLayer); // 渲染场景 m_pNextScene->_render(); pRT->PopLayer(); + pRT->PopAxisAlignedClip(); } } diff --git a/core/Transition/TransitionEmerge.cpp b/core/Transition/TransitionEmerge.cpp index 7b39066b..54c27dfd 100644 --- a/core/Transition/TransitionEmerge.cpp +++ b/core/Transition/TransitionEmerge.cpp @@ -6,6 +6,13 @@ e2d::TransitionEmerge::TransitionEmerge(double duration) { } +void e2d::TransitionEmerge::_init(Scene * prev, Scene * next) +{ + Transition::_init(prev, next); + m_sPrevLayerParam.opacity = 1; + m_sNextLayerParam.opacity = 0; +} + void e2d::TransitionEmerge::_updateCustom() { m_sPrevLayerParam.opacity = float(1 - m_fRateOfProgress); @@ -17,13 +24,6 @@ void e2d::TransitionEmerge::_updateCustom() } } -void e2d::TransitionEmerge::_init(Scene * prev, Scene * next) -{ - Transition::_init(prev, next); - m_sPrevLayerParam.opacity = 1; - m_sNextLayerParam.opacity = 0; -} - void e2d::TransitionEmerge::_reset() { } diff --git a/core/Transition/TransitionFade.cpp b/core/Transition/TransitionFade.cpp index 75be51e5..910f0e3b 100644 --- a/core/Transition/TransitionFade.cpp +++ b/core/Transition/TransitionFade.cpp @@ -17,6 +17,23 @@ e2d::TransitionFade::TransitionFade(double fadeOutDuration, double fadeInDuratio { } +void e2d::TransitionFade::_init(Scene * prev, Scene * next) +{ + Transition::_init(prev, next); + if (m_pPrevScene) + { + m_bFadeOutTransioning = true; + m_fDuration = m_fFadeOutDuration; + } + else + { + m_bFadeOutTransioning = false; + m_fDuration = m_fFadeInDuration; + } + m_sPrevLayerParam.opacity = 1; + m_sNextLayerParam.opacity = 0; +} + void e2d::TransitionFade::_updateCustom() { if (m_bFadeOutTransioning) @@ -39,23 +56,6 @@ void e2d::TransitionFade::_updateCustom() } } -void e2d::TransitionFade::_init(Scene * prev, Scene * next) -{ - Transition::_init(prev, next); - if (m_pPrevScene) - { - m_bFadeOutTransioning = true; - m_fDuration = m_fFadeOutDuration; - } - else - { - m_bFadeOutTransioning = false; - m_fDuration = m_fFadeInDuration; - } - m_sPrevLayerParam.opacity = 1; - m_sNextLayerParam.opacity = 0; -} - void e2d::TransitionFade::_reset() { } diff --git a/core/Transition/TransitionMove.cpp b/core/Transition/TransitionMove.cpp index 995838bd..a3add2d6 100644 --- a/core/Transition/TransitionMove.cpp +++ b/core/Transition/TransitionMove.cpp @@ -7,46 +7,10 @@ e2d::TransitionMove::TransitionMove(double duration, int direct) { } -void e2d::TransitionMove::_updateCustom() -{ - if (m_pPrevScene) - { - auto root = m_pPrevScene->getRoot(); - root->setPos(m_Vector * m_fRateOfProgress); - - Point pos = root->getPos(); - m_sPrevLayerParam.contentBounds = D2D1::RectF( - float(max(pos.x, 0)), - float(max(pos.y, 0)), - float(min(pos.x + m_WindowSize.width, m_WindowSize.width)), - float(min(pos.y + m_WindowSize.height, m_WindowSize.height)) - ); - } - if (m_pNextScene) - { - auto root = m_pNextScene->getRoot(); - root->setPos(m_NextPos + m_Vector * m_fRateOfProgress); - - Point pos = root->getPos(); - m_sNextLayerParam.contentBounds = D2D1::RectF( - float(max(pos.x, 0)), - float(max(pos.y, 0)), - float(min(pos.x + m_WindowSize.width, m_WindowSize.width)), - float(min(pos.y + m_WindowSize.height, m_WindowSize.height)) - ); - } - - if (m_fRateOfProgress >= 1) - { - this->_stop(); - } -} - void e2d::TransitionMove::_init(Scene * prev, Scene * next) { Transition::_init(prev, next); - m_WindowSize = Window::getSize(); double width = m_WindowSize.width; double height = m_WindowSize.height; if (m_Direct == Direct::UP) @@ -74,6 +38,25 @@ void e2d::TransitionMove::_init(Scene * prev, Scene * next) m_pNextScene->getRoot()->setPos(m_NextPos); } +void e2d::TransitionMove::_updateCustom() +{ + if (m_pPrevScene) + { + auto root = m_pPrevScene->getRoot(); + root->setPos(m_Vector * m_fRateOfProgress); + } + if (m_pNextScene) + { + auto root = m_pNextScene->getRoot(); + root->setPos(m_NextPos + m_Vector * m_fRateOfProgress); + } + + if (m_fRateOfProgress >= 1) + { + this->_stop(); + } +} + void e2d::TransitionMove::_reset() { if (m_pPrevScene) m_pPrevScene->getRoot()->setPos(0, 0); diff --git a/core/ebase.h b/core/ebase.h index 86afa0fc..3e262d5e 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -234,6 +234,11 @@ public: UINT32 color ); + // 显示 FPS + static void showFps( + bool show = true + ); + // 获取 ID2D1Factory 对象 static ID2D1Factory * getID2D1Factory(); diff --git a/core/ecommon.h b/core/ecommon.h index 8b8328ff..86ec756f 100644 --- a/core/ecommon.h +++ b/core/ecommon.h @@ -142,8 +142,8 @@ public: static String parse(double value); // 格式化字符串 - String& format(const char * format, ...); - String& format(const wchar_t * format, ...); + static String format(const char * format, ...); + static String format(const wchar_t * format, ...); // 赋值运算符 String& operator= (const String &); diff --git a/core/etransition.h b/core/etransition.h index 64c9d9a1..b1547603 100644 --- a/core/etransition.h +++ b/core/etransition.h @@ -47,6 +47,7 @@ protected: double m_fLast; double m_fDuration; double m_fRateOfProgress; + Size m_WindowSize; Scene * m_pPrevScene; Scene * m_pNextScene; ID2D1Layer * m_pPrevLayer; @@ -136,7 +137,6 @@ protected: int m_Direct; Vector m_Vector; Point m_NextPos; - Size m_WindowSize; }; } \ No newline at end of file