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 组件
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;

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*/)
{
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()
{
// 关闭控制台

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
);
// ´´½¨½ø³Ì»¥³âÌå
static bool __initMutex(
const String & sTitle
);
// 重置窗口属性
static void __uninit();

View File

@ -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; // 高度
};

View File

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

View File

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