Config增加FPS配置

This commit is contained in:
Nomango 2018-07-24 12:49:32 +08:00
parent cca05d9c47
commit dda7e17347
10 changed files with 97 additions and 111 deletions

View File

@ -9,9 +9,10 @@ using namespace std::chrono;
e2d::Game * e2d::Game::_instance = nullptr; e2d::Game * e2d::Game::_instance = nullptr;
e2d::Game::Game() e2d::Game::Game()
: _ended(true) : _quit(true)
, _paused(false) , _paused(false)
, _config(nullptr) , _config()
, _frameInterval(_config.getFrameInterval())
{ {
CoInitialize(nullptr); CoInitialize(nullptr);
@ -20,8 +21,6 @@ e2d::Game::Game()
e2d::Game::~Game() e2d::Game::~Game()
{ {
GC::getInstance()->safeRelease(_config);
CoUninitialize(); CoUninitialize();
} }
@ -68,25 +67,21 @@ void e2d::Game::start()
window->poll(); window->poll();
// ¿ªÊ¼ÓÎÏ· // ¿ªÊ¼ÓÎÏ·
Duration frameInterval(15), interval; Duration interval;
int wait = 0; int wait = 0;
_ended = false; _quit = false;
_last = _now = Time::now(); _last = _now = Time::now();
while (!_ended) while (!_quit)
{ {
_now = Time::now(); _now = Time::now();
interval = _now - _last; interval = _now - _last;
if (frameInterval < interval) if (_frameInterval < interval)
{ {
_last += interval; _last = _now;
if (_config)
{
_config->_update();
}
input->update(); input->update();
timer->update(); timer->update();
actionManager->update(); actionManager->update();
@ -97,7 +92,7 @@ void e2d::Game::start()
} }
else else
{ {
wait = (frameInterval - interval).milliseconds() - 1; wait = (_frameInterval - interval).milliseconds() - 1;
if (wait > 1) if (wait > 1)
{ {
std::this_thread::sleep_for(milliseconds(wait)); std::this_thread::sleep_for(milliseconds(wait));
@ -113,7 +108,7 @@ void e2d::Game::pause()
void e2d::Game::resume() void e2d::Game::resume()
{ {
if (_paused && !_ended) if (_paused && !_quit)
{ {
_last = _now = Time::now(); _last = _now = Time::now();
Timer::getInstance()->updateTime(); Timer::getInstance()->updateTime();
@ -127,21 +122,24 @@ bool e2d::Game::isPaused()
return _paused; return _paused;
} }
void e2d::Game::setConfig(Config* config) void e2d::Game::setConfig(const Config& config)
{ {
if (config && _config != config)
{
if (_config) _config->release();
_config = config; _config = config;
_config->_unconfigured = true;
_config->retain(); if (_config.isSoundEnabled())
{
Player::getInstance()->getXAudio2()->StartEngine();
} }
else
{
Player::getInstance()->getXAudio2()->StopEngine();
}
_frameInterval = Duration(_config.getFrameInterval());
} }
e2d::Config* e2d::Game::getConfig() const e2d::Config& e2d::Game::getConfig()
{ {
if (!_config)
_config = new (e2d::autorelease) Config();
return _config; return _config;
} }
@ -152,7 +150,7 @@ e2d::Duration e2d::Game::getTotalDuration() const
void e2d::Game::quit() void e2d::Game::quit()
{ {
_ended = true; // 这个变量将控制游戏是否结束 _quit = true;
} }
void e2d::Game::cleanup() void e2d::Game::cleanup()

View File

@ -37,10 +37,8 @@ void e2d::Renderer::destroyInstance()
} }
e2d::Renderer::Renderer() e2d::Renderer::Renderer()
: _renderTimes(0) : _lastRenderTime(0)
, _lastRenderTime(0)
, _fpsFormat(nullptr) , _fpsFormat(nullptr)
, _fpsLayout(nullptr)
, _renderTarget(nullptr) , _renderTarget(nullptr)
, _solidBrush(nullptr) , _solidBrush(nullptr)
, _textRenderer(nullptr) , _textRenderer(nullptr)
@ -54,7 +52,6 @@ e2d::Renderer::Renderer()
e2d::Renderer::~Renderer() e2d::Renderer::~Renderer()
{ {
SafeRelease(_fpsFormat); SafeRelease(_fpsFormat);
SafeRelease(_fpsLayout);
SafeRelease(_textRenderer); SafeRelease(_textRenderer);
SafeRelease(_solidBrush); SafeRelease(_solidBrush);
SafeRelease(_renderTarget); SafeRelease(_renderTarget);
@ -131,7 +128,7 @@ void e2d::Renderer::render()
SceneManager::getInstance()->render(); SceneManager::getInstance()->render();
// äÖȾ FPS // äÖȾ FPS
if (Game::getInstance()->getConfig()->isFpsShow()) if (Game::getInstance()->getConfig().isFpsShow())
{ {
_renderFps(); _renderFps();
} }
@ -155,20 +152,17 @@ void e2d::Renderer::render()
void e2d::Renderer::_renderFps() void e2d::Renderer::_renderFps()
{ {
++_renderTimes;
double duration = Game::getInstance()->getTotalDuration().seconds(); double duration = Game::getInstance()->getTotalDuration().seconds();
double delay = duration - _lastRenderTime; if (duration == _lastRenderTime)
if (delay >= 0.1) return;
{
String fpsText = String::format(L"FPS: %.1lf", (1 / delay) * _renderTimes); String fpsText = String::format(L"FPS: %.1lf", (1.0 / (duration - _lastRenderTime)));
_lastRenderTime = duration; _lastRenderTime = duration;
_renderTimes = 0;
auto writeFactory = Renderer::getWriteFactory(); auto writeFactory = Renderer::getWriteFactory();
if (!_fpsFormat) if (!_fpsFormat)
{ {
writeFactory->CreateTextFormat( HRESULT hr = writeFactory->CreateTextFormat(
L"", L"",
nullptr, nullptr,
DWRITE_FONT_WEIGHT_NORMAL, DWRITE_FONT_WEIGHT_NORMAL,
@ -178,25 +172,28 @@ void e2d::Renderer::_renderFps()
L"", L"",
&_fpsFormat &_fpsFormat
); );
}
if (_fpsFormat) if (SUCCEEDED(hr))
{ {
_fpsFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP); _fpsFormat->SetWordWrapping(DWRITE_WORD_WRAPPING_NO_WRAP);
}
else
{
return;
}
}
SafeRelease(_fpsLayout); IDWriteTextLayout * fpsLayout = nullptr;
writeFactory->CreateTextLayout( HRESULT hr = writeFactory->CreateTextLayout(
(const WCHAR *)fpsText, (const WCHAR *)fpsText,
(UINT32)fpsText.getLength(), (UINT32)fpsText.getLength(),
_fpsFormat, _fpsFormat,
0, 0,
0, 0,
&_fpsLayout &fpsLayout
); );
}
}
if (_fpsLayout) if (SUCCEEDED(hr))
{ {
_renderTarget->SetTransform(D2D1::Matrix3x2F::Identity()); _renderTarget->SetTransform(D2D1::Matrix3x2F::Identity());
_solidBrush->SetOpacity(1.0f); _solidBrush->SetOpacity(1.0f);
@ -210,8 +207,10 @@ void e2d::Renderer::_renderFps()
D2D1_LINE_JOIN_ROUND D2D1_LINE_JOIN_ROUND
); );
_fpsLayout->Draw(nullptr, textRenderer, 10, 0); fpsLayout->Draw(nullptr, textRenderer, 10, 0);
} }
SafeRelease(fpsLayout);
} }
e2d::Color e2d::Renderer::getBackgroundColor() e2d::Color e2d::Renderer::getBackgroundColor()

View File

@ -11,7 +11,7 @@ e2d::Collider::Collider(Node * parent)
, _shape(Collider::Shape::None) , _shape(Collider::Shape::None)
, _notify(true) , _notify(true)
{ {
_shape = Game::getInstance()->getConfig()->getDefaultColliderShape(); _shape = Game::getInstance()->getConfig().getDefaultColliderShape();
CollisionManager::getInstance()->__addCollider(this); CollisionManager::getInstance()->__addCollider(this);
} }

View File

@ -5,12 +5,12 @@ e2d::Config::Config()
: _gameName() : _gameName()
, _defaultNodePivot() , _defaultNodePivot()
, _soundEnabled(true) , _soundEnabled(true)
, _frameInterval(15)
, _showFps(false) , _showFps(false)
, _outlineVisible(false) , _outlineVisible(false)
, _collisionEnabled(false) , _collisionEnabled(false)
, _colliderVisible(false) , _colliderVisible(false)
, _defaultColliderShape(Collider::Shape::None) , _defaultColliderShape(Collider::Shape::None)
, _unconfigured(true)
{ {
} }
@ -28,6 +28,11 @@ void e2d::Config::showFps(bool show)
_showFps = show; _showFps = show;
} }
void e2d::Config::setFrameInterval(int interval)
{
_frameInterval = interval;
}
void e2d::Config::setOutlineVisible(bool visible) void e2d::Config::setOutlineVisible(bool visible)
{ {
_outlineVisible = visible; _outlineVisible = visible;
@ -35,11 +40,7 @@ void e2d::Config::setOutlineVisible(bool visible)
void e2d::Config::setSoundEnabled(bool enabled) void e2d::Config::setSoundEnabled(bool enabled)
{ {
if (_soundEnabled != enabled)
{
_soundEnabled = enabled; _soundEnabled = enabled;
_unconfigured = true;
}
} }
void e2d::Config::setCollisionEnabled(bool enabled) void e2d::Config::setCollisionEnabled(bool enabled)
@ -80,6 +81,11 @@ bool e2d::Config::isFpsShow() const
return _showFps; return _showFps;
} }
int e2d::Config::getFrameInterval() const
{
return _frameInterval;
}
bool e2d::Config::isOutlineVisible() const bool e2d::Config::isOutlineVisible() const
{ {
return _outlineVisible; return _outlineVisible;
@ -104,20 +110,3 @@ bool e2d::Config::isColliderVisible() const
{ {
return _colliderVisible; return _colliderVisible;
} }
void e2d::Config::_update()
{
if (!_unconfigured)
return;
_unconfigured = false;
if (_soundEnabled)
{
Player::getInstance()->getXAudio2()->StartEngine();
}
else
{
Player::getInstance()->getXAudio2()->StopEngine();
}
}

View File

@ -20,13 +20,13 @@ void e2d::Scene::render()
{ {
_root->_render(); _root->_render();
if (Game::getInstance()->getConfig()->isOutlineVisible()) if (Game::getInstance()->getConfig().isOutlineVisible())
{ {
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity()); Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
_root->_renderOutline(); _root->_renderOutline();
} }
if (Game::getInstance()->getConfig()->isColliderVisible()) if (Game::getInstance()->getConfig().isColliderVisible())
{ {
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity()); Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
_root->_renderCollider(); _root->_renderCollider();

View File

@ -46,7 +46,7 @@ void e2d::CollisionManager::__removeCollider(Collider * collider)
void e2d::CollisionManager::__updateCollider(Collider* collider) void e2d::CollisionManager::__updateCollider(Collider* collider)
{ {
if (Game::getInstance()->isPaused() || if (Game::getInstance()->isPaused() ||
!Game::getInstance()->getConfig()->isCollisionEnabled() || !Game::getInstance()->getConfig().isCollisionEnabled() ||
SceneManager::getInstance()->isTransitioning() || SceneManager::getInstance()->isTransitioning() ||
!collider->isCollisionNotify()) !collider->isCollisionNotify())
return; return;

View File

@ -69,7 +69,7 @@ e2d::Node::Node()
, _extrapolate(Property::Origin) , _extrapolate(Property::Origin)
{ {
// 设置默认中心点位置 // 设置默认中心点位置
Point defPivot = Game::getInstance()->getConfig()->getNodeDefaultPivot(); Point defPivot = Game::getInstance()->getConfig().getNodeDefaultPivot();
this->_pivotX = float(defPivot.x); this->_pivotX = float(defPivot.x);
this->_pivotY = float(defPivot.y); this->_pivotY = float(defPivot.y);
} }

View File

@ -12,7 +12,7 @@ e2d::String e2d::Path::getDataPath()
{ {
// 设置数据的保存路径 // 设置数据的保存路径
String localAppDataPath = Path::getLocalAppDataPath(); String localAppDataPath = Path::getLocalAppDataPath();
String gameName = Game::getInstance()->getConfig()->getGameName(); String gameName = Game::getInstance()->getConfig().getGameName();
if (!localAppDataPath.isEmpty() && !gameName.isEmpty()) if (!localAppDataPath.isEmpty() && !gameName.isEmpty())
{ {
dataPath = localAppDataPath + L"\\Easy2DGameData\\" << gameName << L"\\"; dataPath = localAppDataPath + L"\\Easy2DGameData\\" << gameName << L"\\";
@ -35,7 +35,7 @@ e2d::String e2d::Path::getTempPath()
{ {
// 设置临时文件保存路径 // 设置临时文件保存路径
wchar_t path[_MAX_PATH]; wchar_t path[_MAX_PATH];
String gameName = Game::getInstance()->getConfig()->getGameName(); String gameName = Game::getInstance()->getConfig().getGameName();
if (0 != ::GetTempPath(_MAX_PATH, path) && !gameName.isEmpty()) if (0 != ::GetTempPath(_MAX_PATH, path) && !gameName.isEmpty())
{ {

View File

@ -40,11 +40,11 @@ public:
// 修改游戏配置 // 修改游戏配置
void setConfig( void setConfig(
Config* config const Config& config
); );
// 获取游戏配置 // 获取游戏配置
Config* getConfig(); const Config& getConfig();
// 获取游戏总时长 // 获取游戏总时长
Duration getTotalDuration() const; Duration getTotalDuration() const;
@ -57,12 +57,13 @@ private:
E2D_DISABLE_COPY(Game); E2D_DISABLE_COPY(Game);
private: private:
bool _ended; bool _quit;
bool _paused; bool _paused;
Config* _config; Config _config;
Time _start; Time _start;
Time _now; Time _now;
Time _last; Time _last;
Duration _frameInterval;
static Game * _instance; static Game * _instance;
}; };
@ -335,12 +336,10 @@ private:
void _renderFps(); void _renderFps();
private: private:
int _renderTimes;
double _lastRenderTime; double _lastRenderTime;
D2D1_COLOR_F _clearColor; D2D1_COLOR_F _clearColor;
TextRenderer* _textRenderer; TextRenderer* _textRenderer;
IDWriteTextFormat* _fpsFormat; IDWriteTextFormat* _fpsFormat;
IDWriteTextLayout* _fpsLayout;
ID2D1SolidColorBrush* _solidBrush; ID2D1SolidColorBrush* _solidBrush;
ID2D1HwndRenderTarget* _renderTarget; ID2D1HwndRenderTarget* _renderTarget;

View File

@ -1045,14 +1045,9 @@ protected:
}; };
class Game;
// 游戏配置 // 游戏配置
class Config : class Config
public Ref
{ {
friend class Game;
public: public:
Config(); Config();
@ -1070,6 +1065,12 @@ public:
bool show bool show
); );
// 设置帧率刷新间隔
// 默认15
void setFrameInterval(
int interval
);
// 显示或隐藏节点轮廓 // 显示或隐藏节点轮廓
// 默认:隐藏 // 默认:隐藏
void setOutlineVisible( void setOutlineVisible(
@ -1115,6 +1116,9 @@ public:
// 获取 FPS 显示状态 // 获取 FPS 显示状态
bool isFpsShow() const; bool isFpsShow() const;
// 获取帧率刷新间隔
int getFrameInterval() const;
// 获取节点轮廓显示状态 // 获取节点轮廓显示状态
bool isOutlineVisible() const; bool isOutlineVisible() const;
@ -1131,15 +1135,12 @@ public:
bool isColliderVisible() const; bool isColliderVisible() const;
protected: protected:
virtual void _update();
protected:
bool _unconfigured;
bool _showFps; bool _showFps;
bool _soundEnabled; bool _soundEnabled;
bool _outlineVisible; bool _outlineVisible;
bool _collisionEnabled; bool _collisionEnabled;
bool _colliderVisible; bool _colliderVisible;
int _frameInterval;
String _gameName; String _gameName;
Point _defaultNodePivot; Point _defaultNodePivot;
Collider::Shape _defaultColliderShape; Collider::Shape _defaultColliderShape;