From d0c167360e11a1aa5eb0221da4deb5280e116920 Mon Sep 17 00:00:00 2001
From: Nomango <569629550@qq.com>
Date: Thu, 1 Mar 2018 00:19:09 +0800
Subject: [PATCH] new feature: singleton process
---
core/Base/Game.cpp | 5 +-
core/Base/Window.cpp | 44 +++++++++++++-
core/Common/Point.cpp | 39 +++++++++++++
core/Common/Size.cpp | 38 +++++++++++++
core/ebase.h | 5 ++
core/ecommon.h | 82 +++++++--------------------
project/vs2017/Easy2D.vcxproj | 2 +
project/vs2017/Easy2D.vcxproj.filters | 6 ++
8 files changed, 158 insertions(+), 63 deletions(-)
create mode 100644 core/Common/Point.cpp
create mode 100644 core/Common/Size.cpp
diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp
index b2cd9025..2089836a 100644
--- a/core/Base/Game.cpp
+++ b/core/Base/Game.cpp
@@ -25,6 +25,9 @@ bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTS
// 初始化 COM 组件
CoInitializeEx(NULL, COINIT_MULTITHREADED);
+ // 设置 AppName
+ s_sAppName = sAppname;
+
// 创建设备无关资源
if (!Renderer::__createDeviceIndependentResources())
{
@@ -62,8 +65,6 @@ bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTS
// 重设 Client 大小
Window::setSize(nWidth, nHeight);
- // 设置 AppName
- s_sAppName = sAppname.isEmpty() ? Window::getTitle() : sAppname;
// 标志初始化成功
s_bInitialized = true;
diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp
index 8c10be50..3d3f89fd 100644
--- a/core/Base/Window.cpp
+++ b/core/Base/Window.cpp
@@ -11,8 +11,14 @@ static bool s_bShowConsole = false;
bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/)
{
+ if (!Window::__initMutex(sTitle))
+ {
+ return false;
+ }
+
// 注册窗口类
- WNDCLASSEX wcex = { sizeof(WNDCLASSEX) };
+ WNDCLASSEX wcex = { 0 };
+ wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = Window::WndProc;
wcex.cbClsExtra = 0;
@@ -100,6 +106,42 @@ bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, L
return SUCCEEDED(hr);
}
+bool e2d::Window::__initMutex(const String & sTitle)
+{
+ // 创建进程互斥体
+ HANDLE m_hMutex = ::CreateMutex(NULL, TRUE, sTitle);
+
+ if (m_hMutex == nullptr)
+ {
+ WARN_IF(true, "CreateMutex Failed!");
+ return true;
+ }
+
+ // 如果程序已经存在并且正在运行
+ if (::GetLastError() == ERROR_ALREADY_EXISTS)
+ {
+ // 获取窗口句柄
+ HWND hProgramWnd = ::FindWindow(L"Easy2DApp", sTitle);
+ if (hProgramWnd)
+ {
+ // 获取窗口显示状态
+ WINDOWPLACEMENT wpm;
+ ::GetWindowPlacement(hProgramWnd, &wpm);
+ // 将运行的程序窗口还原成正常状态
+ wpm.showCmd = SW_SHOW;
+ ::SetWindowPlacement(hProgramWnd, &wpm);
+ ::SetWindowPos(hProgramWnd, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE);
+ }
+
+ // 关闭进程互斥体
+ CloseHandle(m_hMutex);
+ m_hMutex = nullptr;
+ return false;
+ }
+
+ return true;
+}
+
void e2d::Window::__uninit()
{
// 关闭控制台
diff --git a/core/Common/Point.cpp b/core/Common/Point.cpp
new file mode 100644
index 00000000..f648177f
--- /dev/null
+++ b/core/Common/Point.cpp
@@ -0,0 +1,39 @@
+#include "..\ecommon.h"
+
+
+e2d::Point::Point()
+{
+ x = 0;
+ y = 0;
+}
+
+e2d::Point::Point(double x, double y)
+{
+ this->x = x;
+ this->y = y;
+}
+
+e2d::Point e2d::Point::operator+(Point const & p)
+{
+ return Point(x + p.x, y + p.y);
+}
+
+e2d::Point e2d::Point::operator-(Point const & p)
+{
+ return Point(x - p.x, y - p.y);
+}
+
+e2d::Point e2d::Point::operator*(double const & value)
+{
+ return Point(x * value, y * value);
+}
+
+e2d::Point e2d::Point::operator/(double const & value)
+{
+ return Point(x / value, y / value);
+}
+
+e2d::Point::operator e2d::Size() const
+{
+ return Size(x, y);
+}
diff --git a/core/Common/Size.cpp b/core/Common/Size.cpp
new file mode 100644
index 00000000..00f58992
--- /dev/null
+++ b/core/Common/Size.cpp
@@ -0,0 +1,38 @@
+#include "..\ecommon.h"
+
+e2d::Size::Size()
+{
+ width = 0;
+ height = 0;
+}
+
+e2d::Size::Size(double width, double height)
+{
+ this->width = width;
+ this->height = height;
+}
+
+e2d::Size e2d::Size::operator+(Size const & size)
+{
+ return Size(width + size.width, height + size.height);
+}
+
+e2d::Size e2d::Size::operator-(Size const & size)
+{
+ return Size(width - size.width, height - size.height);
+}
+
+e2d::Size e2d::Size::operator*(double const & value)
+{
+ return Size(width * value, height * value);
+}
+
+e2d::Size e2d::Size::operator/(double const & value)
+{
+ return Size(width / value, height / value);
+}
+
+e2d::Size::operator e2d::Point() const
+{
+ return Point(width, height);
+}
diff --git a/core/ebase.h b/core/ebase.h
index 24b2ba63..a8601138 100644
--- a/core/ebase.h
+++ b/core/ebase.h
@@ -95,6 +95,11 @@ private:
LPCTSTR pIconID
);
+ // 创建进程互斥体
+ static bool __initMutex(
+ const String & sTitle
+ );
+
// 重置窗口属性
static void __uninit();
diff --git a/core/ecommon.h b/core/ecommon.h
index 0c2abf2a..0344b648 100644
--- a/core/ecommon.h
+++ b/core/ecommon.h
@@ -8,82 +8,44 @@ namespace e2d
{
+struct Size;
+
// 表示坐标的结构体
struct Point
{
- double x;
- double y;
+ Point();
- Point()
- {
- x = 0;
- y = 0;
- }
+ Point(double x, double y);
- Point(double x, double y)
- {
- this->x = x;
- this->y = y;
- }
+ Point operator + (Point const & p);
+ Point operator - (Point const & p);
+ Point operator * (double const & value);
+ Point operator / (double const & value);
- Point operator + (Point const & p)
- {
- return Point(x + p.x, y + p.y);
- }
+ operator Size() const;
- Point operator - (Point const & p)
- {
- return Point(x - p.x, y - p.y);
- }
-
- Point operator * (double const & value)
- {
- return Point(x * value, y * value);
- }
-
- Point operator / (double const & value)
- {
- return Point(x / value, y / value);
- }
+ /* 成员变量 */
+ double x; // X 坐标
+ double y; // Y 坐标
};
// 表示大小的结构体
struct Size
{
- double width;
- double height;
+ Size();
- Size()
- {
- width = 0;
- height = 0;
- }
+ Size(double width, double height);
- Size(double width, double height)
- {
- this->width = width;
- this->height = height;
- }
+ Size operator + (Size const & size);
+ Size operator - (Size const & size);
+ Size operator * (double const & value);
+ Size operator / (double const & value);
- Size operator + (Size const & size)
- {
- return Size(width + size.width, height + size.height);
- }
+ operator Point() const;
- Size operator - (Size const & size)
- {
- return Size(width - size.width, height - size.height);
- }
-
- Size operator * (double const & value)
- {
- return Size(width * value, height * value);
- }
-
- Size operator / (double const & value)
- {
- return Size(width / value, height / value);
- }
+ /* 成员变量 */
+ double width; // 宽度
+ double height; // 高度
};
diff --git a/project/vs2017/Easy2D.vcxproj b/project/vs2017/Easy2D.vcxproj
index 932d66ab..6d0d5d6f 100644
--- a/project/vs2017/Easy2D.vcxproj
+++ b/project/vs2017/Easy2D.vcxproj
@@ -213,7 +213,9 @@
+
+
diff --git a/project/vs2017/Easy2D.vcxproj.filters b/project/vs2017/Easy2D.vcxproj.filters
index 0806619e..8e16089b 100644
--- a/project/vs2017/Easy2D.vcxproj.filters
+++ b/project/vs2017/Easy2D.vcxproj.filters
@@ -180,6 +180,12 @@
Shape
+
+ Common
+
+
+ Common
+