From b7de2733bfa10567f6120283f722d7a4d7096f31 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Thu, 5 Oct 2017 00:53:03 +0800 Subject: [PATCH] =?UTF-8?q?Object=E4=B8=8D=E5=86=8D=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=87=8A=E6=94=BE=EF=BC=8C=E9=99=A4=E9=9D=9E=E8=B0=83=E7=94=A8?= =?UTF-8?q?autoRelease=E5=87=BD=E6=95=B0=E5=B0=86=E5=85=B6=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E9=87=8A=E6=94=BE=E7=AE=A1=E7=90=86=E6=B1=A0=E4=B8=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Easy2D/Action/Action.cpp | 2 - Easy2D/Action/ActionNeverStop.cpp | 2 +- Easy2D/Action/ActionSequence.cpp | 2 +- Easy2D/Action/ActionTwo.cpp | 8 +- Easy2D/Base/App.cpp | 22 ++-- Easy2D/Base/FreePool.cpp | 4 +- Easy2D/Msg/KeyMsg.cpp | 6 +- Easy2D/Msg/MouseMsg.cpp | 6 +- Easy2D/Object/Button/ImageButton.cpp | 16 +-- Easy2D/Object/Button/TextButton.cpp | 18 +-- Easy2D/Object/Image.cpp | 10 +- Easy2D/Object/Object.cpp | 12 +- Easy2D/Object/Sprite.cpp | 4 +- Easy2D/Object/Text.cpp | 12 +- Easy2D/Tool/FileUtils.cpp | 20 ++-- Easy2D/Tool/MusicUtils.cpp | 22 ++-- Easy2D/Tool/Timer.cpp | 16 +-- Easy2D/easy2d.h | 170 +++++++++++++-------------- 18 files changed, 177 insertions(+), 175 deletions(-) diff --git a/Easy2D/Action/Action.cpp b/Easy2D/Action/Action.cpp index d344d030..d2c008d3 100644 --- a/Easy2D/Action/Action.cpp +++ b/Easy2D/Action/Action.cpp @@ -8,8 +8,6 @@ Action::Action() : { // 默认动作 15ms 运行一次 setInterval(15); - // 保留动作 - this->retain(); } Action::~Action() diff --git a/Easy2D/Action/ActionNeverStop.cpp b/Easy2D/Action/ActionNeverStop.cpp index 72c57bf3..746aa0dc 100644 --- a/Easy2D/Action/ActionNeverStop.cpp +++ b/Easy2D/Action/ActionNeverStop.cpp @@ -8,7 +8,7 @@ ActionNeverStop::ActionNeverStop(Action * action) : ActionNeverStop::~ActionNeverStop() { - SAFE_RELEASE(m_Action); + SafeRelease(m_Action); } ActionNeverStop * ActionNeverStop::copy() const diff --git a/Easy2D/Action/ActionSequence.cpp b/Easy2D/Action/ActionSequence.cpp index 8ed1ae43..841bc569 100644 --- a/Easy2D/Action/ActionSequence.cpp +++ b/Easy2D/Action/ActionSequence.cpp @@ -25,7 +25,7 @@ ActionSequence::~ActionSequence() { for (auto action : m_vActions) { - SAFE_RELEASE(action); + SafeRelease(action); } } diff --git a/Easy2D/Action/ActionTwo.cpp b/Easy2D/Action/ActionTwo.cpp index 3003c113..9e3a7f7c 100644 --- a/Easy2D/Action/ActionTwo.cpp +++ b/Easy2D/Action/ActionTwo.cpp @@ -10,8 +10,8 @@ ActionTwo::ActionTwo(Action * actionFirst, Action * actionSecond) : ActionTwo::~ActionTwo() { - SAFE_RELEASE(m_FirstAction); - SAFE_RELEASE(m_SecondAction); + SafeRelease(m_FirstAction); + SafeRelease(m_SecondAction); } ActionTwo * ActionTwo::copy() const @@ -50,7 +50,7 @@ bool ActionTwo::_exec(LARGE_INTEGER nNow) { // 返回 true 表示第一个动作已经结束,删除这个 // 动作,并初始化第二个动作 - SAFE_RELEASE(m_FirstAction); + SafeRelease(m_FirstAction); m_FirstAction = nullptr; m_SecondAction->_init(); } @@ -59,7 +59,7 @@ bool ActionTwo::_exec(LARGE_INTEGER nNow) { if (m_SecondAction->_exec(nNow)) { - SAFE_RELEASE(m_SecondAction); + SafeRelease(m_SecondAction); m_SecondAction = nullptr; return true; } diff --git a/Easy2D/Base/App.cpp b/Easy2D/Base/App.cpp index e025d384..fe9324a7 100644 --- a/Easy2D/Base/App.cpp +++ b/Easy2D/Base/App.cpp @@ -194,7 +194,7 @@ void App::createWindow(CSize size, int mode) createWindow(size.cx, size.cy, mode); } -void App::createWindow(tstring title, int width, int height, int mode) +void App::createWindow(TString title, int width, int height, int mode) { // 保存窗口信息 m_Size.cx = width; @@ -206,7 +206,7 @@ void App::createWindow(tstring title, int width, int height, int mode) _initGraph(); } -void App::createWindow(tstring title, CSize size, int mode) +void App::createWindow(TString title, CSize size, int mode) { createWindow(title, size.cx, size.cy, mode); } @@ -246,7 +246,7 @@ void App::setWindowSize(CSize size) setWindowSize(size.cx, size.cy); } -void App::setWindowTitle(tstring title) +void App::setWindowTitle(TString title) { // 设置窗口标题 SetWindowText(GetHWnd(), title.c_str()); @@ -254,7 +254,7 @@ void App::setWindowTitle(tstring title) s_pInstance->m_sTitle = title; } -tstring App::getWindowTitle() +TString App::getWindowTitle() { return s_pInstance->m_sTitle; } @@ -286,17 +286,17 @@ void App::clearScene() while (s_pInstance->m_SceneStack.size()) { auto temp = s_pInstance->m_SceneStack.top(); - SAFE_DELETE(temp); + SafeDelete(temp); s_pInstance->m_SceneStack.pop(); } } -void App::setAppName(tstring appname) +void App::setAppName(TString appname) { s_pInstance->m_sAppName = appname; } -tstring App::getAppName() +TString App::getAppName() { return s_pInstance->m_sAppName; } @@ -320,7 +320,7 @@ void App::_enterNextScene() } else { - SAFE_DELETE(m_CurrentScene); // 否则删除当前场景 + SafeDelete(m_CurrentScene); // 否则删除当前场景 } m_CurrentScene = m_NextScene; // 切换场景 @@ -372,13 +372,13 @@ int App::getHeight() void App::free() { // 释放场景内存 - SAFE_DELETE(m_CurrentScene); - SAFE_DELETE(m_NextScene); + SafeDelete(m_CurrentScene); + SafeDelete(m_NextScene); // 清空场景栈 while (m_SceneStack.size()) { auto temp = m_SceneStack.top(); - SAFE_DELETE(temp); + SafeDelete(temp); m_SceneStack.pop(); } // 删除所有定时器 diff --git a/Easy2D/Base/FreePool.cpp b/Easy2D/Base/FreePool.cpp index f2ab7fd0..c992d1e2 100644 --- a/Easy2D/Base/FreePool.cpp +++ b/Easy2D/Base/FreePool.cpp @@ -2,7 +2,7 @@ #include // FreePool 释放池的实现机制: -/// Object 类中的引用计数(m_nRef)保证了指针的使用安全 +/// Object 类中的引用计数(m_nRefCount)保证了指针的使用安全 /// 它记录了对象被使用的次数,当计数为 0 时,FreePool 会自动释放这个对象 /// 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中) /// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数 @@ -19,7 +19,7 @@ void FreePool::__flush() for (iter = pool.begin(); iter != pool.end();) { // 若对象的引用的计数为 0 - if ((*iter)->m_nRef == 0) + if ((*iter)->m_nRefCount == 0) { // 释放该对象 delete (*iter); diff --git a/Easy2D/Msg/KeyMsg.cpp b/Easy2D/Msg/KeyMsg.cpp index faa2c67f..67a041c2 100644 --- a/Easy2D/Msg/KeyMsg.cpp +++ b/Easy2D/Msg/KeyMsg.cpp @@ -80,7 +80,7 @@ const VK_KEY KeyMsg::F12 = VK_F12; static VK_KEY convert(int ascii); -KeyMsg::KeyMsg(tstring name, const KEY_CALLBACK & callback) +KeyMsg::KeyMsg(TString name, const KEY_CALLBACK & callback) { m_sName = name; m_callback = callback; @@ -108,7 +108,7 @@ void KeyMsg::__exec() } } -void KeyMsg::addListener(tstring name, const KEY_CALLBACK & callback) +void KeyMsg::addListener(TString name, const KEY_CALLBACK & callback) { // 创建新的监听对象 auto key = new KeyMsg(name, callback); @@ -116,7 +116,7 @@ void KeyMsg::addListener(tstring name, const KEY_CALLBACK & callback) s_vKeyMsg.push_back(key); } -bool KeyMsg::delListener(tstring name) +bool KeyMsg::delListener(TString name) { // 创建迭代器 std::vector::iterator iter; diff --git a/Easy2D/Msg/MouseMsg.cpp b/Easy2D/Msg/MouseMsg.cpp index a13693f6..d366b936 100644 --- a/Easy2D/Msg/MouseMsg.cpp +++ b/Easy2D/Msg/MouseMsg.cpp @@ -28,7 +28,7 @@ MouseMsg::MouseMsg() { } -MouseMsg::MouseMsg(tstring name, const MOUSE_CALLBACK & callback) +MouseMsg::MouseMsg(TString name, const MOUSE_CALLBACK & callback) { m_sName = name; m_callback = callback; @@ -43,7 +43,7 @@ void MouseMsg::onMouseMsg() m_callback(); } -void MouseMsg::addListener(tstring name, const MOUSE_CALLBACK & callback) +void MouseMsg::addListener(TString name, const MOUSE_CALLBACK & callback) { // 创建新的监听对象 auto mouse = new MouseMsg(name, callback); @@ -51,7 +51,7 @@ void MouseMsg::addListener(tstring name, const MOUSE_CALLBACK & callback) s_vMouseMsg.push_back(mouse); } -bool MouseMsg::delListener(tstring name) +bool MouseMsg::delListener(TString name) { // 创建迭代器 std::vector::iterator iter; diff --git a/Easy2D/Object/Button/ImageButton.cpp b/Easy2D/Object/Button/ImageButton.cpp index 3572e2da..824b8113 100644 --- a/Easy2D/Object/Button/ImageButton.cpp +++ b/Easy2D/Object/Button/ImageButton.cpp @@ -24,10 +24,10 @@ ImageButton::ImageButton(Image * image) : ImageButton::~ImageButton() { // 所有图片的引用计数减一 - SAFE_RELEASE(m_pNormalImage); - SAFE_RELEASE(m_pMouseInImage); - SAFE_RELEASE(m_pSelectedImage); - SAFE_RELEASE(m_pUnableImage); + SafeRelease(m_pNormalImage); + SafeRelease(m_pMouseInImage); + SafeRelease(m_pSelectedImage); + SafeRelease(m_pUnableImage); } void ImageButton::_setStatus(Status status) @@ -99,7 +99,7 @@ void ImageButton::setNormal(Image * image) if (image) { // 原图片引用计数减一 - SAFE_RELEASE(m_pNormalImage); + SafeRelease(m_pNormalImage); // 修改图片 m_pNormalImage = image; // 现图片引用计数加一 @@ -115,7 +115,7 @@ void ImageButton::setMouseIn(Image * image) { if (image) { - SAFE_RELEASE(m_pMouseInImage); + SafeRelease(m_pMouseInImage); m_pMouseInImage = image; m_pMouseInImage->retain(); _resetPosition(); @@ -126,7 +126,7 @@ void ImageButton::setSelected(Image * image) { if (image) { - SAFE_RELEASE(m_pSelectedImage); + SafeRelease(m_pSelectedImage); m_pSelectedImage = image; m_pSelectedImage->retain(); _resetPosition(); @@ -137,7 +137,7 @@ void ImageButton::setUnable(Image * image) { if (image) { - SAFE_RELEASE(m_pUnableImage); + SafeRelease(m_pUnableImage); m_pUnableImage = image; m_pUnableImage->retain(); _resetPosition(); diff --git a/Easy2D/Object/Button/TextButton.cpp b/Easy2D/Object/Button/TextButton.cpp index b5e10acd..fcebafad 100644 --- a/Easy2D/Object/Button/TextButton.cpp +++ b/Easy2D/Object/Button/TextButton.cpp @@ -9,7 +9,7 @@ TextButton::TextButton() : { } -TextButton::TextButton(tstring text) : +TextButton::TextButton(TString text) : TextButton() { setNormal(new Text(text)); // 设置按钮在正常状态时的文字 @@ -24,10 +24,10 @@ TextButton::TextButton(Text * text) : TextButton::~TextButton() { // 所有文本的引用计数减一 - SAFE_RELEASE(m_pNormalText); - SAFE_RELEASE(m_pMouseInText); - SAFE_RELEASE(m_pSelectedText); - SAFE_RELEASE(m_pUnableText); + SafeRelease(m_pNormalText); + SafeRelease(m_pMouseInText); + SafeRelease(m_pSelectedText); + SafeRelease(m_pUnableText); } void TextButton::_setStatus(Status status) @@ -99,7 +99,7 @@ void TextButton::setNormal(Text * text) if (text) { // 原文本引用计数减一 - SAFE_RELEASE(m_pNormalText); + SafeRelease(m_pNormalText); // 修改文本 m_pNormalText = text; // 现文本引用计数加一 @@ -115,7 +115,7 @@ void TextButton::setMouseIn(Text * text) { if (text) { - SAFE_RELEASE(m_pMouseInText); + SafeRelease(m_pMouseInText); m_pMouseInText = text; m_pMouseInText->retain(); _resetPosition(); @@ -126,7 +126,7 @@ void TextButton::setSelected(Text * text) { if (text) { - SAFE_RELEASE(m_pSelectedText); + SafeRelease(m_pSelectedText); m_pSelectedText = text; m_pSelectedText->retain(); _resetPosition(); @@ -137,7 +137,7 @@ void TextButton::setUnable(Text * text) { if (text) { - SAFE_RELEASE(m_pUnableText); + SafeRelease(m_pUnableText); m_pUnableText = text; m_pUnableText->retain(); _resetPosition(); diff --git a/Easy2D/Object/Image.cpp b/Easy2D/Object/Image.cpp index 6bc7c8d7..b4612005 100644 --- a/Easy2D/Object/Image.cpp +++ b/Easy2D/Object/Image.cpp @@ -4,9 +4,9 @@ using namespace std; // 图片缓存 -static map s_mCImages; +static map s_mCImages; // 从图片缓存中读取图片 -static CImage* GetCImage(tstring name, bool fromRes = false); +static CImage* GetCImage(TString name, bool fromRes = false); // 对 PNG 图像进行像素转换 static void CrossImage(CImage &img); @@ -169,7 +169,7 @@ void Image::reset() void Image::saveScreenshot() { - tstring savePath; + TString savePath; // 获取保存位置 if (FileUtils::getSaveFilePath(savePath, _T("截图保存到"), _T("jpg"))) { @@ -197,7 +197,7 @@ void CrossImage(CImage &img) } } -CImage* GetCImage(tstring name, bool fromRes) +CImage* GetCImage(TString name, bool fromRes) { if (s_mCImages.find(name) == s_mCImages.end()) { @@ -230,7 +230,7 @@ CImage* GetCImage(tstring name, bool fromRes) // 透明图片处理 CrossImage(*cImage); } - s_mCImages.insert(map::value_type(name, cImage)); + s_mCImages.insert(map::value_type(name, cImage)); } return s_mCImages.at(name); } \ No newline at end of file diff --git a/Easy2D/Object/Object.cpp b/Easy2D/Object/Object.cpp index 3b7d1a5e..5392f881 100644 --- a/Easy2D/Object/Object.cpp +++ b/Easy2D/Object/Object.cpp @@ -1,9 +1,8 @@ #include "..\easy2d.h" Object::Object() : - m_nRef(0) + m_nRefCount(0) { - FreePool::__add(this); // 将该对象放入释放池中 } Object::~Object() @@ -12,10 +11,15 @@ Object::~Object() void Object::retain() { - m_nRef++; // 引用计数加一 + m_nRefCount++; // 引用计数加一 } void Object::release() { - m_nRef--; // 引用计数减一 + m_nRefCount--; // 引用计数减一 +} + +void Object::autoRelease() +{ + FreePool::__add(this); // 将该对象放入释放池中 } diff --git a/Easy2D/Object/Sprite.cpp b/Easy2D/Object/Sprite.cpp index 35d100e5..46c660b2 100644 --- a/Easy2D/Object/Sprite.cpp +++ b/Easy2D/Object/Sprite.cpp @@ -24,7 +24,7 @@ Sprite::Sprite(LPCTSTR imageFileName) : Sprite::~Sprite() { - SAFE_RELEASE(m_pImage); + SafeRelease(m_pImage); } bool Sprite::_exec(bool active) @@ -52,7 +52,7 @@ void Sprite::_onDraw() void Sprite::setImage(Image * image) { - SAFE_RELEASE(m_pImage); + SafeRelease(m_pImage); m_pImage = image; setSize(int(m_pImage->getWidth() * m_fScaleX), int(m_pImage->getHeight() * m_fScaleY)); m_pImage->retain(); diff --git a/Easy2D/Object/Text.cpp b/Easy2D/Object/Text.cpp index 3b9e7f5a..607eb328 100644 --- a/Easy2D/Object/Text.cpp +++ b/Easy2D/Object/Text.cpp @@ -10,7 +10,7 @@ Text::Text() : m_pFontStyle->retain(); // 字体引用计数加一 } -Text::Text(tstring text, COLORREF color, FontStyle * font) : +Text::Text(TString text, COLORREF color, FontStyle * font) : m_color(color), m_pFontStyle(font) { @@ -18,7 +18,7 @@ Text::Text(tstring text, COLORREF color, FontStyle * font) : m_pFontStyle->retain(); // 字体引用计数加一 } -Text::Text(int x, int y, tstring text, COLORREF color, FontStyle * font) : +Text::Text(int x, int y, TString text, COLORREF color, FontStyle * font) : m_color(color), m_pFontStyle(font) { @@ -29,7 +29,7 @@ Text::Text(int x, int y, tstring text, COLORREF color, FontStyle * font) : Text::~Text() { - SAFE_RELEASE(m_pFontStyle); // 字体引用计数减一 + SafeRelease(m_pFontStyle); // 字体引用计数减一 } void Text::_onDraw() @@ -52,7 +52,7 @@ COLORREF Text::getColor() const return m_color; } -tstring Text::getText() const +TString Text::getText() const { return m_sText; } @@ -67,7 +67,7 @@ bool Text::isEmpty() const return m_sText.empty(); // 文本是否为空 } -void Text::setText(tstring text) +void Text::setText(TString text) { m_sText = text; // 先设置字体,然后获取该文本在该字体下的宽度和高度 @@ -82,7 +82,7 @@ void Text::setColor(COLORREF color) void Text::setFontStyle(FontStyle * style) { - SAFE_RELEASE(m_pFontStyle); // 原字体引用计数减一 + SafeRelease(m_pFontStyle); // 原字体引用计数减一 m_pFontStyle = style; // 修改字体 m_pFontStyle->retain(); // 现字体引用计数加一 // 先设置字体,然后获取该文本在该字体下的宽度和高度 diff --git a/Easy2D/Tool/FileUtils.cpp b/Easy2D/Tool/FileUtils.cpp index 32dd5d0b..4d5179d6 100644 --- a/Easy2D/Tool/FileUtils.cpp +++ b/Easy2D/Tool/FileUtils.cpp @@ -10,7 +10,7 @@ #include #endif -tstring FileUtils::getLocalAppDataPath() +TString FileUtils::getLocalAppDataPath() { TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 }; TCHAR szDocument[MAX_PATH] = { 0 }; @@ -27,7 +27,7 @@ tstring FileUtils::getLocalAppDataPath() return m_lpszDefaultDir; } -tstring FileUtils::getDefaultSavePath() +TString FileUtils::getDefaultSavePath() { TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 }; TCHAR szDocument[MAX_PATH] = { 0 }; @@ -41,7 +41,7 @@ tstring FileUtils::getDefaultSavePath() GetShortPathName(szDocument, m_lpszDefaultDir, _MAX_PATH); } - tstring path = m_lpszDefaultDir; + TString path = m_lpszDefaultDir; path.append(_T("\\")); path.append(App::get()->getAppName()); @@ -84,7 +84,7 @@ void FileUtils::saveDouble(LPCTSTR key, double value) ::WritePrivateProfileString(_T("Default"), key, ss.str().c_str(), getDefaultSavePath().c_str()); } -void FileUtils::saveString(LPCTSTR key, tstring value) +void FileUtils::saveString(LPCTSTR key, TString value) { ::WritePrivateProfileString(_T("Default"), key, value.c_str(), getDefaultSavePath().c_str()); } @@ -119,20 +119,20 @@ double FileUtils::getDouble(LPCTSTR key, double default) return d; } -tstring FileUtils::getString(LPCTSTR key, tstring default) +TString FileUtils::geTString(LPCTSTR key, TString default) { TCHAR temp[128] = { 0 }; ::GetPrivateProfileString(_T("Default"), key, default.c_str(), temp, 128, getDefaultSavePath().c_str()); - return tstring(temp); + return TString(temp); } -tstring FileUtils::getFileExtension(const tstring & filePath) +TString FileUtils::getFileExtension(const TString & filePath) { - tstring fileExtension; + TString fileExtension; // 找到文件名中的最后一个 '.' 的位置 size_t pos = filePath.find_last_of('.'); // 判断 pos 是否是个有效位置 - if (pos != tstring::npos) + if (pos != TString::npos) { // 截取扩展名 fileExtension = filePath.substr(pos, filePath.length()); @@ -143,7 +143,7 @@ tstring FileUtils::getFileExtension(const tstring & filePath) return fileExtension; } -bool FileUtils::getSaveFilePath(tstring& path, LPCTSTR title, LPCTSTR defExt) +bool FileUtils::getSaveFilePath(TString& path, LPCTSTR title, LPCTSTR defExt) { // 弹出保存对话框 OPENFILENAME ofn = { 0 }; diff --git a/Easy2D/Tool/MusicUtils.cpp b/Easy2D/Tool/MusicUtils.cpp index 42666f67..6cfcee7d 100644 --- a/Easy2D/Tool/MusicUtils.cpp +++ b/Easy2D/Tool/MusicUtils.cpp @@ -16,7 +16,7 @@ public: ~MciPlayer(); void close(); - void open(tstring pFileName, UINT uId); + void open(TString pFileName, UINT uId); void play(bool bLoop = false); void pause(); void resume(); @@ -33,7 +33,7 @@ private: UINT m_nSoundID; bool m_bPlaying; bool m_bLoop; - tstring m_sExt; + TString m_sExt; }; @@ -51,7 +51,7 @@ MciPlayer::~MciPlayer() close(); // 关闭播放器 } -void MciPlayer::open(tstring pFileName, UINT uId) +void MciPlayer::open(TString pFileName, UINT uId) { // 忽略不存在的文件 if (pFileName.empty() || !PathFileExists(pFileName.c_str())) return; @@ -206,7 +206,7 @@ void MciPlayer::_sendCommand(int nCommand, DWORD_PTR param1, DWORD_PTR parma2) typedef std::map MusicList; typedef std::pair Music; -static unsigned int _Hash(tstring key); +static unsigned int _Hash(TString key); static MusicList& getMciPlayerList() @@ -228,7 +228,7 @@ void MusicUtils::end() // 停止其他所有音乐 for (auto& iter : getMciPlayerList()) { - SAFE_DELETE(iter.second); + SafeDelete(iter.second); } // 清空音乐列表 getMciPlayerList().clear(); @@ -246,7 +246,7 @@ void MusicUtils::setVolume(float volume) } } -void MusicUtils::setVolume(tstring pszFilePath, float volume) +void MusicUtils::setVolume(TString pszFilePath, float volume) { unsigned int nRet = ::_Hash(pszFilePath); @@ -257,7 +257,7 @@ void MusicUtils::setVolume(tstring pszFilePath, float volume) } } -void MusicUtils::playBackgroundMusic(tstring pszFilePath, bool bLoop) +void MusicUtils::playBackgroundMusic(TString pszFilePath, bool bLoop) { if (pszFilePath.empty()) { @@ -305,7 +305,7 @@ void MusicUtils::setBackgroundMusicVolume(float volume) getBgMciPlayer().setVolume(volume); } -unsigned int MusicUtils::playMusic(tstring pszFilePath, bool bLoop) +unsigned int MusicUtils::playMusic(TString pszFilePath, bool bLoop) { unsigned int nRet = ::_Hash(pszFilePath); @@ -328,7 +328,7 @@ void MusicUtils::stopMusic(unsigned int nSoundId) } } -void MusicUtils::preloadMusic(tstring pszFilePath) +void MusicUtils::preloadMusic(TString pszFilePath) { if (pszFilePath.empty()) return; @@ -396,14 +396,14 @@ void MusicUtils::unloadMusic(LPCTSTR pszFilePath) MusicList::iterator p = getMciPlayerList().find(nID); if (p != getMciPlayerList().end()) { - SAFE_DELETE(p->second); + SafeDelete(p->second); getMciPlayerList().erase(nID); } } -unsigned int _Hash(tstring key) +unsigned int _Hash(TString key) { unsigned int len = unsigned(key.size()); unsigned int hash = 0; diff --git a/Easy2D/Tool/Timer.cpp b/Easy2D/Tool/Timer.cpp index a8141c56..ba9d50e8 100644 --- a/Easy2D/Tool/Timer.cpp +++ b/Easy2D/Tool/Timer.cpp @@ -3,7 +3,7 @@ // 储存所有定时器的容器 static std::vector s_nTimers; -Timer::Timer(tstring name, UINT ms, const TIMER_CALLBACK & callback) : +Timer::Timer(TString name, UINT ms, const TIMER_CALLBACK & callback) : m_sName(name), m_bRunning(false), m_callback(callback) @@ -48,7 +48,7 @@ void Timer::setCallback(const TIMER_CALLBACK & callback) m_callback = callback; // 保存回调函数 } -void Timer::setName(tstring name) +void Timer::setName(TString name) { m_sName = name; // 修改定时器名称 } @@ -58,7 +58,7 @@ UINT Timer::getInterval() const return m_nMilliSeconds; // 获取定时器的时间间隔 } -tstring Timer::getName() const +TString Timer::getName() const { return m_sName; // 获取定时器的名称 } @@ -100,7 +100,7 @@ void Timer::addTimer(Timer * timer) s_nTimers.push_back(timer); } -void Timer::addTimer(tstring name, UINT ms, const TIMER_CALLBACK & callback) +void Timer::addTimer(TString name, UINT ms, const TIMER_CALLBACK & callback) { // 创建定时器 auto timer = new Timer(name, ms, callback); @@ -108,7 +108,7 @@ void Timer::addTimer(tstring name, UINT ms, const TIMER_CALLBACK & callback) addTimer(timer); } -Timer * Timer::getTimer(tstring name) +Timer * Timer::getTimer(TString name) { // 查找是否有相同名称的定时器 for (auto timer : s_nTimers) @@ -123,7 +123,7 @@ Timer * Timer::getTimer(tstring name) return nullptr; } -bool Timer::startTimer(tstring name) +bool Timer::startTimer(TString name) { // 启动指定名称的定时器,先找到该定时器 auto t = getTimer(name); @@ -137,7 +137,7 @@ bool Timer::startTimer(tstring name) return false; } -bool Timer::stopTimer(tstring name) +bool Timer::stopTimer(TString name) { // 停止指定名称的定时器,先找到该定时器 auto t = getTimer(name); @@ -151,7 +151,7 @@ bool Timer::stopTimer(tstring name) return false; } -bool Timer::delTimer(tstring name) +bool Timer::delTimer(TString name) { // 创建迭代器 std::vector::iterator iter; diff --git a/Easy2D/easy2d.h b/Easy2D/easy2d.h index 0a0cb76e..89c40363 100644 --- a/Easy2D/easy2d.h +++ b/Easy2D/easy2d.h @@ -1,8 +1,9 @@ /****************************************************** * Easy2D Game Engine -* http://www.easy2d.cn * -* Depends on EasyX (Ver:20170827(beta)) +* Website: http://www.easy2d.cn +* Github: https://github.com/Nomango/Easy2D +* Gitee: https://gitee.com/werelone/Easy2D ******************************************************/ #pragma once @@ -36,22 +37,6 @@ #endif -// String macros - -#ifdef UNICODE - #define tstring std::wstring -#else - #define tstring std::string -#endif - - -// Safe macros - -#define SAFE_DELETE(p) { delete (p); (p) = nullptr; } -#define SAFE_DELETE_ARRAY(p) { if (p) { delete[] (p); (p) = nullptr; } } -#define SAFE_RELEASE(p) { if (p) p->release(); } - - // Type Declare typedef CPoint CVector; @@ -61,6 +46,11 @@ typedef std::function TIMER_CALLBACK; typedef std::function KEY_CALLBACK; typedef std::function MOUSE_CALLBACK; +#ifdef UNICODE + typedef std::wstring TString; +#else + typedef std::string TString; +#endif // Classes Declare @@ -114,6 +104,9 @@ namespace easy2d class ActionManager; } + +// Classes + namespace easy2d { @@ -131,9 +124,9 @@ public: // 定义绘图窗口 void createWindow(CSize size, int mode = 0); // 定义绘图窗口 - void createWindow(tstring title, int width, int height, int mode = 0); + void createWindow(TString title, int width, int height, int mode = 0); // 定义绘图窗口 - void createWindow(tstring title, CSize size, int mode = 0); + void createWindow(TString title, CSize size, int mode = 0); // 启动程序 int run(); // 释放所有内存资源 @@ -160,9 +153,9 @@ public: // 关闭窗口 static void close(); // 设置窗口标题 - static void setWindowTitle(tstring title); + static void setWindowTitle(TString title); // 获取窗口标题 - static tstring getWindowTitle(); + static TString getWindowTitle(); // 获取窗口宽度 static int getWidth(); // 获取窗口高度 @@ -174,9 +167,9 @@ public: // 清空之前保存的所有场景 static void clearScene(); // 设置 AppName - static void setAppName(tstring appname); + static void setAppName(TString appname); // 获取 AppName - static tstring getAppName(); + static TString getAppName(); // 修改窗口背景色 static void setBkColor(COLORREF color); // 设置帧率 @@ -187,8 +180,8 @@ public: static Scene * getCurrentScene(); protected: - tstring m_sTitle; - tstring m_sAppName; + TString m_sTitle; + TString m_sAppName; Scene* m_CurrentScene; Scene* m_NextScene; std::stack m_SceneStack; @@ -206,9 +199,8 @@ protected: class FreePool { - friend class App; - friend class Object; - + friend App; + friend Object; private: // 刷新内存池 static void __flush(); @@ -218,9 +210,8 @@ private: class Scene { - friend class App; - friend class MouseMsg; - + friend App; + friend MouseMsg; public: Scene(); ~Scene(); @@ -246,32 +237,35 @@ protected: class Object { - friend class FreePool; - + friend FreePool; public: Object(); virtual ~Object(); + // 保留这个对象 void retain(); + // 释放这个对象 void release(); + // 让引擎自动释放这个对象 + void autoRelease(); protected: - int m_nRef; + int m_nRefCount; }; class MouseMsg { - friend class App; + friend App; public: MouseMsg(); - MouseMsg(tstring name, const MOUSE_CALLBACK& callback); + MouseMsg(TString name, const MOUSE_CALLBACK& callback); ~MouseMsg(); // 添加键盘监听 - static void addListener(tstring name, const MOUSE_CALLBACK& callback); + static void addListener(TString name, const MOUSE_CALLBACK& callback); // 删除键盘监听 - static bool delListener(tstring name); + static bool delListener(TString name); // 删除所有键盘监听 static void clearAllListeners(); // 左键是否按下 @@ -317,7 +311,7 @@ private: static void __exec(); protected: - tstring m_sName; + TString m_sName; MOUSE_CALLBACK m_callback; protected: @@ -327,19 +321,19 @@ protected: class KeyMsg { - friend class App; + friend App; public: - KeyMsg(tstring name, const KEY_CALLBACK& callback); + KeyMsg(TString name, const KEY_CALLBACK& callback); ~KeyMsg(); // 执行回调函数 void onKbHit(VK_KEY key); // 添加键盘监听 - static void addListener(tstring name, const KEY_CALLBACK& callback); + static void addListener(TString name, const KEY_CALLBACK& callback); // 删除键盘监听 - static bool delListener(tstring name); + static bool delListener(TString name); // 删除所有键盘监听 static void clearAllListeners(); // 判断键是否被按下,按下返回true @@ -361,14 +355,14 @@ private: static void __exec(); protected: - tstring m_sName; + TString m_sName; KEY_CALLBACK m_callback; }; class FontStyle : public Object { - friend class Text; + friend Text; public: FontStyle(); @@ -465,8 +459,8 @@ public: class Node : public Object { - friend class Scene; - friend class BatchNode; + friend Scene; + friend BatchNode; public: Node(); @@ -599,34 +593,34 @@ protected: class Text : public RectNode { - friend class TextButton; + friend TextButton; public: Text(); // 根据字符串、颜色和字体创建文字 - Text(tstring text, COLORREF color = Color::white, FontStyle * font = FontStyle::getDefault()); + Text(TString text, COLORREF color = Color::white, FontStyle * font = FontStyle::getDefault()); // 根据横纵坐标、字符串、颜色和字体创建文字 - Text(int x, int y, tstring text, COLORREF color = Color::white, FontStyle * font = FontStyle::getDefault()); + Text(int x, int y, TString text, COLORREF color = Color::white, FontStyle * font = FontStyle::getDefault()); virtual ~Text(); // 获取当前颜色 COLORREF getColor() const; // 获取当前文字 - tstring getText() const; + TString getText() const; // 获取当前字体 FontStyle * getFontStyle(); // 文本是否为空 bool isEmpty() const; // 设置文字 - void setText(tstring text); + void setText(TString text); // 设置文字颜色 void setColor(COLORREF color); // 设置字体 void setFontStyle(FontStyle * style); protected: - tstring m_sText; + TString m_sText; COLORREF m_color; FontStyle * m_pFontStyle; @@ -637,8 +631,8 @@ protected: class Image : public RectNode { - friend class Sprite; - friend class ImageButton; + friend Sprite; + friend ImageButton; public: Image(); // 从图片文件获取图像 @@ -708,7 +702,7 @@ protected: class Sprite : public RectNode { - friend class BatchSprite; + friend BatchSprite; public: Sprite(); Sprite(Image * image); @@ -882,7 +876,7 @@ class TextButton : { public: TextButton(); - TextButton(tstring text); + TextButton(TString text); TextButton(Text * text); virtual ~TextButton(); @@ -1029,11 +1023,11 @@ protected: class Action : public Object { - friend class Sprite; - friend class ActionManager; - friend class ActionTwo; - friend class ActionNeverStop; - friend class ActionSequence; + friend Sprite; + friend ActionManager; + friend ActionTwo; + friend ActionNeverStop; + friend ActionSequence; public: Action(); virtual ~Action(); @@ -1326,35 +1320,35 @@ class FileUtils { public: // 获取系统的 AppData\Local 路径 - static tstring getLocalAppDataPath(); + static TString getLocalAppDataPath(); // 获取默认的保存路径 - static tstring getDefaultSavePath(); + static TString getDefaultSavePath(); // 保存 int 型的值 static void saveInt(LPCTSTR key, int value); // 保存 double 型的值 static void saveDouble(LPCTSTR key, double value); // 保存 字符串 型的值(不要在 Unicode 字符集下保存中文字符) - static void saveString(LPCTSTR key, tstring value); + static void saveString(LPCTSTR key, TString value); // 获取 int 型的值(若不存在则返回 default 参数的值) static int getInt(LPCTSTR key, int default); // 获取 double 型的值(若不存在则返回 default 参数的值) static double getDouble(LPCTSTR key, double default); // 获取 字符串 型的值(若不存在则返回 default 参数的值) - static tstring getString(LPCTSTR key, tstring default); + static TString geTString(LPCTSTR key, TString default); // 得到文件扩展名(小写) - static tstring getFileExtension(const tstring& filePath); + static TString getFileExtension(const TString& filePath); /** * 打开保存文件对话框,得到有效保存路径返回 true * 参数:返回文件路径的字符串,窗口标题,设置扩展名过滤,设置默认扩展名 */ - static bool getSaveFilePath(tstring& path, LPCTSTR title = _T("保存到"), LPCTSTR defExt = NULL); + static bool getSaveFilePath(TString& path, LPCTSTR title = _T("保存到"), LPCTSTR defExt = NULL); }; class MusicUtils { public: // 播放背景音乐 - static void playBackgroundMusic(tstring pszFilePath, bool bLoop = true); + static void playBackgroundMusic(TString pszFilePath, bool bLoop = true); // 停止背景音乐 static void stopBackgroundMusic(bool bReleaseData = false); // 暂停背景音乐 @@ -1369,11 +1363,11 @@ public: static void setBackgroundMusicVolume(float volume); // 播放音效 - static unsigned int playMusic(tstring pszFilePath, bool loop = false); + static unsigned int playMusic(TString pszFilePath, bool loop = false); // 停止音效 static void stopMusic(unsigned int nSoundId); // 预加载音效 - static void preloadMusic(tstring pszFilePath); + static void preloadMusic(TString pszFilePath); // 暂停音效 static void pauseMusic(unsigned int nSoundId); // 继续播放音效 @@ -1381,7 +1375,7 @@ public: // 卸载音效 static void unloadMusic(LPCTSTR pszFilePath); // 设置特定音乐的音量,0 ~ 1.0f - static void setVolume(tstring pszFilePath, float volume); + static void setVolume(TString pszFilePath, float volume); // 暂停所有音乐 static void pauseAllMusics(); @@ -1397,10 +1391,10 @@ public: class Timer { - friend class App; + friend App; public: - Timer(tstring name, UINT ms, const TIMER_CALLBACK & callback); + Timer(TString name, UINT ms, const TIMER_CALLBACK & callback); ~Timer(); // 启动定时器 @@ -1414,30 +1408,30 @@ public: // 设置回调函数 void setCallback(const TIMER_CALLBACK& callback); // 设置定时器名称 - void setName(tstring name); + void setName(TString name); // 获取定时器间隔时间 UINT getInterval() const; // 获取定时器名称 - tstring getName() const; + TString getName() const; // 添加定时器 static void addTimer(Timer * timer); // 添加定时器 - static void addTimer(tstring name, UINT ms, const TIMER_CALLBACK & callback); + static void addTimer(TString name, UINT ms, const TIMER_CALLBACK & callback); // 根据名称获取定时器 - static Timer * getTimer(tstring name); + static Timer * getTimer(TString name); // 启动特定定时器 - static bool startTimer(tstring name); + static bool startTimer(TString name); // 停止特定定时器 - static bool stopTimer(tstring name); + static bool stopTimer(TString name); // 删除特定定时器 - static bool delTimer(tstring name); + static bool delTimer(TString name); // 删除所有定时器 static void clearAllTimers(); protected: bool m_bRunning; - tstring m_sName; + TString m_sName; TIMER_CALLBACK m_callback; LARGE_INTEGER m_nLast; LARGE_INTEGER m_nAnimationInterval; @@ -1449,8 +1443,8 @@ private: class ActionManager { - friend class App; - friend class Sprite; + friend App; + friend Sprite; public: // 继续一个特定的动作 static void startAction(Action * action); @@ -1487,4 +1481,10 @@ private: } // End of easy2d namespace + +// Functions Declare + +inline void SafeRelease(easy2d::Object * p) { if (p) p->release(); } +inline void SafeDelete(void * p) { if (p) delete p; } + using namespace easy2d; \ No newline at end of file