From 71b39a8149d0146c7da5cc520c7c775cbfa886c6 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 1 Feb 2018 09:38:25 +0800 Subject: [PATCH] fixed bugs. --- core/Base/Game.cpp | 46 ++++++++++++++++++------------------------ core/Base/Renderer.cpp | 27 +++++++++++++------------ core/Base/Window.cpp | 32 +++-------------------------- core/Tool/File.cpp | 11 +++++----- core/ebase.h | 34 +++++++++---------------------- 5 files changed, 52 insertions(+), 98 deletions(-) diff --git a/core/Base/Game.cpp b/core/Base/Game.cpp index 7ca5095d..83c8587a 100644 --- a/core/Base/Game.cpp +++ b/core/Base/Game.cpp @@ -1,8 +1,5 @@ #include "..\ebase.h" #include "..\emanagers.h" -#include "..\enodes.h" -#include "..\etransitions.h" -#include "..\etools.h" // 控制游戏终止 @@ -15,7 +12,7 @@ static bool s_bInitialized = false; static e2d::EString s_sAppName; -bool e2d::EGame::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, bool bNoClose, bool bNoMiniSize, bool bTopMost) +bool e2d::EGame::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID, LPCTSTR sAppname) { if (s_bInitialized) { @@ -30,16 +27,22 @@ bool e2d::EGame::init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIc if (ERenderer::__createDeviceIndependentResources()) { // 初始化窗口 - if (EWindow::__init(sTitle, nWidth, nHeight, pIconID, bNoClose, bNoMiniSize, bTopMost)) + if (EWindow::__init(sTitle, nWidth, nHeight, pIconID)) { // 创建设备相关资源 if (ERenderer::__createDeviceResources()) { // 重设 Client 大小 EWindow::setSize(nWidth, nHeight); - // 设置默认 AppName - if (s_sAppName.isEmpty()) + // 设置 AppName + if (sAppname) + { + s_sAppName = sAppname; + } + else + { s_sAppName = EWindow::getTitle(); + } // 标志初始化成功 s_bInitialized = true; } @@ -65,7 +68,7 @@ int e2d::EGame::run() // 进入第一个场景 ESceneManager::__enterNextScene(); // 显示窗口 - EWindow::showWindow(); + ::ShowWindow(EWindow::getHWnd(), SW_SHOWNORMAL); // 刷新窗口内容 ::UpdateWindow(EWindow::getHWnd()); // 处理窗口消息 @@ -74,9 +77,6 @@ int e2d::EGame::run() ETime::__updateNow(); ETime::__updateLast(); - // 挂起时长 - int nWaitMS = 0; - while (!s_bEndGame) { // 处理窗口消息 @@ -87,8 +87,9 @@ int e2d::EGame::run() // 判断是否达到了刷新状态 if (ETime::getDeltaTime() >= 17) { - ETime::__updateLast(); - EGame::__update(); + ETime::__updateLast(); // 刷新时间信息 + EGame::__update(); // 更新游戏内容 + ERenderer::__render(); // 渲染游戏画面 } else { @@ -144,20 +145,13 @@ void e2d::EGame::uninit() void e2d::EGame::__update() { - EInput::__updateDeviceState(); // 获取用户输入 + if (s_bPaused) + return; - if (!s_bPaused) - { - ETimerManager::__update(); // 定时器管理器执行程序 - EActionManager::__update(); // 动作管理器执行程序 - ESceneManager::__update(); // 更新游戏内容 - ERenderer::__render(); // 渲染游戏画面 - } -} - -void e2d::EGame::setAppName(const EString &appname) -{ - s_sAppName = appname; + EInput::__updateDeviceState(); // 获取用户输入 + ETimerManager::__update(); // 定时器管理器执行程序 + EActionManager::__update(); // 动作管理器执行程序 + ESceneManager::__update(); // 更新游戏内容 } e2d::EString e2d::EGame::getAppName() diff --git a/core/Base/Renderer.cpp b/core/Base/Renderer.cpp index 5c3ce8e1..bd2e1823 100644 --- a/core/Base/Renderer.cpp +++ b/core/Base/Renderer.cpp @@ -17,7 +17,7 @@ bool e2d::ERenderer::__createDeviceIndependentResources() &s_pDirect2dFactory ); - ASSERT(SUCCEEDED(hr), "Create Device Independent Resources Failed!"); + ASSERT(SUCCEEDED(hr), "Create ID2D1Factory Failed!"); if (SUCCEEDED(hr)) { @@ -29,7 +29,7 @@ bool e2d::ERenderer::__createDeviceIndependentResources() IID_IWICImagingFactory, reinterpret_cast(&s_pIWICFactory) ); - ASSERT(SUCCEEDED(hr), "Create WICImagingFactory Failed!"); + ASSERT(SUCCEEDED(hr), "Create IWICImagingFactory Failed!"); } if (SUCCEEDED(hr)) @@ -40,7 +40,7 @@ bool e2d::ERenderer::__createDeviceIndependentResources() __uuidof(IDWriteFactory), reinterpret_cast(&s_pDWriteFactory) ); - ASSERT(SUCCEEDED(hr), "Create DirectWrite Factory Failed!"); + ASSERT(SUCCEEDED(hr), "Create IDWriteFactory Failed!"); } return SUCCEEDED(hr); @@ -73,18 +73,19 @@ bool e2d::ERenderer::__createDeviceResources() &s_pRenderTarget ); - ASSERT(SUCCEEDED(hr), "Create Render Target Failed!"); - } + ASSERT(SUCCEEDED(hr), "Create ID2D1HwndRenderTarget Failed!"); - if (SUCCEEDED(hr)) - { - // 创建画刷 - hr = s_pRenderTarget->CreateSolidColorBrush( - D2D1::ColorF(D2D1::ColorF::White), - &s_pSolidBrush - ); - ASSERT(SUCCEEDED(hr), "Create Solid Color Brush Failed!"); + if (SUCCEEDED(hr)) + { + // 创建画刷 + hr = s_pRenderTarget->CreateSolidColorBrush( + D2D1::ColorF(D2D1::ColorF::White), + &s_pSolidBrush + ); + ASSERT(SUCCEEDED(hr), "Create ID2D1SolidColorBrush Failed!"); + } } + return SUCCEEDED(hr); } diff --git a/core/Base/Window.cpp b/core/Base/Window.cpp index 8506330b..d4f6d32d 100644 --- a/core/Base/Window.cpp +++ b/core/Base/Window.cpp @@ -9,7 +9,7 @@ static HWND s_HWnd = nullptr; static bool s_bShowConsole = false; -bool e2d::EWindow::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/, bool bNoClose /*= false*/, bool bNoMiniSize /*= false*/, bool bTopMost /*= false*/) +bool e2d::EWindow::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR pIconID /*= nullptr*/) { // 注册窗口类 WNDCLASSEX wcex = { sizeof(WNDCLASSEX) }; @@ -22,11 +22,6 @@ bool e2d::EWindow::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR wcex.lpszMenuName = NULL; wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION); wcex.lpszClassName = L"Easy2DApp"; - // 设置窗口是否有关闭按钮 - if (bNoClose) - { - wcex.style |= CS_NOCLOSE; - } // 设置程序图标 if (pIconID) { @@ -60,17 +55,11 @@ bool e2d::EWindow::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR nWidth = min(nWidth, screenWidth); nHeight = min(nHeight, screenHeight); - // 创建窗口样式 - DWORD dwStyle = WS_OVERLAPPED | WS_SYSMENU; - if (!bNoMiniSize) - { - dwStyle |= WS_MINIMIZEBOX; - } // 创建窗口 s_HWnd = CreateWindow( L"Easy2DApp", sTitle, - dwStyle, + WS_OVERLAPPED | WS_SYSMENU, 0, 0, nWidth, nHeight, NULL, NULL, @@ -84,11 +73,6 @@ bool e2d::EWindow::__init(LPCTSTR sTitle, UINT32 nWidth, UINT32 nHeight, LPCTSTR { // 禁用输入法 EWindow::setTypewritingEnable(false); - // 设置窗口置顶 - if (bTopMost) - { - ::SetWindowPos(s_HWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE); - } // 查找是否存在控制台 HWND hwnd = ::GetConsoleWindow(); if (hwnd) @@ -177,7 +161,7 @@ void e2d::EWindow::setSize(UINT32 width, UINT32 height) void e2d::EWindow::setTitle(const EString &title) { // 设置窗口标题 - SetWindowText(s_HWnd, title); + ::SetWindowText(s_HWnd, title); } e2d::EString e2d::EWindow::getTitle() @@ -229,16 +213,6 @@ void e2d::EWindow::showConsole(bool show /* = true */) } } -void e2d::EWindow::hideWindow() -{ - ::ShowWindow(s_HWnd, SW_HIDE); -} - -void e2d::EWindow::showWindow() -{ - ::ShowWindow(s_HWnd, SW_SHOWNORMAL); -} - void e2d::EWindow::setTypewritingEnable(bool bEnable) { static HIMC hImc = nullptr; diff --git a/core/Tool/File.cpp b/core/Tool/File.cpp index 20b12bee..cbb3bd4b 100644 --- a/core/Tool/File.cpp +++ b/core/Tool/File.cpp @@ -8,10 +8,11 @@ DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91); -typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); e2d::EString e2d::EFile::getLocalAppDataPath() { + typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(const GUID& rfid, DWORD dwFlags, HANDLE hToken, PWSTR *ppszPath); + // 获取 AppData\Local 文件夹的路径 PWSTR pszPath = NULL; HMODULE hModule = LoadLibrary(L"shell32.dll"); @@ -35,9 +36,9 @@ e2d::EString e2d::EFile::getTempPath() // 创建临时文件目录 e2d::EString tempFilePath = path + e2d::EGame::getAppName(); - if (_waccess(tempFilePath, 0) == -1) + if (::_waccess(tempFilePath, 0) == -1) { - _wmkdir(tempFilePath); + ::_wmkdir(tempFilePath); } return tempFilePath; } @@ -49,9 +50,9 @@ e2d::EString e2d::EFile::getDefaultSavePath() path += L"\\" + EGame::getAppName(); - if (_waccess(path, 0) == -1) + if (::_waccess(path, 0) == -1) { - _wmkdir(path); + ::_wmkdir(path); } path += L"\\DefaultData.ini"; diff --git a/core/ebase.h b/core/ebase.h index 34c9ac78..ff3098ff 100644 --- a/core/ebase.h +++ b/core/ebase.h @@ -18,9 +18,7 @@ public: UINT32 nWidth, /* 窗口宽度 */ UINT32 nHeight, /* 窗口高度 */ LPCTSTR pIconID = nullptr, /* 窗口图标 */ - bool bNoClose = false, /* 禁用关闭按钮 */ - bool bNoMiniSize = false, /* 禁用最小化按钮 */ - bool bTopMost = false /* 窗口置顶 */ + LPCTSTR sAppname = nullptr /* AppName */ ); // 启动游戏 @@ -44,11 +42,6 @@ public: // 获取 AppName static EString getAppName(); - // 设置 AppName - static void setAppName( - const EString &appname - ); - private: // 更新游戏内容 static void __update(); @@ -78,26 +71,20 @@ public: // 修改窗口大小 static void setSize( - UINT32 width, - UINT32 height + UINT32 nWidth, + UINT32 nHeight ); // 设置窗口标题 static void setTitle( - const EString & title + const EString & sTitle ); // 打开/隐藏控制台 static void showConsole( - bool show = true + bool bShow = true ); - // 隐藏主窗口 - static void hideWindow(); - - // 显示主窗口 - static void showWindow(); - // 是否允许响应输入法 static void setTypewritingEnable( bool bEnable @@ -106,13 +93,10 @@ public: private: // 初始化窗口 static bool __init( - LPCTSTR sTitle, /* 窗口标题 */ - UINT32 nWidth, /* 窗口宽度 */ - UINT32 nHeight, /* 窗口高度 */ - LPCTSTR pIconID, /* 窗口图标 */ - bool bNoClose, /* 禁用关闭按钮 */ - bool bNoMiniSize, /* 禁用最小化按钮 */ - bool bTopMost /* 窗口置顶 */ + LPCTSTR sTitle, + UINT32 nWidth, + UINT32 nHeight, + LPCTSTR pIconID ); // 重置窗口属性