refactoring: remove class Options

This commit is contained in:
Haibo 2018-10-18 00:17:27 +08:00
parent 5c69133a1f
commit 183143a8c3
10 changed files with 142 additions and 149 deletions

View File

@ -165,7 +165,7 @@ void easy2d::Button::SetPivot(float pivot_x, float pivot_y)
bool easy2d::Button::Dispatch(const MouseEvent & e, bool handled) bool easy2d::Button::Dispatch(const MouseEvent & e, bool handled)
{ {
if (!handled && enabled_ && visible_ && normal_) if (!handled && enabled_ && IsVisible() && normal_)
{ {
bool contains = normal_->ContainsPoint(e.GetPosition()); bool contains = normal_->ContainsPoint(e.GetPosition());
if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains) if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains)
@ -216,7 +216,7 @@ void easy2d::Button::Visit()
{ {
Node::Visit(); Node::Visit();
if (visible_ && if (IsVisible() &&
!enabled_ && !enabled_ &&
normal_ && normal_ &&
normal_->ContainsPoint(Device::GetInput()->GetMousePos())) normal_->ContainsPoint(Device::GetInput()->GetMousePos()))

View File

@ -132,7 +132,7 @@
#ifndef E2D_WARNING #ifndef E2D_WARNING
# if defined( DEBUG ) || defined( _DEBUG ) # if defined( DEBUG ) || defined( _DEBUG )
# define E2D_WARNING(msg) do { ::OutputDebugStringW(L"Warning: " _CRT_WIDE(msg) L"\r\n"); } while(0) # define E2D_WARNING(msg) do { ::OutputDebugStringW(L"[easy2d] Warning: " _CRT_WIDE(msg) L"\r\n"); } while(0)
# else # else
# define E2D_WARNING(msg) ((void)0) # define E2D_WARNING(msg) ((void)0)
# endif # endif
@ -141,7 +141,7 @@
#ifndef E2D_WARNING_IF #ifndef E2D_WARNING_IF
# if defined( DEBUG ) || defined( _DEBUG ) # if defined( DEBUG ) || defined( _DEBUG )
# define E2D_WARNING_IF(exp, msg) do { if (exp) { ::OutputDebugStringW(L"Warning: " _CRT_WIDE(msg) L"\r\n"); } } while(0) # define E2D_WARNING_IF(exp, msg) do { if (exp) { ::OutputDebugStringW(L"[easy2d] Warning: " _CRT_WIDE(msg) L"\r\n"); } } while(0)
# else # else
# define E2D_WARNING_IF(exp, msg) ((void)0) # define E2D_WARNING_IF(exp, msg) ((void)0)
# endif # endif

View File

@ -201,52 +201,33 @@ namespace easy2d
}; };
// 启动选项
struct Options
{
String title; // 标题
int width; // 宽度
int height; // 高度
int icon; // 图标资源 ID
bool debug_mode; // 调试模式
Options()
: title(L"Easy2D Game")
, width(640)
, height(480)
, icon(0)
, debug_mode(false)
{
}
};
// 游戏控制器 // 游戏控制器
class Game class Game
{ {
public: public:
// 开始 // 开始时
virtual void Start() = 0; virtual void OnStart() = 0;
// 更新 // 更新时
virtual void Update(float dt) {} virtual void OnUpdate(float dt) {}
// 退出时
virtual void OnExit() {}
// 窗口关闭时
// 返回值:返回 false 将阻止窗口关闭
virtual bool OnClose() { return true; }
// 运行 // 运行
void Run( void Run();
const Options& options = Options()
);
// 结束 // 结束
void Quit(); void Quit();
// 关闭窗口时触发 // 设置窗口大小
// 返回值:返回 false 将阻止窗口关闭
virtual bool OnExit() { return true; }
// 修改窗体大小
void SetSize( void SetSize(
int width, /* 窗宽度 */ int width, /* 窗口宽度 */
int height /* 窗高度 */ int height /* 窗口高度 */
); );
// 设置窗体标题 // 设置窗体标题
@ -259,6 +240,11 @@ namespace easy2d
int resource_id /* 图标资源 ID */ int resource_id /* 图标资源 ID */
); );
// 调试模式
void SetDebugMode(
bool enabled
);
// 获取窗体标题 // 获取窗体标题
const String& GetTitle() const; const String& GetTitle() const;
@ -283,6 +269,26 @@ namespace easy2d
// 获取当前场景 // 获取当前场景
Scene * GetCurrentScene(); Scene * GetCurrentScene();
// 获取实例
static Game * GetInstance();
protected:
Game();
~Game();
E2D_DISABLE_COPY(Game);
private:
// 初始化
void Init();
// 根据客户区大小定位窗口
Rect Locate(
int width,
int height
);
// 是否正在进行场景过渡 // 是否正在进行场景过渡
bool IsTransitioning() const; bool IsTransitioning() const;
@ -294,23 +300,6 @@ namespace easy2d
// 渲染场景画面 // 渲染场景画面
void DrawScene(); void DrawScene();
// 获取实例
static Game * GetInstance();
protected:
Game();
~Game();
// 初始化
void Init();
// 根据客户区大小定位窗口
Rect Locate(
int width,
int height
);
// Win32 窗口消息回调程序 // Win32 窗口消息回调程序
static LRESULT CALLBACK WndProc( static LRESULT CALLBACK WndProc(
HWND hwnd, HWND hwnd,
@ -319,7 +308,7 @@ namespace easy2d
LPARAM l_param LPARAM l_param
); );
protected: private:
HWND hwnd_; HWND hwnd_;
String title_; String title_;
int width_; int width_;

View File

@ -107,7 +107,7 @@ namespace easy2d
// 清空缓存 // 清空缓存
static void ClearCache(); static void ClearCache();
protected: private:
E2D_DISABLE_COPY(Image); E2D_DISABLE_COPY(Image);
// 缓存 Bitmap 资源 // 缓存 Bitmap 资源
@ -125,7 +125,7 @@ namespace easy2d
ID2D1Bitmap * bitmap ID2D1Bitmap * bitmap
); );
protected: private:
Rect crop_rect_; Rect crop_rect_;
ID2D1Bitmap * bitmap_; ID2D1Bitmap * bitmap_;
@ -155,7 +155,7 @@ namespace easy2d
virtual void OnExit() {} virtual void OnExit() {}
// 更新场景 // 更新场景
virtual void Update(float dt) {} virtual void OnUpdate(float dt) {}
// 设置根节点 // 设置根节点
void SetRoot( void SetRoot(
@ -186,10 +186,10 @@ namespace easy2d
// 获取转换矩阵 // 获取转换矩阵
const D2D1::Matrix3x2F& GetTransform() const; const D2D1::Matrix3x2F& GetTransform() const;
protected: private:
E2D_DISABLE_COPY(Scene); E2D_DISABLE_COPY(Scene);
protected: private:
Node* root_; Node* root_;
D2D1::Matrix3x2F transform_; D2D1::Matrix3x2F transform_;
}; };
@ -235,7 +235,7 @@ namespace easy2d
// 重置计时 // 重置计时
void ResetTime(); void ResetTime();
protected: private:
bool running_; bool running_;
bool stopped_; bool stopped_;
int run_times_; int run_times_;
@ -285,10 +285,10 @@ namespace easy2d
virtual ~Node(); virtual ~Node();
// 渲染节点 // 渲染节点
virtual void Draw() const {} virtual void OnDraw() const {}
// 更新节点 // 更新节点
virtual void Update(float dt) {} virtual void OnUpdate(float dt) {}
// 获取节点显示状态 // 获取节点显示状态
bool IsVisible() const; bool IsVisible() const;
@ -344,12 +344,15 @@ namespace easy2d
// 获取节点旋转角度 // 获取节点旋转角度
float GetRotation() const; float GetRotation() const;
// 获取二维转换 // 获取二维转换矩阵
const Transform& GetTransform() const; const Transform& GetTransform() const;
// 获取节点透明度 // 获取节点透明度
float GetOpacity() const; float GetOpacity() const;
// 获取显示透明度
float GetDisplayOpacity() const;
// 获取父节点 // 获取父节点
Node * GetParent() const; Node * GetParent() const;
@ -634,19 +637,9 @@ namespace easy2d
const Tasks& GetAllTasks() const; const Tasks& GetAllTasks() const;
protected: protected:
E2D_DISABLE_COPY(Node);
// 遍历节点 // 遍历节点
virtual void Visit(); virtual void Visit();
// 渲染节点边缘
void DrawBorder();
// 设置节点所在场景
void SetParentScene(
Scene * scene
);
// 分发鼠标消息 // 分发鼠标消息
virtual bool Dispatch( virtual bool Dispatch(
const MouseEvent& e, const MouseEvent& e,
@ -659,6 +652,17 @@ namespace easy2d
bool handled bool handled
); );
private:
E2D_DISABLE_COPY(Node);
// 渲染节点边缘
void DrawBorder();
// 设置节点所在场景
void SetParentScene(
Scene * scene
);
// 更新子节点 // 更新子节点
void UpdateChildren(float dt); void UpdateChildren(float dt);
@ -677,7 +681,7 @@ namespace easy2d
// 更新节点时间 // 更新节点时间
void UpdateTime(); void UpdateTime();
protected: private:
String name_; String name_;
size_t hash_name_; size_t hash_name_;
Transform transform_; Transform transform_;
@ -755,12 +759,12 @@ namespace easy2d
Image * GetImage() const; Image * GetImage() const;
// 渲染精灵 // 渲染精灵
virtual void Draw() const override; virtual void OnDraw() const override;
protected: private:
E2D_DISABLE_COPY(Sprite); E2D_DISABLE_COPY(Sprite);
protected: private:
Image* image_; Image* image_;
}; };
@ -959,9 +963,9 @@ namespace easy2d
); );
// 渲染文字 // 渲染文字
virtual void Draw() const override; virtual void OnDraw() const override;
protected: private:
E2D_DISABLE_COPY(Text); E2D_DISABLE_COPY(Text);
// 重新排版文字 // 重新排版文字
@ -973,7 +977,7 @@ namespace easy2d
// 创建文字布局 // 创建文字布局
void CreateLayout(); void CreateLayout();
protected: private:
String text_; String text_;
Font font_; Font font_;
Style style_; Style style_;
@ -1082,10 +1086,10 @@ namespace easy2d
float radius_y float radius_y
); );
protected: private:
E2D_DISABLE_COPY(Canvas); E2D_DISABLE_COPY(Canvas);
protected: private:
float stroke_width_; float stroke_width_;
Stroke stroke_; Stroke stroke_;
ID2D1RenderTarget* render_target_; ID2D1RenderTarget* render_target_;

View File

@ -505,7 +505,7 @@ namespace easy2d
E2D_OP_EXPLICIT operator bool() const; E2D_OP_EXPLICIT operator bool() const;
protected: private:
std::function<void()> func_; std::function<void()> func_;
}; };
@ -539,7 +539,7 @@ namespace easy2d
Duration& operator += (Duration const &); Duration& operator += (Duration const &);
Duration& operator -= (Duration const &); Duration& operator -= (Duration const &);
protected: private:
std::chrono::milliseconds duration_ms_; std::chrono::milliseconds duration_ms_;
}; };
@ -567,7 +567,7 @@ namespace easy2d
// 获取当前时间 // 获取当前时间
static Time Now(); static Time Now();
protected: private:
std::chrono::steady_clock::time_point time_; std::chrono::steady_clock::time_point time_;
}; };
@ -663,7 +663,7 @@ namespace easy2d
// 获取引用计数 // 获取引用计数
LONG GetRefCount() const; LONG GetRefCount() const;
protected: private:
LONG ref_count_; LONG ref_count_;
}; };

View File

@ -78,19 +78,13 @@ easy2d::Game::~Game()
::CoUninitialize(); ::CoUninitialize();
} }
void easy2d::Game::Run(const Options& options) void easy2d::Game::Run()
{ {
title_ = options.title;
width_ = options.width;
height_ = options.height;
icon_ = options.icon;
debug_mode_ = options.debug_mode;
// 初始化 // 初始化
Init(); Init();
// 开始 // 开始
Start(); OnStart();
// 刷新场景 // 刷新场景
if (next_scene_) if (next_scene_)
@ -118,7 +112,7 @@ void easy2d::Game::Run(const Options& options)
last = now; last = now;
Device::GetInput()->Flush(); Device::GetInput()->Flush();
Update(dt); OnUpdate(dt);
UpdateScene(dt); UpdateScene(dt);
DrawScene(); DrawScene();
@ -155,11 +149,7 @@ void easy2d::Game::EnterScene(Scene * scene, Transition * transition)
return; return;
} }
if (next_scene_ != nullptr) if (curr_scene_ == scene) { return; }
{
E2D_WARNING("Scene is transitioning...");
return;
}
if (next_scene_) if (next_scene_)
{ {
@ -198,7 +188,7 @@ void easy2d::Game::UpdateScene(float dt)
{ {
if (scene) if (scene)
{ {
scene->Update(dt); scene->OnUpdate(dt);
Node * root = scene->GetRoot(); Node * root = scene->GetRoot();
if (root) if (root)
{ {
@ -487,6 +477,11 @@ void easy2d::Game::SetIcon(int resource_id)
} }
} }
void easy2d::Game::SetDebugMode(bool enabled)
{
debug_mode_ = enabled;
}
LRESULT easy2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) LRESULT easy2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
{ {
@ -596,7 +591,7 @@ LRESULT easy2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_para
// 窗口关闭消息 // 窗口关闭消息
case WM_CLOSE: case WM_CLOSE:
{ {
if (game->OnExit()) if (game->OnClose())
{ {
game->Quit(); game->Quit();
} }

View File

@ -84,7 +84,7 @@ void easy2d::Node::Visit()
if (children_.empty()) if (children_.empty())
{ {
render_target->SetTransform(final_matrix_); render_target->SetTransform(final_matrix_);
Draw(); OnDraw();
} }
else else
{ {
@ -116,7 +116,7 @@ void easy2d::Node::Visit()
} }
render_target->SetTransform(final_matrix_); render_target->SetTransform(final_matrix_);
Draw(); OnDraw();
// 访问剩余节点 // 访问剩余节点
for (; i < children_.size(); ++i) for (; i < children_.size(); ++i)
@ -133,7 +133,7 @@ void easy2d::Node::UpdateChildren(float dt)
{ {
if (children_.empty()) if (children_.empty())
{ {
Update(dt); OnUpdate(dt);
UpdateActions(); UpdateActions();
UpdateTasks(); UpdateTasks();
UpdateTransform(); UpdateTransform();
@ -155,7 +155,7 @@ void easy2d::Node::UpdateChildren(float dt)
} }
} }
Update(dt); OnUpdate(dt);
UpdateActions(); UpdateActions();
UpdateTasks(); UpdateTasks();
UpdateTransform(); UpdateTransform();
@ -416,6 +416,11 @@ float easy2d::Node::GetOpacity() const
return real_opacity_; return real_opacity_;
} }
float easy2d::Node::GetDisplayOpacity() const
{
return display_opacity_;
}
int easy2d::Node::GetOrder() const int easy2d::Node::GetOrder() const
{ {
return order_; return order_;

View File

@ -127,21 +127,21 @@ easy2d::Image * easy2d::Sprite::GetImage() const
return image_; return image_;
} }
void easy2d::Sprite::Draw() const void easy2d::Sprite::OnDraw() const
{ {
if (image_ && image_->GetBitmap()) if (image_ && image_->GetBitmap())
{ {
auto crop_pos = image_->GetCropPos(); auto crop_pos = image_->GetCropPos();
Device::GetGraphics()->GetRenderTarget()->DrawBitmap( Device::GetGraphics()->GetRenderTarget()->DrawBitmap(
image_->GetBitmap(), image_->GetBitmap(),
D2D1::RectF(0, 0, transform_.size.width, transform_.size.height), D2D1::RectF(0, 0, GetTransform().size.width, GetTransform().size.height),
display_opacity_, GetDisplayOpacity(),
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR, D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
D2D1::RectF( D2D1::RectF(
crop_pos.x, crop_pos.x,
crop_pos.y, crop_pos.y,
crop_pos.y + transform_.size.width, crop_pos.y + GetTransform().size.width,
crop_pos.y + transform_.size.height crop_pos.y + GetTransform().size.height
) )
); );
} }

View File

@ -308,15 +308,15 @@ void easy2d::Text::SetOutlineStroke(Stroke outline_stroke)
style_.outline_stroke = outline_stroke; style_.outline_stroke = outline_stroke;
} }
void easy2d::Text::Draw() const void easy2d::Text::OnDraw() const
{ {
if (text_layout_) if (text_layout_)
{ {
auto graphics = Device::GetGraphics(); auto graphics = Device::GetGraphics();
// 创建文本区域 // 创建文本区域
D2D1_RECT_F textLayoutRect = D2D1::RectF(0, 0, transform_.size.width, transform_.size.height); D2D1_RECT_F textLayoutRect = D2D1::RectF(0, 0, GetTransform().size.width, GetTransform().size.height);
// 设置画刷颜色和透明度 // 设置画刷颜色和透明度
graphics->GetSolidBrush()->SetOpacity(display_opacity_); graphics->GetSolidBrush()->SetOpacity(GetDisplayOpacity());
// 获取文本渲染器 // 获取文本渲染器
auto text_renderer = graphics->GetTextRender(); auto text_renderer = graphics->GetTextRender();
graphics->SetTextRendererStyle( graphics->SetTextRendererStyle(
@ -449,7 +449,7 @@ void easy2d::Text::CreateLayout()
(const wchar_t *)text_, (const wchar_t *)text_,
length, length,
text_format_, text_format_,
transform_.size.width, GetTransform().size.width,
0, 0,
&text_layout_ &text_layout_
) )

View File

@ -41,12 +41,12 @@ namespace
inline void TraceError(LPCWSTR output) inline void TraceError(LPCWSTR output)
{ {
OutputDebugStringExW(L"Music error: %s failed!\r\n", output); OutputDebugStringExW(L"[easy2d] Music error: %s failed!\r\n", output);
} }
inline void TraceError(LPCWSTR output, HRESULT hr) inline void TraceError(LPCWSTR output, HRESULT hr)
{ {
OutputDebugStringExW(L"Music error: %s (%#X)\r\n", output, hr); OutputDebugStringExW(L"[easy2d] Music error: %s (%#X)\r\n", output, hr);
} }
} }