add: Game::Update

This commit is contained in:
Nomango 2018-10-06 09:45:28 +08:00
parent 318103b148
commit 1c8de670ac
4 changed files with 74 additions and 31 deletions

View File

@ -215,6 +215,9 @@ namespace e2d
// 开始 // 开始
virtual void Start() = 0; virtual void Start() = 0;
// 更新
virtual void Update(float dt) {}
// 运行 // 运行
void Run( void Run(
const Options& options = Options() const Options& options = Options()
@ -271,7 +274,9 @@ namespace e2d
bool IsTransitioning() const; bool IsTransitioning() const;
// 更新场景内容 // 更新场景内容
void UpdateScene(); void UpdateScene(
float dt
);
// 渲染场景画面 // 渲染场景画面
void DrawScene(); void DrawScene();

View File

@ -153,7 +153,7 @@ namespace e2d
virtual void OnExit() {} virtual void OnExit() {}
// 更新场景 // 更新场景
virtual void Update() {} virtual void Update(float dt) {}
// 设置根节点 // 设置根节点
void SetRoot( void SetRoot(
@ -270,7 +270,6 @@ namespace e2d
class Node class Node
: public Ref : public Ref
{ {
friend class Game;
friend class Scene; friend class Scene;
public: public:
@ -629,11 +628,14 @@ namespace e2d
virtual void Draw() const {} virtual void Draw() const {}
// 更新节点 // 更新节点
virtual void Update() {} virtual void Update(float dt) {}
// 渲染节点边缘 // 渲染节点边缘
void DrawBorder(); void DrawBorder();
// 更新子节点
void UpdateChildren(float dt);
// 分发鼠标消息 // 分发鼠标消息
virtual bool Dispatch( virtual bool Dispatch(
const MouseEvent& e, const MouseEvent& e,

View File

@ -89,7 +89,7 @@ void e2d::Game::Run(const Options& options)
// 刷新场景 // 刷新场景
::ShowWindow(hwnd_, SW_SHOWNORMAL); ::ShowWindow(hwnd_, SW_SHOWNORMAL);
::UpdateWindow(hwnd_); ::UpdateWindow(hwnd_);
UpdateScene(); UpdateScene(0);
// 运行 // 运行
const int min_interval = 5; const int min_interval = 5;
@ -103,10 +103,12 @@ void e2d::Game::Run(const Options& options)
if (dur.Milliseconds() > min_interval) if (dur.Milliseconds() > min_interval)
{ {
float dt = (now - last).Seconds();
last = now; last = now;
Device::GetInput()->Flush();
UpdateScene(); Device::GetInput()->Flush();
Update(dt);
UpdateScene(dt);
DrawScene(); DrawScene();
while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE))
@ -179,8 +181,20 @@ bool e2d::Game::IsTransitioning() const
return transition_ != nullptr; return transition_ != nullptr;
} }
void e2d::Game::UpdateScene() void e2d::Game::UpdateScene(float dt)
{ {
if (curr_scene_)
{
curr_scene_->Update(dt);
curr_scene_->GetRoot()->UpdateChildren(dt);
}
if (next_scene_)
{
next_scene_->Update(dt);
next_scene_->GetRoot()->UpdateChildren(dt);
}
if (transition_) if (transition_)
{ {
transition_->Update(); transition_->Update();
@ -209,16 +223,6 @@ void e2d::Game::UpdateScene()
curr_scene_ = next_scene_; curr_scene_ = next_scene_;
next_scene_ = nullptr; next_scene_ = nullptr;
} }
if (curr_scene_)
{
curr_scene_->Update();
}
if (next_scene_)
{
next_scene_->Update();
}
} }
void e2d::Game::DrawScene() void e2d::Game::DrawScene()
@ -490,7 +494,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
} }
else else
{ {
bool has_handled = false; bool was_handled = false;
Game * game = reinterpret_cast<Game*>( Game * game = reinterpret_cast<Game*>(
static_cast<LONG_PTR>( static_cast<LONG_PTR>(
::GetWindowLongPtrW(hwnd, GWLP_USERDATA) ::GetWindowLongPtrW(hwnd, GWLP_USERDATA)
@ -523,7 +527,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
} }
} }
result = 0; result = 0;
has_handled = true; was_handled = true;
break; break;
// 处理按键消息 // 处理按键消息
@ -540,7 +544,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
} }
} }
result = 0; result = 0;
has_handled = true; was_handled = true;
break; break;
// 处理窗口大小变化消息 // 处理窗口大小变化消息
@ -584,7 +588,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
::InvalidateRect(hwnd, nullptr, FALSE); ::InvalidateRect(hwnd, nullptr, FALSE);
} }
result = 0; result = 0;
has_handled = true; was_handled = true;
break; break;
// 重绘窗口 // 重绘窗口
@ -594,7 +598,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
::ValidateRect(hwnd, nullptr); ::ValidateRect(hwnd, nullptr);
} }
result = 0; result = 0;
has_handled = true; was_handled = true;
break; break;
// 窗口关闭消息 // 窗口关闭消息
@ -606,7 +610,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
} }
} }
result = 0; result = 0;
has_handled = true; was_handled = true;
break; break;
// 窗口销毁消息 // 窗口销毁消息
@ -615,12 +619,12 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param)
::PostQuitMessage(0); ::PostQuitMessage(0);
} }
result = 1; result = 1;
has_handled = true; was_handled = true;
break; break;
} }
if (!has_handled) if (!was_handled)
{ {
result = ::DefWindowProc(hwnd, msg, w_param, l_param); result = ::DefWindowProc(hwnd, msg, w_param, l_param);
} }

View File

@ -71,11 +71,6 @@ void e2d::Node::Visit()
if (!visible_) if (!visible_)
return; return;
Update();
UpdateActions();
UpdateTasks();
UpdateTransform();
auto render_target = Device::GetGraphics()->GetRenderTarget(); auto render_target = Device::GetGraphics()->GetRenderTarget();
if (clip_enabled_) if (clip_enabled_)
{ {
@ -134,6 +129,43 @@ void e2d::Node::Visit()
} }
} }
void e2d::Node::UpdateChildren(float dt)
{
if (children_.empty())
{
Update(dt);
UpdateActions();
UpdateTasks();
UpdateTransform();
}
else
{
size_t i;
for (i = 0; i < children_.size(); ++i)
{
auto child = children_[i];
// 访问 Order 小于零的节点
if (child->GetOrder() < 0)
{
child->UpdateChildren(dt);
}
else
{
break;
}
}
Update(dt);
UpdateActions();
UpdateTasks();
UpdateTransform();
// 访问剩余节点
for (; i < children_.size(); ++i)
children_[i]->UpdateChildren(dt);
}
}
void e2d::Node::DrawBorder() void e2d::Node::DrawBorder()
{ {
if (visible_) if (visible_)