refactoring: remove class Options
This commit is contained in:
parent
5c69133a1f
commit
183143a8c3
|
|
@ -165,7 +165,7 @@ void easy2d::Button::SetPivot(float pivot_x, float pivot_y)
|
|||
|
||||
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());
|
||||
if (e.GetType() == MouseEvent::Type::LeftUp && is_selected_ && contains)
|
||||
|
|
@ -216,7 +216,7 @@ void easy2d::Button::Visit()
|
|||
{
|
||||
Node::Visit();
|
||||
|
||||
if (visible_ &&
|
||||
if (IsVisible() &&
|
||||
!enabled_ &&
|
||||
normal_ &&
|
||||
normal_->ContainsPoint(Device::GetInput()->GetMousePos()))
|
||||
|
|
|
|||
|
|
@ -132,7 +132,7 @@
|
|||
|
||||
#ifndef E2D_WARNING
|
||||
# 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
|
||||
# define E2D_WARNING(msg) ((void)0)
|
||||
# endif
|
||||
|
|
@ -141,7 +141,7 @@
|
|||
|
||||
#ifndef E2D_WARNING_IF
|
||||
# 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
|
||||
# define E2D_WARNING_IF(exp, msg) ((void)0)
|
||||
# endif
|
||||
|
|
|
|||
|
|
@ -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
|
||||
{
|
||||
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(
|
||||
const Options& options = Options()
|
||||
);
|
||||
void Run();
|
||||
|
||||
// 结束
|
||||
void Quit();
|
||||
|
||||
// 关闭窗口时触发
|
||||
// 返回值:返回 false 将阻止窗口关闭
|
||||
virtual bool OnExit() { return true; }
|
||||
|
||||
// 修改窗体大小
|
||||
// 设置窗口大小
|
||||
void SetSize(
|
||||
int width, /* 窗体宽度 */
|
||||
int height /* 窗体高度 */
|
||||
int width, /* 窗口宽度 */
|
||||
int height /* 窗口高度 */
|
||||
);
|
||||
|
||||
// 设置窗体标题
|
||||
|
|
@ -259,6 +240,11 @@ namespace easy2d
|
|||
int resource_id /* 图标资源 ID */
|
||||
);
|
||||
|
||||
// 调试模式
|
||||
void SetDebugMode(
|
||||
bool enabled
|
||||
);
|
||||
|
||||
// 获取窗体标题
|
||||
const String& GetTitle() const;
|
||||
|
||||
|
|
@ -283,6 +269,26 @@ namespace easy2d
|
|||
// 获取当前场景
|
||||
Scene * GetCurrentScene();
|
||||
|
||||
// 获取实例
|
||||
static Game * GetInstance();
|
||||
|
||||
protected:
|
||||
Game();
|
||||
|
||||
~Game();
|
||||
|
||||
E2D_DISABLE_COPY(Game);
|
||||
|
||||
private:
|
||||
// 初始化
|
||||
void Init();
|
||||
|
||||
// 根据客户区大小定位窗口
|
||||
Rect Locate(
|
||||
int width,
|
||||
int height
|
||||
);
|
||||
|
||||
// 是否正在进行场景过渡
|
||||
bool IsTransitioning() const;
|
||||
|
||||
|
|
@ -294,23 +300,6 @@ namespace easy2d
|
|||
// 渲染场景画面
|
||||
void DrawScene();
|
||||
|
||||
// 获取实例
|
||||
static Game * GetInstance();
|
||||
|
||||
protected:
|
||||
Game();
|
||||
|
||||
~Game();
|
||||
|
||||
// 初始化
|
||||
void Init();
|
||||
|
||||
// 根据客户区大小定位窗口
|
||||
Rect Locate(
|
||||
int width,
|
||||
int height
|
||||
);
|
||||
|
||||
// Win32 窗口消息回调程序
|
||||
static LRESULT CALLBACK WndProc(
|
||||
HWND hwnd,
|
||||
|
|
@ -319,7 +308,7 @@ namespace easy2d
|
|||
LPARAM l_param
|
||||
);
|
||||
|
||||
protected:
|
||||
private:
|
||||
HWND hwnd_;
|
||||
String title_;
|
||||
int width_;
|
||||
|
|
|
|||
120
core/e2dobject.h
120
core/e2dobject.h
|
|
@ -107,7 +107,7 @@ namespace easy2d
|
|||
// 清空缓存
|
||||
static void ClearCache();
|
||||
|
||||
protected:
|
||||
private:
|
||||
E2D_DISABLE_COPY(Image);
|
||||
|
||||
// 缓存 Bitmap 资源
|
||||
|
|
@ -125,7 +125,7 @@ namespace easy2d
|
|||
ID2D1Bitmap * bitmap
|
||||
);
|
||||
|
||||
protected:
|
||||
private:
|
||||
Rect crop_rect_;
|
||||
ID2D1Bitmap * bitmap_;
|
||||
|
||||
|
|
@ -155,7 +155,7 @@ namespace easy2d
|
|||
virtual void OnExit() {}
|
||||
|
||||
// 更新场景
|
||||
virtual void Update(float dt) {}
|
||||
virtual void OnUpdate(float dt) {}
|
||||
|
||||
// 设置根节点
|
||||
void SetRoot(
|
||||
|
|
@ -186,12 +186,12 @@ namespace easy2d
|
|||
// 获取转换矩阵
|
||||
const D2D1::Matrix3x2F& GetTransform() const;
|
||||
|
||||
protected:
|
||||
private:
|
||||
E2D_DISABLE_COPY(Scene);
|
||||
|
||||
protected:
|
||||
Node* root_;
|
||||
D2D1::Matrix3x2F transform_;
|
||||
private:
|
||||
Node* root_;
|
||||
D2D1::Matrix3x2F transform_;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -235,7 +235,7 @@ namespace easy2d
|
|||
// 重置计时
|
||||
void ResetTime();
|
||||
|
||||
protected:
|
||||
private:
|
||||
bool running_;
|
||||
bool stopped_;
|
||||
int run_times_;
|
||||
|
|
@ -285,10 +285,10 @@ namespace easy2d
|
|||
virtual ~Node();
|
||||
|
||||
// 渲染节点
|
||||
virtual void Draw() const {}
|
||||
virtual void OnDraw() const {}
|
||||
|
||||
// 更新节点
|
||||
virtual void Update(float dt) {}
|
||||
virtual void OnUpdate(float dt) {}
|
||||
|
||||
// 获取节点显示状态
|
||||
bool IsVisible() const;
|
||||
|
|
@ -344,12 +344,15 @@ namespace easy2d
|
|||
// 获取节点旋转角度
|
||||
float GetRotation() const;
|
||||
|
||||
// 获取二维转换
|
||||
// 获取二维转换矩阵
|
||||
const Transform& GetTransform() const;
|
||||
|
||||
// 获取节点透明度
|
||||
float GetOpacity() const;
|
||||
|
||||
// 获取显示透明度
|
||||
float GetDisplayOpacity() const;
|
||||
|
||||
// 获取父节点
|
||||
Node * GetParent() const;
|
||||
|
||||
|
|
@ -634,19 +637,9 @@ namespace easy2d
|
|||
const Tasks& GetAllTasks() const;
|
||||
|
||||
protected:
|
||||
E2D_DISABLE_COPY(Node);
|
||||
|
||||
// 遍历节点
|
||||
virtual void Visit();
|
||||
|
||||
// 渲染节点边缘
|
||||
void DrawBorder();
|
||||
|
||||
// 设置节点所在场景
|
||||
void SetParentScene(
|
||||
Scene * scene
|
||||
);
|
||||
|
||||
// 分发鼠标消息
|
||||
virtual bool Dispatch(
|
||||
const MouseEvent& e,
|
||||
|
|
@ -659,6 +652,17 @@ namespace easy2d
|
|||
bool handled
|
||||
);
|
||||
|
||||
private:
|
||||
E2D_DISABLE_COPY(Node);
|
||||
|
||||
// 渲染节点边缘
|
||||
void DrawBorder();
|
||||
|
||||
// 设置节点所在场景
|
||||
void SetParentScene(
|
||||
Scene * scene
|
||||
);
|
||||
|
||||
// 更新子节点
|
||||
void UpdateChildren(float dt);
|
||||
|
||||
|
|
@ -677,23 +681,23 @@ namespace easy2d
|
|||
// 更新节点时间
|
||||
void UpdateTime();
|
||||
|
||||
protected:
|
||||
String name_;
|
||||
size_t hash_name_;
|
||||
Transform transform_;
|
||||
float display_opacity_;
|
||||
float real_opacity_;
|
||||
int order_;
|
||||
bool visible_;
|
||||
bool clip_enabled_;
|
||||
bool dirty_sort_;
|
||||
bool dirty_transform_;
|
||||
Scene * parent_scene_;
|
||||
Node * parent_;
|
||||
Color border_color_;
|
||||
Actions actions_;
|
||||
Tasks tasks_;
|
||||
Nodes children_;
|
||||
private:
|
||||
String name_;
|
||||
size_t hash_name_;
|
||||
Transform transform_;
|
||||
float display_opacity_;
|
||||
float real_opacity_;
|
||||
int order_;
|
||||
bool visible_;
|
||||
bool clip_enabled_;
|
||||
bool dirty_sort_;
|
||||
bool dirty_transform_;
|
||||
Scene* parent_scene_;
|
||||
Node* parent_;
|
||||
Color border_color_;
|
||||
Actions actions_;
|
||||
Tasks tasks_;
|
||||
Nodes children_;
|
||||
ID2D1Geometry* border_;
|
||||
D2D1::Matrix3x2F initial_matrix_;
|
||||
D2D1::Matrix3x2F final_matrix_;
|
||||
|
|
@ -755,13 +759,13 @@ namespace easy2d
|
|||
Image * GetImage() const;
|
||||
|
||||
// 渲染精灵
|
||||
virtual void Draw() const override;
|
||||
virtual void OnDraw() const override;
|
||||
|
||||
protected:
|
||||
private:
|
||||
E2D_DISABLE_COPY(Sprite);
|
||||
|
||||
protected:
|
||||
Image * image_;
|
||||
private:
|
||||
Image* image_;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -959,9 +963,9 @@ namespace easy2d
|
|||
);
|
||||
|
||||
// 渲染文字
|
||||
virtual void Draw() const override;
|
||||
virtual void OnDraw() const override;
|
||||
|
||||
protected:
|
||||
private:
|
||||
E2D_DISABLE_COPY(Text);
|
||||
|
||||
// 重新排版文字
|
||||
|
|
@ -973,12 +977,12 @@ namespace easy2d
|
|||
// 创建文字布局
|
||||
void CreateLayout();
|
||||
|
||||
protected:
|
||||
String text_;
|
||||
Font font_;
|
||||
Style style_;
|
||||
IDWriteTextFormat * text_format_;
|
||||
IDWriteTextLayout * text_layout_;
|
||||
private:
|
||||
String text_;
|
||||
Font font_;
|
||||
Style style_;
|
||||
IDWriteTextFormat* text_format_;
|
||||
IDWriteTextLayout* text_layout_;
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -1082,16 +1086,16 @@ namespace easy2d
|
|||
float radius_y
|
||||
);
|
||||
|
||||
protected:
|
||||
private:
|
||||
E2D_DISABLE_COPY(Canvas);
|
||||
|
||||
protected:
|
||||
float stroke_width_;
|
||||
Stroke stroke_;
|
||||
ID2D1RenderTarget * render_target_;
|
||||
ID2D1SolidColorBrush * fill_brush_;
|
||||
ID2D1SolidColorBrush * line_brush_;
|
||||
ID2D1StrokeStyle * stroke_style_;
|
||||
private:
|
||||
float stroke_width_;
|
||||
Stroke stroke_;
|
||||
ID2D1RenderTarget* render_target_;
|
||||
ID2D1SolidColorBrush* fill_brush_;
|
||||
ID2D1SolidColorBrush* line_brush_;
|
||||
ID2D1StrokeStyle* stroke_style_;
|
||||
};
|
||||
|
||||
} // end of easy2d namespace
|
||||
|
|
|
|||
|
|
@ -505,7 +505,7 @@ namespace easy2d
|
|||
|
||||
E2D_OP_EXPLICIT operator bool() const;
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::function<void()> func_;
|
||||
};
|
||||
|
||||
|
|
@ -539,7 +539,7 @@ namespace easy2d
|
|||
Duration& operator += (Duration const &);
|
||||
Duration& operator -= (Duration const &);
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::chrono::milliseconds duration_ms_;
|
||||
};
|
||||
|
||||
|
|
@ -567,7 +567,7 @@ namespace easy2d
|
|||
// 获取当前时间
|
||||
static Time Now();
|
||||
|
||||
protected:
|
||||
private:
|
||||
std::chrono::steady_clock::time_point time_;
|
||||
};
|
||||
|
||||
|
|
@ -663,7 +663,7 @@ namespace easy2d
|
|||
// 获取引用计数
|
||||
LONG GetRefCount() const;
|
||||
|
||||
protected:
|
||||
private:
|
||||
LONG ref_count_;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -78,19 +78,13 @@ easy2d::Game::~Game()
|
|||
::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();
|
||||
|
||||
// 开始
|
||||
Start();
|
||||
OnStart();
|
||||
|
||||
// 刷新场景
|
||||
if (next_scene_)
|
||||
|
|
@ -118,7 +112,7 @@ void easy2d::Game::Run(const Options& options)
|
|||
last = now;
|
||||
|
||||
Device::GetInput()->Flush();
|
||||
Update(dt);
|
||||
OnUpdate(dt);
|
||||
UpdateScene(dt);
|
||||
DrawScene();
|
||||
|
||||
|
|
@ -155,11 +149,7 @@ void easy2d::Game::EnterScene(Scene * scene, Transition * transition)
|
|||
return;
|
||||
}
|
||||
|
||||
if (next_scene_ != nullptr)
|
||||
{
|
||||
E2D_WARNING("Scene is transitioning...");
|
||||
return;
|
||||
}
|
||||
if (curr_scene_ == scene) { return; }
|
||||
|
||||
if (next_scene_)
|
||||
{
|
||||
|
|
@ -198,7 +188,7 @@ void easy2d::Game::UpdateScene(float dt)
|
|||
{
|
||||
if (scene)
|
||||
{
|
||||
scene->Update(dt);
|
||||
scene->OnUpdate(dt);
|
||||
Node * root = scene->GetRoot();
|
||||
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)
|
||||
{
|
||||
|
|
@ -596,7 +591,7 @@ LRESULT easy2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_para
|
|||
// 窗口关闭消息
|
||||
case WM_CLOSE:
|
||||
{
|
||||
if (game->OnExit())
|
||||
if (game->OnClose())
|
||||
{
|
||||
game->Quit();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -84,7 +84,7 @@ void easy2d::Node::Visit()
|
|||
if (children_.empty())
|
||||
{
|
||||
render_target->SetTransform(final_matrix_);
|
||||
Draw();
|
||||
OnDraw();
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -116,7 +116,7 @@ void easy2d::Node::Visit()
|
|||
}
|
||||
|
||||
render_target->SetTransform(final_matrix_);
|
||||
Draw();
|
||||
OnDraw();
|
||||
|
||||
// 访问剩余节点
|
||||
for (; i < children_.size(); ++i)
|
||||
|
|
@ -133,7 +133,7 @@ void easy2d::Node::UpdateChildren(float dt)
|
|||
{
|
||||
if (children_.empty())
|
||||
{
|
||||
Update(dt);
|
||||
OnUpdate(dt);
|
||||
UpdateActions();
|
||||
UpdateTasks();
|
||||
UpdateTransform();
|
||||
|
|
@ -155,7 +155,7 @@ void easy2d::Node::UpdateChildren(float dt)
|
|||
}
|
||||
}
|
||||
|
||||
Update(dt);
|
||||
OnUpdate(dt);
|
||||
UpdateActions();
|
||||
UpdateTasks();
|
||||
UpdateTransform();
|
||||
|
|
@ -416,6 +416,11 @@ float easy2d::Node::GetOpacity() const
|
|||
return real_opacity_;
|
||||
}
|
||||
|
||||
float easy2d::Node::GetDisplayOpacity() const
|
||||
{
|
||||
return display_opacity_;
|
||||
}
|
||||
|
||||
int easy2d::Node::GetOrder() const
|
||||
{
|
||||
return order_;
|
||||
|
|
|
|||
|
|
@ -127,21 +127,21 @@ easy2d::Image * easy2d::Sprite::GetImage() const
|
|||
return image_;
|
||||
}
|
||||
|
||||
void easy2d::Sprite::Draw() const
|
||||
void easy2d::Sprite::OnDraw() const
|
||||
{
|
||||
if (image_ && image_->GetBitmap())
|
||||
{
|
||||
auto crop_pos = image_->GetCropPos();
|
||||
Device::GetGraphics()->GetRenderTarget()->DrawBitmap(
|
||||
image_->GetBitmap(),
|
||||
D2D1::RectF(0, 0, transform_.size.width, transform_.size.height),
|
||||
display_opacity_,
|
||||
D2D1::RectF(0, 0, GetTransform().size.width, GetTransform().size.height),
|
||||
GetDisplayOpacity(),
|
||||
D2D1_BITMAP_INTERPOLATION_MODE_LINEAR,
|
||||
D2D1::RectF(
|
||||
crop_pos.x,
|
||||
crop_pos.y,
|
||||
crop_pos.y + transform_.size.width,
|
||||
crop_pos.y + transform_.size.height
|
||||
crop_pos.y + GetTransform().size.width,
|
||||
crop_pos.y + GetTransform().size.height
|
||||
)
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -308,15 +308,15 @@ void easy2d::Text::SetOutlineStroke(Stroke outline_stroke)
|
|||
style_.outline_stroke = outline_stroke;
|
||||
}
|
||||
|
||||
void easy2d::Text::Draw() const
|
||||
void easy2d::Text::OnDraw() const
|
||||
{
|
||||
if (text_layout_)
|
||||
{
|
||||
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();
|
||||
graphics->SetTextRendererStyle(
|
||||
|
|
@ -449,7 +449,7 @@ void easy2d::Text::CreateLayout()
|
|||
(const wchar_t *)text_,
|
||||
length,
|
||||
text_format_,
|
||||
transform_.size.width,
|
||||
GetTransform().size.width,
|
||||
0,
|
||||
&text_layout_
|
||||
)
|
||||
|
|
|
|||
|
|
@ -41,12 +41,12 @@ namespace
|
|||
|
||||
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)
|
||||
{
|
||||
OutputDebugStringExW(L"Music error: %s (%#X)\r\n", output, hr);
|
||||
OutputDebugStringExW(L"[easy2d] Music error: %s (%#X)\r\n", output, hr);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue