From 6d45a70ff2f5c1cdaf2cbf9566f60925fed78b3e Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Mon, 14 May 2018 00:57:16 +0800 Subject: [PATCH] =?UTF-8?q?=E5=B0=86Game::createMutex=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E6=95=B4=E5=90=88=E5=88=B0Game::init=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/Action/Sequence.cpp | 2 +- core/Action/Spawn.cpp | 2 +- core/Base/Game.cpp | 58 ++++++++++++++-------------------------- core/Base/Window.cpp | 2 +- core/e2dbase.h | 13 +++------ 5 files changed, 27 insertions(+), 50 deletions(-) diff --git a/core/Action/Sequence.cpp b/core/Action/Sequence.cpp index 0f208c71..82d58c9d 100644 --- a/core/Action/Sequence.cpp +++ b/core/Action/Sequence.cpp @@ -118,7 +118,7 @@ e2d::Sequence * e2d::Sequence::reverse() const { if (*iter) { - newActions.push_back(*iter); + newActions.push_back((*iter)->reverse()); } } sequence->add(newActions); diff --git a/core/Action/Spawn.cpp b/core/Action/Spawn.cpp index c6de4d91..d92a51d2 100644 --- a/core/Action/Spawn.cpp +++ b/core/Action/Spawn.cpp @@ -116,7 +116,7 @@ e2d::Spawn * e2d::Spawn::reverse() const { if (*iter) { - newActions.push_back(*iter); + newActions.push_back((*iter)->reverse()); } } spawn->add(newActions); diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index df2ec536..43d22d38 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -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; diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index 72fdc5d5..93775623 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -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(); diff --git a/core/e2dbase.h b/core/e2dbase.h index 77a53e52..1462017a 100644 --- a/core/e2dbase.h +++ b/core/e2dbase.h @@ -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" /* 窗口标题 */ );