增加Exception类

This commit is contained in:
Nomango 2018-05-24 00:58:16 +08:00
parent dfd49588cd
commit 7499e1af7f
28 changed files with 285 additions and 197 deletions

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::Animate::Animate() e2d::Animate::Animate()
: _frameIndex(0) : _frameIndex(0)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::JumpBy::JumpBy(double duration, const Vector & vec, double height, int jumps) e2d::JumpBy::JumpBy(double duration, const Vector & vec, double height, int jumps)
: FiniteTimeAction(duration) : FiniteTimeAction(duration)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::JumpTo::JumpTo(double duration, const Point & pos, double height, int jumps) e2d::JumpTo::JumpTo(double duration, const Point & pos, double height, int jumps)
: JumpBy(duration, Point(), height, jumps) : JumpBy(duration, Point(), height, jumps)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::MoveBy::MoveBy(double duration, Vector vector) e2d::MoveBy::MoveBy(double duration, Vector vector)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::MoveTo::MoveTo(double duration, Point pos) e2d::MoveTo::MoveTo(double duration, Point pos)
: MoveBy(duration, Vector()) : MoveBy(duration, Vector())

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::OpacityBy::OpacityBy(double duration, double opacity) e2d::OpacityBy::OpacityBy(double duration, double opacity)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::OpacityTo::OpacityTo(double duration, double opacity) e2d::OpacityTo::OpacityTo(double duration, double opacity)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::RotateBy::RotateBy(double duration, double rotation) e2d::RotateBy::RotateBy(double duration, double rotation)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::RotateTo::RotateTo(double duration, double rotation) e2d::RotateTo::RotateTo(double duration, double rotation)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::ScaleBy::ScaleBy(double duration, double scale) e2d::ScaleBy::ScaleBy(double duration, double scale)

View File

@ -1,4 +1,5 @@
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
e2d::ScaleTo::ScaleTo(double duration, double scale) e2d::ScaleTo::ScaleTo(double duration, double scale)
: ScaleBy(duration, 0, 0) : ScaleBy(duration, 0, 0)

View File

@ -5,7 +5,7 @@
// 控制游戏终止 // 控制游戏终止
static bool s_bEndGame = false; static bool s_bEndGame = true;
// 控制游戏暂停 // 控制游戏暂停
static bool s_bPaused = false; static bool s_bPaused = false;
// 是否进行过初始化 // 是否进行过初始化
@ -127,6 +127,8 @@ int e2d::Game::start(bool autoRelease/* true */)
// 初始化计时 // 初始化计时
Time::__init(); Time::__init();
s_bEndGame = false;
while (!s_bEndGame) while (!s_bEndGame)
{ {
// 处理窗口消息 // 处理窗口消息
@ -154,6 +156,8 @@ int e2d::Game::start(bool autoRelease/* true */)
} }
} }
s_bEndGame = true;
if (autoRelease) if (autoRelease)
{ {
Game::destroy(); Game::destroy();
@ -178,11 +182,12 @@ void e2d::Game::resume()
void e2d::Game::reset() void e2d::Game::reset()
{ {
// 刷新当前时间 if (s_bInitialized && !s_bEndGame)
{
Time::__reset(); Time::__reset();
// 重置动作和定时器
ActionManager::__resetAll(); ActionManager::__resetAll();
Timer::__resetAll(); Timer::__resetAll();
}
} }
bool e2d::Game::isPaused() bool e2d::Game::isPaused()

View File

@ -4,13 +4,11 @@
typedef std::pair<UINT, UINT> HashPair; typedef std::pair<UINT, UINT> HashPair;
// 监听器容器 static std::vector<e2d::Listener*> s_vListeners; // 监听器容器
static std::vector<e2d::Listener*> s_vListeners; static bool s_bCollisionEnable = false; // 碰撞触发状态
// 碰撞触发状态 static e2d::Node * s_pActiveNode = nullptr; // 主动碰撞体
static bool s_bCollisionEnable = false; static e2d::Node * s_pPassiveNode = nullptr; // 被动碰撞体
static e2d::Node * s_pActiveNode = nullptr; static std::set<HashPair> s_sCollisionList; // 碰撞映射
static e2d::Node * s_pPassiveNode = nullptr;
static std::set<HashPair> s_sCollisionList;
void e2d::Collision::addName(const String & name1, const String & name2) void e2d::Collision::addName(const String & name1, const String & name2)

View File

@ -15,7 +15,7 @@ e2d::Scene::Scene()
} }
else else
{ {
// TODO: 抛出一个异常 throw Exception(L"场景根节点构造失败!");
} }
} }

37
core/Custom/Exception.cpp Normal file
View File

@ -0,0 +1,37 @@
#include "..\e2dcustom.h"
e2d::Exception::Exception()
: _message()
{
}
e2d::Exception::Exception(String message)
: _message(message)
{
}
e2d::Exception::Exception(Exception const& other)
: _message(other._message)
{
}
e2d::Exception& e2d::Exception::operator=(Exception const& other)
{
if (this == &other)
{
return *this;
}
_message = other._message;
return *this;
}
e2d::Exception::~Exception()
{
}
e2d::String e2d::Exception::msg() const
{
return _message;
}

View File

@ -1,5 +1,6 @@
#include "..\e2dmanager.h" #include "..\e2dmanager.h"
#include "..\e2daction.h" #include "..\e2daction.h"
#include "..\e2dnode.h"
static std::vector<e2d::Action*> s_vActions; static std::vector<e2d::Action*> s_vActions;
static std::vector<e2d::Action*> s_vRunningActions; static std::vector<e2d::Action*> s_vRunningActions;

View File

@ -596,13 +596,17 @@ void e2d::Node::addChild(Node * child, int order /* = 0 */)
if (child) if (child)
{ {
// TODO: 抛出一个异常 if (child->_parent != nullptr)
ASSERT(child->_parent == nullptr, "Node already added. It can't be added again!"); {
throw Exception(L"节点已有父节点, 不能再添加到其他节点!");
}
for (Node * parent = this; parent != nullptr; parent = parent->getParent()) for (Node * parent = this; parent != nullptr; parent = parent->getParent())
{ {
// TODO: 抛出一个异常 if (child == parent)
ASSERT(child != parent, "A Node cannot be the child of his own children!"); {
throw Exception(L"一个节点不能同时是另一个节点的父节点和子节点!");
}
} }
_children.push_back(child); _children.push_back(child);

View File

@ -1,24 +1,26 @@
#pragma once #pragma once
#include "e2dnode.h" #include "e2dcommon.h"
#include "e2dbase.h"
namespace e2d namespace e2d
{ {
class ActionManager; class Node;
class Loop; class Loop;
class Sequence; class Sequence;
class Spawn; class Spawn;
class ActionManager;
// 基础动作 // 基础动作
class Action : class Action :
public Object public Object
{ {
friend ActionManager; friend class ActionManager;
friend Loop; friend class Loop;
friend Sequence; friend class Sequence;
friend Spawn; friend class Spawn;
public: public:
Action(); Action();

View File

@ -51,7 +51,7 @@ public:
// 窗口控制 // 窗口控制
class Window class Window
{ {
friend Game; friend class Game;
public: public:
// 鼠标指针样式 // 鼠标指针样式
@ -147,7 +147,7 @@ private:
// 时间控制 // 时间控制
class Time class Time
{ {
friend Game; friend class Game;
public: public:
// 获取上一帧与当前帧的时间间隔(秒) // 获取上一帧与当前帧的时间间隔(秒)
@ -188,7 +188,7 @@ class Listener;
// 输入控制 // 输入控制
class Input class Input
{ {
friend Game; friend class Game;
public: public:
// 鼠标键值 // 鼠标键值
@ -375,8 +375,8 @@ private:
// 渲染器 // 渲染器
class Renderer class Renderer
{ {
friend Game; friend class Game;
friend Window; friend class Window;
public: public:
// 获取背景色 // 获取背景色
@ -437,8 +437,8 @@ private:
// 垃圾回收装置 // 垃圾回收装置
class GC class GC
{ {
friend Game; friend class Game;
friend Object; friend class Object;
public: public:
// 创建可自动回收内存的对象 // 创建可自动回收内存的对象

View File

@ -13,8 +13,8 @@ class ColliderManager;
// 碰撞事件 // 碰撞事件
class Collision class Collision
{ {
friend Game; friend class Game;
friend ColliderManager; friend class ColliderManager;
public: public:
// 添加可互相碰撞物体的名称 // 添加可互相碰撞物体的名称
@ -123,8 +123,8 @@ private:
class Collider : class Collider :
public Object public Object
{ {
friend ColliderManager; friend class ColliderManager;
friend Node; friend class Node;
public: public:
// 碰撞体类别 // 碰撞体类别

View File

@ -227,8 +227,8 @@ public:
String operator+ (const wchar_t *); String operator+ (const wchar_t *);
// ÓÑÔªÔËËã·û // ÓÑÔªÔËËã·û
friend String operator+ (const char *, const String &); friend class String operator+ (const char *, const String &);
friend String operator+ (const wchar_t*, const String &); friend class String operator+ (const wchar_t*, const String &);
// ÀàÐÍת»»²Ù×÷·û // ÀàÐÍת»»²Ù×÷·û
operator const wchar_t* () const; operator const wchar_t* () const;
@ -260,11 +260,11 @@ public:
// ÆäËûÔËËã·û // ÆäËûÔËËã·û
wchar_t& operator[] (int); wchar_t& operator[] (int);
friend std::ostream& operator<< (std::ostream &, const String &); friend class std::ostream& operator<< (std::ostream &, const String &);
friend std::wostream& operator<< (std::wostream &, const String &); friend class std::wostream& operator<< (std::wostream &, const String &);
friend std::istream& operator>> (std::istream &, String &); friend class std::istream& operator>> (std::istream &, String &);
friend std::wistream& operator>> (std::wistream &, String &); friend class std::wistream& operator>> (std::wistream &, String &);
private: private:
std::wstring _str; std::wstring _str;
@ -571,8 +571,8 @@ class Transition;
class Scene : class Scene :
public Object public Object
{ {
friend SceneManager; friend class SceneManager;
friend Transition; friend class Transition;
public: public:
Scene(); Scene();

View File

@ -4,23 +4,26 @@
namespace e2d namespace e2d
{ {
template<class Interface>
inline void SafeRelease(Interface*& p)
{ template<class Interface>
inline void SafeRelease(Interface*& p)
{
if (p != nullptr) if (p != nullptr)
{ {
p->Release(); p->Release();
p = nullptr; p = nullptr;
} }
} }
class Music;
// 音源回调 class Music;
class VoiceCallback
// 音源回调
class VoiceCallback
: public IXAudio2VoiceCallback : public IXAudio2VoiceCallback
{ {
public: public:
VoiceCallback(Music * music); VoiceCallback(Music * music);
~VoiceCallback(); ~VoiceCallback();
@ -57,18 +60,18 @@ namespace e2d
const Function& func const Function& func
); );
protected: protected:
Music * _music; Music * _music;
Function _loopEndFunc; Function _loopEndFunc;
Function _streamEndFunc; Function _streamEndFunc;
}; };
// 文字渲染器 // 文字渲染器
class TextRenderer class TextRenderer
: public IDWriteTextRenderer : public IDWriteTextRenderer
{ {
public: public:
TextRenderer( TextRenderer(
ID2D1Factory* pD2DFactory, ID2D1Factory* pD2DFactory,
ID2D1HwndRenderTarget* pRT, ID2D1HwndRenderTarget* pRT,
@ -136,7 +139,7 @@ namespace e2d
__out FLOAT* pixelsPerDip __out FLOAT* pixelsPerDip
); );
public: public:
unsigned long STDMETHODCALLTYPE AddRef(); unsigned long STDMETHODCALLTYPE AddRef();
unsigned long STDMETHODCALLTYPE Release(); unsigned long STDMETHODCALLTYPE Release();
HRESULT STDMETHODCALLTYPE QueryInterface( HRESULT STDMETHODCALLTYPE QueryInterface(
@ -144,7 +147,7 @@ namespace e2d
void** ppvObject void** ppvObject
); );
private: private:
unsigned long cRefCount_; unsigned long cRefCount_;
D2D1_COLOR_F sFillColor_; D2D1_COLOR_F sFillColor_;
D2D1_COLOR_F sOutlineColor_; D2D1_COLOR_F sOutlineColor_;
@ -154,6 +157,29 @@ namespace e2d
ID2D1Factory* pD2DFactory_; ID2D1Factory* pD2DFactory_;
ID2D1HwndRenderTarget* pRT_; ID2D1HwndRenderTarget* pRT_;
ID2D1SolidColorBrush* pBrush_; ID2D1SolidColorBrush* pBrush_;
}; };
// 异常
class Exception
{
public:
Exception() throw();
explicit Exception(String message) throw();
Exception(Exception const& other) throw();
virtual ~Exception() throw();
Exception& operator=(Exception const& other) throw();
// 获取异常信息
virtual String msg() const;
private:
String _message;
};
} }

View File

@ -19,8 +19,8 @@ class Transition;
// 场景管理器 // 场景管理器
class SceneManager class SceneManager
{ {
friend Game; friend class Game;
friend Renderer; friend class Renderer;
public: public:
// 切换场景 // 切换场景
@ -65,9 +65,9 @@ private:
// 动作管理器 // 动作管理器
class ActionManager class ActionManager
{ {
friend Game; friend class Game;
friend Node; friend class Node;
friend Action; friend class Action;
public: public:
// 执行动作 // 执行动作
@ -154,8 +154,8 @@ private:
// 碰撞体管理器 // 碰撞体管理器
class ColliderManager class ColliderManager
{ {
friend Node; friend class Node;
friend Collider; friend class Collider;
private: private:
// 更新碰撞体 // 更新碰撞体

View File

@ -13,10 +13,10 @@ class ColliderManager;
class Node : class Node :
public Object public Object
{ {
friend Scene; friend class Scene;
friend Collider; friend class Collider;
friend Transition; friend class Transition;
friend ColliderManager; friend class ColliderManager;
public: public:
// 节点属性 // 节点属性

View File

@ -52,7 +52,7 @@ private:
class Music : class Music :
public Object public Object
{ {
friend Game; friend class Game;
public: public:
Music(); Music();
@ -162,7 +162,7 @@ protected:
// 音乐播放器 // 音乐播放器
class Player class Player
{ {
friend Game; friend class Game;
public: public:
// 预加载音乐资源 // 预加载音乐资源
@ -258,7 +258,7 @@ private:
// 定时器 // 定时器
class Timer class Timer
{ {
friend Game; friend class Game;
public: public:
// 启动定时器(每帧执行一次) // 启动定时器(每帧执行一次)
@ -324,8 +324,8 @@ class Collision;
class Listener class Listener
: public Object : public Object
{ {
friend Input; friend class Input;
friend Collision; friend class Collision;
public: public:
Listener(); Listener();
@ -447,7 +447,7 @@ public:
// 路径工具 // 路径工具
class Path class Path
{ {
friend Game; friend class Game;
public: public:
// 获取数据的默认保存路径 // 获取数据的默认保存路径

View File

@ -12,7 +12,7 @@ class SceneManager;
class Transition : class Transition :
public Object public Object
{ {
friend SceneManager; friend class SceneManager;
public: public:
Transition(double duration); Transition(double duration);

View File

@ -236,6 +236,7 @@
<ClCompile Include="..\..\core\Common\Size.cpp" /> <ClCompile Include="..\..\core\Common\Size.cpp" />
<ClCompile Include="..\..\core\Common\String.cpp" /> <ClCompile Include="..\..\core\Common\String.cpp" />
<ClCompile Include="..\..\core\Common\Image.cpp" /> <ClCompile Include="..\..\core\Common\Image.cpp" />
<ClCompile Include="..\..\core\Custom\Exception.cpp" />
<ClCompile Include="..\..\core\Custom\TextRenderer.cpp" /> <ClCompile Include="..\..\core\Custom\TextRenderer.cpp" />
<ClCompile Include="..\..\core\Custom\VoiceCallback.cpp" /> <ClCompile Include="..\..\core\Custom\VoiceCallback.cpp" />
<ClCompile Include="..\..\core\Manager\ActionManager.cpp" /> <ClCompile Include="..\..\core\Manager\ActionManager.cpp" />

View File

@ -237,6 +237,9 @@
<ClCompile Include="..\..\core\Common\Rect.cpp"> <ClCompile Include="..\..\core\Common\Rect.cpp">
<Filter>Common</Filter> <Filter>Common</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="..\..\core\Custom\Exception.cpp">
<Filter>Custom</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="..\..\core\easy2d.h" /> <ClInclude Include="..\..\core\easy2d.h" />