new feature: singleton process

This commit is contained in:
Nomango 2018-03-01 00:19:09 +08:00
parent bca9f6f792
commit d0c167360e
8 changed files with 158 additions and 63 deletions

View File

@ -25,6 +25,9 @@ bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTS
// 初始化 COM 组件 // 初始化 COM 组件
CoInitializeEx(NULL, COINIT_MULTITHREADED); CoInitializeEx(NULL, COINIT_MULTITHREADED);
// ÉèÖÃ AppName
s_sAppName = sAppname;
// 创建设备无关资源 // 创建设备无关资源
if (!Renderer::__createDeviceIndependentResources()) if (!Renderer::__createDeviceIndependentResources())
{ {
@ -62,8 +65,6 @@ bool e2d::Game::init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTS
// 重设 Client 大小 // 重设 Client 大小
Window::setSize(nWidth, nHeight); Window::setSize(nWidth, nHeight);
// ÉèÖÃ AppName
s_sAppName = sAppname.isEmpty() ? Window::getTitle() : sAppname;
// 标志初始化成功 // 标志初始化成功
s_bInitialized = true; s_bInitialized = true;

View File

@ -11,8 +11,14 @@ static bool s_bShowConsole = false;
bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/) 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.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = Window::WndProc; wcex.lpfnWndProc = Window::WndProc;
wcex.cbClsExtra = 0; wcex.cbClsExtra = 0;
@ -100,6 +106,42 @@ bool e2d::Window::__init(const String & sTitle, UINT32 nWidth, UINT32 nHeight, L
return SUCCEEDED(hr); 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() void e2d::Window::__uninit()
{ {
// 关闭控制台 // 关闭控制台

39
core/Common/Point.cpp Normal file
View File

@ -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);
}

38
core/Common/Size.cpp Normal file
View File

@ -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);
}

View File

@ -95,6 +95,11 @@ private:
LPCTSTR pIconID LPCTSTR pIconID
); );
// ´´½¨½ø³Ì»¥³âÌå
static bool __initMutex(
const String & sTitle
);
// 重置窗口属性 // 重置窗口属性
static void __uninit(); static void __uninit();

View File

@ -8,82 +8,44 @@ namespace e2d
{ {
struct Size;
// 表示坐标的结构体 // 表示坐标的结构体
struct Point struct Point
{ {
double x; Point();
double y;
Point() Point(double x, double y);
{
x = 0;
y = 0;
}
Point(double x, double y) Point operator + (Point const & p);
{ Point operator - (Point const & p);
this->x = x; Point operator * (double const & value);
this->y = y; Point operator / (double const & value);
}
Point operator + (Point const & p) operator Size() const;
{
return Point(x + p.x, y + p.y);
}
Point operator - (Point const & p) /* 成员变量 */
{ double x; // X 坐标
return Point(x - p.x, y - p.y); double y; // Y 坐标
}
Point operator * (double const & value)
{
return Point(x * value, y * value);
}
Point operator / (double const & value)
{
return Point(x / value, y / value);
}
}; };
// 表示大小的结构体 // 表示大小的结构体
struct Size struct Size
{ {
double width; Size();
double height;
Size() Size(double width, double height);
{
width = 0;
height = 0;
}
Size(double width, double height) Size operator + (Size const & size);
{ Size operator - (Size const & size);
this->width = width; Size operator * (double const & value);
this->height = height; Size operator / (double const & value);
}
Size operator + (Size const & size) operator Point() const;
{
return Size(width + size.width, height + size.height);
}
Size operator - (Size const & size) /* 成员变量 */
{ double width; // 宽度
return Size(width - size.width, height - size.height); double height; // 高度
}
Size operator * (double const & value)
{
return Size(width * value, height * value);
}
Size operator / (double const & value)
{
return Size(width / value, height / value);
}
}; };

View File

@ -213,7 +213,9 @@
<ClCompile Include="..\..\core\Base\Window.cpp" /> <ClCompile Include="..\..\core\Base\Window.cpp" />
<ClCompile Include="..\..\core\Common\Font.cpp" /> <ClCompile Include="..\..\core\Common\Font.cpp" />
<ClCompile Include="..\..\core\Common\Object.cpp" /> <ClCompile Include="..\..\core\Common\Object.cpp" />
<ClCompile Include="..\..\core\Common\Point.cpp" />
<ClCompile Include="..\..\core\Common\Scene.cpp" /> <ClCompile Include="..\..\core\Common\Scene.cpp" />
<ClCompile Include="..\..\core\Common\Size.cpp" />
<ClCompile Include="..\..\core\Common\String.cpp" /> <ClCompile Include="..\..\core\Common\String.cpp" />
<ClCompile Include="..\..\core\Common\Image.cpp" /> <ClCompile Include="..\..\core\Common\Image.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" /> <ClCompile Include="..\..\core\Manager\ActionManager.cpp" />

View File

@ -180,6 +180,12 @@
<ClCompile Include="..\..\core\Shape\Rect.cpp"> <ClCompile Include="..\..\core\Shape\Rect.cpp">
<Filter>Shape</Filter> <Filter>Shape</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Common\Point.cpp">
<Filter>Common</Filter>
</ClCompile>
<ClCompile Include="..\..\core\Common\Size.cpp">
<Filter>Common</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\core\etools.h" /> <ClInclude Include="..\..\core\etools.h" />