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 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();

View File

@ -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,

View File

@ -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<Game*>(
static_cast<LONG_PTR>(
::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);
}

View File

@ -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_)