new feature: show fps

This commit is contained in:
Nomango 2018-04-17 11:41:33 +08:00
parent 9ac4acb50e
commit 3fe75c7325
11 changed files with 131 additions and 77 deletions

View File

@ -1,5 +1,6 @@
#include "..\ebase.h" #include "..\ebase.h"
#include "..\emanager.h" #include "..\emanager.h"
#include "..\enode.h"
static ID2D1Factory * s_pDirect2dFactory = nullptr; static ID2D1Factory * s_pDirect2dFactory = nullptr;
static ID2D1HwndRenderTarget * s_pRenderTarget = nullptr; static ID2D1HwndRenderTarget * s_pRenderTarget = nullptr;
@ -8,6 +9,13 @@ static IWICImagingFactory * s_pIWICFactory = nullptr;
static IDWriteFactory * s_pDWriteFactory = nullptr; static IDWriteFactory * s_pDWriteFactory = nullptr;
static D2D1_COLOR_F s_nClearColor = D2D1::ColorF(D2D1::ColorF::Black); 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() bool e2d::Renderer::__createDeviceIndependentResources()
{ {
@ -43,6 +51,21 @@ bool e2d::Renderer::__createDeviceIndependentResources()
ASSERT(SUCCEEDED(hr), "Create IDWriteFactory Failed!"); 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); return SUCCEEDED(hr);
} }
@ -119,6 +142,27 @@ void e2d::Renderer::__render()
// äÖȾ³¡¾° // äÖȾ³¡¾°
SceneManager::__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(); hr = s_pRenderTarget->EndDraw();
@ -144,6 +188,11 @@ void e2d::Renderer::setBackgroundColor(UINT32 color)
s_nClearColor = D2D1::ColorF(color); s_nClearColor = D2D1::ColorF(color);
} }
void e2d::Renderer::showFps(bool show)
{
s_bShowFps = show;
}
ID2D1Factory * e2d::Renderer::getID2D1Factory() ID2D1Factory * e2d::Renderer::getID2D1Factory()
{ {
return s_pDirect2dFactory; return s_pDirect2dFactory;

View File

@ -39,7 +39,7 @@ static milliseconds s_tExceptedInvertal;
bool e2d::Time::__init() bool e2d::Time::__init()
{ {
s_tStart = s_tLastUpdate = s_tFixedUpdate = s_tNow = steady_clock::now(); s_tStart = s_tLastUpdate = s_tFixedUpdate = s_tNow = steady_clock::now();
s_tExceptedInvertal = milliseconds(17); s_tExceptedInvertal = milliseconds(15);
return true; return true;
} }
@ -107,7 +107,7 @@ bool e2d::Time::__init()
::QueryPerformanceFrequency(&s_tFreq); // 获取时钟频率 ::QueryPerformanceFrequency(&s_tFreq); // 获取时钟频率
::QueryPerformanceCounter(&s_tNow); // 刷新当前时间 ::QueryPerformanceCounter(&s_tNow); // 刷新当前时间
s_tStart = s_tLastUpdate = s_tFixedUpdate = 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; return true;
} }

View File

@ -90,7 +90,7 @@ e2d::String e2d::String::parse(double value)
return std::move(tmp); return std::move(tmp);
} }
e2d::String & e2d::String::format(const char * format, ...) e2d::String e2d::String::format(const char * format, ...)
{ {
std::string tmp; std::string tmp;
@ -108,11 +108,11 @@ e2d::String & e2d::String::format(const char * format, ...)
va_end(marker); va_end(marker);
m_str = static_cast<wchar_t*>(_bstr_t(tmp.c_str())); String str = tmp.c_str();
return (*this); 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; std::wstring tmp;
@ -130,8 +130,8 @@ e2d::String & e2d::String::format(const wchar_t * format, ...)
va_end(marker); va_end(marker);
m_str = tmp.c_str(); String str = tmp.c_str();
return (*this); return std::move(str);
} }
e2d::String & e2d::String::operator=(const String &str) e2d::String & e2d::String::operator=(const String &str)

View File

@ -245,7 +245,7 @@ void e2d::Text::_createFormat()
m_Font.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL, m_Font.italic ? DWRITE_FONT_STYLE_ITALIC : DWRITE_FONT_STYLE_NORMAL,
DWRITE_FONT_STRETCH_NORMAL, DWRITE_FONT_STRETCH_NORMAL,
static_cast<float>(m_Font.size), static_cast<float>(m_Font.size),
L"zh-cn", L"",
&m_pDWriteTextFormat &m_pDWriteTextFormat
); );

View File

@ -1,5 +1,6 @@
#include "..\ebase.h" #include "..\ebase.h"
#include "..\etransition.h" #include "..\etransition.h"
#include "..\enode.h"
e2d::Transition::Transition(double duration) e2d::Transition::Transition(double duration)
: m_bEnd(false) : m_bEnd(false)
@ -49,10 +50,8 @@ void e2d::Transition::_init(Scene * prev, Scene * next)
if (m_pPrevScene) m_pPrevScene->retain(); if (m_pPrevScene) m_pPrevScene->retain();
if (m_pNextScene) m_pNextScene->retain(); if (m_pNextScene) m_pNextScene->retain();
auto size = Window::getSize(); m_WindowSize = Window::getSize();
m_sPrevLayerParam = m_sNextLayerParam = D2D1::LayerParameters( m_sPrevLayerParam = m_sNextLayerParam = D2D1::LayerParameters();
D2D1::RectF(0, 0, float(size.width), float(size.height))
);
} }
void e2d::Transition::_update() void e2d::Transition::_update()
@ -83,27 +82,45 @@ void e2d::Transition::_update()
void e2d::Transition::_render() void e2d::Transition::_render()
{ {
auto pRT = Renderer::getRenderTarget(); auto pRT = Renderer::getRenderTarget();
Size windowSize = Window::getSize();
if (m_pPrevScene) 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->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pRT->PushLayer(m_sPrevLayerParam, m_pPrevLayer); pRT->PushLayer(m_sPrevLayerParam, m_pPrevLayer);
// äÖȾ³¡¾° // äÖȾ³¡¾°
m_pPrevScene->_render(); m_pPrevScene->_render();
pRT->PopLayer(); pRT->PopLayer();
pRT->PopAxisAlignedClip();
} }
if (m_pNextScene) 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->SetTransform(D2D1::Matrix3x2F::Identity());
pRT->PushAxisAlignedClip(clipRect, D2D1_ANTIALIAS_MODE_PER_PRIMITIVE);
pRT->PushLayer(m_sNextLayerParam, m_pNextLayer); pRT->PushLayer(m_sNextLayerParam, m_pNextLayer);
// äÖȾ³¡¾° // äÖȾ³¡¾°
m_pNextScene->_render(); m_pNextScene->_render();
pRT->PopLayer(); pRT->PopLayer();
pRT->PopAxisAlignedClip();
} }
} }

View File

@ -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() void e2d::TransitionEmerge::_updateCustom()
{ {
m_sPrevLayerParam.opacity = float(1 - m_fRateOfProgress); 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() void e2d::TransitionEmerge::_reset()
{ {
} }

View File

@ -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() void e2d::TransitionFade::_updateCustom()
{ {
if (m_bFadeOutTransioning) 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() void e2d::TransitionFade::_reset()
{ {
} }

View File

@ -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) void e2d::TransitionMove::_init(Scene * prev, Scene * next)
{ {
Transition::_init(prev, next); Transition::_init(prev, next);
m_WindowSize = Window::getSize();
double width = m_WindowSize.width; double width = m_WindowSize.width;
double height = m_WindowSize.height; double height = m_WindowSize.height;
if (m_Direct == Direct::UP) if (m_Direct == Direct::UP)
@ -74,6 +38,25 @@ void e2d::TransitionMove::_init(Scene * prev, Scene * next)
m_pNextScene->getRoot()->setPos(m_NextPos); 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() void e2d::TransitionMove::_reset()
{ {
if (m_pPrevScene) m_pPrevScene->getRoot()->setPos(0, 0); if (m_pPrevScene) m_pPrevScene->getRoot()->setPos(0, 0);

View File

@ -234,6 +234,11 @@ public:
UINT32 color UINT32 color
); );
// ÏÔʾ FPS
static void showFps(
bool show = true
);
// 获取 ID2D1Factory 对象 // 获取 ID2D1Factory 对象
static ID2D1Factory * getID2D1Factory(); static ID2D1Factory * getID2D1Factory();

View File

@ -142,8 +142,8 @@ public:
static String parse(double value); static String parse(double value);
// ¸ñʽ»¯×Ö·û´® // ¸ñʽ»¯×Ö·û´®
String& format(const char * format, ...); static String format(const char * format, ...);
String& format(const wchar_t * format, ...); static String format(const wchar_t * format, ...);
// ¸³ÖµÔËËã·û // ¸³ÖµÔËËã·û
String& operator= (const String &); String& operator= (const String &);

View File

@ -47,6 +47,7 @@ protected:
double m_fLast; double m_fLast;
double m_fDuration; double m_fDuration;
double m_fRateOfProgress; double m_fRateOfProgress;
Size m_WindowSize;
Scene * m_pPrevScene; Scene * m_pPrevScene;
Scene * m_pNextScene; Scene * m_pNextScene;
ID2D1Layer * m_pPrevLayer; ID2D1Layer * m_pPrevLayer;
@ -136,7 +137,6 @@ protected:
int m_Direct; int m_Direct;
Vector m_Vector; Vector m_Vector;
Point m_NextPos; Point m_NextPos;
Size m_WindowSize;
}; };
} }