将Game::createMutex函数整合到Game::init中

This commit is contained in:
Nomango 2018-05-14 00:57:16 +08:00
parent d125e5cf5b
commit 6d45a70ff2
5 changed files with 27 additions and 50 deletions

View File

@ -118,7 +118,7 @@ e2d::Sequence * e2d::Sequence::reverse() const
{
if (*iter)
{
newActions.push_back(*iter);
newActions.push_back((*iter)->reverse());
}
}
sequence->add(newActions);

View File

@ -116,7 +116,7 @@ e2d::Spawn * e2d::Spawn::reverse() const
{
if (*iter)
{
newActions.push_back(*iter);
newActions.push_back((*iter)->reverse());
}
}
spawn->add(newActions);

View File

@ -13,7 +13,7 @@ static bool s_bInitialized = false;
static e2d::String s_sGameName;
bool e2d::Game::init(const String& name)
bool e2d::Game::init(const String& name, const String& mutexName)
{
if (s_bInitialized)
{
@ -21,6 +21,25 @@ bool e2d::Game::init(const String& name)
return false;
}
if (!mutexName.isEmpty())
{
// 创建进程互斥体
HANDLE hMutex = ::CreateMutex(NULL, TRUE, L"Easy2DApp-" + mutexName);
if (hMutex == nullptr)
{
WARN_IF(true, "CreateMutex Failed!");
}
else if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
// 如果程序已经存在并且正在运行,弹窗提示
Window::info(L"游戏已在其他窗口中打开!", L"提示");
// 关闭进程互斥体
::CloseHandle(hMutex);
return false;
}
}
// 初始化 COM 组件
CoInitialize(NULL);
@ -206,43 +225,6 @@ void e2d::Game::destroy()
s_bInitialized = false;
}
bool e2d::Game::createMutex(const String& sMutexName, const String& sWindowTitle)
{
// 创建进程互斥体
HANDLE _hMutex = ::CreateMutex(NULL, TRUE, L"Easy2DApp-" + sMutexName);
if (_hMutex == nullptr)
{
WARN_IF(true, "CreateMutex Failed!");
return true;
}
// 如果程序已经存在并且正在运行
if (::GetLastError() == ERROR_ALREADY_EXISTS)
{
// 关闭进程互斥体
::CloseHandle(_hMutex);
// 打开指定窗口
if (!sWindowTitle.isEmpty())
{
// 获取窗口句柄
HWND hProgramWnd = ::FindWindow(L"Easy2DApp", sWindowTitle);
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);
}
}
return false;
}
return true;
}
e2d::String e2d::Game::getName()
{
return s_sGameName;

View File

@ -291,7 +291,7 @@ void e2d::Window::setTypewritingEnable(bool enable)
}
}
void e2d::Window::prompt(const String & text, const String & title)
void e2d::Window::info(const String & text, const String & title)
{
::MessageBox(s_HWnd, text, title, MB_ICONINFORMATION | MB_OK);
Game::reset();

View File

@ -14,12 +14,13 @@ class Game
public:
// 初始化游戏
static bool init(
const String& name = L"" /* 游戏英文名称 */
const String& name = L"", /* 游戏英文名称 */
const String& mutexName = L"" /* 进程互斥体名称 */
);
// 启动游戏
static int start(
bool autoRelease = true /* 游戏结束时自动回收资源 */
bool autoRelease = true /* 游戏结束时自动回收资源 */
);
// 暂停游戏
@ -40,12 +41,6 @@ public:
// 游戏是否暂停
static bool isPaused();
// 创建进程互斥体
static bool createMutex(
const String& sMutexName, /* 互斥体名称 */
const String& sWindowTitle = L"" /* 窗口标题 */
);
// 获取游戏名称
static String getName();
};
@ -104,7 +99,7 @@ public:
);
// 弹出提示窗口
static void prompt(
static void info(
const String& text, /* 内容 */
const String& title = L"Prompt" /* 窗口标题 */
);