From 1c8de670acdb2b72ccaf5a410e187bbd6ed45ed5 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Sat, 6 Oct 2018 09:45:28 +0800 Subject: [PATCH] add: Game::Update --- core/e2dmodule.h | 7 ++++++- core/e2dobject.h | 8 +++++--- core/modules/Game.cpp | 48 +++++++++++++++++++++++-------------------- core/objects/Node.cpp | 42 ++++++++++++++++++++++++++++++++----- 4 files changed, 74 insertions(+), 31 deletions(-) diff --git a/core/e2dmodule.h b/core/e2dmodule.h index aec975ab..eecec8df 100644 --- a/core/e2dmodule.h +++ b/core/e2dmodule.h @@ -215,6 +215,9 @@ namespace e2d // 开始 virtual void Start() = 0; + // 更新 + virtual void Update(float dt) {} + // 运行 void Run( const Options& options = Options() @@ -271,7 +274,9 @@ namespace e2d bool IsTransitioning() const; // 更新场景内容 - void UpdateScene(); + void UpdateScene( + float dt + ); // 渲染场景画面 void DrawScene(); diff --git a/core/e2dobject.h b/core/e2dobject.h index 6b7fc234..aa762f87 100644 --- a/core/e2dobject.h +++ b/core/e2dobject.h @@ -153,7 +153,7 @@ namespace e2d virtual void OnExit() {} // 更新场景 - virtual void Update() {} + virtual void Update(float dt) {} // 设置根节点 void SetRoot( @@ -270,7 +270,6 @@ namespace e2d class Node : public Ref { - friend class Game; friend class Scene; public: @@ -629,11 +628,14 @@ namespace e2d virtual void Draw() const {} // 更新节点 - virtual void Update() {} + virtual void Update(float dt) {} // 渲染节点边缘 void DrawBorder(); + // 更新子节点 + void UpdateChildren(float dt); + // 分发鼠标消息 virtual bool Dispatch( const MouseEvent& e, diff --git a/core/modules/Game.cpp b/core/modules/Game.cpp index d6523d8e..77bc5192 100644 --- a/core/modules/Game.cpp +++ b/core/modules/Game.cpp @@ -89,7 +89,7 @@ void e2d::Game::Run(const Options& options) // 刷新场景 ::ShowWindow(hwnd_, SW_SHOWNORMAL); ::UpdateWindow(hwnd_); - UpdateScene(); + UpdateScene(0); // 运行 const int min_interval = 5; @@ -103,10 +103,12 @@ void e2d::Game::Run(const Options& options) if (dur.Milliseconds() > min_interval) { + float dt = (now - last).Seconds(); last = now; - Device::GetInput()->Flush(); - UpdateScene(); + Device::GetInput()->Flush(); + Update(dt); + UpdateScene(dt); DrawScene(); while (::PeekMessage(&msg, nullptr, 0, 0, PM_REMOVE)) @@ -179,8 +181,20 @@ bool e2d::Game::IsTransitioning() const 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_) { transition_->Update(); @@ -209,16 +223,6 @@ void e2d::Game::UpdateScene() curr_scene_ = next_scene_; next_scene_ = nullptr; } - - if (curr_scene_) - { - curr_scene_->Update(); - } - - if (next_scene_) - { - next_scene_->Update(); - } } void e2d::Game::DrawScene() @@ -490,7 +494,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) } else { - bool has_handled = false; + bool was_handled = false; Game * game = reinterpret_cast( static_cast( ::GetWindowLongPtrW(hwnd, GWLP_USERDATA) @@ -523,7 +527,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) } } result = 0; - has_handled = true; + was_handled = true; break; // 处理按键消息 @@ -540,7 +544,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) } } result = 0; - has_handled = true; + was_handled = true; break; // 处理窗口大小变化消息 @@ -584,7 +588,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) ::InvalidateRect(hwnd, nullptr, FALSE); } result = 0; - has_handled = true; + was_handled = true; break; // 重绘窗口 @@ -594,7 +598,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) ::ValidateRect(hwnd, nullptr); } result = 0; - has_handled = true; + was_handled = true; break; // 窗口关闭消息 @@ -606,7 +610,7 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) } } result = 0; - has_handled = true; + was_handled = true; break; // 窗口销毁消息 @@ -615,12 +619,12 @@ LRESULT e2d::Game::WndProc(HWND hwnd, UINT msg, WPARAM w_param, LPARAM l_param) ::PostQuitMessage(0); } result = 1; - has_handled = true; + was_handled = true; break; } - if (!has_handled) + if (!was_handled) { result = ::DefWindowProc(hwnd, msg, w_param, l_param); } diff --git a/core/objects/Node.cpp b/core/objects/Node.cpp index 6db35273..7db772ed 100644 --- a/core/objects/Node.cpp +++ b/core/objects/Node.cpp @@ -71,11 +71,6 @@ void e2d::Node::Visit() if (!visible_) return; - Update(); - UpdateActions(); - UpdateTasks(); - UpdateTransform(); - auto render_target = Device::GetGraphics()->GetRenderTarget(); 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() { if (visible_)