From 8b277ed59d703c4a2aed97c30bfacbc6fda387a9 Mon Sep 17 00:00:00 2001 From: Nomango <569629550@qq.com> Date: Mon, 12 Aug 2019 14:51:54 +0800 Subject: [PATCH] [build] add Stage component --- projects/kiwano.vcxproj | 2 + projects/kiwano.vcxproj.filters | 6 + src/kiwano-audio/src/audio.cpp | 2 +- src/kiwano-audio/src/audio.h | 2 +- src/kiwano-imgui/src/ImGuiModule.cpp | 6 +- src/kiwano-imgui/src/ImGuiModule.h | 4 +- src/kiwano-network/src/HttpClient.cpp | 7 +- src/kiwano-network/src/HttpClient.h | 2 +- src/kiwano-network/src/helper.h | 2 +- src/kiwano/2d/DebugNode.cpp | 2 +- src/kiwano/2d/Node.h | 6 +- src/kiwano/2d/Scene.cpp | 11 -- src/kiwano/2d/Scene.h | 11 +- src/kiwano/2d/Transition.h | 4 +- src/kiwano/base/Component.h | 13 +- src/kiwano/base/Input.h | 2 +- src/kiwano/base/Stage.cpp | 152 +++++++++++++++ src/kiwano/base/Stage.h | 81 ++++++++ src/kiwano/kiwano.h | 3 +- src/kiwano/macros.h | 2 +- src/kiwano/platform/Application.cpp | 269 +++++++++----------------- src/kiwano/platform/Application.h | 68 ++----- src/kiwano/renderer/TextRenderer.cpp | 36 ++-- src/kiwano/renderer/render.cpp | 2 +- src/kiwano/renderer/render.h | 2 +- 25 files changed, 401 insertions(+), 296 deletions(-) create mode 100644 src/kiwano/base/Stage.cpp create mode 100644 src/kiwano/base/Stage.h diff --git a/projects/kiwano.vcxproj b/projects/kiwano.vcxproj index 2b4e6407..2bd2673f 100644 --- a/projects/kiwano.vcxproj +++ b/projects/kiwano.vcxproj @@ -2,6 +2,7 @@ + @@ -108,6 +109,7 @@ + diff --git a/projects/kiwano.vcxproj.filters b/projects/kiwano.vcxproj.filters index 6415693e..f8fc490f 100644 --- a/projects/kiwano.vcxproj.filters +++ b/projects/kiwano.vcxproj.filters @@ -267,6 +267,9 @@ 2d + + base + @@ -404,5 +407,8 @@ 2d + + base + \ No newline at end of file diff --git a/src/kiwano-audio/src/audio.cpp b/src/kiwano-audio/src/audio.cpp index 66b04904..38fce8ce 100644 --- a/src/kiwano-audio/src/audio.cpp +++ b/src/kiwano-audio/src/audio.cpp @@ -36,7 +36,7 @@ namespace kiwano { } - void Audio::SetupComponent(Application*) + void Audio::SetupComponent() { KGE_LOG(L"Creating audio resources"); diff --git a/src/kiwano-audio/src/audio.h b/src/kiwano-audio/src/audio.h index 42259e09..5ec3ffe4 100644 --- a/src/kiwano-audio/src/audio.h +++ b/src/kiwano-audio/src/audio.h @@ -33,7 +33,7 @@ namespace kiwano KGE_DECLARE_SINGLETON(Audio); public: - void SetupComponent(Application*) override; + void SetupComponent() override; void DestroyComponent() override; diff --git a/src/kiwano-imgui/src/ImGuiModule.cpp b/src/kiwano-imgui/src/ImGuiModule.cpp index 3575b65a..a0ca02e1 100644 --- a/src/kiwano-imgui/src/ImGuiModule.cpp +++ b/src/kiwano-imgui/src/ImGuiModule.cpp @@ -32,7 +32,7 @@ namespace kiwano { } - void ImGuiModule::SetupComponent(Application* app) + void ImGuiModule::SetupComponent() { // Setup Dear ImGui context IMGUI_CHECKVERSION(); @@ -56,12 +56,12 @@ namespace kiwano ImGui::DestroyContext(); } - void ImGuiModule::BeforeUpdate(float dt) + void ImGuiModule::OnUpdate(Duration dt) { ImGuiIO& io = ImGui::GetIO(); // Setup time step - io.DeltaTime = dt; + io.DeltaTime = dt.Seconds(); // Read keyboard modifiers inputs io.KeyCtrl = Input::Instance()->IsDown(KeyCode::Ctrl); diff --git a/src/kiwano-imgui/src/ImGuiModule.h b/src/kiwano-imgui/src/ImGuiModule.h index 4ae214db..ae0b0c0b 100644 --- a/src/kiwano-imgui/src/ImGuiModule.h +++ b/src/kiwano-imgui/src/ImGuiModule.h @@ -42,11 +42,11 @@ namespace kiwano public: ImGuiModule(); - void SetupComponent(Application* app) override; + void SetupComponent() override; void DestroyComponent() override; - void BeforeUpdate(float dt) override; + void OnUpdate(Duration dt) override; void BeforeRender() override; diff --git a/src/kiwano-network/src/HttpClient.cpp b/src/kiwano-network/src/HttpClient.cpp index 9a2b5a73..d5d01c25 100644 --- a/src/kiwano-network/src/HttpClient.cpp +++ b/src/kiwano-network/src/HttpClient.cpp @@ -20,7 +20,10 @@ #include #include -#include "../kiwano-network.h" +#include "helper.h" +#include "HttpRequest.hpp" +#include "HttpResponse.hpp" +#include "HttpClient.h" #include #include @@ -249,7 +252,7 @@ namespace kiwano { } - void HttpClient::SetupComponent(Application * app) + void HttpClient::SetupComponent() { ::curl_global_init(CURL_GLOBAL_ALL); diff --git a/src/kiwano-network/src/HttpClient.h b/src/kiwano-network/src/HttpClient.h index a61ee343..e587d56d 100644 --- a/src/kiwano-network/src/HttpClient.h +++ b/src/kiwano-network/src/HttpClient.h @@ -71,7 +71,7 @@ namespace kiwano } public: - virtual void SetupComponent(Application* app) override; + virtual void SetupComponent() override; virtual void DestroyComponent() override; diff --git a/src/kiwano-network/src/helper.h b/src/kiwano-network/src/helper.h index 8367e577..969471c0 100644 --- a/src/kiwano-network/src/helper.h +++ b/src/kiwano-network/src/helper.h @@ -19,7 +19,7 @@ // THE SOFTWARE. #pragma once -#include +#include namespace kiwano { diff --git a/src/kiwano/2d/DebugNode.cpp b/src/kiwano/2d/DebugNode.cpp index d3ade3d8..e0a9e5a4 100644 --- a/src/kiwano/2d/DebugNode.cpp +++ b/src/kiwano/2d/DebugNode.cpp @@ -71,7 +71,7 @@ namespace kiwano void DebugNode::OnUpdate(Duration dt) { - KGE_NOT_USED(dt); + KGE_UNUSED(dt); frame_time_.push_back(Time::Now()); while (frame_time_.back() - frame_time_.front() >= time::Sec) diff --git a/src/kiwano/2d/Node.h b/src/kiwano/2d/Node.h index 673415bc..5ecfc004 100644 --- a/src/kiwano/2d/Node.h +++ b/src/kiwano/2d/Node.h @@ -27,7 +27,7 @@ namespace kiwano { - class Application; + class Stage; // 节点 class KGE_API Node @@ -37,7 +37,7 @@ namespace kiwano , public EventDispatcher , public IntrusiveListItem { - friend class Application; + friend class Stage; friend class Transition; friend class IntrusiveList; @@ -48,7 +48,7 @@ namespace kiwano Node(); // 更新节点 - virtual void OnUpdate(Duration dt) { KGE_NOT_USED(dt); } + virtual void OnUpdate(Duration dt) { KGE_UNUSED(dt); } // 渲染节点 virtual void OnRender() {} diff --git a/src/kiwano/2d/Scene.cpp b/src/kiwano/2d/Scene.cpp index a19667f5..5cb6e6a5 100644 --- a/src/kiwano/2d/Scene.cpp +++ b/src/kiwano/2d/Scene.cpp @@ -25,7 +25,6 @@ namespace kiwano { Scene::Scene() - : render_border_enabled_(false) { scene_ = this; @@ -47,14 +46,4 @@ namespace kiwano KGE_LOG(L"Scene exited"); } - void Scene::Render() - { - Node::Render(); - - if (render_border_enabled_) - { - Node::RenderBorder(); - } - } - } diff --git a/src/kiwano/2d/Scene.h b/src/kiwano/2d/Scene.h index f42f8ae8..207d759b 100644 --- a/src/kiwano/2d/Scene.h +++ b/src/kiwano/2d/Scene.h @@ -28,7 +28,7 @@ namespace kiwano : public Node { friend class Transition; - friend class Application; + friend class Stage; public: Scene(); @@ -40,14 +40,5 @@ namespace kiwano // 退出场景 virtual void OnExit(); - - // 启用或禁用场景内的节点边界渲染功能 - inline void SetRenderBorderEnabled(bool enabled) { render_border_enabled_ = enabled; } - - protected: - void Render() override; - - protected: - bool render_border_enabled_; }; } diff --git a/src/kiwano/2d/Transition.h b/src/kiwano/2d/Transition.h index 20d1ab3c..5690bab6 100644 --- a/src/kiwano/2d/Transition.h +++ b/src/kiwano/2d/Transition.h @@ -24,13 +24,13 @@ namespace kiwano { - class Scene; + class Stage; // 场景过渡 class KGE_API Transition : public Object { - friend class Application; + friend class Stage; public: explicit Transition( diff --git a/src/kiwano/base/Component.h b/src/kiwano/base/Component.h index f0e4c64f..cb753b58 100644 --- a/src/kiwano/base/Component.h +++ b/src/kiwano/base/Component.h @@ -20,23 +20,26 @@ #pragma once #include "../macros.h" +#include "time.h" +#include "Event.hpp" namespace kiwano { - class Application; - class KGE_API Component { public: - virtual void SetupComponent(Application*) = 0; + virtual void SetupComponent() = 0; virtual void DestroyComponent() = 0; - virtual void BeforeUpdate(float dt) {} + virtual void BeforeUpdate() {} + virtual void OnUpdate(Duration) {} virtual void AfterUpdate() {} virtual void BeforeRender() {} + virtual void OnRender() {} virtual void AfterRender() {} - virtual void HandleMessage(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {} + virtual void HandleEvent(Event&) {} + virtual void HandleMessage(HWND, UINT, WPARAM, LPARAM) {} }; } diff --git a/src/kiwano/base/Input.h b/src/kiwano/base/Input.h index c9d37658..fc146b76 100644 --- a/src/kiwano/base/Input.h +++ b/src/kiwano/base/Input.h @@ -59,7 +59,7 @@ namespace kiwano Point GetMousePos(); public: - void SetupComponent(Application*) override {} + void SetupComponent() override {} void DestroyComponent() override {} diff --git a/src/kiwano/base/Stage.cpp b/src/kiwano/base/Stage.cpp new file mode 100644 index 00000000..6c06559e --- /dev/null +++ b/src/kiwano/base/Stage.cpp @@ -0,0 +1,152 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#include "Stage.h" +#include "../2d/Node.h" +#include "../2d/Scene.h" +#include "../2d/Transition.h" +#include "../2d/DebugNode.h" + +namespace kiwano +{ + Stage::Stage() + : render_border_enabled_(false) + { + } + + Stage::~Stage() + { + } + + void Stage::EnterScene(ScenePtr scene) + { + KGE_ASSERT(scene && "Stage::EnterScene failed, NULL pointer exception"); + + if (curr_scene_ == scene || next_scene_ == scene) + return; + + next_scene_ = scene; + } + + void Stage::EnterScene(ScenePtr scene, TransitionPtr transition) + { + EnterScene(scene); + + if (transition && next_scene_) + { + if (transition_) + { + transition_->Stop(); + } + transition_ = transition; + transition_->Init(curr_scene_, next_scene_); + } + } + + ScenePtr Stage::GetCurrentScene() + { + return curr_scene_; + } + + void Stage::SetRenderBorderEnabled(bool enabled) + { + render_border_enabled_ = enabled; + } + + void Stage::ShowDebugInfo(bool show) + { + if (show) + { + if (!debug_node_) + debug_node_ = new DebugNode; + } + else + { + debug_node_.Reset(); + } + } + + void Stage::OnUpdate(Duration dt) + { + if (transition_) + { + transition_->Update(dt); + + if (transition_->IsDone()) + transition_ = nullptr; + } + + if (next_scene_ && !transition_) + { + if (curr_scene_) + { + curr_scene_->OnExit(); + } + + next_scene_->OnEnter(); + + curr_scene_ = next_scene_; + next_scene_ = nullptr; + } + + if (curr_scene_) + curr_scene_->Update(dt); + + if (next_scene_) + next_scene_->Update(dt); + + if (debug_node_) + debug_node_->Update(dt); + } + + void Stage::OnRender() + { + if (transition_) + { + transition_->Render(); + } + else if (curr_scene_) + { + curr_scene_->Render(); + } + + if (debug_node_) + debug_node_->Render(); + } + + void Stage::AfterRender() + { + if (render_border_enabled_) + { + if (curr_scene_) + curr_scene_->RenderBorder(); + } + } + + void Stage::HandleEvent(Event& evt) + { + if (debug_node_) + debug_node_->Dispatch(evt); + + if (curr_scene_) + curr_scene_->Dispatch(evt); + } + +} diff --git a/src/kiwano/base/Stage.h b/src/kiwano/base/Stage.h new file mode 100644 index 00000000..1ff924c5 --- /dev/null +++ b/src/kiwano/base/Stage.h @@ -0,0 +1,81 @@ +// Copyright (c) 2016-2018 Kiwano - Nomango +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + +#pragma once +#include "../macros.h" +#include "../common/Singleton.hpp" +#include "../2d/include-forwards.h" +#include "Component.h" + +namespace kiwano +{ + class KGE_API Stage + : public Singleton + , public Component + { + KGE_DECLARE_SINGLETON(Stage); + + public: + // 切换场景 + void EnterScene( + ScenePtr scene /* 场景 */ + ); + + // 切换场景 + void EnterScene( + ScenePtr scene, /* 场景 */ + TransitionPtr transition /* 场景动画 */ + ); + + // 获取当前场景 + ScenePtr GetCurrentScene(); + + // 启用或禁用场景内的节点边界渲染功能 + void SetRenderBorderEnabled(bool enabled); + + // 显示调试信息 + void ShowDebugInfo(bool show = true); + + public: + void SetupComponent() override {} + + void DestroyComponent() override {} + + void OnUpdate(Duration dt) override; + + void OnRender() override; + + void AfterRender() override; + + void HandleEvent(Event& evt) override; + + protected: + Stage(); + + virtual ~Stage(); + + protected: + bool render_border_enabled_; + ScenePtr curr_scene_; + ScenePtr next_scene_; + NodePtr debug_node_; + TransitionPtr transition_; + }; +} diff --git a/src/kiwano/kiwano.h b/src/kiwano/kiwano.h index 30489ed3..720902b0 100644 --- a/src/kiwano/kiwano.h +++ b/src/kiwano/kiwano.h @@ -60,9 +60,10 @@ // base // +#include "base/time.h" #include "base/window.h" #include "base/input.h" -#include "base/time.h" +#include "base/Stage.h" #include "base/logs.h" #include "renderer/render.h" #include "platform/modules.h" diff --git a/src/kiwano/macros.h b/src/kiwano/macros.h index ea65e106..25260d3b 100644 --- a/src/kiwano/macros.h +++ b/src/kiwano/macros.h @@ -115,6 +115,6 @@ # pragma warning (disable: 4251) #endif -#define KGE_NOT_USED(VAR) ((void)VAR) +#define KGE_UNUSED(VAR) ((void)VAR) #define KGE_DEPRECATED(...) __declspec(deprecated(__VA_ARGS__)) diff --git a/src/kiwano/platform/Application.cpp b/src/kiwano/platform/Application.cpp index eb901026..30af0120 100644 --- a/src/kiwano/platform/Application.cpp +++ b/src/kiwano/platform/Application.cpp @@ -20,12 +20,11 @@ #include "Application.h" #include "modules.h" +#include "../base/window.h" #include "../base/logs.h" #include "../base/input.h" +#include "../base/Stage.h" #include "../renderer/render.h" -#include "../2d/Scene.h" -#include "../2d/DebugNode.h" -#include "../2d/Transition.h" #include // GET_X_LPARAM, GET_Y_LPARAM #include // ImmAssociateContext #include // std::mutex @@ -34,13 +33,24 @@ namespace kiwano { - using FunctionToPerform = Closure; - namespace { + using FunctionToPerform = Closure; + std::mutex perform_mutex_; Queue functions_to_perform_; } + + Options::Options(String const& title, int width, int height, LPCWSTR icon, Color clear_color, bool vsync, bool fullscreen, bool debug) + : title(title) + , width(width) + , height(height) + , icon(icon) + , clear_color(clear_color) + , vsync(vsync) + , fullscreen(fullscreen) + , debug(debug) + {} } namespace kiwano @@ -56,6 +66,7 @@ namespace kiwano Use(Renderer::Instance()); Use(Input::Instance()); + Use(Stage::Instance()); } Application::~Application() @@ -84,11 +95,17 @@ namespace kiwano // Setup all components for (Component* c : components_) { - c->SetupComponent(this); + c->SetupComponent(); + } + + if (options.debug) + { + Stage::Instance()->ShowDebugInfo(true); + Renderer::Instance()->SetCollectingStatus(true); } // Everything is ready - OnStart(); + OnReady(); HWND hwnd = Window::Instance()->GetHandle(); @@ -129,11 +146,6 @@ namespace kiwano void Application::Destroy() { - transition_.Reset(); - next_scene_.Reset(); - curr_scene_.Reset(); - debug_node_.Reset(); - if (inited_) { inited_ = false; @@ -146,6 +158,7 @@ namespace kiwano } // Destroy all instances + Stage::Destroy(); Input::Destroy(); Renderer::Destroy(); Window::Destroy(); @@ -186,98 +199,17 @@ namespace kiwano } } - void Application::EnterScene(ScenePtr scene) - { - KGE_ASSERT(scene && "Application::EnterScene failed, NULL pointer exception"); - - if (curr_scene_ == scene || next_scene_ == scene) - return; - - next_scene_ = scene; - } - - void Application::EnterScene(ScenePtr scene, TransitionPtr transition) - { - EnterScene(scene); - - if (transition && next_scene_) - { - if (transition_) - { - transition_->Stop(); - } - transition_ = transition; - transition_->Init(curr_scene_, next_scene_); - } - } - - ScenePtr Application::GetCurrentScene() - { - return curr_scene_; - } - void Application::SetTimeScale(float scale_factor) { time_scale_ = scale_factor; } - void Application::ShowDebugInfo(bool show) - { - if (show) - { - debug_node_ = new DebugNode; - Renderer::Instance()->SetCollectingStatus(true); - } - else - { - debug_node_.Reset(); - Renderer::Instance()->SetCollectingStatus(false); - } - } - - void Application::Dispatch(Event& evt) - { - if (debug_node_) - debug_node_->Dispatch(evt); - - if (curr_scene_) - curr_scene_->Dispatch(evt); - } - void Application::Update() { - static auto last = Time::Now(); - - const auto now = Time::Now(); - const auto dt = (now - last) * time_scale_; - last = now; - // Before update for (Component* c : components_) { - c->BeforeUpdate(dt.Seconds()); - } - - // Updating - if (transition_) - { - transition_->Update(dt); - - if (transition_->IsDone()) - transition_ = nullptr; - } - - if (next_scene_ && !transition_) - { - if (curr_scene_) - { - curr_scene_->OnExit(); - } - - next_scene_->OnEnter(); - - curr_scene_ = next_scene_; - next_scene_ = nullptr; + c->BeforeUpdate(); } // perform functions @@ -300,16 +232,19 @@ namespace kiwano } } - OnUpdate(dt); + // Updating + { + static auto last = Time::Now(); - if (curr_scene_) - curr_scene_->Update(dt); + const auto now = Time::Now(); + const auto dt = (now - last) * time_scale_; + last = now; - if (next_scene_) - next_scene_->Update(dt); - - if (debug_node_) - debug_node_->Update(dt); + for (Component* c : components_) + { + c->OnUpdate(dt); + } + } // After update for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) @@ -327,19 +262,10 @@ namespace kiwano } // Rendering - if (transition_) + for (Component* c : components_) { - transition_->Render(); + c->OnRender(); } - else if (curr_scene_) - { - curr_scene_->Render(); - } - - OnRender(); - - if (debug_node_) - debug_node_->Render(); // After render for (auto rit = components_.rbegin(); rit != components_.rend(); ++rit) @@ -348,6 +274,14 @@ namespace kiwano } } + void Application::DispatchEvent(Event& evt) + { + for (Component* c : components_) + { + c->HandleEvent(evt); + } + } + void Application::PreformInMainThread(Closure function) { std::lock_guard lock(perform_mutex_); @@ -366,7 +300,7 @@ namespace kiwano { c->HandleMessage(hwnd, msg, wparam, lparam); } - + switch (msg) { case WM_PAINT: @@ -384,28 +318,22 @@ namespace kiwano case WM_KEYUP: case WM_SYSKEYUP: { - if (!app->transition_ && app->curr_scene_) - { - bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; - Event evt(down ? Event::KeyDown : Event::KeyUp); - evt.key.code = static_cast(wparam); - evt.key.count = static_cast(lparam & 0xFF); + bool down = msg == WM_KEYDOWN || msg == WM_SYSKEYDOWN; + Event evt(down ? Event::KeyDown : Event::KeyUp); + evt.key.code = static_cast(wparam); + evt.key.count = static_cast(lparam & 0xFF); - app->Dispatch(evt); - } + app->DispatchEvent(evt); } break; case WM_CHAR: { - if (!app->transition_ && app->curr_scene_) - { - Event evt(Event::Char); - evt.key.c = static_cast(wparam); - evt.key.count = static_cast(lparam & 0xFF); + Event evt(Event::Char); + evt.key.c = static_cast(wparam); + evt.key.count = static_cast(lparam & 0xFF); - app->Dispatch(evt); - } + app->DispatchEvent(evt); } break; @@ -421,26 +349,23 @@ namespace kiwano case WM_MOUSEMOVE: case WM_MOUSEWHEEL: { - if (!app->transition_ && app->curr_scene_) - { - Event evt; + Event evt; - evt.mouse.x = static_cast(GET_X_LPARAM(lparam)); - evt.mouse.y = static_cast(GET_Y_LPARAM(lparam)); - evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); - evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); + evt.mouse.x = static_cast(GET_X_LPARAM(lparam)); + evt.mouse.y = static_cast(GET_Y_LPARAM(lparam)); + evt.mouse.left_btn_down = !!(wparam & MK_LBUTTON); + evt.mouse.left_btn_down = !!(wparam & MK_RBUTTON); - if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; } - else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; } - else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; } - else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; } + if (msg == WM_MOUSEMOVE) { evt.type = Event::MouseMove; } + else if (msg == WM_LBUTTONDOWN || msg == WM_RBUTTONDOWN || msg == WM_MBUTTONDOWN) { evt.type = Event::MouseBtnDown; } + else if (msg == WM_LBUTTONUP || msg == WM_RBUTTONUP || msg == WM_MBUTTONUP) { evt.type = Event::MouseBtnUp; } + else if (msg == WM_MOUSEWHEEL) { evt.type = Event::MouseWheel; evt.mouse.wheel = GET_WHEEL_DELTA_WPARAM(wparam) / (float)WHEEL_DELTA; } - if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; } - else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; } - else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; } + if (msg == WM_LBUTTONDOWN || msg == WM_LBUTTONUP) { evt.mouse.button = MouseButton::Left; } + else if (msg == WM_RBUTTONDOWN || msg == WM_RBUTTONUP) { evt.mouse.button = MouseButton::Right; } + else if (msg == WM_MBUTTONDOWN || msg == WM_MBUTTONUP) { evt.mouse.button = MouseButton::Middle; } - app->Dispatch(evt); - } + app->DispatchEvent(evt); } break; @@ -454,31 +379,25 @@ namespace kiwano { KGE_LOG(L"Window resized"); - if (app->curr_scene_) - { - Event evt(Event::WindowResized); - evt.win.width = LOWORD(lparam); - evt.win.height = HIWORD(lparam); - app->Dispatch(evt); - } - Window::Instance()->UpdateWindowRect(); + + Event evt(Event::WindowResized); + evt.win.width = LOWORD(lparam); + evt.win.height = HIWORD(lparam); + app->DispatchEvent(evt); } } break; case WM_MOVE: { - if (app->curr_scene_) - { - int x = (int)(short)LOWORD(lparam); - int y = (int)(short)HIWORD(lparam); + int x = (int)(short)LOWORD(lparam); + int y = (int)(short)HIWORD(lparam); - Event evt(Event::WindowMoved); - evt.win.x = x; - evt.win.y = y; - app->Dispatch(evt); - } + Event evt(Event::WindowMoved); + evt.win.x = x; + evt.win.y = y; + app->DispatchEvent(evt); } break; @@ -488,12 +407,9 @@ namespace kiwano Window::Instance()->SetActive(active); - if (app->curr_scene_) - { - Event evt(Event::WindowFocusChanged); - evt.win.focus = active; - app->Dispatch(evt); - } + Event evt(Event::WindowFocusChanged); + evt.win.focus = active; + app->DispatchEvent(evt); } break; @@ -501,12 +417,9 @@ namespace kiwano { KGE_LOG(L"Window title changed"); - if (app->curr_scene_) - { - Event evt(Event::WindowTitleChanged); - evt.win.title = reinterpret_cast(lparam); - app->Dispatch(evt); - } + Event evt(Event::WindowTitleChanged); + evt.win.title = reinterpret_cast(lparam); + app->DispatchEvent(evt); } break; @@ -539,12 +452,8 @@ namespace kiwano { KGE_LOG(L"Window was destroyed"); - if (app->curr_scene_) - { - Event evt(Event::WindowClosed); - app->Dispatch(evt); - } - + Event evt(Event::WindowClosed); + app->DispatchEvent(evt); app->OnDestroy(); ::PostQuitMessage(0); diff --git a/src/kiwano/platform/Application.h b/src/kiwano/platform/Application.h index 0cd7f9c8..03d6671c 100644 --- a/src/kiwano/platform/Application.h +++ b/src/kiwano/platform/Application.h @@ -19,11 +19,14 @@ // THE SOFTWARE. #pragma once -#include "../2d/include-forwards.h" +#include "../common/String.hpp" +#include "../common/Array.hpp" +#include "../common/Closure.hpp" +#include "../common/Noncopyable.hpp" #include "../base/time.h" -#include "../base/window.h" #include "../base/Component.h" #include "../base/Event.hpp" +#include "../2d/Color.h" namespace kiwano { @@ -36,6 +39,7 @@ namespace kiwano Color clear_color; // 清屏颜色 bool vsync; // 垂直同步 bool fullscreen; // 全屏模式 + bool debug; // 调试模式 Options( String const& title = L"Kiwano Game", @@ -44,19 +48,13 @@ namespace kiwano LPCWSTR icon = nullptr, Color clear_color = Color::Black, bool vsync = true, - bool fullscreen = false - ) - : title(title) - , width(width) - , height(height) - , icon(icon) - , clear_color(clear_color) - , vsync(vsync) - , fullscreen(fullscreen) - {} + bool fullscreen = false, + bool debug = false + ); }; + // 应用 class KGE_API Application : protected Noncopyable { @@ -67,24 +65,18 @@ namespace kiwano // 初始化 void Init( - Options const& options + Options const& options = Options{} ); - // 启动时 - virtual void OnStart() {} + // 初始化成功时 + virtual void OnReady() {} - // 关闭时 + // 窗口关闭时 virtual bool OnClosing() { return true; } // 销毁时 virtual void OnDestroy() {} - // 渲染时 - virtual void OnRender() {} - - // 更新时 - virtual void OnUpdate(Duration dt) { KGE_NOT_USED(dt); } - // 运行 void Run(); @@ -104,32 +96,13 @@ namespace kiwano Component* component ); - // 切换场景 - void EnterScene( - ScenePtr scene /* 场景 */ - ); - - // 切换场景 - void EnterScene( - ScenePtr scene, /* 场景 */ - TransitionPtr transition /* 场景动画 */ - ); - - // 获取当前场景 - ScenePtr GetCurrentScene(); - // 设置时间缩放因子 void SetTimeScale( float scale_factor ); - // 显示调试信息 - void ShowDebugInfo( - bool show = true - ); - // 分发事件 - void Dispatch(Event& evt); + void DispatchEvent(Event& evt); // 在 Kiwano 主线程中执行函数 // 当在其他线程调用 Kiwano 函数时使用 @@ -145,14 +118,9 @@ namespace kiwano static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); protected: - bool end_; - bool inited_; - float time_scale_; - - ScenePtr curr_scene_; - ScenePtr next_scene_; - NodePtr debug_node_; - TransitionPtr transition_; + bool end_; + bool inited_; + float time_scale_; Array components_; }; diff --git a/src/kiwano/renderer/TextRenderer.cpp b/src/kiwano/renderer/TextRenderer.cpp index dfef5fdd..a0694a21 100644 --- a/src/kiwano/renderer/TextRenderer.cpp +++ b/src/kiwano/renderer/TextRenderer.cpp @@ -213,10 +213,10 @@ namespace kiwano __in DWRITE_GLYPH_RUN_DESCRIPTION const* glyphRunDescription, IUnknown* clientDrawingEffect) { - KGE_NOT_USED(clientDrawingContext); - KGE_NOT_USED(measuringMode); - KGE_NOT_USED(glyphRunDescription); - KGE_NOT_USED(clientDrawingEffect); + KGE_UNUSED(clientDrawingContext); + KGE_UNUSED(measuringMode); + KGE_UNUSED(glyphRunDescription); + KGE_UNUSED(clientDrawingEffect); HRESULT hr = S_OK; @@ -304,8 +304,8 @@ namespace kiwano __in DWRITE_UNDERLINE const* underline, IUnknown* clientDrawingEffect) { - KGE_NOT_USED(clientDrawingContext); - KGE_NOT_USED(clientDrawingEffect); + KGE_UNUSED(clientDrawingContext); + KGE_UNUSED(clientDrawingEffect); HRESULT hr; @@ -373,8 +373,8 @@ namespace kiwano __in DWRITE_STRIKETHROUGH const* strikethrough, IUnknown* clientDrawingEffect) { - KGE_NOT_USED(clientDrawingContext); - KGE_NOT_USED(clientDrawingEffect); + KGE_UNUSED(clientDrawingContext); + KGE_UNUSED(clientDrawingEffect); HRESULT hr; @@ -444,13 +444,13 @@ namespace kiwano BOOL IsRightToLeft, IUnknown* clientDrawingEffect) { - KGE_NOT_USED(clientDrawingContext); - KGE_NOT_USED(originX); - KGE_NOT_USED(originY); - KGE_NOT_USED(inlineObject); - KGE_NOT_USED(IsSideways); - KGE_NOT_USED(IsRightToLeft); - KGE_NOT_USED(clientDrawingEffect); + KGE_UNUSED(clientDrawingContext); + KGE_UNUSED(originX); + KGE_UNUSED(originY); + KGE_UNUSED(inlineObject); + KGE_UNUSED(IsSideways); + KGE_UNUSED(IsRightToLeft); + KGE_UNUSED(clientDrawingEffect); return E_NOTIMPL; } @@ -476,7 +476,7 @@ namespace kiwano __maybenull void* clientDrawingContext, __out BOOL* isDisabled) { - KGE_NOT_USED(clientDrawingContext); + KGE_UNUSED(clientDrawingContext); *isDisabled = FALSE; return S_OK; @@ -486,7 +486,7 @@ namespace kiwano __maybenull void* clientDrawingContext, __out DWRITE_MATRIX* transform) { - KGE_NOT_USED(clientDrawingContext); + KGE_UNUSED(clientDrawingContext); pRT_->GetTransform(reinterpret_cast(transform)); return S_OK; @@ -496,7 +496,7 @@ namespace kiwano __maybenull void* clientDrawingContext, __out FLOAT* pixelsPerDip) { - KGE_NOT_USED(clientDrawingContext); + KGE_UNUSED(clientDrawingContext); float x, yUnused; diff --git a/src/kiwano/renderer/render.cpp b/src/kiwano/renderer/render.cpp index a447b760..1354071d 100644 --- a/src/kiwano/renderer/render.cpp +++ b/src/kiwano/renderer/render.cpp @@ -41,7 +41,7 @@ namespace kiwano { } - void Renderer::SetupComponent(Application* app) + void Renderer::SetupComponent() { KGE_LOG(L"Creating device resources"); diff --git a/src/kiwano/renderer/render.h b/src/kiwano/renderer/render.h index cdd9e23b..2c03a701 100644 --- a/src/kiwano/renderer/render.h +++ b/src/kiwano/renderer/render.h @@ -152,7 +152,7 @@ namespace kiwano ); public: - void SetupComponent(Application*) override; + void SetupComponent() override; void DestroyComponent() override;