add: Graphics::GetDpi

This commit is contained in:
Nomango 2018-10-06 11:15:32 +08:00
parent 665735626e
commit fdbece1663
6 changed files with 70 additions and 67 deletions

View File

@ -73,6 +73,9 @@ namespace e2d
// 获取 Round 样式的 ID2D1StrokeStyle // 获取 Round 样式的 ID2D1StrokeStyle
ID2D1StrokeStyle * GetRoundStrokeStyle(); ID2D1StrokeStyle * GetRoundStrokeStyle();
// 获取 DPI
static float GetDpi();
protected: protected:
D2D1_COLOR_F clear_color_; D2D1_COLOR_F clear_color_;
ID2D1Factory* factory_; ID2D1Factory* factory_;
@ -298,6 +301,9 @@ namespace e2d
int height int height
); );
// 进入下一场景
void EnterNextScene();
// Win32 窗口消息回调程序 // Win32 窗口消息回调程序
static LRESULT CALLBACK WndProc( static LRESULT CALLBACK WndProc(
HWND hwnd, HWND hwnd,

View File

@ -270,6 +270,7 @@ namespace e2d
class Node class Node
: public Ref : public Ref
{ {
friend class Game;
friend class Scene; friend class Scene;
public: public:
@ -281,6 +282,12 @@ namespace e2d
virtual ~Node(); virtual ~Node();
// 渲染节点
virtual void Draw() const {}
// 更新节点
virtual void Update(float dt) {}
// 获取节点显示状态 // 获取节点显示状态
bool IsVisible() const; bool IsVisible() const;
@ -624,17 +631,19 @@ namespace e2d
// 获取所有任务 // 获取所有任务
const Tasks& GetAllTasks() const; const Tasks& GetAllTasks() const;
// 渲染节点 protected:
virtual void Draw() const {} E2D_DISABLE_COPY(Node);
// 更新节点 // 遍历节点
virtual void Update(float dt) {} virtual void Visit();
// 渲染节点边缘 // 渲染节点边缘
void DrawBorder(); void DrawBorder();
// 更新子节点 // 设置节点所在场景
void UpdateChildren(float dt); void SetParentScene(
Scene * scene
);
// 分发鼠标消息 // 分发鼠标消息
virtual bool Dispatch( virtual bool Dispatch(
@ -648,16 +657,8 @@ namespace e2d
bool handled bool handled
); );
protected: // 更新子节点
E2D_DISABLE_COPY(Node); void UpdateChildren(float dt);
// 遍历节点
virtual void Visit();
// 设置节点所在场景
void SetParentScene(
Scene * scene
);
// 更新转换矩阵 // 更新转换矩阵
void UpdateTransform(); void UpdateTransform();

View File

@ -30,29 +30,22 @@ e2d::MouseEvent::MouseEvent(UINT message, WPARAM w_param, LPARAM l_param)
float e2d::MouseEvent::GetX() const float e2d::MouseEvent::GetX() const
{ {
HDC hdc = ::GetDC(0); float dpi = Graphics::GetDpi();
int dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX); return ((float)(short)LOWORD(l_param_)) * 96.f / dpi;
::ReleaseDC(0, hdc);
return ((float)(short)LOWORD(l_param_)) * 96.f / dpi_x;
} }
float e2d::MouseEvent::GetY() const float e2d::MouseEvent::GetY() const
{ {
HDC hdc = ::GetDC(0); float dpi = Graphics::GetDpi();
int dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY); return ((float)(short)HIWORD(l_param_)) * 96.f / dpi;
::ReleaseDC(0, hdc);
return ((float)(short)HIWORD(l_param_)) * 96.f / dpi_y;
} }
e2d::Point e2d::MouseEvent::GetPosition() const e2d::Point e2d::MouseEvent::GetPosition() const
{ {
HDC hdc = ::GetDC(0); float dpi = Graphics::GetDpi();
int dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX);
int dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(0, hdc);
return Point( return Point(
((float)(short)LOWORD(l_param_)) * 96.f / dpi_x, ((float)(short)LOWORD(l_param_)) * 96.f / dpi,
((float)(short)HIWORD(l_param_)) * 96.f / dpi_y ((float)(short)HIWORD(l_param_)) * 96.f / dpi
); );
} }

View File

@ -32,6 +32,11 @@
static e2d::Game * instance = nullptr; static e2d::Game * instance = nullptr;
e2d::Game * e2d::Game::GetInstance()
{
return instance;
}
e2d::Game::Game() e2d::Game::Game()
: hwnd_(nullptr) : hwnd_(nullptr)
, quit_(true) , quit_(true)
@ -88,9 +93,9 @@ void e2d::Game::Run(const Options& options)
Start(); Start();
// 刷新场景 // 刷新场景
EnterNextScene();
::ShowWindow(hwnd_, SW_SHOWNORMAL); ::ShowWindow(hwnd_, SW_SHOWNORMAL);
::UpdateWindow(hwnd_); ::UpdateWindow(hwnd_);
UpdateScene(0);
// 运行 // 运行
const int min_interval = 5; const int min_interval = 5;
@ -211,19 +216,7 @@ void e2d::Game::UpdateScene(float dt)
} }
} }
if (next_scene_) EnterNextScene();
{
if (curr_scene_)
{
curr_scene_->OnExit();
curr_scene_->Release();
}
next_scene_->OnEnter();
curr_scene_ = next_scene_;
next_scene_ = nullptr;
}
} }
void e2d::Game::DrawScene() void e2d::Game::DrawScene()
@ -260,11 +253,6 @@ void e2d::Game::DrawScene()
graphics->EndDraw(); graphics->EndDraw();
} }
e2d::Game * e2d::Game::GetInstance()
{
return instance;
}
void e2d::Game::Init() void e2d::Game::Init()
{ {
WNDCLASSEX wcex = { 0 }; WNDCLASSEX wcex = { 0 };
@ -369,11 +357,8 @@ e2d::Rect e2d::Game::Locate(int width, int height)
int max_width = ::GetSystemMetrics(SM_CXSCREEN); int max_width = ::GetSystemMetrics(SM_CXSCREEN);
int max_height = ::GetSystemMetrics(SM_CYSCREEN); int max_height = ::GetSystemMetrics(SM_CYSCREEN);
HDC hdc = ::GetDC(0); float dpi = Graphics::GetDpi();
int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); RECT rect = { 0, 0, LONG(ceil(width * dpi / 96.f)), LONG(ceil(height * dpi / 96.f)) };
int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(0, hdc);
RECT rect = { 0, 0, LONG(ceil(width * dpi_x / 96.f)), LONG(ceil(height * dpi_y / 96.f)) };
// 计算合适的窗口大小 // 计算合适的窗口大小
::AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, NULL); ::AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, NULL);
@ -394,6 +379,23 @@ e2d::Rect e2d::Game::Locate(int width, int height)
return std::move(client_rect); return std::move(client_rect);
} }
void e2d::Game::EnterNextScene()
{
if (next_scene_)
{
if (curr_scene_)
{
curr_scene_->OnExit();
curr_scene_->Release();
}
next_scene_->OnEnter();
curr_scene_ = next_scene_;
next_scene_ = nullptr;
}
}
int e2d::Game::GetWidth() const int e2d::Game::GetWidth() const
{ {
return width_; return width_;
@ -556,12 +558,9 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
if (w_param == SIZE_RESTORED) if (w_param == SIZE_RESTORED)
{ {
HDC hdc = ::GetDC(0); float dpi = Graphics::GetDpi();
int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); game->width_ = static_cast<int>(width * 96.f / dpi);
int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); game->height_ = static_cast<int>(height * 96.f / dpi);
::ReleaseDC(0, hdc);
game->width_ = static_cast<int>(width * 96.f / dpi_x);
game->height_ = static_cast<int>(height * 96.f / dpi_y);
} }
// 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染 // 如果程序接收到一个 WM_SIZE 消息,这个方法将调整渲染
@ -570,7 +569,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
auto render_target = Device::GetGraphics()->GetRenderTarget(); auto render_target = Device::GetGraphics()->GetRenderTarget();
if (render_target) if (render_target)
{ {
render_target->Resize(D2D1::SizeU(game->width_, game->height_)); render_target->Resize(D2D1::SizeU(width, height));
} }
} }
break; break;

View File

@ -311,3 +311,11 @@ ID2D1StrokeStyle * e2d::Graphics::GetRoundStrokeStyle()
} }
return round_stroke_style_; return round_stroke_style_;
} }
float e2d::Graphics::GetDpi()
{
HDC hdc = ::GetDC(0);
int dpi = ::GetDeviceCaps(hdc, LOGPIXELSX);
::ReleaseDC(0, hdc);
return static_cast<float>(dpi);
}

View File

@ -147,15 +147,11 @@ float e2d::Input::GetMouseY()
e2d::Point e2d::Input::GetMousePos() e2d::Point e2d::Input::GetMousePos()
{ {
HDC hdc = ::GetDC(0);
int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX);
int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY);
::ReleaseDC(0, hdc);
POINT mousePos; POINT mousePos;
::GetCursorPos(&mousePos); ::GetCursorPos(&mousePos);
::ScreenToClient(Game::GetInstance()->GetHWnd(), &mousePos); ::ScreenToClient(Game::GetInstance()->GetHWnd(), &mousePos);
return Point(mousePos.x * 96.f / dpi_x, mousePos.y * 96.f / dpi_y); float dpi = Graphics::GetDpi();
return Point(mousePos.x * 96.f / dpi, mousePos.y * 96.f / dpi);
} }
float e2d::Input::GetMouseDeltaX() float e2d::Input::GetMouseDeltaX()