add: Graphics::GetDpi
This commit is contained in:
		
							parent
							
								
									665735626e
								
							
						
					
					
						commit
						fdbece1663
					
				|  | @ -73,6 +73,9 @@ namespace e2d | |||
| 		// 获取 Round 样式的 ID2D1StrokeStyle
 | ||||
| 		ID2D1StrokeStyle * GetRoundStrokeStyle(); | ||||
| 
 | ||||
| 		// 获取 DPI
 | ||||
| 		static float GetDpi(); | ||||
| 
 | ||||
| 	protected: | ||||
| 		D2D1_COLOR_F			clear_color_; | ||||
| 		ID2D1Factory*			factory_; | ||||
|  | @ -298,6 +301,9 @@ namespace e2d | |||
| 			int height | ||||
| 		); | ||||
| 
 | ||||
| 		// 进入下一场景
 | ||||
| 		void EnterNextScene(); | ||||
| 
 | ||||
| 		// Win32 窗口消息回调程序
 | ||||
| 		static LRESULT CALLBACK WndProc( | ||||
| 			HWND hwnd, | ||||
|  |  | |||
|  | @ -270,6 +270,7 @@ namespace e2d | |||
| 	class Node | ||||
| 		: public Ref | ||||
| 	{ | ||||
| 		friend class Game; | ||||
| 		friend class Scene; | ||||
| 
 | ||||
| 	public: | ||||
|  | @ -281,6 +282,12 @@ namespace e2d | |||
| 
 | ||||
| 		virtual ~Node(); | ||||
| 
 | ||||
| 		// 渲染节点
 | ||||
| 		virtual void Draw() const {} | ||||
| 
 | ||||
| 		// 更新节点
 | ||||
| 		virtual void Update(float dt) {} | ||||
| 
 | ||||
| 		// 获取节点显示状态
 | ||||
| 		bool IsVisible() const; | ||||
| 
 | ||||
|  | @ -624,17 +631,19 @@ namespace e2d | |||
| 		// 获取所有任务
 | ||||
| 		const Tasks& GetAllTasks() const; | ||||
| 
 | ||||
| 		// 渲染节点
 | ||||
| 		virtual void Draw() const {} | ||||
| 	protected: | ||||
| 		E2D_DISABLE_COPY(Node); | ||||
| 
 | ||||
| 		// 更新节点
 | ||||
| 		virtual void Update(float dt) {} | ||||
| 		// 遍历节点
 | ||||
| 		virtual void Visit(); | ||||
| 
 | ||||
| 		// 渲染节点边缘
 | ||||
| 		void DrawBorder(); | ||||
| 
 | ||||
| 		// 更新子节点
 | ||||
| 		void UpdateChildren(float dt); | ||||
| 		// 设置节点所在场景
 | ||||
| 		void SetParentScene( | ||||
| 			Scene * scene | ||||
| 		); | ||||
| 
 | ||||
| 		// 分发鼠标消息
 | ||||
| 		virtual bool Dispatch( | ||||
|  | @ -648,16 +657,8 @@ namespace e2d | |||
| 			bool handled | ||||
| 		); | ||||
| 
 | ||||
| 	protected: | ||||
| 		E2D_DISABLE_COPY(Node); | ||||
| 
 | ||||
| 		// 遍历节点
 | ||||
| 		virtual void Visit(); | ||||
| 
 | ||||
| 		// 设置节点所在场景
 | ||||
| 		void SetParentScene( | ||||
| 			Scene * scene | ||||
| 		); | ||||
| 		// 更新子节点
 | ||||
| 		void UpdateChildren(float dt); | ||||
| 
 | ||||
| 		// 更新转换矩阵
 | ||||
| 		void UpdateTransform(); | ||||
|  |  | |||
|  | @ -30,29 +30,22 @@ e2d::MouseEvent::MouseEvent(UINT message, WPARAM w_param, LPARAM l_param) | |||
| 
 | ||||
| float e2d::MouseEvent::GetX() const | ||||
| { | ||||
| 	HDC hdc = ::GetDC(0); | ||||
| 	int dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX); | ||||
| 	::ReleaseDC(0, hdc); | ||||
| 	return ((float)(short)LOWORD(l_param_)) * 96.f / dpi_x; | ||||
| 	float dpi = Graphics::GetDpi(); | ||||
| 	return ((float)(short)LOWORD(l_param_)) * 96.f / dpi; | ||||
| } | ||||
| 
 | ||||
| float e2d::MouseEvent::GetY() const | ||||
| { | ||||
| 	HDC hdc = ::GetDC(0); | ||||
| 	int dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY); | ||||
| 	::ReleaseDC(0, hdc); | ||||
| 	return ((float)(short)HIWORD(l_param_)) * 96.f / dpi_y; | ||||
| 	float dpi = Graphics::GetDpi(); | ||||
| 	return ((float)(short)HIWORD(l_param_)) * 96.f / dpi; | ||||
| } | ||||
| 
 | ||||
| e2d::Point e2d::MouseEvent::GetPosition() const | ||||
| { | ||||
| 	HDC hdc = ::GetDC(0); | ||||
| 	int dpi_x = ::GetDeviceCaps(hdc, LOGPIXELSX); | ||||
| 	int dpi_y = ::GetDeviceCaps(hdc, LOGPIXELSY); | ||||
| 	::ReleaseDC(0, hdc); | ||||
| 	float dpi = Graphics::GetDpi(); | ||||
| 	return Point( | ||||
| 		((float)(short)LOWORD(l_param_)) * 96.f / dpi_x, | ||||
| 		((float)(short)HIWORD(l_param_)) * 96.f / dpi_y | ||||
| 		((float)(short)LOWORD(l_param_)) * 96.f / dpi, | ||||
| 		((float)(short)HIWORD(l_param_)) * 96.f / dpi | ||||
| 	); | ||||
| } | ||||
| 
 | ||||
|  |  | |||
|  | @ -32,6 +32,11 @@ | |||
| 
 | ||||
| static e2d::Game * instance = nullptr; | ||||
| 
 | ||||
| e2d::Game * e2d::Game::GetInstance() | ||||
| { | ||||
| 	return instance; | ||||
| } | ||||
| 
 | ||||
| e2d::Game::Game() | ||||
| 	: hwnd_(nullptr) | ||||
| 	, quit_(true) | ||||
|  | @ -88,9 +93,9 @@ void e2d::Game::Run(const Options& options) | |||
| 	Start(); | ||||
| 
 | ||||
| 	// 刷新场景
 | ||||
| 	EnterNextScene(); | ||||
| 	::ShowWindow(hwnd_, SW_SHOWNORMAL); | ||||
| 	::UpdateWindow(hwnd_); | ||||
| 	UpdateScene(0); | ||||
| 
 | ||||
| 	// 运行
 | ||||
| 	const int min_interval = 5; | ||||
|  | @ -211,19 +216,7 @@ void e2d::Game::UpdateScene(float dt) | |||
| 		} | ||||
| 	} | ||||
| 
 | ||||
| 	if (next_scene_) | ||||
| 	{ | ||||
| 		if (curr_scene_) | ||||
| 		{ | ||||
| 			curr_scene_->OnExit(); | ||||
| 			curr_scene_->Release(); | ||||
| 		} | ||||
| 
 | ||||
| 		next_scene_->OnEnter(); | ||||
| 
 | ||||
| 		curr_scene_ = next_scene_; | ||||
| 		next_scene_ = nullptr; | ||||
| 	} | ||||
| 	EnterNextScene(); | ||||
| } | ||||
| 
 | ||||
| void e2d::Game::DrawScene() | ||||
|  | @ -260,11 +253,6 @@ void e2d::Game::DrawScene() | |||
| 	graphics->EndDraw(); | ||||
| } | ||||
| 
 | ||||
| e2d::Game * e2d::Game::GetInstance() | ||||
| { | ||||
| 	return instance; | ||||
| } | ||||
| 
 | ||||
| void e2d::Game::Init() | ||||
| { | ||||
| 	WNDCLASSEX wcex = { 0 }; | ||||
|  | @ -369,11 +357,8 @@ e2d::Rect e2d::Game::Locate(int width, int height) | |||
| 	int max_width = ::GetSystemMetrics(SM_CXSCREEN); | ||||
| 	int max_height = ::GetSystemMetrics(SM_CYSCREEN); | ||||
| 
 | ||||
| 	HDC hdc = ::GetDC(0); | ||||
| 	int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); | ||||
| 	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)) }; | ||||
| 	float dpi = Graphics::GetDpi(); | ||||
| 	RECT rect = { 0, 0, LONG(ceil(width * dpi / 96.f)), LONG(ceil(height * dpi / 96.f)) }; | ||||
| 
 | ||||
| 	// 计算合适的窗口大小
 | ||||
| 	::AdjustWindowRectEx(&rect, WINDOW_STYLE, FALSE, NULL); | ||||
|  | @ -394,6 +379,23 @@ e2d::Rect e2d::Game::Locate(int width, int height) | |||
| 	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 | ||||
| { | ||||
| 	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) | ||||
| 			{ | ||||
| 				HDC hdc = ::GetDC(0); | ||||
| 				int dpi_x = GetDeviceCaps(hdc, LOGPIXELSX); | ||||
| 				int dpi_y = GetDeviceCaps(hdc, LOGPIXELSY); | ||||
| 				::ReleaseDC(0, hdc); | ||||
| 				game->width_ = static_cast<int>(width * 96.f / dpi_x); | ||||
| 				game->height_ = static_cast<int>(height * 96.f / dpi_y); | ||||
| 				float dpi = Graphics::GetDpi(); | ||||
| 				game->width_ = static_cast<int>(width * 96.f / dpi); | ||||
| 				game->height_ = static_cast<int>(height * 96.f / dpi); | ||||
| 			} | ||||
| 
 | ||||
| 			// 如果程序接收到一个 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(); | ||||
| 			if (render_target) | ||||
| 			{ | ||||
| 				render_target->Resize(D2D1::SizeU(game->width_, game->height_)); | ||||
| 				render_target->Resize(D2D1::SizeU(width, height)); | ||||
| 			} | ||||
| 		} | ||||
| 		break; | ||||
|  |  | |||
|  | @ -311,3 +311,11 @@ ID2D1StrokeStyle * e2d::Graphics::GetRoundStrokeStyle() | |||
| 	} | ||||
| 	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); | ||||
| } | ||||
|  |  | |||
|  | @ -147,15 +147,11 @@ float e2d::Input::GetMouseY() | |||
| 
 | ||||
| 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; | ||||
| 	::GetCursorPos(&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() | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue