From 109bebb1e44ddd3ac614e9e1ad84e4f666978894 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Tue, 17 Jul 2018 12:32:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=BE=E7=A4=BAFPS=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E7=A7=BB=E5=8A=A8=E5=88=B0Config=E7=B1=BB=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Base/Renderer.cpp | 98 +++++++++++++++---------------- core/Common/Config.cpp | 11 ++++ core/Manager/CollisionManager.cpp | 2 +- core/Node/Node.cpp | 4 +- core/e2dbase.h | 12 ++-- core/e2dcommon.h | 10 ++++ 6 files changed, 79 insertions(+), 58 deletions(-) diff --git a/core/Base/Renderer.cpp b/core/Base/Renderer.cpp index a553b62f..d6395ed8 100644 --- a/core/Base/Renderer.cpp +++ b/core/Base/Renderer.cpp @@ -39,7 +39,9 @@ void e2d::Renderer::destroyInstance() } e2d::Renderer::Renderer() - : _showFps(false) + : _renderTimes(0) + , _lastRenderTime(0) + , _fpsText() , _renderTarget(nullptr) , _solidBrush(nullptr) , _textRenderer(nullptr) @@ -133,50 +135,9 @@ void e2d::Renderer::render() SceneManager::getInstance()->render(); // 渲染 FPS - if (_showFps) + if (Game::getInstance()->getConfig()->isFpsShow()) { - static int s_nRenderTimes = 0; - static double s_fLastRenderTime = 0; - static String s_sFpsText; - - ++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; - } - - IDWriteTextLayout * pTextLayout = nullptr; - IDWriteTextFormat * pTextFormat = Renderer::getFpsTextFormat(); - - hr = _writeFactory->CreateTextLayout( - (const WCHAR *)s_sFpsText, - (UINT32)s_sFpsText.getLength(), - pTextFormat, - 0, - 0, - &pTextLayout - ); - - if (SUCCEEDED(hr)) - { - _renderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); - _solidBrush->SetOpacity(1.0f); - _textRenderer->SetTextStyle( - D2D1::ColorF(D2D1::ColorF::White), - TRUE, - D2D1::ColorF(D2D1::ColorF::Black, 0.4f), - 1.5f, - D2D1_LINE_JOIN_ROUND - ); - - pTextLayout->Draw(nullptr, _textRenderer, 10, 0); - - SafeRelease(pTextLayout); - } + _renderFps(); } // 终止渲染 @@ -196,6 +157,50 @@ void e2d::Renderer::render() } } +void e2d::Renderer::_renderFps() +{ + ++_renderTimes; + + double fDelay = Time::getTotalTime() - _lastRenderTime; + if (fDelay >= 0.1) + { + _fpsText = String::format(L"FPS: %.1lf", (1 / fDelay) * _renderTimes); + _lastRenderTime = Time::getTotalTime(); + _renderTimes = 0; + } + + IDWriteTextLayout * pTextLayout = nullptr; + IDWriteTextFormat * pTextFormat = Renderer::getFpsTextFormat(); + + HRESULT hr = _writeFactory->CreateTextLayout( + (const WCHAR *)_fpsText, + (UINT32)_fpsText.getLength(), + pTextFormat, + 0, + 0, + &pTextLayout + ); + + if (SUCCEEDED(hr)) + { + _renderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); + _solidBrush->SetOpacity(1.0f); + + auto textRenderer = this->getTextRenderer(); + textRenderer->SetTextStyle( + D2D1::ColorF(D2D1::ColorF::White), + TRUE, + D2D1::ColorF(D2D1::ColorF::Black, 0.4f), + 1.5f, + D2D1_LINE_JOIN_ROUND + ); + + pTextLayout->Draw(nullptr, textRenderer, 10, 0); + + SafeRelease(pTextLayout); + } +} + e2d::Color e2d::Renderer::getBackgroundColor() { return Color(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a); @@ -206,11 +211,6 @@ void e2d::Renderer::setBackgroundColor(Color color) _clearColor = color.toD2DColorF(); } -void e2d::Renderer::showFps(bool show) -{ - _showFps = show; -} - ID2D1HwndRenderTarget * e2d::Renderer::getRenderTarget() { return _renderTarget; diff --git a/core/Common/Config.cpp b/core/Common/Config.cpp index f4f4f91b..89512686 100644 --- a/core/Common/Config.cpp +++ b/core/Common/Config.cpp @@ -5,6 +5,7 @@ e2d::Config::Config() : _gameName() , _defaultNodePivot() , _soundEnabled(true) + , _showFps(false) , _outlineVisible(false) , _collisionEnabled(false) , _colliderVisible(false) @@ -22,6 +23,11 @@ void e2d::Config::setGameName(const String & name) _gameName = name; } +void e2d::Config::showFps(bool show) +{ + _showFps = show; +} + void e2d::Config::setOutlineVisible(bool visible) { _outlineVisible = visible; @@ -69,6 +75,11 @@ bool e2d::Config::isSoundEnabled() const return _soundEnabled; } +bool e2d::Config::isFpsShow() const +{ + return _showFps; +} + bool e2d::Config::isOutlineVisible() const { return _outlineVisible; diff --git a/core/Manager/CollisionManager.cpp b/core/Manager/CollisionManager.cpp index e3c71fc7..0aafe0e9 100644 --- a/core/Manager/CollisionManager.cpp +++ b/core/Manager/CollisionManager.cpp @@ -71,7 +71,7 @@ void e2d::CollisionManager::__updateCollider(Collider* collider) relation != Collider::Relation::Disjoin) { Collision collision(passive, relation); - SceneManager::getInstance()->getCurrentScene()->onCollision(collision); + active->getParentScene()->onCollision(collision); active->onCollision(collision); } } diff --git a/core/Node/Node.cpp b/core/Node/Node.cpp index 29bed8c6..661ee7d9 100644 --- a/core/Node/Node.cpp +++ b/core/Node/Node.cpp @@ -149,7 +149,7 @@ void e2d::Node::_render() // 转换渲染器的二维矩阵 Renderer::getInstance()->getRenderTarget()->SetTransform(_finalMatri); // 渲染自身 - this->onRender(); + //this->onRender(); } else { @@ -174,7 +174,7 @@ void e2d::Node::_render() // 转换渲染器的二维矩阵 Renderer::getInstance()->getRenderTarget()->SetTransform(_finalMatri); // 渲染自身 - this->onRender(); + //this->onRender(); // 访问剩余节点 for (; i < _children.size(); ++i) diff --git a/core/e2dbase.h b/core/e2dbase.h index 9e5b8c02..54ccbb75 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -323,11 +323,6 @@ public: Color color ); - // 显示 FPS - void showFps( - bool show = true - ); - // 渲染游戏画面 void render(); @@ -374,8 +369,13 @@ private: // 删除设备相关资源 void __discardDeviceResources(); + // 渲染 FPS + void _renderFps(); + private: - bool _showFps; + int _renderTimes; + double _lastRenderTime; + String _fpsText; D2D1_COLOR_F _clearColor; ID2D1HwndRenderTarget* _renderTarget; ID2D1SolidColorBrush* _solidBrush; diff --git a/core/e2dcommon.h b/core/e2dcommon.h index 6b1d61ac..98690f0b 100644 --- a/core/e2dcommon.h +++ b/core/e2dcommon.h @@ -1003,6 +1003,12 @@ public: const String& name ); + // 显示或隐藏 FPS + // 默认:隐藏 + void showFps( + bool show + ); + // 显示或隐藏节点轮廓 // 默认:隐藏 void setOutlineVisible( @@ -1045,6 +1051,9 @@ public: // 获取声音打开状态 bool isSoundEnabled() const; + // 获取 FPS 显示状态 + bool isFpsShow() const; + // 获取节点轮廓显示状态 bool isOutlineVisible() const; @@ -1065,6 +1074,7 @@ protected: protected: bool _unconfigured; + bool _showFps; bool _soundEnabled; bool _outlineVisible; bool _collisionEnabled;