显示FPS配置移动到Config类中
This commit is contained in:
		
							parent
							
								
									167bf51d4c
								
							
						
					
					
						commit
						109bebb1e4
					
				|  | @ -39,7 +39,9 @@ void e2d::Renderer::destroyInstance() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| e2d::Renderer::Renderer() | e2d::Renderer::Renderer() | ||||||
| 	: _showFps(false) | 	: _renderTimes(0) | ||||||
|  | 	, _lastRenderTime(0) | ||||||
|  | 	, _fpsText() | ||||||
| 	, _renderTarget(nullptr) | 	, _renderTarget(nullptr) | ||||||
| 	, _solidBrush(nullptr) | 	, _solidBrush(nullptr) | ||||||
| 	, _textRenderer(nullptr) | 	, _textRenderer(nullptr) | ||||||
|  | @ -133,50 +135,9 @@ void e2d::Renderer::render() | ||||||
| 	SceneManager::getInstance()->render(); | 	SceneManager::getInstance()->render(); | ||||||
| 
 | 
 | ||||||
| 	// äÖȾ FPS
 | 	// äÖȾ FPS
 | ||||||
| 	if (_showFps) | 	if (Game::getInstance()->getConfig()->isFpsShow()) | ||||||
| 	{ | 	{ | ||||||
| 		static int s_nRenderTimes = 0; | 		_renderFps(); | ||||||
| 		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); |  | ||||||
| 		} |  | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	// ÖÕÖ¹äÖȾ
 | 	// ÖÕÖ¹äÖȾ
 | ||||||
|  | @ -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() | e2d::Color e2d::Renderer::getBackgroundColor() | ||||||
| { | { | ||||||
| 	return Color(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a); | 	return Color(_clearColor.r, _clearColor.g, _clearColor.b, _clearColor.a); | ||||||
|  | @ -206,11 +211,6 @@ void e2d::Renderer::setBackgroundColor(Color color) | ||||||
| 	_clearColor = color.toD2DColorF(); | 	_clearColor = color.toD2DColorF(); | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| void e2d::Renderer::showFps(bool show) |  | ||||||
| { |  | ||||||
| 	_showFps = show; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| ID2D1HwndRenderTarget * e2d::Renderer::getRenderTarget() | ID2D1HwndRenderTarget * e2d::Renderer::getRenderTarget() | ||||||
| { | { | ||||||
| 	return _renderTarget; | 	return _renderTarget; | ||||||
|  |  | ||||||
|  | @ -5,6 +5,7 @@ e2d::Config::Config() | ||||||
| 	: _gameName() | 	: _gameName() | ||||||
| 	, _defaultNodePivot() | 	, _defaultNodePivot() | ||||||
| 	, _soundEnabled(true) | 	, _soundEnabled(true) | ||||||
|  | 	, _showFps(false) | ||||||
| 	, _outlineVisible(false) | 	, _outlineVisible(false) | ||||||
| 	, _collisionEnabled(false) | 	, _collisionEnabled(false) | ||||||
| 	, _colliderVisible(false) | 	, _colliderVisible(false) | ||||||
|  | @ -22,6 +23,11 @@ void e2d::Config::setGameName(const String & name) | ||||||
| 	_gameName = name; | 	_gameName = name; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | void e2d::Config::showFps(bool show) | ||||||
|  | { | ||||||
|  | 	_showFps = show; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| void e2d::Config::setOutlineVisible(bool visible) | void e2d::Config::setOutlineVisible(bool visible) | ||||||
| { | { | ||||||
| 	_outlineVisible = visible; | 	_outlineVisible = visible; | ||||||
|  | @ -69,6 +75,11 @@ bool e2d::Config::isSoundEnabled() const | ||||||
| 	return _soundEnabled; | 	return _soundEnabled; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | bool e2d::Config::isFpsShow() const | ||||||
|  | { | ||||||
|  | 	return _showFps; | ||||||
|  | } | ||||||
|  | 
 | ||||||
| bool e2d::Config::isOutlineVisible() const | bool e2d::Config::isOutlineVisible() const | ||||||
| { | { | ||||||
| 	return _outlineVisible; | 	return _outlineVisible; | ||||||
|  |  | ||||||
|  | @ -71,7 +71,7 @@ void e2d::CollisionManager::__updateCollider(Collider* collider) | ||||||
| 			relation != Collider::Relation::Disjoin) | 			relation != Collider::Relation::Disjoin) | ||||||
| 		{ | 		{ | ||||||
| 			Collision collision(passive, relation); | 			Collision collision(passive, relation); | ||||||
| 			SceneManager::getInstance()->getCurrentScene()->onCollision(collision); | 			active->getParentScene()->onCollision(collision); | ||||||
| 			active->onCollision(collision); | 			active->onCollision(collision); | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
|  | @ -149,7 +149,7 @@ void e2d::Node::_render() | ||||||
| 		// 转换渲染器的二维矩阵
 | 		// 转换渲染器的二维矩阵
 | ||||||
| 		Renderer::getInstance()->getRenderTarget()->SetTransform(_finalMatri); | 		Renderer::getInstance()->getRenderTarget()->SetTransform(_finalMatri); | ||||||
| 		// 渲染自身
 | 		// 渲染自身
 | ||||||
| 		this->onRender(); | 		//this->onRender();
 | ||||||
| 	} | 	} | ||||||
| 	else | 	else | ||||||
| 	{ | 	{ | ||||||
|  | @ -174,7 +174,7 @@ void e2d::Node::_render() | ||||||
| 		// 转换渲染器的二维矩阵
 | 		// 转换渲染器的二维矩阵
 | ||||||
| 		Renderer::getInstance()->getRenderTarget()->SetTransform(_finalMatri); | 		Renderer::getInstance()->getRenderTarget()->SetTransform(_finalMatri); | ||||||
| 		// 渲染自身
 | 		// 渲染自身
 | ||||||
| 		this->onRender(); | 		//this->onRender();
 | ||||||
| 
 | 
 | ||||||
| 		// 访问剩余节点
 | 		// 访问剩余节点
 | ||||||
| 		for (; i < _children.size(); ++i) | 		for (; i < _children.size(); ++i) | ||||||
|  |  | ||||||
|  | @ -323,11 +323,6 @@ public: | ||||||
| 		Color color | 		Color color | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
| 	// ÏÔʾ FPS
 |  | ||||||
| 	void showFps( |  | ||||||
| 		bool show = true |  | ||||||
| 	); |  | ||||||
| 
 |  | ||||||
| 	// 渲染游戏画面
 | 	// 渲染游戏画面
 | ||||||
| 	void render(); | 	void render(); | ||||||
| 
 | 
 | ||||||
|  | @ -374,8 +369,13 @@ private: | ||||||
| 	// 删除设备相关资源
 | 	// 删除设备相关资源
 | ||||||
| 	void __discardDeviceResources(); | 	void __discardDeviceResources(); | ||||||
| 
 | 
 | ||||||
|  | 	// äÖȾ FPS
 | ||||||
|  | 	void _renderFps(); | ||||||
|  | 
 | ||||||
| private: | private: | ||||||
| 	bool					_showFps; | 	int						_renderTimes; | ||||||
|  | 	double					_lastRenderTime; | ||||||
|  | 	String					_fpsText; | ||||||
| 	D2D1_COLOR_F			_clearColor; | 	D2D1_COLOR_F			_clearColor; | ||||||
| 	ID2D1HwndRenderTarget*	_renderTarget; | 	ID2D1HwndRenderTarget*	_renderTarget; | ||||||
| 	ID2D1SolidColorBrush*	_solidBrush; | 	ID2D1SolidColorBrush*	_solidBrush; | ||||||
|  |  | ||||||
|  | @ -1003,6 +1003,12 @@ public: | ||||||
| 		const String& name | 		const String& name | ||||||
| 	); | 	); | ||||||
| 
 | 
 | ||||||
|  | 	// 显示或隐藏 FPS
 | ||||||
|  | 	// 默认:隐藏
 | ||||||
|  | 	void showFps( | ||||||
|  | 		bool show | ||||||
|  | 	); | ||||||
|  | 
 | ||||||
| 	// 显示或隐藏节点轮廓
 | 	// 显示或隐藏节点轮廓
 | ||||||
| 	// 默认:隐藏
 | 	// 默认:隐藏
 | ||||||
| 	void setOutlineVisible( | 	void setOutlineVisible( | ||||||
|  | @ -1045,6 +1051,9 @@ public: | ||||||
| 	// 获取声音打开状态
 | 	// 获取声音打开状态
 | ||||||
| 	bool isSoundEnabled() const; | 	bool isSoundEnabled() const; | ||||||
| 
 | 
 | ||||||
|  | 	// 获取 FPS 显示状态
 | ||||||
|  | 	bool isFpsShow() const; | ||||||
|  | 
 | ||||||
| 	// 获取节点轮廓显示状态
 | 	// 获取节点轮廓显示状态
 | ||||||
| 	bool isOutlineVisible() const; | 	bool isOutlineVisible() const; | ||||||
| 
 | 
 | ||||||
|  | @ -1065,6 +1074,7 @@ protected: | ||||||
| 
 | 
 | ||||||
| protected: | protected: | ||||||
| 	bool			_unconfigured; | 	bool			_unconfigured; | ||||||
|  | 	bool			_showFps; | ||||||
| 	bool			_soundEnabled; | 	bool			_soundEnabled; | ||||||
| 	bool			_outlineVisible; | 	bool			_outlineVisible; | ||||||
| 	bool			_collisionEnabled; | 	bool			_collisionEnabled; | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue