Object不再自动释放,除非调用autoRelease函数将其加入释放管理池中

This commit is contained in:
Nomango 2017-10-05 00:53:03 +08:00
parent a32e3431a6
commit b7de2733bf
18 changed files with 177 additions and 175 deletions

View File

@ -8,8 +8,6 @@ Action::Action() :
{ {
// 默认动作 15ms 运行一次 // 默认动作 15ms 运行一次
setInterval(15); setInterval(15);
// 保留动作
this->retain();
} }
Action::~Action() Action::~Action()

View File

@ -8,7 +8,7 @@ ActionNeverStop::ActionNeverStop(Action * action) :
ActionNeverStop::~ActionNeverStop() ActionNeverStop::~ActionNeverStop()
{ {
SAFE_RELEASE(m_Action); SafeRelease(m_Action);
} }
ActionNeverStop * ActionNeverStop::copy() const ActionNeverStop * ActionNeverStop::copy() const

View File

@ -25,7 +25,7 @@ ActionSequence::~ActionSequence()
{ {
for (auto action : m_vActions) for (auto action : m_vActions)
{ {
SAFE_RELEASE(action); SafeRelease(action);
} }
} }

View File

@ -10,8 +10,8 @@ ActionTwo::ActionTwo(Action * actionFirst, Action * actionSecond) :
ActionTwo::~ActionTwo() ActionTwo::~ActionTwo()
{ {
SAFE_RELEASE(m_FirstAction); SafeRelease(m_FirstAction);
SAFE_RELEASE(m_SecondAction); SafeRelease(m_SecondAction);
} }
ActionTwo * ActionTwo::copy() const ActionTwo * ActionTwo::copy() const
@ -50,7 +50,7 @@ bool ActionTwo::_exec(LARGE_INTEGER nNow)
{ {
// 返回 true 表示第一个动作已经结束,删除这个 // 返回 true 表示第一个动作已经结束,删除这个
// 动作,并初始化第二个动作 // 动作,并初始化第二个动作
SAFE_RELEASE(m_FirstAction); SafeRelease(m_FirstAction);
m_FirstAction = nullptr; m_FirstAction = nullptr;
m_SecondAction->_init(); m_SecondAction->_init();
} }
@ -59,7 +59,7 @@ bool ActionTwo::_exec(LARGE_INTEGER nNow)
{ {
if (m_SecondAction->_exec(nNow)) if (m_SecondAction->_exec(nNow))
{ {
SAFE_RELEASE(m_SecondAction); SafeRelease(m_SecondAction);
m_SecondAction = nullptr; m_SecondAction = nullptr;
return true; return true;
} }

View File

@ -194,7 +194,7 @@ void App::createWindow(CSize size, int mode)
createWindow(size.cx, size.cy, 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; m_Size.cx = width;
@ -206,7 +206,7 @@ void App::createWindow(tstring title, int width, int height, int mode)
_initGraph(); _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); createWindow(title, size.cx, size.cy, mode);
} }
@ -246,7 +246,7 @@ void App::setWindowSize(CSize size)
setWindowSize(size.cx, size.cy); setWindowSize(size.cx, size.cy);
} }
void App::setWindowTitle(tstring title) void App::setWindowTitle(TString title)
{ {
// 设置窗口标题 // 设置窗口标题
SetWindowText(GetHWnd(), title.c_str()); SetWindowText(GetHWnd(), title.c_str());
@ -254,7 +254,7 @@ void App::setWindowTitle(tstring title)
s_pInstance->m_sTitle = title; s_pInstance->m_sTitle = title;
} }
tstring App::getWindowTitle() TString App::getWindowTitle()
{ {
return s_pInstance->m_sTitle; return s_pInstance->m_sTitle;
} }
@ -286,17 +286,17 @@ void App::clearScene()
while (s_pInstance->m_SceneStack.size()) while (s_pInstance->m_SceneStack.size())
{ {
auto temp = s_pInstance->m_SceneStack.top(); auto temp = s_pInstance->m_SceneStack.top();
SAFE_DELETE(temp); SafeDelete(temp);
s_pInstance->m_SceneStack.pop(); s_pInstance->m_SceneStack.pop();
} }
} }
void App::setAppName(tstring appname) void App::setAppName(TString appname)
{ {
s_pInstance->m_sAppName = appname; s_pInstance->m_sAppName = appname;
} }
tstring App::getAppName() TString App::getAppName()
{ {
return s_pInstance->m_sAppName; return s_pInstance->m_sAppName;
} }
@ -320,7 +320,7 @@ void App::_enterNextScene()
} }
else else
{ {
SAFE_DELETE(m_CurrentScene); // ·ñÔòɾ³ýµ±Ç°³¡¾° SafeDelete(m_CurrentScene); // ·ñÔòɾ³ýµ±Ç°³¡¾°
} }
m_CurrentScene = m_NextScene; // 切换场景 m_CurrentScene = m_NextScene; // 切换场景
@ -372,13 +372,13 @@ int App::getHeight()
void App::free() void App::free()
{ {
// 释放场景内存 // 释放场景内存
SAFE_DELETE(m_CurrentScene); SafeDelete(m_CurrentScene);
SAFE_DELETE(m_NextScene); SafeDelete(m_NextScene);
// 清空场景栈 // 清空场景栈
while (m_SceneStack.size()) while (m_SceneStack.size())
{ {
auto temp = m_SceneStack.top(); auto temp = m_SceneStack.top();
SAFE_DELETE(temp); SafeDelete(temp);
m_SceneStack.pop(); m_SceneStack.pop();
} }
// 删除所有定时器 // 删除所有定时器

View File

@ -2,7 +2,7 @@
#include <assert.h> #include <assert.h>
// FreePool 释放池的实现机制: // FreePool 释放池的实现机制:
/// Object 类中的引用计数m_nRef)保证了指针的使用安全 /// Object 类中的引用计数m_nRefCount)保证了指针的使用安全
/// 它记录了对象被使用的次数,当计数为 0 时FreePool 会自动释放这个对象 /// 它记录了对象被使用的次数,当计数为 0 时FreePool 会自动释放这个对象
/// 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中) /// 所有的 Object 对象都应在被使用时(例如 Text 添加到了场景中)
/// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数 /// 调用 retain 函数保证该对象不被删除,并在不再使用时调用 release 函数
@ -19,7 +19,7 @@ void FreePool::__flush()
for (iter = pool.begin(); iter != pool.end();) for (iter = pool.begin(); iter != pool.end();)
{ {
// 若对象的引用的计数为 0 // 若对象的引用的计数为 0
if ((*iter)->m_nRef == 0) if ((*iter)->m_nRefCount == 0)
{ {
// 释放该对象 // 释放该对象
delete (*iter); delete (*iter);

View File

@ -80,7 +80,7 @@ const VK_KEY KeyMsg::F12 = VK_F12;
static VK_KEY convert(int ascii); 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_sName = name;
m_callback = callback; 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); auto key = new KeyMsg(name, callback);
@ -116,7 +116,7 @@ void KeyMsg::addListener(tstring name, const KEY_CALLBACK & callback)
s_vKeyMsg.push_back(key); s_vKeyMsg.push_back(key);
} }
bool KeyMsg::delListener(tstring name) bool KeyMsg::delListener(TString name)
{ {
// 创建迭代器 // 创建迭代器
std::vector<KeyMsg*>::iterator iter; std::vector<KeyMsg*>::iterator iter;

View File

@ -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_sName = name;
m_callback = callback; m_callback = callback;
@ -43,7 +43,7 @@ void MouseMsg::onMouseMsg()
m_callback(); 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); auto mouse = new MouseMsg(name, callback);
@ -51,7 +51,7 @@ void MouseMsg::addListener(tstring name, const MOUSE_CALLBACK & callback)
s_vMouseMsg.push_back(mouse); s_vMouseMsg.push_back(mouse);
} }
bool MouseMsg::delListener(tstring name) bool MouseMsg::delListener(TString name)
{ {
// 创建迭代器 // 创建迭代器
std::vector<MouseMsg*>::iterator iter; std::vector<MouseMsg*>::iterator iter;

View File

@ -24,10 +24,10 @@ ImageButton::ImageButton(Image * image) :
ImageButton::~ImageButton() ImageButton::~ImageButton()
{ {
// 所有图片的引用计数减一 // 所有图片的引用计数减一
SAFE_RELEASE(m_pNormalImage); SafeRelease(m_pNormalImage);
SAFE_RELEASE(m_pMouseInImage); SafeRelease(m_pMouseInImage);
SAFE_RELEASE(m_pSelectedImage); SafeRelease(m_pSelectedImage);
SAFE_RELEASE(m_pUnableImage); SafeRelease(m_pUnableImage);
} }
void ImageButton::_setStatus(Status status) void ImageButton::_setStatus(Status status)
@ -99,7 +99,7 @@ void ImageButton::setNormal(Image * image)
if (image) if (image)
{ {
// 原图片引用计数减一 // 原图片引用计数减一
SAFE_RELEASE(m_pNormalImage); SafeRelease(m_pNormalImage);
// 修改图片 // 修改图片
m_pNormalImage = image; m_pNormalImage = image;
// 现图片引用计数加一 // 现图片引用计数加一
@ -115,7 +115,7 @@ void ImageButton::setMouseIn(Image * image)
{ {
if (image) if (image)
{ {
SAFE_RELEASE(m_pMouseInImage); SafeRelease(m_pMouseInImage);
m_pMouseInImage = image; m_pMouseInImage = image;
m_pMouseInImage->retain(); m_pMouseInImage->retain();
_resetPosition(); _resetPosition();
@ -126,7 +126,7 @@ void ImageButton::setSelected(Image * image)
{ {
if (image) if (image)
{ {
SAFE_RELEASE(m_pSelectedImage); SafeRelease(m_pSelectedImage);
m_pSelectedImage = image; m_pSelectedImage = image;
m_pSelectedImage->retain(); m_pSelectedImage->retain();
_resetPosition(); _resetPosition();
@ -137,7 +137,7 @@ void ImageButton::setUnable(Image * image)
{ {
if (image) if (image)
{ {
SAFE_RELEASE(m_pUnableImage); SafeRelease(m_pUnableImage);
m_pUnableImage = image; m_pUnableImage = image;
m_pUnableImage->retain(); m_pUnableImage->retain();
_resetPosition(); _resetPosition();

View File

@ -9,7 +9,7 @@ TextButton::TextButton() :
{ {
} }
TextButton::TextButton(tstring text) : TextButton::TextButton(TString text) :
TextButton() TextButton()
{ {
setNormal(new Text(text)); // 设置按钮在正常状态时的文字 setNormal(new Text(text)); // 设置按钮在正常状态时的文字
@ -24,10 +24,10 @@ TextButton::TextButton(Text * text) :
TextButton::~TextButton() TextButton::~TextButton()
{ {
// 所有文本的引用计数减一 // 所有文本的引用计数减一
SAFE_RELEASE(m_pNormalText); SafeRelease(m_pNormalText);
SAFE_RELEASE(m_pMouseInText); SafeRelease(m_pMouseInText);
SAFE_RELEASE(m_pSelectedText); SafeRelease(m_pSelectedText);
SAFE_RELEASE(m_pUnableText); SafeRelease(m_pUnableText);
} }
void TextButton::_setStatus(Status status) void TextButton::_setStatus(Status status)
@ -99,7 +99,7 @@ void TextButton::setNormal(Text * text)
if (text) if (text)
{ {
// 原文本引用计数减一 // 原文本引用计数减一
SAFE_RELEASE(m_pNormalText); SafeRelease(m_pNormalText);
// 修改文本 // 修改文本
m_pNormalText = text; m_pNormalText = text;
// 现文本引用计数加一 // 现文本引用计数加一
@ -115,7 +115,7 @@ void TextButton::setMouseIn(Text * text)
{ {
if (text) if (text)
{ {
SAFE_RELEASE(m_pMouseInText); SafeRelease(m_pMouseInText);
m_pMouseInText = text; m_pMouseInText = text;
m_pMouseInText->retain(); m_pMouseInText->retain();
_resetPosition(); _resetPosition();
@ -126,7 +126,7 @@ void TextButton::setSelected(Text * text)
{ {
if (text) if (text)
{ {
SAFE_RELEASE(m_pSelectedText); SafeRelease(m_pSelectedText);
m_pSelectedText = text; m_pSelectedText = text;
m_pSelectedText->retain(); m_pSelectedText->retain();
_resetPosition(); _resetPosition();
@ -137,7 +137,7 @@ void TextButton::setUnable(Text * text)
{ {
if (text) if (text)
{ {
SAFE_RELEASE(m_pUnableText); SafeRelease(m_pUnableText);
m_pUnableText = text; m_pUnableText = text;
m_pUnableText->retain(); m_pUnableText->retain();
_resetPosition(); _resetPosition();

View File

@ -4,9 +4,9 @@
using namespace std; using namespace std;
// 图片缓存 // 图片缓存
static map<tstring, CImage*> s_mCImages; static map<TString, CImage*> s_mCImages;
// 从图片缓存中读取图片 // 从图片缓存中读取图片
static CImage* GetCImage(tstring name, bool fromRes = false); static CImage* GetCImage(TString name, bool fromRes = false);
// 对 PNG 图像进行像素转换 // 对 PNG 图像进行像素转换
static void CrossImage(CImage &img); static void CrossImage(CImage &img);
@ -169,7 +169,7 @@ void Image::reset()
void Image::saveScreenshot() void Image::saveScreenshot()
{ {
tstring savePath; TString savePath;
// 获取保存位置 // 获取保存位置
if (FileUtils::getSaveFilePath(savePath, _T("截图保存到"), _T("jpg"))) 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()) if (s_mCImages.find(name) == s_mCImages.end())
{ {
@ -230,7 +230,7 @@ CImage* GetCImage(tstring name, bool fromRes)
// 透明图片处理 // 透明图片处理
CrossImage(*cImage); CrossImage(*cImage);
} }
s_mCImages.insert(map<tstring, CImage*>::value_type(name, cImage)); s_mCImages.insert(map<TString, CImage*>::value_type(name, cImage));
} }
return s_mCImages.at(name); return s_mCImages.at(name);
} }

View File

@ -1,9 +1,8 @@
#include "..\easy2d.h" #include "..\easy2d.h"
Object::Object() : Object::Object() :
m_nRef(0) m_nRefCount(0)
{ {
FreePool::__add(this); // 将该对象放入释放池中
} }
Object::~Object() Object::~Object()
@ -12,10 +11,15 @@ Object::~Object()
void Object::retain() void Object::retain()
{ {
m_nRef++; // 引用计数加一 m_nRefCount++; // 引用计数加一
} }
void Object::release() void Object::release()
{ {
m_nRef--; // 引用计数减一 m_nRefCount--; // 引用计数减一
}
void Object::autoRelease()
{
FreePool::__add(this); // 将该对象放入释放池中
} }

View File

@ -24,7 +24,7 @@ Sprite::Sprite(LPCTSTR imageFileName) :
Sprite::~Sprite() Sprite::~Sprite()
{ {
SAFE_RELEASE(m_pImage); SafeRelease(m_pImage);
} }
bool Sprite::_exec(bool active) bool Sprite::_exec(bool active)
@ -52,7 +52,7 @@ void Sprite::_onDraw()
void Sprite::setImage(Image * image) void Sprite::setImage(Image * image)
{ {
SAFE_RELEASE(m_pImage); SafeRelease(m_pImage);
m_pImage = image; m_pImage = image;
setSize(int(m_pImage->getWidth() * m_fScaleX), int(m_pImage->getHeight() * m_fScaleY)); setSize(int(m_pImage->getWidth() * m_fScaleX), int(m_pImage->getHeight() * m_fScaleY));
m_pImage->retain(); m_pImage->retain();

View File

@ -10,7 +10,7 @@ Text::Text() :
m_pFontStyle->retain(); // 字体引用计数加一 m_pFontStyle->retain(); // 字体引用计数加一
} }
Text::Text(tstring text, COLORREF color, FontStyle * font) : Text::Text(TString text, COLORREF color, FontStyle * font) :
m_color(color), m_color(color),
m_pFontStyle(font) m_pFontStyle(font)
{ {
@ -18,7 +18,7 @@ Text::Text(tstring text, COLORREF color, FontStyle * font) :
m_pFontStyle->retain(); // 字体引用计数加一 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_color(color),
m_pFontStyle(font) m_pFontStyle(font)
{ {
@ -29,7 +29,7 @@ Text::Text(int x, int y, tstring text, COLORREF color, FontStyle * font) :
Text::~Text() Text::~Text()
{ {
SAFE_RELEASE(m_pFontStyle); // 字体引用计数减一 SafeRelease(m_pFontStyle); // 字体引用计数减一
} }
void Text::_onDraw() void Text::_onDraw()
@ -52,7 +52,7 @@ COLORREF Text::getColor() const
return m_color; return m_color;
} }
tstring Text::getText() const TString Text::getText() const
{ {
return m_sText; return m_sText;
} }
@ -67,7 +67,7 @@ bool Text::isEmpty() const
return m_sText.empty(); // 文本是否为空 return m_sText.empty(); // 文本是否为空
} }
void Text::setText(tstring text) void Text::setText(TString text)
{ {
m_sText = text; m_sText = text;
// 先设置字体,然后获取该文本在该字体下的宽度和高度 // 先设置字体,然后获取该文本在该字体下的宽度和高度
@ -82,7 +82,7 @@ void Text::setColor(COLORREF color)
void Text::setFontStyle(FontStyle * style) void Text::setFontStyle(FontStyle * style)
{ {
SAFE_RELEASE(m_pFontStyle); // 原字体引用计数减一 SafeRelease(m_pFontStyle); // 原字体引用计数减一
m_pFontStyle = style; // 修改字体 m_pFontStyle = style; // 修改字体
m_pFontStyle->retain(); // 现字体引用计数加一 m_pFontStyle->retain(); // 现字体引用计数加一
// 先设置字体,然后获取该文本在该字体下的宽度和高度 // 先设置字体,然后获取该文本在该字体下的宽度和高度

View File

@ -10,7 +10,7 @@
#include <direct.h> #include <direct.h>
#endif #endif
tstring FileUtils::getLocalAppDataPath() TString FileUtils::getLocalAppDataPath()
{ {
TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 }; TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 };
TCHAR szDocument[MAX_PATH] = { 0 }; TCHAR szDocument[MAX_PATH] = { 0 };
@ -27,7 +27,7 @@ tstring FileUtils::getLocalAppDataPath()
return m_lpszDefaultDir; return m_lpszDefaultDir;
} }
tstring FileUtils::getDefaultSavePath() TString FileUtils::getDefaultSavePath()
{ {
TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 }; TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 };
TCHAR szDocument[MAX_PATH] = { 0 }; TCHAR szDocument[MAX_PATH] = { 0 };
@ -41,7 +41,7 @@ tstring FileUtils::getDefaultSavePath()
GetShortPathName(szDocument, m_lpszDefaultDir, _MAX_PATH); GetShortPathName(szDocument, m_lpszDefaultDir, _MAX_PATH);
} }
tstring path = m_lpszDefaultDir; TString path = m_lpszDefaultDir;
path.append(_T("\\")); path.append(_T("\\"));
path.append(App::get()->getAppName()); 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()); ::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()); ::WritePrivateProfileString(_T("Default"), key, value.c_str(), getDefaultSavePath().c_str());
} }
@ -119,20 +119,20 @@ double FileUtils::getDouble(LPCTSTR key, double default)
return d; return d;
} }
tstring FileUtils::getString(LPCTSTR key, tstring default) TString FileUtils::geTString(LPCTSTR key, TString default)
{ {
TCHAR temp[128] = { 0 }; TCHAR temp[128] = { 0 };
::GetPrivateProfileString(_T("Default"), key, default.c_str(), temp, 128, getDefaultSavePath().c_str()); ::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('.'); size_t pos = filePath.find_last_of('.');
// 判断 pos 是否是个有效位置 // 判断 pos 是否是个有效位置
if (pos != tstring::npos) if (pos != TString::npos)
{ {
// 截取扩展名 // 截取扩展名
fileExtension = filePath.substr(pos, filePath.length()); fileExtension = filePath.substr(pos, filePath.length());
@ -143,7 +143,7 @@ tstring FileUtils::getFileExtension(const tstring & filePath)
return fileExtension; return fileExtension;
} }
bool FileUtils::getSaveFilePath(tstring& path, LPCTSTR title, LPCTSTR defExt) bool FileUtils::getSaveFilePath(TString& path, LPCTSTR title, LPCTSTR defExt)
{ {
// 弹出保存对话框 // 弹出保存对话框
OPENFILENAME ofn = { 0 }; OPENFILENAME ofn = { 0 };

View File

@ -16,7 +16,7 @@ public:
~MciPlayer(); ~MciPlayer();
void close(); void close();
void open(tstring pFileName, UINT uId); void open(TString pFileName, UINT uId);
void play(bool bLoop = false); void play(bool bLoop = false);
void pause(); void pause();
void resume(); void resume();
@ -33,7 +33,7 @@ private:
UINT m_nSoundID; UINT m_nSoundID;
bool m_bPlaying; bool m_bPlaying;
bool m_bLoop; bool m_bLoop;
tstring m_sExt; TString m_sExt;
}; };
@ -51,7 +51,7 @@ MciPlayer::~MciPlayer()
close(); // 关闭播放器 close(); // 关闭播放器
} }
void MciPlayer::open(tstring pFileName, UINT uId) void MciPlayer::open(TString pFileName, UINT uId)
{ {
// 忽略不存在的文件 // 忽略不存在的文件
if (pFileName.empty() || !PathFileExists(pFileName.c_str())) return; 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<unsigned int, MciPlayer *> MusicList; typedef std::map<unsigned int, MciPlayer *> MusicList;
typedef std::pair<unsigned int, MciPlayer *> Music; typedef std::pair<unsigned int, MciPlayer *> Music;
static unsigned int _Hash(tstring key); static unsigned int _Hash(TString key);
static MusicList& getMciPlayerList() static MusicList& getMciPlayerList()
@ -228,7 +228,7 @@ void MusicUtils::end()
// 停止其他所有音乐 // 停止其他所有音乐
for (auto& iter : getMciPlayerList()) for (auto& iter : getMciPlayerList())
{ {
SAFE_DELETE(iter.second); SafeDelete(iter.second);
} }
// 清空音乐列表 // 清空音乐列表
getMciPlayerList().clear(); 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); 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()) if (pszFilePath.empty())
{ {
@ -305,7 +305,7 @@ void MusicUtils::setBackgroundMusicVolume(float volume)
getBgMciPlayer().setVolume(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); 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; if (pszFilePath.empty()) return;
@ -396,14 +396,14 @@ void MusicUtils::unloadMusic(LPCTSTR pszFilePath)
MusicList::iterator p = getMciPlayerList().find(nID); MusicList::iterator p = getMciPlayerList().find(nID);
if (p != getMciPlayerList().end()) if (p != getMciPlayerList().end())
{ {
SAFE_DELETE(p->second); SafeDelete(p->second);
getMciPlayerList().erase(nID); getMciPlayerList().erase(nID);
} }
} }
unsigned int _Hash(tstring key) unsigned int _Hash(TString key)
{ {
unsigned int len = unsigned(key.size()); unsigned int len = unsigned(key.size());
unsigned int hash = 0; unsigned int hash = 0;

View File

@ -3,7 +3,7 @@
// 储存所有定时器的容器 // 储存所有定时器的容器
static std::vector<Timer*> s_nTimers; static std::vector<Timer*> 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_sName(name),
m_bRunning(false), m_bRunning(false),
m_callback(callback) m_callback(callback)
@ -48,7 +48,7 @@ void Timer::setCallback(const TIMER_CALLBACK & callback)
m_callback = callback; // 保存回调函数 m_callback = callback; // 保存回调函数
} }
void Timer::setName(tstring name) void Timer::setName(TString name)
{ {
m_sName = name; // 修改定时器名称 m_sName = name; // 修改定时器名称
} }
@ -58,7 +58,7 @@ UINT Timer::getInterval() const
return m_nMilliSeconds; // 获取定时器的时间间隔 return m_nMilliSeconds; // 获取定时器的时间间隔
} }
tstring Timer::getName() const TString Timer::getName() const
{ {
return m_sName; // 获取定时器的名称 return m_sName; // 获取定时器的名称
} }
@ -100,7 +100,7 @@ void Timer::addTimer(Timer * timer)
s_nTimers.push_back(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); auto timer = new Timer(name, ms, callback);
@ -108,7 +108,7 @@ void Timer::addTimer(tstring name, UINT ms, const TIMER_CALLBACK & callback)
addTimer(timer); addTimer(timer);
} }
Timer * Timer::getTimer(tstring name) Timer * Timer::getTimer(TString name)
{ {
// 查找是否有相同名称的定时器 // 查找是否有相同名称的定时器
for (auto timer : s_nTimers) for (auto timer : s_nTimers)
@ -123,7 +123,7 @@ Timer * Timer::getTimer(tstring name)
return nullptr; return nullptr;
} }
bool Timer::startTimer(tstring name) bool Timer::startTimer(TString name)
{ {
// 启动指定名称的定时器,先找到该定时器 // 启动指定名称的定时器,先找到该定时器
auto t = getTimer(name); auto t = getTimer(name);
@ -137,7 +137,7 @@ bool Timer::startTimer(tstring name)
return false; return false;
} }
bool Timer::stopTimer(tstring name) bool Timer::stopTimer(TString name)
{ {
// 停止指定名称的定时器,先找到该定时器 // 停止指定名称的定时器,先找到该定时器
auto t = getTimer(name); auto t = getTimer(name);
@ -151,7 +151,7 @@ bool Timer::stopTimer(tstring name)
return false; return false;
} }
bool Timer::delTimer(tstring name) bool Timer::delTimer(TString name)
{ {
// 创建迭代器 // 创建迭代器
std::vector<Timer*>::iterator iter; std::vector<Timer*>::iterator iter;

View File

@ -1,8 +1,9 @@
/****************************************************** /******************************************************
* Easy2D Game Engine * 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 #pragma once
@ -36,22 +37,6 @@
#endif #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 // Type Declare
typedef CPoint CVector; typedef CPoint CVector;
@ -61,6 +46,11 @@ typedef std::function<void()> TIMER_CALLBACK;
typedef std::function<void(VK_KEY)> KEY_CALLBACK; typedef std::function<void(VK_KEY)> KEY_CALLBACK;
typedef std::function<void()> MOUSE_CALLBACK; typedef std::function<void()> MOUSE_CALLBACK;
#ifdef UNICODE
typedef std::wstring TString;
#else
typedef std::string TString;
#endif
// Classes Declare // Classes Declare
@ -114,6 +104,9 @@ namespace easy2d
class ActionManager; class ActionManager;
} }
// Classes
namespace easy2d namespace easy2d
{ {
@ -131,9 +124,9 @@ public:
// 定义绘图窗口 // 定义绘图窗口
void createWindow(CSize size, int mode = 0); 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(); int run();
// 释放所有内存资源 // 释放所有内存资源
@ -160,9 +153,9 @@ public:
// 关闭窗口 // 关闭窗口
static void close(); static void close();
// 设置窗口标题 // 设置窗口标题
static void setWindowTitle(tstring title); static void setWindowTitle(TString title);
// 获取窗口标题 // 获取窗口标题
static tstring getWindowTitle(); static TString getWindowTitle();
// 获取窗口宽度 // 获取窗口宽度
static int getWidth(); static int getWidth();
// 获取窗口高度 // 获取窗口高度
@ -174,9 +167,9 @@ public:
// 清空之前保存的所有场景 // 清空之前保存的所有场景
static void clearScene(); static void clearScene();
// 设置 AppName // 设置 AppName
static void setAppName(tstring appname); static void setAppName(TString appname);
// 获取 AppName // 获取 AppName
static tstring getAppName(); static TString getAppName();
// 修改窗口背景色 // 修改窗口背景色
static void setBkColor(COLORREF color); static void setBkColor(COLORREF color);
// 设置帧率 // 设置帧率
@ -187,8 +180,8 @@ public:
static Scene * getCurrentScene(); static Scene * getCurrentScene();
protected: protected:
tstring m_sTitle; TString m_sTitle;
tstring m_sAppName; TString m_sAppName;
Scene* m_CurrentScene; Scene* m_CurrentScene;
Scene* m_NextScene; Scene* m_NextScene;
std::stack<Scene*> m_SceneStack; std::stack<Scene*> m_SceneStack;
@ -206,9 +199,8 @@ protected:
class FreePool class FreePool
{ {
friend class App; friend App;
friend class Object; friend Object;
private: private:
// 刷新内存池 // 刷新内存池
static void __flush(); static void __flush();
@ -218,9 +210,8 @@ private:
class Scene class Scene
{ {
friend class App; friend App;
friend class MouseMsg; friend MouseMsg;
public: public:
Scene(); Scene();
~Scene(); ~Scene();
@ -246,32 +237,35 @@ protected:
class Object class Object
{ {
friend class FreePool; friend FreePool;
public: public:
Object(); Object();
virtual ~Object(); virtual ~Object();
// 保留这个对象
void retain(); void retain();
// 释放这个对象
void release(); void release();
// 让引擎自动释放这个对象
void autoRelease();
protected: protected:
int m_nRef; int m_nRefCount;
}; };
class MouseMsg class MouseMsg
{ {
friend class App; friend App;
public: public:
MouseMsg(); MouseMsg();
MouseMsg(tstring name, const MOUSE_CALLBACK& callback); MouseMsg(TString name, const MOUSE_CALLBACK& callback);
~MouseMsg(); ~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(); static void clearAllListeners();
// 左键是否按下 // 左键是否按下
@ -317,7 +311,7 @@ private:
static void __exec(); static void __exec();
protected: protected:
tstring m_sName; TString m_sName;
MOUSE_CALLBACK m_callback; MOUSE_CALLBACK m_callback;
protected: protected:
@ -327,19 +321,19 @@ protected:
class KeyMsg class KeyMsg
{ {
friend class App; friend App;
public: public:
KeyMsg(tstring name, const KEY_CALLBACK& callback); KeyMsg(TString name, const KEY_CALLBACK& callback);
~KeyMsg(); ~KeyMsg();
// 执行回调函数 // 执行回调函数
void onKbHit(VK_KEY key); 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(); static void clearAllListeners();
// 判断键是否被按下按下返回true // 判断键是否被按下按下返回true
@ -361,14 +355,14 @@ private:
static void __exec(); static void __exec();
protected: protected:
tstring m_sName; TString m_sName;
KEY_CALLBACK m_callback; KEY_CALLBACK m_callback;
}; };
class FontStyle : class FontStyle :
public Object public Object
{ {
friend class Text; friend Text;
public: public:
FontStyle(); FontStyle();
@ -465,8 +459,8 @@ public:
class Node : class Node :
public Object public Object
{ {
friend class Scene; friend Scene;
friend class BatchNode; friend BatchNode;
public: public:
Node(); Node();
@ -599,34 +593,34 @@ protected:
class Text : class Text :
public RectNode public RectNode
{ {
friend class TextButton; friend TextButton;
public: public:
Text(); 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(); virtual ~Text();
// 获取当前颜色 // 获取当前颜色
COLORREF getColor() const; COLORREF getColor() const;
// 获取当前文字 // 获取当前文字
tstring getText() const; TString getText() const;
// 获取当前字体 // 获取当前字体
FontStyle * getFontStyle(); FontStyle * getFontStyle();
// 文本是否为空 // 文本是否为空
bool isEmpty() const; bool isEmpty() const;
// 设置文字 // 设置文字
void setText(tstring text); void setText(TString text);
// 设置文字颜色 // 设置文字颜色
void setColor(COLORREF color); void setColor(COLORREF color);
// 设置字体 // 设置字体
void setFontStyle(FontStyle * style); void setFontStyle(FontStyle * style);
protected: protected:
tstring m_sText; TString m_sText;
COLORREF m_color; COLORREF m_color;
FontStyle * m_pFontStyle; FontStyle * m_pFontStyle;
@ -637,8 +631,8 @@ protected:
class Image : class Image :
public RectNode public RectNode
{ {
friend class Sprite; friend Sprite;
friend class ImageButton; friend ImageButton;
public: public:
Image(); Image();
// 从图片文件获取图像 // 从图片文件获取图像
@ -708,7 +702,7 @@ protected:
class Sprite : class Sprite :
public RectNode public RectNode
{ {
friend class BatchSprite; friend BatchSprite;
public: public:
Sprite(); Sprite();
Sprite(Image * image); Sprite(Image * image);
@ -882,7 +876,7 @@ class TextButton :
{ {
public: public:
TextButton(); TextButton();
TextButton(tstring text); TextButton(TString text);
TextButton(Text * text); TextButton(Text * text);
virtual ~TextButton(); virtual ~TextButton();
@ -1029,11 +1023,11 @@ protected:
class Action : class Action :
public Object public Object
{ {
friend class Sprite; friend Sprite;
friend class ActionManager; friend ActionManager;
friend class ActionTwo; friend ActionTwo;
friend class ActionNeverStop; friend ActionNeverStop;
friend class ActionSequence; friend ActionSequence;
public: public:
Action(); Action();
virtual ~Action(); virtual ~Action();
@ -1326,35 +1320,35 @@ class FileUtils
{ {
public: public:
// 获取系统的 AppData\Local 路径 // 获取系统的 AppData\Local 路径
static tstring getLocalAppDataPath(); static TString getLocalAppDataPath();
// 获取默认的保存路径 // 获取默认的保存路径
static tstring getDefaultSavePath(); static TString getDefaultSavePath();
// 保存 int 型的值 // 保存 int 型的值
static void saveInt(LPCTSTR key, int value); static void saveInt(LPCTSTR key, int value);
// 保存 double 型的值 // 保存 double 型的值
static void saveDouble(LPCTSTR key, double value); static void saveDouble(LPCTSTR key, double value);
// 保存 字符串 型的值(不要在 Unicode 字符集下保存中文字符) // 保存 字符串 型的值(不要在 Unicode 字符集下保存中文字符)
static void saveString(LPCTSTR key, tstring value); static void saveString(LPCTSTR key, TString value);
// 获取 int 型的值(若不存在则返回 default 参数的值) // 获取 int 型的值(若不存在则返回 default 参数的值)
static int getInt(LPCTSTR key, int default); static int getInt(LPCTSTR key, int default);
// 获取 double 型的值(若不存在则返回 default 参数的值) // 获取 double 型的值(若不存在则返回 default 参数的值)
static double getDouble(LPCTSTR key, double default); static double getDouble(LPCTSTR key, double default);
// 获取 字符串 型的值(若不存在则返回 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 * 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 class MusicUtils
{ {
public: public:
// 播放背景音乐 // 播放背景音乐
static void playBackgroundMusic(tstring pszFilePath, bool bLoop = true); static void playBackgroundMusic(TString pszFilePath, bool bLoop = true);
// 停止背景音乐 // 停止背景音乐
static void stopBackgroundMusic(bool bReleaseData = false); static void stopBackgroundMusic(bool bReleaseData = false);
// 暂停背景音乐 // 暂停背景音乐
@ -1369,11 +1363,11 @@ public:
static void setBackgroundMusicVolume(float volume); 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 stopMusic(unsigned int nSoundId);
// 预加载音效 // 预加载音效
static void preloadMusic(tstring pszFilePath); static void preloadMusic(TString pszFilePath);
// 暂停音效 // 暂停音效
static void pauseMusic(unsigned int nSoundId); static void pauseMusic(unsigned int nSoundId);
// 继续播放音效 // 继续播放音效
@ -1381,7 +1375,7 @@ public:
// 卸载音效 // 卸载音效
static void unloadMusic(LPCTSTR pszFilePath); static void unloadMusic(LPCTSTR pszFilePath);
// 设置特定音乐的音量0 ~ 1.0f // 设置特定音乐的音量0 ~ 1.0f
static void setVolume(tstring pszFilePath, float volume); static void setVolume(TString pszFilePath, float volume);
// 暂停所有音乐 // 暂停所有音乐
static void pauseAllMusics(); static void pauseAllMusics();
@ -1397,10 +1391,10 @@ public:
class Timer class Timer
{ {
friend class App; friend App;
public: public:
Timer(tstring name, UINT ms, const TIMER_CALLBACK & callback); Timer(TString name, UINT ms, const TIMER_CALLBACK & callback);
~Timer(); ~Timer();
// 启动定时器 // 启动定时器
@ -1414,30 +1408,30 @@ public:
// 设置回调函数 // 设置回调函数
void setCallback(const TIMER_CALLBACK& callback); void setCallback(const TIMER_CALLBACK& callback);
// 设置定时器名称 // 设置定时器名称
void setName(tstring name); void setName(TString name);
// 获取定时器间隔时间 // 获取定时器间隔时间
UINT getInterval() const; UINT getInterval() const;
// 获取定时器名称 // 获取定时器名称
tstring getName() const; TString getName() const;
// 添加定时器 // 添加定时器
static void addTimer(Timer * timer); 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(); static void clearAllTimers();
protected: protected:
bool m_bRunning; bool m_bRunning;
tstring m_sName; TString m_sName;
TIMER_CALLBACK m_callback; TIMER_CALLBACK m_callback;
LARGE_INTEGER m_nLast; LARGE_INTEGER m_nLast;
LARGE_INTEGER m_nAnimationInterval; LARGE_INTEGER m_nAnimationInterval;
@ -1449,8 +1443,8 @@ private:
class ActionManager class ActionManager
{ {
friend class App; friend App;
friend class Sprite; friend Sprite;
public: public:
// 继续一个特定的动作 // 继续一个特定的动作
static void startAction(Action * action); static void startAction(Action * action);
@ -1487,4 +1481,10 @@ private:
} // End of easy2d namespace } // 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; using namespace easy2d;