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

View File

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

View File

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

View File

@ -5,12 +5,12 @@ e2d::Config::Config()
: _gameName()
, _defaultNodePivot()
, _soundEnabled(true)
, _frameInterval(15)
, _showFps(false)
, _outlineVisible(false)
, _collisionEnabled(false)
, _colliderVisible(false)
, _defaultColliderShape(Collider::Shape::None)
, _unconfigured(true)
{
}
@ -28,6 +28,11 @@ void e2d::Config::showFps(bool show)
_showFps = show;
}
void e2d::Config::setFrameInterval(int interval)
{
_frameInterval = interval;
}
void e2d::Config::setOutlineVisible(bool visible)
{
_outlineVisible = visible;
@ -35,11 +40,7 @@ void e2d::Config::setOutlineVisible(bool visible)
void e2d::Config::setSoundEnabled(bool enabled)
{
if (_soundEnabled != enabled)
{
_soundEnabled = enabled;
_unconfigured = true;
}
_soundEnabled = enabled;
}
void e2d::Config::setCollisionEnabled(bool enabled)
@ -80,6 +81,11 @@ bool e2d::Config::isFpsShow() const
return _showFps;
}
int e2d::Config::getFrameInterval() const
{
return _frameInterval;
}
bool e2d::Config::isOutlineVisible() const
{
return _outlineVisible;
@ -104,20 +110,3 @@ bool e2d::Config::isColliderVisible() const
{
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();
if (Game::getInstance()->getConfig()->isOutlineVisible())
if (Game::getInstance()->getConfig().isOutlineVisible())
{
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
_root->_renderOutline();
}
if (Game::getInstance()->getConfig()->isColliderVisible())
if (Game::getInstance()->getConfig().isColliderVisible())
{
Renderer::getInstance()->getRenderTarget()->SetTransform(D2D1::Matrix3x2F::Identity());
_root->_renderCollider();

View File

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

View File

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

View File

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

View File

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

View File

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