add: Window::GetScreenSize function

This commit is contained in:
Nomango 2018-09-07 17:26:21 +08:00
parent 2d68b8e2cc
commit 9472aebff2
3 changed files with 56 additions and 43 deletions

3
.gitignore vendored
View File

@ -11,4 +11,5 @@ ipch/
*.user *.user
*.lnk *.lnk
.vs .vs
.vscode

View File

@ -37,6 +37,9 @@ namespace e2d
// 销毁窗体实例 // 销毁窗体实例
static void DestroyInstance(); static void DestroyInstance();
// 获取屏幕大小
static Size GetScreenSize();
// 获取窗体标题 // 获取窗体标题
const String& GetTitle() const; const String& GetTitle() const;
@ -146,6 +149,33 @@ namespace e2d
// 销毁实例 // 销毁实例
static void DestroyInstance(); static void DestroyInstance();
// 获取 ID2D1Factory 对象
static ID2D1Factory * GetFactory();
// 获取 IWICImagingFactory 对象
static IWICImagingFactory * GetImagingFactory();
// 获取 IDWriteFactory 对象
static IDWriteFactory * GetWriteFactory();
// 获取 Miter 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * GetMiterStrokeStyle();
// 获取 Bevel 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * GetBevelStrokeStyle();
// 获取 Round 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * GetRoundStrokeStyle();
// 获取文字渲染器
E2DTextRenderer * GetTextRenderer();
// 获取 ID2D1HwndRenderTarget 对象
ID2D1HwndRenderTarget * GetRenderTarget();
// 获取 ID2D1SolidColorBrush 对象
ID2D1SolidColorBrush * GetSolidBrush();
// 获取背景色 // 获取背景色
Color GetBackgroundColor(); Color GetBackgroundColor();
@ -166,33 +196,6 @@ namespace e2d
// 结束渲染 // 结束渲染
void EndDraw(); void EndDraw();
// 获取文字渲染器
E2DTextRenderer * GetTextRenderer();
// 获取 ID2D1HwndRenderTarget 对象
ID2D1HwndRenderTarget * GetRenderTarget();
// 获取 ID2D1SolidColorBrush 对象
ID2D1SolidColorBrush * GetSolidBrush();
// 获取 ID2D1Factory 对象
static ID2D1Factory * GetFactory();
// 获取 IWICImagingFactory 对象
static IWICImagingFactory * GetImagingFactory();
// 获取 IDWriteFactory 对象
static IDWriteFactory * GetWriteFactory();
// 获取 Miter 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * GetMiterStrokeStyle();
// 获取 Bevel 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * GetBevelStrokeStyle();
// 获取 Round 样式的 ID2D1StrokeStyle
static ID2D1StrokeStyle * GetRoundStrokeStyle();
protected: protected:
Renderer(); Renderer();

View File

@ -26,6 +26,17 @@ void e2d::Window::DestroyInstance()
} }
} }
e2d::Size e2d::Window::GetScreenSize()
{
int screen_width = ::GetSystemMetrics(SM_CXSCREEN);
int screen_height = ::GetSystemMetrics(SM_CYSCREEN);
Size screen_size(
static_cast<float>(screen_width),
static_cast<float>(screen_height)
);
return std::move(screen_size);
}
e2d::Window::Window() e2d::Window::Window()
: hWnd_(nullptr) : hWnd_(nullptr)
, width_(640) , width_(640)
@ -42,11 +53,9 @@ e2d::Window::Window()
e2d::Window::~Window() e2d::Window::~Window()
{ {
// ¹Ø±Õ¿ØÖÆÌ¨
if (::GetConsoleWindow()) if (::GetConsoleWindow())
::FreeConsole(); ::FreeConsole();
// ¹Ø±Õ´°¿Ú
if (hWnd_) if (hWnd_)
::DestroyWindow(hWnd_); ::DestroyWindow(hWnd_);
@ -74,8 +83,8 @@ e2d::Rect e2d::Window::Locate(int width, int height)
{ {
Rect result; Rect result;
RECT wRECT = { 0, 0, LONG(ceil(width * dpi_ / 96.f)), LONG(ceil(height * dpi_ / 96.f)) }; RECT wRECT = { 0, 0, LONG(ceil(width * dpi_ / 96.f)), LONG(ceil(height * dpi_ / 96.f)) };
int maxWidth = ::GetSystemMetrics(SM_CXSCREEN); int max_width = ::GetSystemMetrics(SM_CXSCREEN);
int maxHeight = ::GetSystemMetrics(SM_CYSCREEN); int max_height = ::GetSystemMetrics(SM_CYSCREEN);
// 计算合适的窗口大小 // 计算合适的窗口大小
::AdjustWindowRectEx(&wRECT, WINDOW_STYLE, FALSE, NULL); ::AdjustWindowRectEx(&wRECT, WINDOW_STYLE, FALSE, NULL);
@ -83,11 +92,11 @@ e2d::Rect e2d::Window::Locate(int width, int height)
height = static_cast<int>(wRECT.bottom - wRECT.top); height = static_cast<int>(wRECT.bottom - wRECT.top);
// 当输入的窗口大小比分辨率大时,给出警告 // 当输入的窗口大小比分辨率大时,给出警告
WARN_IF(maxWidth < width || maxHeight < height, "The window Is larger than screen!"); WARN_IF(max_width < width || max_height < height, "The window Is larger than screen!");
width = std::min(width, maxWidth); width = std::min(width, max_width);
height = std::min(height, maxHeight); height = std::min(height, max_height);
float x = float((maxWidth - width) / 2), y = float((maxHeight - height) / 2); float x = float((max_width - width) / 2), y = float((max_height - height) / 2);
return std::move(Rect(x, y, float(width), float(height))); return std::move(Rect(x, y, float(width), float(height)));
} }
@ -448,10 +457,10 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
// 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染
// 目标适当。它可能会调用失败,但是这里可以忽略有可能的 // 目标适当。它可能会调用失败,但是这里可以忽略有可能的
// 错误,因为这个错误将在下一次调用 EndDraw 时产生 // 错误,因为这个错误将在下一次调用 EndDraw 时产生
auto pRT = Renderer::GetInstance()->GetRenderTarget(); auto render_target = Renderer::GetInstance()->GetRenderTarget();
if (pRT) if (render_target)
{ {
pRT->Resize(D2D1::SizeU(width, height)); render_target->Resize(D2D1::SizeU(width, height));
} }
} }
break; break;
@ -467,7 +476,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
case WM_DISPLAYCHANGE: case WM_DISPLAYCHANGE:
{ {
// 重绘客户区 // 重绘客户区
InvalidateRect(hWnd, nullptr, FALSE); ::InvalidateRect(hWnd, nullptr, FALSE);
} }
result = 0; result = 0;
hasHandled = true; hasHandled = true;
@ -477,7 +486,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
case WM_PAINT: case WM_PAINT:
{ {
Game::GetInstance()->DrawScene(); Game::GetInstance()->DrawScene();
ValidateRect(hWnd, nullptr); ::ValidateRect(hWnd, nullptr);
} }
result = 0; result = 0;
hasHandled = true; hasHandled = true;
@ -500,7 +509,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
// 窗口销毁消息 // 窗口销毁消息
case WM_DESTROY: case WM_DESTROY:
{ {
PostQuitMessage(0); ::PostQuitMessage(0);
} }
result = 1; result = 1;
hasHandled = true; hasHandled = true;
@ -510,7 +519,7 @@ LRESULT e2d::Window::WndProc(HWND hWnd, UINT msg, WPARAM w_param, LPARAM l_param
if (!hasHandled) if (!hasHandled)
{ {
result = DefWindowProc(hWnd, msg, w_param, l_param); result = ::DefWindowProc(hWnd, msg, w_param, l_param);
} }
} }
return result; return result;